Skip to content

Commit

Permalink
fic(hcl2cdk): use fqn if no property is accessed
Browse files Browse the repository at this point in the history
  • Loading branch information
DanielMSchmidt committed Jul 13, 2021
1 parent 172c37a commit 56d65f8
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 22 deletions.
2 changes: 1 addition & 1 deletion packages/@cdktf/hcl2cdk/lib/expressions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ export function referenceToAst(ref: Reference) {
variableReference as t.Expression
);

if (ref.useFqn) {
if (ref.useFqn || selector.length === 0) {
return t.memberExpression(accessor, t.identifier("fqn"));
} else {
return accessor;
Expand Down
8 changes: 0 additions & 8 deletions packages/@cdktf/hcl2cdk/lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,6 @@ const valueToTs = (item: any, nodeIds: readonly string[]): t.Expression => {
return t.arrayExpression(item.map((i) => valueToTs(i, nodeIds)));
}

if (Object.keys(item).includes("count")) {
throw new Error(
`Unsupported Terraform feature found: count loops are not yet supported: ${JSON.stringify(
item
)}`
);
}

return t.objectExpression(
Object.entries(item)
.filter(([_key, value]) => value !== undefined)
Expand Down
26 changes: 13 additions & 13 deletions packages/@cdktf/hcl2cdk/test/__snapshots__/hcl2cdk.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ exports[`convert Cross-Language Support supports typescript 1`] = `Promise {}`;
exports[`convert arithmetics configuration 1`] = `
"const admins = new TerraformVariable(this, \\"admins\\", {});
const members = new TerraformVariable(this, \\"members\\", {});
const users = \`\\\\\${\${members} + \${admins}}\`;
const users = \`\\\\\${\${members.fqn} + \${admins.fqn}}\`;
"
`;

Expand Down Expand Up @@ -159,13 +159,13 @@ new aws.S3BucketObject(this, \\"examplebucket_object\\", {
exports[`convert count loops configuration 1`] = `
"const users = new TerraformVariable(this, \\"users\\", {});
new aws.IamUser(this, \\"lb\\", {
name: \`\\\\\${element(\${users}, count.index)}\`,
name: \`\\\\\${element(\${users.fqn}, count.index)}\`,
path: \\"/system/\\",
tags: {
tagKey: \\"tag-value\\",
},
});
awsIamUserAwsIamUserLb.addOverride(\\"count\\", \`\\\\\${length(\${users})}\`);
awsIamUserAwsIamUserLb.addOverride(\\"count\\", \`\\\\\${length(\${users.fqn})}\`);
"
`;
Expand All @@ -175,7 +175,7 @@ exports[`convert data references configuration 1`] = `
});
const dataAwsS3Bucket = new aws.S3Bucket(this, \\"examplebucket\\", {
acl: \\"private\\",
bucket: bucketName,
bucket: bucketName.fqn,
});
new aws.S3BucketObject(this, \\"examplebucket_object\\", {
bucket: dataAwsS3Bucket.examplebucket.arn,
Expand All @@ -191,9 +191,9 @@ exports[`convert double references configuration 1`] = `
});
new aws.S3Bucket(this, \\"examplebucket\\", {
acl: \\"private\\",
bucket: bucketName,
bucket: bucketName.fqn,
tags: {
tagKey: bucketName,
tagKey: bucketName.fqn,
},
});
"
Expand Down Expand Up @@ -239,11 +239,11 @@ awsS3BucketObjectAwsS3BucketObjectExamplebucketObject.addOverride(
exports[`convert for expression 1 configuration 1`] = `
"const users = new TerraformVariable(this, \\"users\\", {});
const adminUsers = \`\\\\\${{
for name, user in \${users} : name => user
for name, user in \${users.fqn} : name => user
if user.is_admin
}}\`;
const regularUsers = \`\\\\\${{
for name, user in \${users} : name => user
for name, user in \${users.fqn} : name => user
if !user.is_admin
}}\`;
"
Expand All @@ -252,7 +252,7 @@ const regularUsers = \`\\\\\${{
exports[`convert for expression 2 configuration 1`] = `
"const users = new TerraformVariable(this, \\"users\\", {});
const usersByRole = \`\\\\\${{
for name, user in \${users} : user.role => name...
for name, user in \${users.fqn} : user.role => name...
}}\`;
"
`;
Expand All @@ -266,7 +266,7 @@ new aws.IamUser(this, \\"lb\\", {
tagKey: \\"tag-value\\",
},
});
awsIamUserAwsIamUserLb.addOverride(\\"for_each\\", users);
awsIamUserAwsIamUserLb.addOverride(\\"for_each\\", users.fqn);
"
`;
Expand All @@ -291,7 +291,7 @@ exports[`convert locals references configuration 1`] = `
"const bucketName = \\"foo\\";
new aws.S3Bucket(this, \\"examplebucket\\", {
acl: \\"private\\",
bucket: bucketName,
bucket: bucketName.fqn,
});
"
`;
Expand Down Expand Up @@ -424,7 +424,7 @@ const awsS3BucketExamplebucket = new aws.S3Bucket(this, \\"examplebucket\\", {
bucket: \\"examplebuckettftest\\",
});
new aws.S3BucketObject(this, \\"examplebucket_object\\", {
bucket: \`\\\\\${element(\${awsS3BucketExamplebucket}, 0).id}\`,
bucket: \`\\\\\${element(\${awsS3BucketExamplebucket.fqn}, 0).id}\`,
key: \\"someobject\\",
kmsKeyId: awsKmsKeyExamplekms.arn,
source: \\"index.html\\",
Expand Down Expand Up @@ -465,7 +465,7 @@ exports[`convert variable references configuration 1`] = `
});
new aws.S3Bucket(this, \\"examplebucket\\", {
acl: \\"private\\",
bucket: bucketName,
bucket: bucketName.fqn,
});
"
`;
Expand Down

0 comments on commit 56d65f8

Please sign in to comment.