Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .changelog/2018.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:note
manifest/morph.go: Add Diagnostic message when determining a type fails
```
62 changes: 44 additions & 18 deletions manifest/morph/morph.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func ValueToType(v tftypes.Value, t tftypes.Type, p *tftypes.AttributePath) (tft
return tftypes.Value{}, diags
}
if v.IsNull() {
return tftypes.NewValue(t, nil), nil
return newValue(t, nil, p)
}
switch {
case v.Type().Is(tftypes.String):
Expand Down Expand Up @@ -84,7 +84,7 @@ func morphBoolToType(v tftypes.Value, t tftypes.Type, p *tftypes.AttributePath)
}
switch {
case t.Is(tftypes.String):
return tftypes.NewValue(t, strconv.FormatBool(bnat)), nil
return newValue(t, strconv.FormatBool(bnat), p)
case t.Is(tftypes.DynamicPseudoType):
return v, diags
}
Expand Down Expand Up @@ -115,7 +115,7 @@ func morphNumberToType(v tftypes.Value, t tftypes.Type, p *tftypes.AttributePath
}
switch {
case t.Is(tftypes.String):
return tftypes.NewValue(t, vnat.String()), nil
return newValue(t, vnat.String(), p)
case t.Is(tftypes.DynamicPseudoType):
return v, diags
}
Expand Down Expand Up @@ -157,7 +157,7 @@ func morphStringToType(v tftypes.Value, t tftypes.Type, p *tftypes.AttributePath
return tftypes.Value{}, diags
}
nv := new(big.Float).SetFloat64(fv)
return tftypes.NewValue(t, nv), nil
return newValue(t, nv, p)
case t.Is(tftypes.Bool):
bv, err := strconv.ParseBool(vnat)
if err != nil {
Expand All @@ -169,7 +169,7 @@ func morphStringToType(v tftypes.Value, t tftypes.Type, p *tftypes.AttributePath
})
return tftypes.Value{}, diags
}
return tftypes.NewValue(t, bv), nil
return newValue(t, bv, p)
case t.Is(tftypes.DynamicPseudoType):
return v, diags
}
Expand Down Expand Up @@ -217,7 +217,7 @@ func morphListToType(v tftypes.Value, t tftypes.Type, p *tftypes.AttributePath)
}
nlvals[i] = nv
}
return tftypes.NewValue(t, nlvals), nil
return newValue(t, nlvals, p)
case t.Is(tftypes.Tuple{}):
if len(t.(tftypes.Tuple).ElementTypes) != len(lvals) {
diags = append(diags, &tfprotov5.Diagnostic{
Expand Down Expand Up @@ -248,7 +248,7 @@ func morphListToType(v tftypes.Value, t tftypes.Type, p *tftypes.AttributePath)
}
tvals[i] = nv
}
return tftypes.NewValue(t, tvals), nil
return newValue(t, tvals, p)
case t.Is(tftypes.Set{}):
var svals []tftypes.Value = make([]tftypes.Value, len(lvals))
for i, v := range lvals {
Expand All @@ -270,7 +270,7 @@ func morphListToType(v tftypes.Value, t tftypes.Type, p *tftypes.AttributePath)
}
svals[i] = nv
}
return tftypes.NewValue(t, svals), nil
return newValue(t, svals, p)
case t.Is(tftypes.DynamicPseudoType):
return v, diags
}
Expand Down Expand Up @@ -339,7 +339,7 @@ func morphTupleIntoType(v tftypes.Value, t tftypes.Type, p *tftypes.AttributePat
lvals[i] = nv
eltypes[i] = nv.Type()
}
return tftypes.NewValue(tftypes.Tuple{ElementTypes: eltypes}, lvals), diags
return newValue(tftypes.Tuple{ElementTypes: eltypes}, lvals, p)
case t.Is(tftypes.List{}):
var lvals []tftypes.Value = make([]tftypes.Value, len(tvals))
for i, v := range tvals {
Expand All @@ -361,7 +361,7 @@ func morphTupleIntoType(v tftypes.Value, t tftypes.Type, p *tftypes.AttributePat
}
lvals[i] = nv
}
return tftypes.NewValue(t, lvals), diags
return newValue(t, lvals, p)
case t.Is(tftypes.Set{}):
var svals []tftypes.Value = make([]tftypes.Value, len(tvals))
for i, v := range tvals {
Expand All @@ -383,7 +383,7 @@ func morphTupleIntoType(v tftypes.Value, t tftypes.Type, p *tftypes.AttributePat
}
svals[i] = nv
}
return tftypes.NewValue(t, svals), diags
return newValue(t, svals, p)
case t.Is(tftypes.DynamicPseudoType):
return v, diags
}
Expand Down Expand Up @@ -431,7 +431,7 @@ func morphSetToType(v tftypes.Value, t tftypes.Type, p *tftypes.AttributePath) (
}
svals[i] = nv
}
return tftypes.NewValue(t, svals), diags
return newValue(t, svals, p)
case t.Is(tftypes.List{}):
var lvals []tftypes.Value = make([]tftypes.Value, len(svals))
for i, v := range svals {
Expand All @@ -453,7 +453,7 @@ func morphSetToType(v tftypes.Value, t tftypes.Type, p *tftypes.AttributePath) (
}
lvals[i] = nv
}
return tftypes.NewValue(t, lvals), diags
return newValue(t, lvals, p)
case t.Is(tftypes.Tuple{}):
if len(t.(tftypes.Tuple).ElementTypes) != len(svals) {
diags = append(diags, &tfprotov5.Diagnostic{
Expand Down Expand Up @@ -484,7 +484,7 @@ func morphSetToType(v tftypes.Value, t tftypes.Type, p *tftypes.AttributePath) (
}
tvals[i] = nv
}
return tftypes.NewValue(t, tvals), diags
return newValue(t, tvals, p)
case t.Is(tftypes.DynamicPseudoType):
return v, diags
}
Expand Down Expand Up @@ -542,7 +542,7 @@ func morphMapToType(v tftypes.Value, t tftypes.Type, p *tftypes.AttributePath) (
}
ovals[k] = nv
}
return tftypes.NewValue(t, ovals), diags
return newValue(t, ovals, p)
case t.Is(tftypes.Map{}):
var nmvals map[string]tftypes.Value = make(map[string]tftypes.Value, len(mvals))
for k, v := range mvals {
Expand All @@ -564,7 +564,7 @@ func morphMapToType(v tftypes.Value, t tftypes.Type, p *tftypes.AttributePath) (
}
nmvals[k] = nv
}
return tftypes.NewValue(t, nmvals), diags
return newValue(t, nmvals, p)
case t.Is(tftypes.DynamicPseudoType):
return v, diags
}
Expand Down Expand Up @@ -626,7 +626,12 @@ func morphObjectToType(v tftypes.Value, t tftypes.Type, p *tftypes.AttributePath
// tftypes.NewValue() fails if any of the attributes in the object don't have a corresponding value
for k := range t.(tftypes.Object).AttributeTypes {
if _, ok := ovals[k]; !ok {
ovals[k] = tftypes.NewValue(t.(tftypes.Object).AttributeTypes[k], nil)
nv, d := newValue(t.(tftypes.Object).AttributeTypes[k], nil, p)
if d != nil {
diags = append(diags, d...)
return tftypes.Value{}, diags
}
ovals[k] = nv
}
}
otypes := make(map[string]tftypes.Type, len(ovals))
Expand Down Expand Up @@ -655,7 +660,7 @@ func morphObjectToType(v tftypes.Value, t tftypes.Type, p *tftypes.AttributePath
}
mvals[k] = nv
}
return tftypes.NewValue(t, mvals), diags
return newValue(t, mvals, p)
case t.Is(tftypes.DynamicPseudoType):
return v, diags
}
Expand Down Expand Up @@ -713,3 +718,24 @@ func attributePathSummary(p *tftypes.AttributePath) string {
}
return b.String()
}

func validateValue(t tftypes.Type, val interface{}, p *tftypes.AttributePath) []*tfprotov5.Diagnostic {
var diags []*tfprotov5.Diagnostic
if err := tftypes.ValidateValue(t, val); err != nil {
diags = append(diags, &tfprotov5.Diagnostic{
Attribute: p,
Severity: tfprotov5.DiagnosticSeverityError,
Summary: "Provider encountered an error when trying to determine the Terraform type information for the configured manifest",
Detail: err.(error).Error(),
})
return diags
}
return nil
}

func newValue(t tftypes.Type, val interface{}, p *tftypes.AttributePath) (tftypes.Value, []*tfprotov5.Diagnostic) {
if diags := validateValue(t, val, p); diags != nil {
return tftypes.Value{}, diags
}
return tftypes.NewValue(t, val), nil
}