diff --git a/hcl2template/types.hcl_post-processor.go b/hcl2template/types.hcl_post-processor.go index b09bcd73c..249e1e7c5 100644 --- a/hcl2template/types.hcl_post-processor.go +++ b/hcl2template/types.hcl_post-processor.go @@ -2,7 +2,6 @@ package hcl2template import ( "context" - "fmt" "github.com/hashicorp/hcl/v2" "github.com/hashicorp/hcl/v2/hcldec" @@ -33,18 +32,12 @@ func (p *HCL2PostProcessor) HCL2Prepare(buildVars map[string]interface{}) error ectx = p.evalContext.NewChild() buildValues := map[string]cty.Value{} for k, v := range buildVars { - switch v := v.(type) { - case string: - buildValues[k] = cty.StringVal(v) - case int64: - buildValues[k] = cty.NumberIntVal(v) - case uint64: - buildValues[k] = cty.NumberUIntVal(v) - case bool: - buildValues[k] = cty.BoolVal(v) - default: - return fmt.Errorf("unhandled buildvar type: %T", v) + val, err := ConvertPluginConfigValueToHCLValue(v) + if err != nil { + return err } + + buildValues[k] = val } ectx.Variables = map[string]cty.Value{ buildAccessor: cty.ObjectVal(buildValues), diff --git a/hcl2template/types.hcl_provisioner.go b/hcl2template/types.hcl_provisioner.go index c5ee23a5a..61ba775ab 100644 --- a/hcl2template/types.hcl_provisioner.go +++ b/hcl2template/types.hcl_provisioner.go @@ -2,7 +2,6 @@ package hcl2template import ( "context" - "fmt" "github.com/hashicorp/hcl/v2" "github.com/hashicorp/hcl/v2/hcldec" @@ -37,20 +36,12 @@ func (p *HCL2Provisioner) HCL2Prepare(buildVars map[string]interface{}) error { buildValues = p.evalContext.Variables[buildAccessor].AsValueMap() } for k, v := range buildVars { - switch v := v.(type) { - case string: - buildValues[k] = cty.StringVal(v) - case uint8: - buildValues[k] = cty.NumberUIntVal(uint64(v)) - case int64: - buildValues[k] = cty.NumberIntVal(v) - case uint64: - buildValues[k] = cty.NumberUIntVal(v) - case bool: - buildValues[k] = cty.BoolVal(v) - default: - return fmt.Errorf("unhandled buildvar type: %T", v) + val, err := ConvertPluginConfigValueToHCLValue(v) + if err != nil { + return err } + + buildValues[k] = val } ectx.Variables = map[string]cty.Value{ buildAccessor: cty.ObjectVal(buildValues), diff --git a/hcl2template/utils.go b/hcl2template/utils.go index 02430a86a..f27d10497 100644 --- a/hcl2template/utils.go +++ b/hcl2template/utils.go @@ -124,3 +124,48 @@ func PrintableCtyValue(v cty.Value) string { str := repl.FormatResult(gval) return str } + +func ConvertPluginConfigValueToHCLValue(v interface{}) (cty.Value, error) { + var buildValue cty.Value + switch v := v.(type) { + case bool: + buildValue = cty.BoolVal(v) + case string: + buildValue = cty.StringVal(v) + case uint8: + buildValue = cty.NumberUIntVal(uint64(v)) + case float64: + buildValue = cty.NumberFloatVal(v) + case int64: + buildValue = cty.NumberIntVal(v) + case uint64: + buildValue = cty.NumberUIntVal(v) + case []string: + vals := make([]cty.Value, len(v)) + for i, ev := range v { + vals[i] = cty.StringVal(ev) + } + buildValue = cty.ListVal(vals) + case []uint8: + vals := make([]cty.Value, len(v)) + for i, ev := range v { + vals[i] = cty.NumberUIntVal(uint64(ev)) + } + buildValue = cty.ListVal(vals) + case []int64: + vals := make([]cty.Value, len(v)) + for i, ev := range v { + vals[i] = cty.NumberIntVal(ev) + } + buildValue = cty.ListVal(vals) + case []uint64: + vals := make([]cty.Value, len(v)) + for i, ev := range v { + vals[i] = cty.NumberUIntVal(ev) + } + buildValue = cty.ListVal(vals) + default: + return cty.Value{}, fmt.Errorf("unhandled buildvar type: %T", v) + } + return buildValue, nil +}