-
Notifications
You must be signed in to change notification settings - Fork 9.7k
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
character encoding and decoding functions #25387
Comments
Hi @r0bnet, As you've seen, Terraform strings are sequences of unicode characters rather than sequences of bytes, and as a consequence of that Terraform must always apply some sort of character encoding when moving to or from stored external data, such as files on disk. By convention, Terraform's own functions all use UTF-8 because that is a standard encoding that is relatively easy to create on all modern platforms, and the Terraform language itself is encoded in UTF-8. Of course, we do sometimes need to interact with other systems that have different expectations, and so the convention for that is to represent sequences of bytes via base64 encoding, thus allowing the byte sequence to be stored (encoded) in a Terraform language string. That's generally fine as long as Terraform is treating the byte sequence as opaque, but doesn't work for cases where you want Terraform to perform character-based operations (such as template rendering, in your case) because Terraform would then not be able to understand the template sequences within. Fortunately it looks like this With that said, my initial proposal would be to add two new functions to terraform, named
An implication of that design is that you would still need to encode the template itself in UTF-8 so that Terraform can interpret it, but then you can pass the result of template rendering to locals {
connect_file_share_script = templatefile("${path.module}/connect-azure-file-share.tpl.ps1", {
storage_account_file_host = azurerm_storage_account.storage_account.primary_file_host
storage_account_name = azurerm_storage_account.storage_account.name
storage_account_key = azurerm_storage_account.storage_account.primary_access_key
file_share_name = azurerm_storage_share.files.name
drive_letter = "Z"
})
}
resource "azurerm_virtual_machine_extension" "attach_file_share" {
name = "attach_file_share"
virtual_machine_id = azurerm_windows_virtual_machine.myvm.id
publisher = "Microsoft.Compute"
type = "CustomScriptExtension"
type_handler_version = "1.10"
settings = jsonencode({
commandToExecute = "powershell -EncodedCommand ${encodetextbase64(local.connect_file_share_script, "UTF-16")}"
})
} Terraform already has a (currently, indirect) dependency on |
Hey @apparentlymart, Thanks for your answer. This is definitely a good approach. I was thinking of doing something like that on server side (with the extension) but there's always this escaping stuff why I originally wanted to use the encoded command. |
Thanks for confirming that this proposal looks like it would meet your use-case, @r0bnet! This is not something that the Terraform team at HashiCorp will be able to work on in the near future because our focus is currently elsewhere, but if you or someone else would like to work on a proposed implementation then we'd be happy to review a pull request for it. |
Yeah, i'll try to create a PR for that because it would help me (and potentially others) a lot. Will update this issue with PR # when done. // Update: PR #25470 |
It looks like we neglected to close this out after merging #25470. Whoops! 🤦♂️ Thanks again for contributing the PR. 😀 |
I'm going to lock this issue because it has been closed for 30 days ⏳. This helps our maintainers find and focus on the active issues. |
Current Terraform Version
Use-cases
For example I want to create a VM in Azure with a Storage Account /w file share. After VM provisioning I want to attach that file share as mount. Problem is that the template file has to be UTF-8 encoded but PowerShell only accepts UTF-16 encoded strings when using
-EncodedCommand
.Attempted Solutions
connect-azure-file-share.tpl.ps1
Proposal
Not sure if that's currently possible as I can imagine that TF currently ONLY supports UTF-8 everywhere.
References
Terraform Error
PowerShell output
The text was updated successfully, but these errors were encountered: