mirror of https://github.com/hashicorp/packer
Merge pull request #8390 from vicyap/f-fixer-for-qemu-disk-size-option
[WIP] Implement fixer to convert qemu disk size type from int to stringpull/8398/head
commit
ca4bb238f7
@ -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.`
|
||||||
|
}
|
||||||
@ -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)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Reference in new issue