2020-08-17 23:59:16 +00:00
|
|
|
package automation
|
|
|
|
|
|
|
|
import (
|
2020-09-20 05:53:01 +00:00
|
|
|
"fmt"
|
2020-08-17 23:59:16 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func getStringValue(change *TerraformShowResourceChange, key string) (string, bool) {
|
2020-09-20 05:53:01 +00:00
|
|
|
interfaceValue, has := change.Values[key]
|
|
|
|
if !has {
|
|
|
|
return "", false
|
|
|
|
}
|
|
|
|
switch stringValue := interfaceValue.(type) {
|
|
|
|
case string:
|
|
|
|
return stringValue, true
|
|
|
|
default:
|
|
|
|
return "", false
|
|
|
|
}
|
2020-08-17 23:59:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func getStringSliceValue(change *TerraformShowResourceChange, key string) ([]string, bool) {
|
2020-09-20 05:53:01 +00:00
|
|
|
interfaceValue, has := change.Values[key]
|
|
|
|
if !has {
|
|
|
|
return []string{}, false
|
|
|
|
}
|
|
|
|
switch stringSliceValue := interfaceValue.(type) {
|
|
|
|
case []string:
|
|
|
|
return stringSliceValue, true
|
|
|
|
default:
|
|
|
|
return []string{}, false
|
|
|
|
}
|
2020-08-17 23:59:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func removeAndImportResource(terraformDirectory, address, id string) int {
|
|
|
|
|
2020-09-20 05:53:01 +00:00
|
|
|
// 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
|
2020-08-17 23:59:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func filterResourceChanges(tfShow *TerraformShow, filter ResourceChangeFilter) []*TerraformShowResourceChange {
|
2020-09-20 05:53:01 +00:00
|
|
|
toReturn := []*TerraformShowResourceChange{}
|
2020-08-17 23:59:16 +00:00
|
|
|
|
2020-09-20 05:53:01 +00:00
|
|
|
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)
|
|
|
|
}
|
2020-08-17 23:59:16 +00:00
|
|
|
|
2020-09-20 05:53:01 +00:00
|
|
|
}
|
|
|
|
return toReturn
|
2020-08-17 23:59:16 +00:00
|
|
|
}
|