Skip to content

Commit

Permalink
Run terraform init -reconfigure when running terraform workspace.…
Browse files Browse the repository at this point in the history
… Fix infrastructure stack name in Spacelift processor (#75)

* Run `terraform init -reconfigure` when running `terraform workspace` command

* Fix infrastructure stack name in Spacelift processor
  • Loading branch information
aknysh committed Nov 9, 2021
1 parent 846ced7 commit 2cfb75b
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 9 deletions.
7 changes: 5 additions & 2 deletions internal/exec/terraform.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,13 +158,16 @@ func ExecuteTerraform(cmd *cobra.Command, args []string) error {
// Run `terraform init`
runTerraformInit := true
if info.SubCommand == "init" ||
info.SubCommand == "workspace" ||
info.SubCommand == "clean" ||
(info.SubCommand == "deploy" && c.Config.Components.Terraform.DeployRunInit == false) {
runTerraformInit = false
}
if runTerraformInit == true {
err = execCommand(info.Command, []string{"init"}, componentPath, nil)
initCommandWithArguments := []string{"init"}
if info.SubCommand == "workspace" {
initCommandWithArguments = []string{"init", "-reconfigure"}
}
err = execCommand(info.Command, initCommandWithArguments, componentPath, nil)
if err != nil {
return err
}
Expand Down
5 changes: 2 additions & 3 deletions pkg/spacelift/spacelift_stack_processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -347,10 +347,8 @@ func TransformStackConfigToSpaceliftStacks(
return nil, err
}

spaceliftStackName := strings.Replace(fmt.Sprintf("%s-%s", contextPrefix, component), "/", "-", -1)

spaceliftConfig["component"] = component
spaceliftConfig["stack"] = spaceliftStackName
spaceliftConfig["stack"] = contextPrefix
spaceliftConfig["imports"] = imports
spaceliftConfig["vars"] = componentVars
spaceliftConfig["settings"] = componentSettings
Expand Down Expand Up @@ -426,6 +424,7 @@ func TransformStackConfigToSpaceliftStacks(
spaceliftConfig["labels"] = u.UniqueStrings(labels)

// Add Spacelift stack config to the final map
spaceliftStackName := strings.Replace(fmt.Sprintf("%s-%s", contextPrefix, component), "/", "-", -1)
res[spaceliftStackName] = spaceliftConfig
}
}
Expand Down
8 changes: 4 additions & 4 deletions pkg/spacelift/spacelift_stack_processor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,20 @@ func TestSpaceliftStackProcessor(t *testing.T) {
assert.Equal(t, 24, len(spaceliftStacks))

tenant1Ue2DevInfraVpcStack := spaceliftStacks["tenant1-ue2-dev-infra-vpc"].(map[string]interface{})
tenant1Ue2DevInfraVpcStackStackName := tenant1Ue2DevInfraVpcStack["stack"].(string)
tenant1Ue2DevInfraVpcStackInfrastructureStackName := tenant1Ue2DevInfraVpcStack["stack"].(string)
tenant1Ue2DevInfraVpcStackBackend := tenant1Ue2DevInfraVpcStack["backend"].(map[interface{}]interface{})
tenant1Ue2DevInfraVpcStackBackendWorkspaceKeyPrefix := tenant1Ue2DevInfraVpcStackBackend["workspace_key_prefix"].(string)
assert.Equal(t, "tenant1-ue2-dev-infra-vpc", tenant1Ue2DevInfraVpcStackStackName)
assert.Equal(t, "tenant1-ue2-dev", tenant1Ue2DevInfraVpcStackInfrastructureStackName)
assert.Equal(t, "infra-vpc", tenant1Ue2DevInfraVpcStackBackendWorkspaceKeyPrefix)

tenant1Ue2DevTestTestComponentOverrideComponent := spaceliftStacks["tenant1-ue2-dev-test-test-component-override"].(map[string]interface{})
tenant1Ue2DevTestTestComponentOverrideComponentStackName := tenant1Ue2DevTestTestComponentOverrideComponent["stack"].(string)
tenant1Ue2DevTestTestComponentOverrideComponentInfrastructureStackName := tenant1Ue2DevTestTestComponentOverrideComponent["stack"].(string)
tenant1Ue2DevTestTestComponentOverrideComponentBackend := tenant1Ue2DevTestTestComponentOverrideComponent["backend"].(map[interface{}]interface{})
tenant1Ue2DevTestTestComponentOverrideComponentBaseComponent := tenant1Ue2DevTestTestComponentOverrideComponent["base_component"].(string)
tenant1Ue2DevTestTestComponentOverrideComponentBackendWorkspaceKeyPrefix := tenant1Ue2DevTestTestComponentOverrideComponentBackend["workspace_key_prefix"].(string)
tenant1Ue2DevTestTestComponentOverrideComponentDeps := tenant1Ue2DevTestTestComponentOverrideComponent["deps"].([]string)
tenant1Ue2DevTestTestComponentOverrideComponentLabels := tenant1Ue2DevTestTestComponentOverrideComponent["labels"].([]string)
assert.Equal(t, "tenant1-ue2-dev-test-test-component-override", tenant1Ue2DevTestTestComponentOverrideComponentStackName)
assert.Equal(t, "tenant1-ue2-dev", tenant1Ue2DevTestTestComponentOverrideComponentInfrastructureStackName)
assert.Equal(t, "test-test-component", tenant1Ue2DevTestTestComponentOverrideComponentBackendWorkspaceKeyPrefix)
assert.Equal(t, "test/test-component", tenant1Ue2DevTestTestComponentOverrideComponentBaseComponent)
assert.Equal(t, 11, len(tenant1Ue2DevTestTestComponentOverrideComponentDeps))
Expand Down

0 comments on commit 2cfb75b

Please sign in to comment.