Skip to content

Commit

Permalink
fix(cdktf): support dot-seperated nested accessors
Browse files Browse the repository at this point in the history
Closes #3420
  • Loading branch information
DanielMSchmidt committed Jan 19, 2024
1 parent f311d86 commit 3ff1890
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 1 deletion.
5 changes: 4 additions & 1 deletion packages/cdktf/lib/terraform-provisioner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -353,7 +353,10 @@ export interface RemoteExecProvisioner {
*/
export class TerraformSelf {
private static getInterpolation(key: string) {
return `self.${snakeCase(key)}`;
return `self.${key
.split(".")
.map((s) => snakeCase(s))
.join(".")}`;
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`self with 1`] = `
"{
"provider": {
"test": [
{
}
]
},
"resource": {
"test_resource": {
"bar": {
"name": "test",
"provisioner": [
{
"local-exec": {
"command": "say 'hello world'",
"environment": {
"ECR_IMAGE_COMMAND": "delete",
"ECR_IMAGE_SOURCE_IMAGE": "\${self.triggers_replace.source_image}",
"ECR_IMAGE_TARGET_IMAGE": "\${self.image_name_with_hash}",
"ECR_IMAGE_TARGET_REGION": "\${self.triggers_replace.target_region}"
},
"interpreter": [
"bash"
],
"when": "destroy"
}
}
]
}
}
},
"terraform": {
"required_providers": {
"test": {
"version": "~> 2.0"
}
}
}
}"
`;
35 changes: 35 additions & 0 deletions packages/cdktf/test/terraform-provisioner.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// Copyright (c) HashiCorp, Inc
// SPDX-License-Identifier: MPL-2.0
import { Testing, TerraformStack, TerraformSelf } from "../lib";
import { TestResource } from "./helper";
import { TestProvider } from "./helper/provider";

test("self with ", () => {
const app = Testing.app();
const stack = new TerraformStack(app, "test");
new TestProvider(stack, "foo", {});
new TestResource(stack, "bar", {
name: "test",
provisioners: [
{
when: "destroy",
type: "local-exec",
environment: {
ECR_IMAGE_COMMAND: "delete",
ECR_IMAGE_SOURCE_IMAGE: TerraformSelf.getString(
"triggers_replace.sourceImage"
),
ECR_IMAGE_TARGET_IMAGE: TerraformSelf.getString(
"image_name_with_hash"
),
ECR_IMAGE_TARGET_REGION: TerraformSelf.getString(
"triggers_replace.target_region"
),
},
interpreter: ["bash"],
command: "say 'hello world'",
},
],
});
expect(Testing.synth(stack)).toMatchSnapshot();
});

0 comments on commit 3ff1890

Please sign in to comment.