diff --git a/fix/fixer.go b/fix/fixer.go index bd822a4c2..dceb866fb 100644 --- a/fix/fixer.go +++ b/fix/fixer.go @@ -44,6 +44,7 @@ func init() { "vmware-compaction": new(FixerVMwareCompaction), "clean-image-name": new(FixerCleanImageName), "spot-price-auto-product": new(FixerAmazonSpotPriceProductDeprecation), + "qemu-disk-size": new(FixerQEMUDiskSize), } FixerOrder = []string{ @@ -71,5 +72,6 @@ func init() { "hyperv-cpu-and-ram", "clean-image-name", "spot-price-auto-product", + "qemu-disk-size", } } diff --git a/fix/fixer_qemu_disk_size.go b/fix/fixer_qemu_disk_size.go new file mode 100644 index 000000000..9a7f15165 --- /dev/null +++ b/fix/fixer_qemu_disk_size.go @@ -0,0 +1,53 @@ +package fix + +import ( + "strconv" + + "github.com/mitchellh/mapstructure" +) + +// FixerQEMUDiskSize updates disk_size from a string to int for QEMU builders +type FixerQEMUDiskSize struct{} + +func (FixerQEMUDiskSize) Fix(input map[string]interface{}) (map[string]interface{}, error) { + 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 != "qemu" { + continue + } + + switch diskSize := builder["disk_size"].(type) { + case float64: + builder["disk_size"] = strconv.Itoa(int(diskSize)) + "M" + case int: + builder["disk_size"] = strconv.Itoa(diskSize) + "M" + } + + } + + input["builders"] = tpl.Builders + return input, nil +} + +func (FixerQEMUDiskSize) Synopsis() string { + return `Updates "disk_size" from int to string in QEMU builders.` +} diff --git a/fix/fixer_qemu_disk_size_test.go b/fix/fixer_qemu_disk_size_test.go new file mode 100644 index 000000000..6381dc68d --- /dev/null +++ b/fix/fixer_qemu_disk_size_test.go @@ -0,0 +1,61 @@ +package fix + +import ( + "reflect" + "testing" +) + +func TestFixerQEMUDiskSize_impl(t *testing.T) { + var _ Fixer = new(FixerQEMUDiskSize) +} + +func TestFixerQEMUDiskSize(t *testing.T) { + cases := []struct { + Input map[string]interface{} + Expected map[string]interface{} + }{ + { + Input: map[string]interface{}{ + "type": "qemu", + "disk_size": int(40960), + }, + + Expected: map[string]interface{}{ + "type": "qemu", + "disk_size": "40960M", + }, + }, + { + Input: map[string]interface{}{ + "type": "qemu", + "disk_size": float64(50000), + }, + + Expected: map[string]interface{}{ + "type": "qemu", + "disk_size": "50000M", + }, + }, + } + + for _, tc := range cases { + var f FixerQEMUDiskSize + + 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) + } + + if !reflect.DeepEqual(output, expected) { + t.Fatalf("unexpected: %#v\nexpected: %#v\n", output, expected) + } + } +}