rootsystem/automation/patchHelpers.go

82 lines
2.2 KiB
Go
Raw Permalink Normal View History

package automation
import (
"fmt"
)
func getStringValue(change *TerraformShowResourceChange, key string) (string, bool) {
interfaceValue, has := change.Values[key]
if !has {
return "", false
}
switch stringValue := interfaceValue.(type) {
case string:
return stringValue, true
default:
return "", false
}
}
func getStringSliceValue(change *TerraformShowResourceChange, key string) ([]string, bool) {
interfaceValue, has := change.Values[key]
if !has {
return []string{}, false
}
switch stringSliceValue := interfaceValue.(type) {
case []string:
return stringSliceValue, true
default:
return []string{}, false
}
}
func removeAndImportResource(terraformDirectory, address, id string) int {
// fullyQualifiedResourcePath := fmt.Sprintf("%s[%d]", tfResourcePath, index)
fmt.Printf(
"to fix this, I will run: \n(%s) $ terraform state rm %s\n(%s) $ terraform import %s %s\n\n",
terraformDirectory, address, terraformDirectory, address, id,
)
exitCode, stdout, stderr, _ := shellExec(terraformDirectory, "terraform", "state", "rm", address)
fmt.Printf(
"exitCode: %d\n\nstdout:\n%s\n\nstderr:\n%s\n\n",
exitCode, stdout, stderr,
)
exitCode, stdout, stderr, err := shellExec(terraformDirectory, "terraform", "import", address, id)
fmt.Printf(
"exitCode: %d\n\nstdout:\n%s\n\nstderr:\n%s\n\n",
exitCode, stdout, stderr,
)
if err != nil {
fmt.Println("shellExec failed! aborting.")
return exitCode
}
return exitCode
}
func filterResourceChanges(tfShow *TerraformShow, filter ResourceChangeFilter) []*TerraformShowResourceChange {
toReturn := []*TerraformShowResourceChange{}
for _, resource := range tfShow.ResourceChanges {
hasAction := filter.Action == ""
hasNotAction := false
for _, action := range resource.Change.Actions {
if action == filter.Action {
hasAction = true
}
if action == filter.NotAction {
hasNotAction = true
}
}
// fmt.Printf(
// "resource.Type: %s == filter.ResourceType: %s, hasAction %t, !hasNotAction %t \n",
// resource.Type, filter.ResourceType, hasAction, !hasNotAction,
// )
if resource.Type == filter.ResourceType && hasAction && !hasNotAction {
toReturn = append(toReturn, resource)
}
}
return toReturn
}