Skip to content

Commit

Permalink
Update provider code gen logic to create useLogGroup() method
Browse files Browse the repository at this point in the history
  • Loading branch information
samson-keung committed Jun 11, 2024
1 parent e4ee959 commit 25d6fb2
Show file tree
Hide file tree
Showing 4 changed files with 108 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,7 @@ export abstract class HandlerFrameworkClass extends ClassType {

const idStatement = stmt.constVar(expr.ident('id'), expr.directCode('`${uniqueid}CustomResourceProvider`'));
const stackFromScopeStatement = stmt.constVar(expr.ident('stack'), expr.directCode('Stack.of(scope)'));
const logContextKeyStatement = stmt.constVar(expr.ident('key'), expr.directCode('`${uniqueid}CustomResourceLogGroup`'));
const getProviderMethod = this.addMethod({
name: 'getProvider',
static: true,
Expand All @@ -268,6 +269,34 @@ export abstract class HandlerFrameworkClass extends ClassType {
stmt.ret(expr.directCode(`stack.node.tryFindChild(id) as ${this.type}`)),
);

const useLogGroupMethod = this.addMethod({
name: 'useLogGroup',
static: true,
docs: {
summary: 'Set the log group to be used by the singleton provider',
},
});
useLogGroupMethod.addParameter({
name: 'scope',
type: CONSTRUCTS_MODULE.Construct,
});
useLogGroupMethod.addParameter({
name: 'uniqueid',
type: Type.STRING,
});
useLogGroupMethod.addParameter({
name: 'logGroupName',
type: Type.STRING,
});
useLogGroupMethod.addBody(
stackFromScopeStatement,
logContextKeyStatement,
expr.directCode('stack.node.addMetadata(key, logGroupName)'),
stmt.constVar(expr.ident('existing'), expr.directCode('this.getProvider(scope, uniqueid)')),
stmt.if_(expr.directCode('existing'))
.then(expr.directCode('existing.configureLambdaLogGroup(logGroupName)')),
);

const getOrCreateProviderMethod = this.addMethod({
name: 'getOrCreateProvider',
static: true,
Expand All @@ -293,9 +322,14 @@ export abstract class HandlerFrameworkClass extends ClassType {
});
getOrCreateProviderMethod.addBody(
idStatement,
stmt.constVar(expr.ident('existing'), expr.directCode('this.getProvider(scope, uniqueid)')),
stackFromScopeStatement,
stmt.ret(expr.directCode(`existing ?? new ${this.name}(stack, id, props)`)),
stmt.constVar(expr.ident('provider'), expr.directCode(`this.getProvider(scope, uniqueid) ?? new ${this.name}(stack, id, props)`)),
logContextKeyStatement,
stmt.constVar(expr.ident('logGroupMetadata'),
expr.directCode('stack.node.metadata.find(m => m.type === key)')),
stmt.if_(expr.directCode('logGroupMetadata?.data'))
.then(expr.directCode('provider.configureLambdaLogGroup(logGroupMetadata.data)')),
stmt.ret(expr.directCode('provider')),
);

const superProps = new ObjectLiteral([
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,28 @@ export class TestProvider extends CustomResourceProviderBase {
return stack.node.tryFindChild(id) as TestProvider;
}

/**
* Set the log group to be used by the singleton provider
*/
public static useLogGroup(scope: Construct, uniqueid: string, logGroupName: string): void {
const stack = Stack.of(scope);
const key = `${uniqueid}CustomResourceLogGroup`;
stack.node.addMetadata(key, logGroupName);
const existing = this.getProvider(scope, uniqueid);
if (existing) existing.configureLambdaLogGroup(logGroupName);
}

/**
* Returns a stack-level singleton for the custom resource provider.
*/
public static getOrCreateProvider(scope: Construct, uniqueid: string, props?: CustomResourceProviderOptions): TestProvider {
const id = `${uniqueid}CustomResourceProvider`;
const existing = this.getProvider(scope, uniqueid);
const stack = Stack.of(scope);
return existing ?? new TestProvider(stack, id, props);
const provider = this.getProvider(scope, uniqueid) ?? new TestProvider(stack, id, props);
const key = `${uniqueid}CustomResourceLogGroup`;
const logGroupMetadata = stack.node.metadata.find(m => m.type === key);
if (logGroupMetadata?.data) provider.configureLambdaLogGroup(logGroupMetadata.data);
return provider;
}

public constructor(scope: Construct, id: string, props?: CustomResourceProviderOptions) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,28 @@ export class TestProvider extends CustomResourceProviderBase {
return stack.node.tryFindChild(id) as TestProvider;
}

/**
* Set the log group to be used by the singleton provider
*/
public static useLogGroup(scope: Construct, uniqueid: string, logGroupName: string): void {
const stack = Stack.of(scope);
const key = `${uniqueid}CustomResourceLogGroup`;
stack.node.addMetadata(key, logGroupName);
const existing = this.getProvider(scope, uniqueid);
if (existing) existing.configureLambdaLogGroup(logGroupName);
}

/**
* Returns a stack-level singleton for the custom resource provider.
*/
public static getOrCreateProvider(scope: Construct, uniqueid: string, props?: CustomResourceProviderOptions): TestProvider {
const id = `${uniqueid}CustomResourceProvider`;
const existing = this.getProvider(scope, uniqueid);
const stack = Stack.of(scope);
return existing ?? new TestProvider(stack, id, props);
const provider = this.getProvider(scope, uniqueid) ?? new TestProvider(stack, id, props);
const key = `${uniqueid}CustomResourceLogGroup`;
const logGroupMetadata = stack.node.metadata.find(m => m.type === key);
if (logGroupMetadata?.data) provider.configureLambdaLogGroup(logGroupMetadata.data);
return provider;
}

public constructor(scope: Construct, id: string, props?: CustomResourceProviderOptions) {
Expand Down
44 changes: 40 additions & 4 deletions packages/@aws-cdk/cx-api/FEATURE_FLAGS.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,9 @@ Flags come in three types:
| [@aws-cdk/aws-codepipeline:defaultPipelineTypeToV2](#aws-cdkaws-codepipelinedefaultpipelinetypetov2) | Enables Pipeline to set the default pipeline type to V2. | 2.133.0 | (default) |
| [@aws-cdk/aws-kms:reduceCrossAccountRegionPolicyScope](#aws-cdkaws-kmsreducecrossaccountregionpolicyscope) | When enabled, IAM Policy created from KMS key grant will reduce the resource scope to this key only. | 2.134.0 | (fix) |
| [@aws-cdk/aws-eks:nodegroupNameAttribute](#aws-cdkaws-eksnodegroupnameattribute) | When enabled, nodegroupName attribute of the provisioned EKS NodeGroup will not have the cluster name prefix. | 2.139.0 | (fix) |
| [@aws-cdk/aws-ec2:ebsDefaultGp3Volume](#aws-cdkaws-ec2ebsdefaultgp3volume) | When enabled, the default volume type of the EBS volume will be GP3 | V2NEXT | (default) |
| [@aws-cdk/aws-ec2:ebsDefaultGp3Volume](#aws-cdkaws-ec2ebsdefaultgp3volume) | When enabled, the default volume type of the EBS volume will be GP3 | 2.140.0 | (default) |
| [@aws-cdk/pipelines:reduceAssetRoleTrustScope](#aws-cdkpipelinesreduceassetroletrustscope) | Remove the root account principal from PipelineAssetsFileRole trust policy | 2.141.0 | (default) |
| [@aws-cdk/aws-ecs:removeDefaultDeploymentAlarm](#aws-cdkaws-ecsremovedefaultdeploymentalarm) | When enabled, remove default deployment alarm settings | 2.143.0 | (default) |

<!-- END table -->

Expand Down Expand Up @@ -128,7 +130,8 @@ The following json shows the current recommended set of flags, as `cdk init` wou
"@aws-cdk/aws-codepipeline:defaultPipelineTypeToV2": true,
"@aws-cdk/aws-kms:reduceCrossAccountRegionPolicyScope": true,
"@aws-cdk/aws-eks:nodegroupNameAttribute": true,
"@aws-cdk/aws-ec2:ebsDefaultGp3Volume": true
"@aws-cdk/aws-ec2:ebsDefaultGp3Volume": true,
"@aws-cdk/aws-ecs:removeDefaultDeploymentAlarm": true
}
}
```
Expand Down Expand Up @@ -171,6 +174,7 @@ are migrating a v1 CDK project to v2, explicitly set any of these flags which do
| [@aws-cdk/aws-apigateway:usagePlanKeyOrderInsensitiveId](#aws-cdkaws-apigatewayusageplankeyorderinsensitiveid) | Allow adding/removing multiple UsagePlanKeys independently | (fix) | 1.98.0 | `false` | `true` |
| [@aws-cdk/aws-lambda:recognizeVersionProps](#aws-cdkaws-lambdarecognizeversionprops) | Enable this feature flag to opt in to the updated logical id calculation for Lambda Version created using the `fn.currentVersion`. | (fix) | 1.106.0 | `false` | `true` |
| [@aws-cdk/aws-cloudfront:defaultSecurityPolicyTLSv1.2\_2021](#aws-cdkaws-cloudfrontdefaultsecuritypolicytlsv12_2021) | Enable this feature flag to have cloudfront distributions use the security policy TLSv1.2_2021 by default. | (fix) | 1.117.0 | `false` | `true` |
| [@aws-cdk/pipelines:reduceAssetRoleTrustScope](#aws-cdkpipelinesreduceassetroletrustscope) | Remove the root account principal from PipelineAssetsFileRole trust policy | (default) | | `false` | `true` |

<!-- END diff -->

Expand All @@ -185,7 +189,8 @@ Here is an example of a `cdk.json` file that restores v1 behavior for these flag
"@aws-cdk/aws-rds:lowercaseDbIdentifier": false,
"@aws-cdk/aws-apigateway:usagePlanKeyOrderInsensitiveId": false,
"@aws-cdk/aws-lambda:recognizeVersionProps": false,
"@aws-cdk/aws-cloudfront:defaultSecurityPolicyTLSv1.2_2021": false
"@aws-cdk/aws-cloudfront:defaultSecurityPolicyTLSv1.2_2021": false,
"@aws-cdk/pipelines:reduceAssetRoleTrustScope": false
}
}
```
Expand Down Expand Up @@ -1293,9 +1298,40 @@ When this featuer flag is enabled, the default volume type of the EBS volume wil
| Since | Default | Recommended |
| ----- | ----- | ----- |
| (not in v1) | | |
| V2NEXT | `false` | `true` |
| 2.140.0 | `false` | `true` |

**Compatibility with old behavior:** Pass `volumeType: EbsDeviceVolumeType.GENERAL_PURPOSE_SSD` to `Volume` construct to restore the previous behavior.


### @aws-cdk/pipelines:reduceAssetRoleTrustScope

*Remove the root account principal from PipelineAssetsFileRole trust policy* (default)

When this feature flag is enabled, the root account principal will not be added to the trust policy of asset role.
When this feature flag is disabled, it will keep the root account principal in the trust policy.


| Since | Default | Recommended |
| ----- | ----- | ----- |
| (not in v1) | | |
| 2.141.0 | `true` | `true` |

**Compatibility with old behavior:** Disable the feature flag to add the root account principal back


### @aws-cdk/aws-ecs:removeDefaultDeploymentAlarm

*When enabled, remove default deployment alarm settings* (default)

When this featuer flag is enabled, remove the default deployment alarm settings when creating a AWS ECS service.


| Since | Default | Recommended |
| ----- | ----- | ----- |
| (not in v1) | | |
| 2.143.0 | `false` | `true` |

**Compatibility with old behavior:** Set AWS::ECS::Service 'DeploymentAlarms' manually to restore the previous behavior.


<!-- END details -->

0 comments on commit 25d6fb2

Please sign in to comment.