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

Escape Terraform Unsafe Characters instead of Replacing with -- #263

Conversation

t0rr3sp3dr0
Copy link
Contributor

@t0rr3sp3dr0 t0rr3sp3dr0 commented Oct 28, 2019

Previously, characters that could not be used in Terraform identifiers were replaced by --. This behavior cloud lead to a resource name collision, for example (example) and [example] would generate --example--.

Instead of replacing those characters with a fixed string, we can escape those characters making any two different string generate a unique Terraform identifier.

As specified in Terraform's documentation, - character is a valid identifier character, although it is not recommended to be used and _ should be used instead. So I used - as an escape character.

Everything that is not _ and is not between 0 and 9, A and Z, and a and z should be escaped, - included. To escape characters we could simply use the character's Unicode value, so even emojis could be used on resources' identifier and generate a safe Terraform identifier.

  • ~ would become -007E-
  • ! would become -0021-
  • @ would become -0040-
  • - would become -002D-

If a resource was named as 3x 4mp !3 it now would be identified as tfer--3x-0020-4mp-0009--0021-3 on Terraform.

Signed-off-by: Pedro Tôrres <t0rr3sp3dr0@gmail.com>
@sergeylanzman sergeylanzman merged commit 7b42fa9 into GoogleCloudPlatform:master Oct 28, 2019
@t0rr3sp3dr0 t0rr3sp3dr0 deleted the feat/tf-sanitize-with-escape branch October 29, 2019 01:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants