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

Stack wide setting to use construct ids as names #322

Closed
wants to merge 1 commit into from
Closed
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
4 changes: 4 additions & 0 deletions packages/cdktf/lib/terraform-element.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ export class TerraformElement extends Construct {

public get friendlyUniqueId() {
const node = this.constructNode
if (this.stack.useStaticNames) {
return node.id;
}

const components = node.scopes.slice(1).map(c => Node.of(c).id);
return components.length > 0 ? makeUniqueId(components) : '';
}
Expand Down
9 changes: 9 additions & 0 deletions packages/cdktf/lib/terraform-stack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export class TerraformStack extends Construct {
public readonly artifactFile: string;
private readonly rawOverrides: any = {}
private readonly cdktfVersion: string;
private staticNames = false;

constructor(scope: Construct, id: string) {
super(scope, id);
Expand Down Expand Up @@ -72,6 +73,14 @@ export class TerraformStack extends Construct {
curr[lastKey] = value;
}

public useConstructIdsAsNames() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Couldn't this be an option for the constructor?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It could be. I was going for more of a this is something non-default to opt in to, but maybe that's just my Java background showing through.

Or maybe this is actually more of a long term different mode that people would continue to use than just as a transition.

this.staticNames = true;
}

public get useStaticNames(): boolean {
return this.staticNames;
}

public allProviders(): TerraformProvider[] {
const providers: TerraformProvider[] = [];

Expand Down
88 changes: 88 additions & 0 deletions packages/cdktf/test/__snapshots__/stack.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -87,3 +87,91 @@ exports[`stack synthesis merges all elements into a single output 1`] = `
}
}"
`;

exports[`use static names 1`] = `
"{
\\"//\\": {
\\"metadata\\": {
\\"version\\": \\"stubbed\\",
\\"stackName\\": \\"MyStack\\"
}
},
\\"provider\\": {
\\"test\\": [
{
\\"access_key\\": \\"foo\\"
}
]
},
\\"resource\\": {
\\"aws_bucket\\": {
\\"Resource1\\": {
\\"foo\\": \\"Resource1\\",
\\"prop1\\": \\"bar1\\",
\\"prop2\\": 1234,
\\"prop3\\": {
\\"name\\": \\"should be overwritten in resource 2\\",
\\"value\\": 5678
},
\\"//\\": {
\\"metadata\\": {
\\"path\\": \\"MyStack/Resource1\\",
\\"uniqueId\\": \\"Resource1\\"
}
}
}
},
\\"aws_topic\\": {
\\"Resource2\\": {
\\"foo\\": \\"Resource2\\",
\\"prop1\\": \\"bar1\\",
\\"prop3\\": {
\\"name\\": \\"test\\",
\\"value\\": 5678
},
\\"//\\": {
\\"metadata\\": {
\\"path\\": \\"MyStack/Resource2\\",
\\"uniqueId\\": \\"Resource2\\"
}
},
\\"provisioner\\": [
{
\\"local-exec\\": {
\\"command\\": \\"echo 'Hello World' >example.txt\\"
}
}
]
}
}
},
\\"module\\": {
\\"EksModule\\": {
\\"cluster_name\\": \\"my_cluster_name\\",
\\"source\\": \\"terraform-aws-modules/eks/aws\\",
\\"version\\": \\"7.0.1\\",
\\"//\\": {
\\"metadata\\": {
\\"path\\": \\"MyStack/EksModule\\",
\\"uniqueId\\": \\"EksModule\\"
}
}
}
},
\\"output\\": {
\\"eks_version\\": {
\\"value\\": \\"7.0.1\\"
}
},
\\"terraform\\": {
\\"backend\\": {
\\"remote\\": {
\\"organization\\": \\"test\\",
\\"workspaces\\": {
\\"name\\": \\"test\\"
}
}
}
}
}"
`;
46 changes: 46 additions & 0 deletions packages/cdktf/test/stack.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,52 @@ test('stack synthesis merges all elements into a single output', () => {
expect(Testing.synth(stack)).toMatchSnapshot();
});

test('use static names', () => {
const app = Testing.stubVersion(new App({stackTraces: false}));
const stack = new TerraformStack(app, 'MyStack');
stack.useConstructIdsAsNames();

new TestProvider(stack, 'test-provider', {
accessKey: 'foo'
})

new MyResource(stack, 'Resource1', {
terraformResourceType: 'aws_bucket'
});

const overrideResource = new MyResource(stack, 'Resource2', {
terraformResourceType: 'aws_topic',
})
overrideResource.addOverride('//', 'this is a comment');
overrideResource.addOverride('prop2', undefined);
overrideResource.addOverride('prop3.name', 'test');
overrideResource.addOverride('provisioner', [{
'local-exec': {
command: "echo 'Hello World' >example.txt"
}
}]);

const eks = new MyModule(stack, 'EksModule', {
source: 'terraform-aws-modules/eks/aws',
version: '7.0.1',
});

new TerraformOutput(stack, "eks_version", {
value: eks.version
})

stack.addOverride('terraform.backend', {
remote: {
organization: 'test',
workspaces: {
name: 'test'
}
}
});

expect(Testing.synth(stack)).toMatchSnapshot();
});

class MyModule extends TerraformModule {
protected synthesizeAttributes() {
return {
Expand Down