diff --git a/fix/fixer.go b/fix/fixer.go index ae8aac40d..cc8aa1258 100644 --- a/fix/fixer.go +++ b/fix/fixer.go @@ -36,6 +36,7 @@ func init() { "amazon-private-ip": new(FixerAmazonPrivateIP), "docker-email": new(FixerDockerEmail), "powershell-escapes": new(FixerPowerShellEscapes), + "hyperv-deprecations": new(FixerHypervDeprecations), } FixerOrder = []string{ @@ -55,5 +56,6 @@ func init() { "amazon-private-ip", "docker-email", "powershell-escapes", + "hyperv-deprecations", } } diff --git a/fix/fixer_hyperv_deprecations.go b/fix/fixer_hyperv_deprecations.go new file mode 100644 index 000000000..79a75d83d --- /dev/null +++ b/fix/fixer_hyperv_deprecations.go @@ -0,0 +1,50 @@ +package fix + +import ( + "github.com/mitchellh/mapstructure" +) + +// FixerHypervDeprecations removes the deprecated "vhd_temp_path" setting +// from Hyper-V ISO builder templates +type FixerHypervDeprecations struct{} + +func (FixerHypervDeprecations) Fix(input map[string]interface{}) (map[string]interface{}, error) { + // The type we'll decode into; we only care about builders + type template struct { + Builders []map[string]interface{} + } + + // Decode the input into our structure, if we can + var tpl template + if err := mapstructure.Decode(input, &tpl); err != nil { + return nil, err + } + + for _, builder := range tpl.Builders { + builderTypeRaw, ok := builder["type"] + if !ok { + continue + } + + builderType, ok := builderTypeRaw.(string) + if !ok { + continue + } + + if builderType != "hyperv-iso" { + continue + } + + _, ok = builder["vhd_temp_path"] + if ok { + delete(builder, "vhd_temp_path") + } + } + + input["builders"] = tpl.Builders + return input, nil +} + +func (FixerHypervDeprecations) Synopsis() string { + return `Removes the deprecated "vhd_temp_path" setting from Hyper-V ISO builder templates` +} diff --git a/fix/fixer_hyperv_deprecations_test.go b/fix/fixer_hyperv_deprecations_test.go new file mode 100644 index 000000000..8c90bff9b --- /dev/null +++ b/fix/fixer_hyperv_deprecations_test.go @@ -0,0 +1,60 @@ +package fix + +import ( + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestFixerHypervDeprecations_impl(t *testing.T) { + var _ Fixer = new(FixerHypervDeprecations) +} + +func TestFixerHypervDeprecations_Fix(t *testing.T) { + cases := []struct { + Input map[string]interface{} + Expected map[string]interface{} + }{ + // No vhd_temp_path field in template - noop + { + Input: map[string]interface{}{ + "type": "hyperv-iso", + }, + + Expected: map[string]interface{}{ + "type": "hyperv-iso", + }, + }, + + // Deprecated vhd_temp_path field in template should be deleted + { + Input: map[string]interface{}{ + "type": "hyperv-iso", + "vhd_temp_path": "foopath", + }, + + Expected: map[string]interface{}{ + "type": "hyperv-iso", + }, + }, + } + + for _, tc := range cases { + var f FixerHypervDeprecations + + input := map[string]interface{}{ + "builders": []map[string]interface{}{tc.Input}, + } + + expected := map[string]interface{}{ + "builders": []map[string]interface{}{tc.Expected}, + } + + output, err := f.Fix(input) + if err != nil { + t.Fatalf("err: %s", err) + } + + assert.Equal(t, expected, output, "Should be equal") + } +}