Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions tools/@aws-cdk/spec2cdk/lib/cdk/resource-class.ts
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,7 @@ export class ResourceClass extends ClassType implements Referenceable {
});

this.makeFromCloudFormationFactory();
this.makeIsAResource();
this.makeFromArnFactory();
this.makeFromNameFactory();
this.addArnForResourceMethod();
Expand Down Expand Up @@ -232,6 +233,39 @@ export class ResourceClass extends ClassType implements Referenceable {
this.makeMustRenderStructs();
}

/**
* Adds the static isCfn<Resource> method to the class.
*
* @example
* public static isCfnBucket(x: any): construct is CfnBucket {
* return CfnResource.isCfnResource(x) && x.cfnResourceType === this.constructor.CFN_RESOURCE_TYPE_NAME;
* }
*/
public makeIsAResource() {
// Add the factory method to the outer class
const isA = this.addMethod({
name: `is${this.name}`,
static: true,
returnType: Type.ambient(`x is ${this.name}`),
docs: {
summary: `Checks whether the given object is a ${this.name}`,
},
});

const x = isA.addParameter({
name: 'x',
type: Type.ANY,
});

isA.addBody(
stmt.ret(expr.binOp(
$T(CDK_CORE.CfnResource).isCfnResource(x),
'&&',
expr.eq($E(x).cfnResourceType, $T(this.type).CFN_RESOURCE_TYPE_NAME),
)),
);
}

/**
* Create the reference interface types
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,13 @@ export class CfnResource extends cdk.CfnResource implements cdk.IInspectable, IR
return ret;
}

/**
* Checks whether the given object is a CfnResource
*/
public static isCfnResource(x: any): x is CfnResource {
return (cdk.CfnResource.isCfnResource(x) && (x.cfnResourceType === CfnResource.CFN_RESOURCE_TYPE_NAME));
}

/**
* The identifier of the resource.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,13 @@ export class CfnRole extends cdk.CfnResource implements cdk.IInspectable, IRoleR
return ret;
}

/**
* Checks whether the given object is a CfnRole
*/
public static isCfnRole(x: any): x is CfnRole {
return (cdk.CfnResource.isCfnResource(x) && (x.cfnResourceType === CfnRole.CFN_RESOURCE_TYPE_NAME));
}

public static arnForRole(resource: IRoleRef): string {
return resource.roleRef.roleArn;
}
Expand Down Expand Up @@ -178,6 +185,13 @@ export class CfnResource extends cdk.CfnResource implements cdk.IInspectable, IR
return ret;
}

/**
* Checks whether the given object is a CfnResource
*/
public static isCfnResource(x: any): x is CfnResource {
return (cdk.CfnResource.isCfnResource(x) && (x.cfnResourceType === CfnResource.CFN_RESOURCE_TYPE_NAME));
}

public permissions?: Array<cdk.IResolvable | CfnResource.PermissionProperty> | cdk.IResolvable;

/**
Expand Down Expand Up @@ -381,6 +395,13 @@ export class CfnRole extends cdk.CfnResource implements cdk.IInspectable, IRoleR
return ret;
}

/**
* Checks whether the given object is a CfnRole
*/
public static isCfnRole(x: any): x is CfnRole {
return (cdk.CfnResource.isCfnResource(x) && (x.cfnResourceType === CfnRole.CFN_RESOURCE_TYPE_NAME));
}

public static arnForRole(resource: IRoleRef): string {
return resource.roleRef.roleArn;
}
Expand Down Expand Up @@ -515,6 +536,13 @@ export class CfnUser extends cdk.CfnResource implements cdk.IInspectable, IUserR
return ret;
}

/**
* Checks whether the given object is a CfnUser
*/
public static isCfnUser(x: any): x is CfnUser {
return (cdk.CfnResource.isCfnResource(x) && (x.cfnResourceType === CfnUser.CFN_RESOURCE_TYPE_NAME));
}

public static arnForUser(resource: IUserRef): string {
return resource.userRef.userArn;
}
Expand Down Expand Up @@ -649,6 +677,13 @@ export class CfnPolicy extends cdk.CfnResource implements cdk.IInspectable, IPol
return ret;
}

/**
* Checks whether the given object is a CfnPolicy
*/
public static isCfnPolicy(x: any): x is CfnPolicy {
return (cdk.CfnResource.isCfnResource(x) && (x.cfnResourceType === CfnPolicy.CFN_RESOURCE_TYPE_NAME));
}

public principalArn?: string;

/**
Expand Down Expand Up @@ -796,6 +831,13 @@ export class CfnRole extends cdk.CfnResource implements cdk.IInspectable, IRoleR
return ret;
}

/**
* Checks whether the given object is a CfnRole
*/
public static isCfnRole(x: any): x is CfnRole {
return (cdk.CfnResource.isCfnResource(x) && (x.cfnResourceType === CfnRole.CFN_RESOURCE_TYPE_NAME));
}

public static arnForRole(resource: IRoleRef): string {
return resource.roleRef.roleArn;
}
Expand Down Expand Up @@ -930,6 +972,13 @@ export class CfnTask extends cdk.CfnResource implements cdk.IInspectable, ITaskR
return ret;
}

/**
* Checks whether the given object is a CfnTask
*/
public static isCfnTask(x: any): x is CfnTask {
return (cdk.CfnResource.isCfnResource(x) && (x.cfnResourceType === CfnTask.CFN_RESOURCE_TYPE_NAME));
}

public executionConfig?: CfnTask.ExecutionConfigProperty | cdk.IResolvable;

/**
Expand Down Expand Up @@ -1132,6 +1181,13 @@ export class CfnRole extends cdk.CfnResource implements cdk.IInspectable, IRoleR
return ret;
}

/**
* Checks whether the given object is a CfnRole
*/
public static isCfnRole(x: any): x is CfnRole {
return (cdk.CfnResource.isCfnResource(x) && (x.cfnResourceType === CfnRole.CFN_RESOURCE_TYPE_NAME));
}

public static arnForRole(resource: IRoleRef): string {
return resource.roleRef.roleArn;
}
Expand Down Expand Up @@ -1266,6 +1322,13 @@ export class CfnJob extends cdk.CfnResource implements cdk.IInspectable, IJobRef
return ret;
}

/**
* Checks whether the given object is a CfnJob
*/
public static isCfnJob(x: any): x is CfnJob {
return (cdk.CfnResource.isCfnResource(x) && (x.cfnResourceType === CfnJob.CFN_RESOURCE_TYPE_NAME));
}

public config?: cdk.IResolvable | CfnJob.OldConfigProperty;

/**
Expand Down Expand Up @@ -1530,6 +1593,13 @@ export class CfnRole extends cdk.CfnResource implements cdk.IInspectable, IRoleR
return ret;
}

/**
* Checks whether the given object is a CfnRole
*/
public static isCfnRole(x: any): x is CfnRole {
return (cdk.CfnResource.isCfnResource(x) && (x.cfnResourceType === CfnRole.CFN_RESOURCE_TYPE_NAME));
}

public static arnForRole(resource: IRoleRef): string {
return resource.roleRef.roleArn;
}
Expand Down Expand Up @@ -1664,6 +1734,13 @@ export class CfnFunction extends cdk.CfnResource implements cdk.IInspectable, IF
return ret;
}

/**
* Checks whether the given object is a CfnFunction
*/
public static isCfnFunction(x: any): x is CfnFunction {
return (cdk.CfnResource.isCfnResource(x) && (x.cfnResourceType === CfnFunction.CFN_RESOURCE_TYPE_NAME));
}

public roleArn?: string;

/**
Expand Down
77 changes: 77 additions & 0 deletions tools/@aws-cdk/spec2cdk/test/__snapshots__/resources.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,13 @@ export class CfnResource extends cdk.CfnResource implements cdk.IInspectable, IR
return ret;
}

/**
* Checks whether the given object is a CfnResource
*/
public static isCfnResource(x: any): x is CfnResource {
return (cdk.CfnResource.isCfnResource(x) && (x.cfnResourceType === CfnResource.CFN_RESOURCE_TYPE_NAME));
}

/**
* The identifier of the resource.
*/
Expand Down Expand Up @@ -225,6 +232,13 @@ export class CfnResource extends cdk.CfnResource implements cdk.IInspectable, IR
return ret;
}

/**
* Checks whether the given object is a CfnResource
*/
public static isCfnResource(x: any): x is CfnResource {
return (cdk.CfnResource.isCfnResource(x) && (x.cfnResourceType === CfnResource.CFN_RESOURCE_TYPE_NAME));
}

/**
* The identifier of the resource.
*/
Expand Down Expand Up @@ -376,6 +390,13 @@ export class CfnResource extends cdk.CfnResource implements cdk.IInspectable, IR
return ret;
}

/**
* Checks whether the given object is a CfnResource
*/
public static isCfnResource(x: any): x is CfnResource {
return (cdk.CfnResource.isCfnResource(x) && (x.cfnResourceType === CfnResource.CFN_RESOURCE_TYPE_NAME));
}

/**
* The identifier of the resource
*
Expand Down Expand Up @@ -518,6 +539,13 @@ export class CfnSomething extends cdk.CfnResource implements cdk.IInspectable, I
return ret;
}

/**
* Checks whether the given object is a CfnSomething
*/
public static isCfnSomething(x: any): x is CfnSomething {
return (cdk.CfnResource.isCfnResource(x) && (x.cfnResourceType === CfnSomething.CFN_RESOURCE_TYPE_NAME));
}

public static arnForSomething(resource: ISomethingRef): string {
return resource.somethingRef.somethingArn;
}
Expand Down Expand Up @@ -673,6 +701,13 @@ export class CfnResource extends cdk.CfnResource implements cdk.IInspectable, IR
return ret;
}

/**
* Checks whether the given object is a CfnResource
*/
public static isCfnResource(x: any): x is CfnResource {
return (cdk.CfnResource.isCfnResource(x) && (x.cfnResourceType === CfnResource.CFN_RESOURCE_TYPE_NAME));
}

public static arnForResource(resource: IResourceRef): string {
return resource.resourceRef.resourceArn;
}
Expand Down Expand Up @@ -828,6 +863,13 @@ export class CfnResource extends cdk.CfnResource implements cdk.IInspectable, IR
return ret;
}

/**
* Checks whether the given object is a CfnResource
*/
public static isCfnResource(x: any): x is CfnResource {
return (cdk.CfnResource.isCfnResource(x) && (x.cfnResourceType === CfnResource.CFN_RESOURCE_TYPE_NAME));
}

/**
* The identifier of the resource
*
Expand Down Expand Up @@ -987,6 +1029,13 @@ export class CfnResource extends cdk.CfnResource implements cdk.IInspectable, IR
return ret;
}

/**
* Checks whether the given object is a CfnResource
*/
public static isCfnResource(x: any): x is CfnResource {
return (cdk.CfnResource.isCfnResource(x) && (x.cfnResourceType === CfnResource.CFN_RESOURCE_TYPE_NAME));
}

public static arnForResource(resource: IResourceRef): string {
return resource.resourceRef.resourceArn;
}
Expand Down Expand Up @@ -1133,6 +1182,13 @@ export class CfnResource extends cdk.CfnResource implements cdk.IInspectable, IR
return ret;
}

/**
* Checks whether the given object is a CfnResource
*/
public static isCfnResource(x: any): x is CfnResource {
return (cdk.CfnResource.isCfnResource(x) && (x.cfnResourceType === CfnResource.CFN_RESOURCE_TYPE_NAME));
}

/**
* Another identifier of the resource
*
Expand Down Expand Up @@ -1284,6 +1340,13 @@ export class CfnResource extends cdk.CfnResource implements cdk.IInspectable, IR
return ret;
}

/**
* Checks whether the given object is a CfnResource
*/
public static isCfnResource(x: any): x is CfnResource {
return (cdk.CfnResource.isCfnResource(x) && (x.cfnResourceType === CfnResource.CFN_RESOURCE_TYPE_NAME));
}

/**
* Creates a new IResourceRef from a resourceId
*/
Expand Down Expand Up @@ -1468,6 +1531,13 @@ export class CfnResource extends cdk.CfnResource implements cdk.IInspectable, IR
return ret;
}

/**
* Checks whether the given object is a CfnResource
*/
public static isCfnResource(x: any): x is CfnResource {
return (cdk.CfnResource.isCfnResource(x) && (x.cfnResourceType === CfnResource.CFN_RESOURCE_TYPE_NAME));
}

/**
* Another identifier of the resource.
*/
Expand Down Expand Up @@ -1637,6 +1707,13 @@ export class CfnResource extends cdk.CfnResource implements cdk.IInspectable, IR
return ret;
}

/**
* Checks whether the given object is a CfnResource
*/
public static isCfnResource(x: any): x is CfnResource {
return (cdk.CfnResource.isCfnResource(x) && (x.cfnResourceType === CfnResource.CFN_RESOURCE_TYPE_NAME));
}

/**
* The identifier of the resource.
*/
Expand Down
Loading