gruntwork-io / terragrunt

Terragrunt is a flexible orchestration tool that allows Infrastructure as Code written in OpenTofu/Terraform to scale.
https://terragrunt.gruntwork.io/
MIT License
8.04k stars 975 forks source link

Scaffold command not handling well multiline description #3264

Closed carnei-ro closed 3 months ago

carnei-ro commented 3 months ago

Describe the bug

terragrunt scaffold automatically reads the variables of a module and adds the "Description" of the variable as a comment into the generated terraform.hcl. The problem happens when the Description of the variable has multiple lines.

Steps To Reproduce

Steps to reproduce the behavior, code snippets and examples which can be used to reproduce the issue.

terragrunt scaffold "git::https://github.com/Azure/terraform-azurerm-avm-res-compute-virtualmachine.git//.?ref=v0.15.0"

Expected behavior

Somehow the multiline description should also be commented out in the generated terragrunt.hcl file.

Nice to haves

ERRO[0003] on /tmp//a/terragrunt.hcl line 30: ERRO[0003] 30: - <map key> - Use a custom map key to define each network interface ERRO[0003]
ERRO[0003] The "`" character is not valid. To create a multi-line string, use the "heredoc" syntax, like "<<EOT".

ERRO[0003] Error: Invalid character

ERRO[0003] on /tmp//a/terragrunt.hcl line 31: ERRO[0003] 31: - name = (Required) The name of the Network Interface. Changing this forces a new resource to be created. ERRO[0003]
ERRO[0003] The "`" character is not valid. To create a multi-line string, use the "heredoc" syntax, like "<<EOT".

ERRO[0003] Error: Invalid character

ERRO[0003] on /tmp//a/terragrunt.hcl line 48: ERRO[0003] 48: - private_ip_address_allocation = (Optional) - The allocation method used for the Private IP Address. Possible values are Dynamic and Static. Dynamic means "An IP is automatically assigned during creation of this Network Interface" and is the default; Static means "User supplied IP address will be used"
ERRO[0003]
ERRO[0003] The ";" character is not valid. Use newlines to separate arguments and blocks, and commas to separate items in collection values.

ERRO[0003] Error: Invalid character

ERRO[0003] on /tmp//a/terragrunt.hcl line 348: ERRO[0003] 348: - disk_size_gb (Optional) - (Required for a new managed disk) - Specifies the size of the managed disk to create in gigabytes. If create_option is Copy or FromImage, then the value must be equal to or greater than the source's size. The size can only be increased.If No Downtime Resizing is not available, be aware that changing this value is disruptive if the disk is attached to a Virtual Machine. The VM will be shut down and de-allocated as required by Azure to action the change. Terraform will attempt to start the machine again after the update if it was in a running state when the apply was started. When upgrading disk_size_gb from value less than 4095 to a value greater than 4095, the disk will be detached from its associated Virtual Machine as required by Azure to action the change. Terraform will attempt to reattach the disk again after the update. ERRO[0003]
ERRO[0003] Single quotes are not valid. Use double quotes (") to enclose strings.

ERRO[0003] Error: Invalid character

ERRO[0003] on /tmp//a/terragrunt.hcl line 483: ERRO[0003] 483: - protected_settings (Optional) - The protected_settings passed to the extension, like settings, these are specified as a JSON object in a string. Certain VM Extensions require that the keys in the protected_settings block are case sensitive. If you're seeing unhelpful errors, please ensure the keys are consistent with how Azure is expecting them (for instance, for the JsonADDomainExtension extension, the keys are expected to be in TitleCase.) ERRO[0003]
ERRO[0003] Single quotes are not valid. Use double quotes (") to enclose strings.

ERRO[0003] Error: Invalid expression

ERRO[0003] on /tmp//a/terragrunt.hcl line 30: ERRO[0003] 30: - <map key> - Use a custom map key to define each network interface ERRO[0003]
ERRO[0003] Expected the start of an expression, but found an invalid expression token.

ERRO[0003] Error parsing /tmp//a/terragrunt.hcl ERRO[0003] 1 error occurred:

ERRO[0003] Unable to determine underlying exit code, so Terragrunt will exit with error code 1


- [ ] Screenshots

## Versions

- Terragrunt version: 0.58.3
- OpenTofu/Terraform version: OpenTofu 1.7.1
- Environment details (Ubuntu 20.04, Windows 10, etc.): MacOS 14.5
carnei-ro commented 3 months ago

The following boilerplate seems to do the job.

If the description has only one line, it is used as normal. If the description has multiple lines, it gets wrapped in a new line with a comment and 4 empty spaces, like this:

  # Description: Should public network access be enabled for the Web App. Defaults to true.
  # Type: bool
  # public_network_access_enabled = null

  # Description:
  #     An identity block supports the following:
  #     
  #       type - (Required) The Managed Service Identity Type to use for this Linux Web App. Possible values are SystemAssigned, UserAssigned, and SystemAssigned, UserAssigned.
  #       identity_ids - (Optional) A list of User Assigned Identity IDs to assign to this Linux Web App. Required if type is UserAssigned.    
  # Type: object
  # identity = null
terraform {
  source = "{{ .sourceUrl }}"
}

include "root" {
  path = find_in_parent_folders()
}

inputs = {
  # --------------------------------------------------------------------------------------------------------------------
  # Required input variables
  # --------------------------------------------------------------------------------------------------------------------
  {{ range .requiredVariables }}
  {{- if eq 1 (regexSplit "\n" .Description -1 | len ) }}
  # Description: {{ .Description }}
  {{- else }}
  # Description:
    {{- range $line := regexSplit "\n" .Description -1 }}
    # {{ $line | indent 4 }}
    {{- end }}
  {{- end }}
  # Type: {{ .Type }}
  {{ .Name }} = {{ .DefaultValuePlaceholder }}  # TODO: fill in value
  {{ end }}

  # --------------------------------------------------------------------------------------------------------------------
  # Optional input variables
  # Uncomment the ones you wish to set
  # --------------------------------------------------------------------------------------------------------------------
  {{ range .optionalVariables }}
  {{- if eq 1 (regexSplit "\n" .Description -1 | len ) }}
  # Description: {{ .Description }}
  {{- else }}
  # Description:
    {{- range $line := regexSplit "\n" .Description -1 }}
    # {{ $line | indent 4 }}
    {{- end }}
  {{- end }}
  # Type: {{ .Type }}
  # {{ .Name }} = {{ .DefaultValue }}
  {{ end }}
}
denis256 commented 3 months ago

Included fix in https://github.com/gruntwork-io/terragrunt/releases/tag/v0.62.3