Skip to content

Commit

Permalink
feat: configuration by resource ids (#284)
Browse files Browse the repository at this point in the history
* check for resource id in path

* absent checkers in test

* absent checkers in test

* adding test for long nested paths

* replacing includes with regex
  • Loading branch information
malachi-constant committed Mar 23, 2023
1 parent b2dc1f7 commit 6b0120f
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 4 deletions.
4 changes: 3 additions & 1 deletion src/config/configurator.ts
Expand Up @@ -116,7 +116,9 @@ class ConfiguratorAspect implements cdk.IAspect {
node.addPropertyOverride(this.propertyName, this.propertyValue);
}

if (this.resourceId && cdk.CfnResource.isCfnResource(node)) {
const nodePathItemRegex = new RegExp(`^(.*\/)?(${this.resourceId})(\/.*)?$`);

if (this.resourceId && cdk.CfnResource.isCfnResource(node) && nodePathItemRegex.test(node.node.path)) {
node.addPropertyOverride(this.propertyName, this.propertyValue);
}
}
Expand Down
26 changes: 23 additions & 3 deletions test/config.test.ts
@@ -1,10 +1,11 @@
import { assert } from "console";
import path from "path";
import * as cdk from "aws-cdk-lib";
import { Template } from "aws-cdk-lib/assertions";
import { Match, Template } from "aws-cdk-lib/assertions";
import * as lambda from "aws-cdk-lib/aws-lambda";
import * as s3 from "aws-cdk-lib/aws-s3";
import * as sqs from "aws-cdk-lib/aws-sqs";
import { Construct } from "constructs";

import {
Configurator,
Expand Down Expand Up @@ -58,13 +59,22 @@ test("Config Simple Override", () => {
});

test("Config Override By Id", () => {
class NestedStack extends cdk.Stack {
constructor(scope: Construct, id: string) {
super(scope, id);
new s3.Bucket(this, "AVeryLongBucketNameInsideANestedStack");
}
}
const sampleConfig = {
environments: {
dev: {
resources: {
MyBucket: {
BucketName: "my-exact-bucket-name-for-this-resource",
},
AVeryLongBucketNameInsideANestedStack: {
BucketName: "override-bucket-name",
},
MyQueue: {
KmsMasterKeyId: "alias/aws/sqs",
},
Expand All @@ -75,16 +85,26 @@ test("Config Override By Id", () => {
const stack = new cdk.Stack();
new s3.Bucket(stack, "MyBucket");
new sqs.Queue(stack, "MyQueue");

new sqs.Queue(stack, "MyUnencryptedQueue");
new Configurator(stack, sampleConfig, "dev");

const template = Template.fromStack(stack);
template.hasResourceProperties("AWS::S3::Bucket", {
BucketName: "my-exact-bucket-name-for-this-resource",
});
template.hasResourceProperties("AWS::SQS::Queue", {
KmsMasterKeyId: "alias/aws/sqs",
});
template.hasResourceProperties("AWS::SQS::Queue", {
KmsMasterKeyId: Match.absent(),
});

const app = new cdk.App();
const nestedStack = new NestedStack(app, "PlaceholderWithExcessivelyLongNameABCDEFGHIJFKLMNOPQRSTUVXXYZ");
new Configurator(nestedStack, sampleConfig, "dev");
const nestedTemplate = Template.fromStack(nestedStack);
nestedTemplate.hasResourceProperties("AWS::S3::Bucket", {
BucketName: "override-bucket-name",
});
});

test("Different values per environment", () => {
Expand Down

0 comments on commit 6b0120f

Please sign in to comment.