initialize empty diff in apply

While the schema Diff fucntion returns a nil diff when creating an empty
(except for id) resource, the Apply function expects the diff to be
initialized and ampty.
This commit is contained in:
James Bardin 2018-10-06 13:06:41 -04:00 committed by Martin Atkins
parent 0b8c38207a
commit caf74a218d
2 changed files with 75 additions and 0 deletions

View File

@ -502,7 +502,14 @@ func (s *GRPCProviderServer) ApplyResourceChange(_ context.Context, req *proto.A
return resp, nil
}
if diff == nil {
diff = &terraform.InstanceDiff{
Attributes: make(map[string]*terraform.ResourceAttrDiff),
Meta: make(map[string]interface{}),
}
}
diff.Meta = private
newInstanceState, err := s.provider.Apply(info, priorState, diff)
if err != nil {
resp.Diagnostics = convert.AppendProtoDiag(resp.Diagnostics, err)

View File

@ -357,3 +357,71 @@ func TestPlanResourceChange(t *testing.T) {
t.Fatal(cmp.Diff(proposedVal, plannedStateVal, valueComparer))
}
}
func TestApplyResourceChange(t *testing.T) {
r := &schema.Resource{
SchemaVersion: 4,
Schema: map[string]*schema.Schema{
"foo": {
Type: schema.TypeInt,
Optional: true,
},
},
Create: func(rd *schema.ResourceData, _ interface{}) error {
rd.SetId("bar")
return nil
},
}
server := &GRPCProviderServer{
provider: &schema.Provider{
ResourcesMap: map[string]*schema.Resource{
"test": r,
},
},
}
schema := r.CoreConfigSchema()
priorState, err := msgpack.Marshal(cty.NullVal(schema.ImpliedType()), schema.ImpliedType())
if err != nil {
t.Fatal(err)
}
// A propsed state with only the ID unknown will produce a nil diff, and
// should return the propsed state value.
plannedVal, err := schema.CoerceValue(cty.ObjectVal(map[string]cty.Value{
"id": cty.UnknownVal(cty.String),
}))
if err != nil {
t.Fatal(err)
}
plannedState, err := msgpack.Marshal(plannedVal, schema.ImpliedType())
if err != nil {
t.Fatal(err)
}
testReq := &proto.ApplyResourceChange_Request{
TypeName: "test",
PriorState: &proto.DynamicValue{
Msgpack: priorState,
},
PlannedState: &proto.DynamicValue{
Msgpack: plannedState,
},
}
resp, err := server.ApplyResourceChange(context.Background(), testReq)
if err != nil {
t.Fatal(err)
}
newStateVal, err := msgpack.Unmarshal(resp.NewState.Msgpack, schema.ImpliedType())
if err != nil {
t.Fatal(err)
}
id := newStateVal.GetAttr("id").AsString()
if id != "bar" {
t.Fatalf("incorrect final state: %#v\n", newStateVal)
}
}