Skip to content

Commit

Permalink
reset version for terraform_workflow_tool too
Browse files Browse the repository at this point in the history
  • Loading branch information
truszkowski committed May 17, 2024
1 parent 057cd04 commit 257df16
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 2 deletions.
23 changes: 21 additions & 2 deletions spacelift/resource_stack.go
Original file line number Diff line number Diff line change
Expand Up @@ -973,7 +973,7 @@ func getVendorConfig(d *schema.ResourceData) *structs.VendorConfigInput {
terragruntConfig.Tool = toOptionalString(tool)
}

if shouldWeReComputeTerraformVersion(d) {
if shouldWeReComputeTerraformVersionForTerragrunt(d) {
terragruntConfig.TerraformVersion = nil
}

Expand All @@ -990,6 +990,9 @@ func getVendorConfig(d *schema.ResourceData) *structs.VendorConfigInput {

if terraformWorkflowTool, ok := d.GetOk("terraform_workflow_tool"); ok {
terraformConfig.WorkflowTool = toOptionalString(terraformWorkflowTool)
if shouldWeReComputeTerraformVersionForTerraformWorkflowTool(d) {
terraformConfig.Version = nil
}
}

if terraformWorkspace, ok := d.GetOk("terraform_workspace"); ok {
Expand All @@ -1011,7 +1014,7 @@ func getVendorConfig(d *schema.ResourceData) *structs.VendorConfigInput {
return &structs.VendorConfigInput{Terraform: terraformConfig}
}

func shouldWeReComputeTerraformVersion(d *schema.ResourceData) bool {
func shouldWeReComputeTerraformVersionForTerragrunt(d *schema.ResourceData) bool {
// When tool is changed, we need to recompute terraform version
oldTool, newTool := d.GetChange("terragrunt.0.tool")
if oldTool.(string) != newTool.(string) {
Expand All @@ -1027,6 +1030,22 @@ func shouldWeReComputeTerraformVersion(d *schema.ResourceData) bool {
return false
}

func shouldWeReComputeTerraformVersionForTerraformWorkflowTool(d *schema.ResourceData) bool {
// When tool is changed, we need to recompute terraform version
oldTool, newTool := d.GetChange("terraform_workflow_tool")
if oldTool.(string) != newTool.(string) {
// but only if version isn't provided manually in the config
inConfig := d.GetRawConfig().AsValueMap()
if value, ok := inConfig["terraform_version"]; ok {
if value.IsNull() || value.AsString() == "" {
return true
}
}
}

return false
}

func getStrings(d *schema.ResourceData, fieldName string) []graphql.String {
values := []graphql.String{}
if commands, ok := d.GetOk(fieldName); ok {
Expand Down
37 changes: 37 additions & 0 deletions spacelift/resource_stack_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1488,6 +1488,43 @@ func TestStackResourceSpace(t *testing.T) {
Attribute("terraform_workflow_tool", Equals("CUSTOM")),
),
},
// Check to change from TERRAFORM_FOSS to CUSTOM with a specific version
// Actually, we don't need to specify the version, but when we don't do it, it will be
// evaluated on the first run. So, it's simpler just to specify a version for that test case.
{
Config: fmt.Sprintf(`
resource "spacelift_stack" "terraform_workflow_tool_custom_with_run" {
branch = "master"
name = "Provider test stack workflow_tool with run %s"
project_root = "root"
repository = "demo"
terraform_workflow_tool = "TERRAFORM_FOSS"
terraform_version = "1.5.7"
autodeploy = true
}
`, randomID),
Check: Resource(
"spacelift_stack.terraform_workflow_tool_custom_with_run",
Attribute("terraform_workflow_tool", Equals("TERRAFORM_FOSS")),
),
},
{
Config: fmt.Sprintf(`
resource "spacelift_stack" "terraform_workflow_tool_custom_with_run" {
branch = "master"
name = "Provider test stack workflow_tool with run %s"
project_root = "root"
repository = "demo"
terraform_workflow_tool = "CUSTOM"
terraform_version = ""
autodeploy = true
}
`, randomID),
Check: Resource(
"spacelift_stack.terraform_workflow_tool_custom_with_run",
Attribute("terraform_workflow_tool", Equals("CUSTOM")),
),
},
})
})
}
Expand Down

0 comments on commit 257df16

Please sign in to comment.