warrensbox/terraform-switcher

Implement support for multiple Terraform version constraints from the module directory

Closed this issue · 0 comments

When parsing Terraform version constraint from the module directory, only the first constraint found is taken into account, whilst Terraform configuration supports multiple terraform {} configuration blocks and each may have its own required_version parameter.
To improve Terraform versions detection the code needs to be adjusted to gather all the constraints and leverage combined constraint string to detect allowed required Terraform version.

Refs:

  • Relevant existing code:
    tfconstraint := module.RequiredCore[0] //we skip duplicated definitions and use only first one
  • Discussion: #356 (comment)
  • Simplified implementation example:
    func installTFProvidedModule(dir string, custBinPath, mirrorURL *string) {
        fmt.Printf("Reading required version from terraform file\n")
        module, _ := tfconfig.LoadModule(dir)
        var tfconstraints []string
        for key := range module.RequiredCore {
            tfconstraints = append(tfconstraints, module.RequiredCore[key])
        }
        tfconstraint := strings.Join(tfconstraints, ", ")
        installFromConstraint(&tfconstraint, custBinPath, mirrorURL)
    }