Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: ignore resource tags #38

Merged
merged 1 commit into from
Aug 27, 2024
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
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