Skip to content

Commit

Permalink
Merge pull request #38 from webratz/master
Browse files Browse the repository at this point in the history
feat: ignore resource tags
  • Loading branch information
hupe1980 authored Aug 27, 2024
2 parents f6317c1 + b3e908d commit 0907c3c
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 0 deletions.
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,17 @@ test('default setup', () => {
});
```

## Ignore Resource Tags

With this enabled, tags on all resources that have tags configured are ignored.
This can be useful in situations where tags contain changing informatione like the commit or a pipeline id.

```typescript
expect(stack).toMatchCdkSnapshot({
ignoreTags: true,
});
```

## License

[MIT](LICENSE)
13 changes: 13 additions & 0 deletions src/__tests__/__snapshots__/index.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,19 @@ exports[`subsetResourceTypes 1`] = `
}
`;
exports[`tags should not be included if skipped 1`] = `
{
"Resources": {
"FooDFE0DD70": {
"DeletionPolicy": "Retain",
"Properties": {},
"Type": "AWS::S3::Bucket",
"UpdateReplacePolicy": "Retain",
},
},
}
`;
exports[`yaml setup 1`] = `
"Resources:
FooDFE0DD70:
Expand Down
12 changes: 12 additions & 0 deletions src/__tests__/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
CfnResource,
IAspect,
Stack,
Tags,
} from "aws-cdk-lib";
import { Code, Function, Runtime } from "aws-cdk-lib/aws-lambda";
import { AccountPrincipal } from "aws-cdk-lib/aws-iam";
Expand Down Expand Up @@ -181,3 +182,14 @@ test("metadata should not be included if skipped", () => {
ignoreMetadata: true,
});
});

test("tags should not be included if skipped", () => {
const stack = new Stack();
const bucket = new Bucket(stack, "Foo");

Tags.of(bucket).add("dummy", "test");

expect(stack).toMatchCdkSnapshot({
ignoreTags: true,
});
});
15 changes: 15 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,11 @@ export type Options = StageSynthesisOptions & {
* Ignore Metadata
*/
ignoreMetadata?: boolean;

/**
* Ignore Tags on resources
*/
ignoreTags?: boolean;
};

const currentVersionRegex = /^(.+CurrentVersion[0-9A-F]{8})[0-9a-f]{32}$/;
Expand Down Expand Up @@ -127,6 +132,7 @@ const convertStack = (stack: Stack, options: Options = {}) => {
ignoreBootstrapVersion = true,
ignoreCurrentVersion = false,
ignoreMetadata = false,
ignoreTags = false,
subsetResourceTypes,
subsetResourceKeys,
...synthOptions
Expand Down Expand Up @@ -203,6 +209,15 @@ const convertStack = (stack: Stack, options: Options = {}) => {
});
}

if (ignoreTags && template.Resources) {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
Object.values(template.Resources).forEach((resource: any) => {
if (resource?.Properties?.Tags) {
delete resource.Properties.Tags;
}
});
}

return yaml ? jsYaml.safeDump(template) : template;
};

Expand Down

0 comments on commit 0907c3c

Please sign in to comment.