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

fix(hcl2cdk): suffix variables and locals #845

Merged
merged 5 commits into from
Jul 29, 2021

Conversation

DanielMSchmidt
Copy link
Contributor

Input

variable "test" {
  type = string
}

locals {
  test = "${var.test} + 1"
}

output "test" {
  value = "${local.test}"
}

Output Before PR

import * as cdktf from "cdktf";
const test = new cdktf.TerraformVariable(this, "test", {});
const test = `\${${test.value}} + 1`;
new cdktf.TerraformOutput(this, "test", {
  value: test,
});

Output After PR

import * as cdktf from "cdktf";
const testVar = new cdktf.TerraformVariable(this, "test", {});
const testLocal = `\${${testVar.value}} + 1`;
new cdktf.TerraformOutput(this, "test", {
  value: testLocal,
});

@jsteinich
Copy link
Collaborator

What's the motivation for this change?

@DanielMSchmidt
Copy link
Contributor Author

@jsteinich When converting https://github.com/futurice/terraform-examples/tree/master/azure/azure_linux_docker_app_service I found that locals, variables, and outputs can have the same name. Previously we just used the name directly, this resulted in multiple variables with the same name. Now they are suffixed so no more collisions can happen

@jsteinich
Copy link
Collaborator

Locals are certainly often better as local variables, but I wonder if they should be translated that way or instead stay as TerraformLocal?

@jsteinich
Copy link
Collaborator

Previously we just used the name directly, this resulted in multiple variables with the same name. Now they are suffixed so no more collisions can happen

They will still collide in construct id space.

Probably other (if not all) variables that will encounter the same issue.

@DanielMSchmidt
Copy link
Contributor Author

@jsteinich Ah I see. We never translated locals into TerraformLocal so that's not new / the change in this PR. This one is just around variable naming. Is there a good reason to use TerraformLocal over local constants?

@DanielMSchmidt
Copy link
Contributor Author

I'm sorry, I meant locals and variables sharing the same name. Since locals are just constants there is no construct id involved.

@jsteinich
Copy link
Collaborator

Is there a good reason to use TerraformLocal over local constants?

Probably depends what the generation ends up looking like. As long as a Terraform expression is just passed along, then probably not.

I'm sorry, I meant locals and variables sharing the same name. Since locals are just constants there is no construct id involved.

The variable and the output in your example overlap.

@DanielMSchmidt
Copy link
Contributor Author

Probably depends what the generation ends up looking like. As long as a Terraform expression is just passed along, then probably not.

It is passed along, so no problem then 👍

The variable and the output in your example overlap.

Ah snap, I'll add this to the known issues or is there a nice and easy way around it?

@jsteinich
Copy link
Collaborator

or is there a nice and easy way around it?

You could do the same suffix that you are using for the variable name, though there's no guarantee that you won't then collide with a user named resource.

@DanielMSchmidt
Copy link
Contributor Author

We have this problem across all resources, I think using the same formula we use to get variable names for scopes makes sense so that they are all properly "namespaced" by their respective resource type

packages/@cdktf/hcl2cdk/lib/index.ts Outdated Show resolved Hide resolved
@jsteinich
Copy link
Collaborator

How feasible would it be to only do the variable qualifying if conflicts are detected?

@DanielMSchmidt
Copy link
Contributor Author

@jsteinich I think this is much better, thank you!

@DanielMSchmidt DanielMSchmidt force-pushed the fix-same-name-issue-in-conver branch 2 times, most recently from d1d3f38 to d4b25d9 Compare July 28, 2021 12:39
: [resource, name].join("_")
);
function validVarName(name: string) {
if (!Number.isNaN(parseInt(name[0], 10))) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

I didn't think just a number was valid in hcl, but I could imagine invalid names coming up, i.e. reserved keywords.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I am checking for reserved words in JS now. Rosetta will handle / check for reserved words in other languages afaik.

if (!Number.isNaN(parseInt(proposedName[0], 10))) {
return `d${proposedName}`;
// we only cache one per name
return validVarName(camelCase([resource, name].join("_")));
Copy link
Collaborator

Choose a reason for hiding this comment

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

Technically this could still collide, but probably don't need to worry about that.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

How could it collide? There can't be two identical resources with the same name in TF, can there?

packages/@cdktf/hcl2cdk/lib/index.ts Outdated Show resolved Hide resolved
@DanielMSchmidt DanielMSchmidt force-pushed the fix-same-name-issue-in-conver branch 3 times, most recently from a77fcb5 to 544386b Compare July 29, 2021 15:20
@ansgarm ansgarm merged commit e92a233 into main Jul 29, 2021
@ansgarm ansgarm deleted the fix-same-name-issue-in-conver branch July 29, 2021 16:26
@github-actions
Copy link
Contributor

github-actions bot commented Dec 9, 2022

I'm going to lock this pull request because it has been closed for 30 days. This helps our maintainers find and focus on the active issues. If you've found a problem that seems related to this change, please open a new issue and complete the issue template so we can capture all the details necessary to investigate further.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Dec 9, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants