check IsKnown on values that may panic

This commit is contained in:
James Bardin 2018-10-18 19:21:32 -04:00
parent 9e3c23d890
commit e08a388d3c
3 changed files with 25 additions and 5 deletions

View File

@ -856,6 +856,10 @@ func ctyGetAttrMaybeNull(val cty.Value, name string) cty.Value {
} }
func ctyCollectionValues(val cty.Value) []cty.Value { func ctyCollectionValues(val cty.Value) []cty.Value {
if !val.IsKnown() {
return nil
}
ret := make([]cty.Value, 0, val.LengthInt()) ret := make([]cty.Value, 0, val.LengthInt())
for it := val.ElementIterator(); it.Next(); { for it := val.ElementIterator(); it.Next(); {
_, value := it.Element() _, value := it.Element()

View File

@ -64,7 +64,7 @@ func assertObjectCompatible(schema *configschema.Block, planned, actual cty.Valu
return errs return errs
} }
case configschema.NestingList, configschema.NestingMap, configschema.NestingSet: case configschema.NestingList, configschema.NestingMap, configschema.NestingSet:
if plannedV.LengthInt() == 1 { if plannedV.IsKnown() && plannedV.LengthInt() == 1 {
elemVs := plannedV.AsValueSlice() elemVs := plannedV.AsValueSlice()
if allLeafValuesUnknown(elemVs[0]) { if allLeafValuesUnknown(elemVs[0]) {
return errs return errs
@ -84,6 +84,10 @@ func assertObjectCompatible(schema *configschema.Block, planned, actual cty.Valu
// whether there are dynamically-typed attributes inside. However, // whether there are dynamically-typed attributes inside. However,
// both support a similar-enough API that we can treat them the // both support a similar-enough API that we can treat them the
// same for our purposes here. // same for our purposes here.
if !plannedV.IsKnown() {
continue
}
plannedL := plannedV.LengthInt() plannedL := plannedV.LengthInt()
actualL := actualV.LengthInt() actualL := actualV.LengthInt()
if plannedL != actualL { if plannedL != actualL {
@ -125,6 +129,9 @@ func assertObjectCompatible(schema *configschema.Block, planned, actual cty.Valu
} }
} }
} else { } else {
if !plannedV.IsKnown() {
continue
}
plannedL := plannedV.LengthInt() plannedL := plannedV.LengthInt()
actualL := actualV.LengthInt() actualL := actualV.LengthInt()
if plannedL != actualL { if plannedL != actualL {
@ -146,6 +153,9 @@ func assertObjectCompatible(schema *configschema.Block, planned, actual cty.Valu
// content is also their key, and so we have no way to correlate // content is also their key, and so we have no way to correlate
// them. Because of this, we simply verify that we still have the // them. Because of this, we simply verify that we still have the
// same number of elements. // same number of elements.
if !plannedV.IsKnown() {
continue
}
plannedL := plannedV.LengthInt() plannedL := plannedV.LengthInt()
actualL := actualV.LengthInt() actualL := actualV.LengthInt()
if plannedL < actualL { if plannedL < actualL {
@ -241,10 +251,12 @@ func assertValueCompatible(planned, actual cty.Value, path cty.Path) []error {
// so we can't correlate them properly. However, we will at least check // so we can't correlate them properly. However, we will at least check
// to ensure that the number of elements is consistent, along with // to ensure that the number of elements is consistent, along with
// the general type-match checks we ran earlier in this function. // the general type-match checks we ran earlier in this function.
plannedL := planned.LengthInt() if planned.IsKnown() {
actualL := actual.LengthInt() plannedL := planned.LengthInt()
if plannedL < actualL { actualL := actual.LengthInt()
errs = append(errs, path.NewErrorf("length changed from %d to %d", plannedL, actualL)) if plannedL < actualL {
errs = append(errs, path.NewErrorf("length changed from %d to %d", plannedL, actualL))
}
} }
} }

View File

@ -276,6 +276,10 @@ func newResourceConfigShimmedComputedKeys(obj cty.Value, schema *configschema.Bl
} }
blockVal := obj.GetAttr(typeName) blockVal := obj.GetAttr(typeName)
if !blockVal.IsKnown() {
continue
}
switch blockS.Nesting { switch blockS.Nesting {
case configschema.NestingSingle: case configschema.NestingSingle:
keys := newResourceConfigShimmedComputedKeys(blockVal, &blockS.Block, fmt.Sprintf("%s%s.", prefix, typeName)) keys := newResourceConfigShimmedComputedKeys(blockVal, &blockS.Block, fmt.Sprintf("%s%s.", prefix, typeName))