command/jsonplan: fix panic when filteredAfter is null (#20096)
* command/jsonplan: fix panic when filteredAfter is null * command/jsonconfig: provider short name is required to properly look up resource schema
This commit is contained in:
parent
c70954aeab
commit
e9099b4fcc
|
@ -224,8 +224,15 @@ func marshalResources(resources map[string]*configs.Resource, schemas *terraform
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
schema, schemaVersion := schemas.ResourceTypeConfig(v.ProviderConfigAddr().String(), v.Mode, v.Type)
|
schema, schemaVer := schemas.ResourceTypeConfig(
|
||||||
r.SchemaVersion = schemaVersion
|
v.ProviderConfigAddr().StringCompact(),
|
||||||
|
v.Mode,
|
||||||
|
v.Type,
|
||||||
|
)
|
||||||
|
if schema == nil {
|
||||||
|
return nil, fmt.Errorf("no schema found for %s", v.Addr().String())
|
||||||
|
}
|
||||||
|
r.SchemaVersion = schemaVer
|
||||||
|
|
||||||
r.Expressions = marshalExpressions(v.Config, schema)
|
r.Expressions = marshalExpressions(v.Config, schema)
|
||||||
|
|
||||||
|
|
|
@ -0,0 +1,73 @@
|
||||||
|
package jsonconfig
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
"reflect"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/zclconf/go-cty/cty"
|
||||||
|
|
||||||
|
"github.com/hashicorp/hcl2/hcl"
|
||||||
|
"github.com/hashicorp/hcl2/hcl/hclsyntax"
|
||||||
|
"github.com/hashicorp/terraform/configs/configschema"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestMarshalExpressions(t *testing.T) {
|
||||||
|
tests := []struct {
|
||||||
|
Input hcl.Body
|
||||||
|
Schema *configschema.Block
|
||||||
|
Want expressions
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
&hclsyntax.Body{
|
||||||
|
Attributes: hclsyntax.Attributes{
|
||||||
|
"foo": &hclsyntax.Attribute{
|
||||||
|
Expr: &hclsyntax.LiteralValueExpr{
|
||||||
|
Val: cty.StringVal("bar"),
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
&configschema.Block{
|
||||||
|
Attributes: map[string]*configschema.Attribute{
|
||||||
|
"foo": {
|
||||||
|
Type: cty.String,
|
||||||
|
Optional: true,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
expressions{
|
||||||
|
"foo": expression{
|
||||||
|
ConstantValue: json.RawMessage([]byte(`"bar"`)),
|
||||||
|
References: []string(nil),
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, test := range tests {
|
||||||
|
got := marshalExpressions(test.Input, test.Schema)
|
||||||
|
if !reflect.DeepEqual(got, test.Want) {
|
||||||
|
t.Fatalf("wrong result:\nGot: %#v\nWant: %#v\n", got, test.Want)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestMarshalExpression(t *testing.T) {
|
||||||
|
tests := []struct {
|
||||||
|
Input hcl.Expression
|
||||||
|
Want expression
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
nil,
|
||||||
|
expression{},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, test := range tests {
|
||||||
|
got := marshalExpression(test.Input)
|
||||||
|
if !reflect.DeepEqual(got, test.Want) {
|
||||||
|
t.Fatalf("wrong result:\nGot: %#v\nWant: %#v\n", got, test.Want)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -176,10 +176,14 @@ func (p *plan) marshalResourceChanges(changes *plans.Changes, schemas *terraform
|
||||||
})
|
})
|
||||||
} else {
|
} else {
|
||||||
filteredAfter := omitUnknowns(changeV.After)
|
filteredAfter := omitUnknowns(changeV.After)
|
||||||
|
if filteredAfter.IsNull() {
|
||||||
|
after = nil
|
||||||
|
} else {
|
||||||
after, err = ctyjson.Marshal(filteredAfter, filteredAfter.Type())
|
after, err = ctyjson.Marshal(filteredAfter, filteredAfter.Type())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
}
|
||||||
afterUnknown = unknownAsBool(changeV.After)
|
afterUnknown = unknownAsBool(changeV.After)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue