check responses for nil values

While providers shouldn't return nil values, we need to protect against
them. Always assign valid null cty values when the response contains a
nil.
This commit is contained in:
James Bardin 2018-10-03 13:45:12 -04:00 committed by Martin Atkins
parent ece8b6e734
commit 35b375d3ee
1 changed files with 41 additions and 30 deletions

View File

@ -231,10 +231,13 @@ func (p *GRPCProvider) UpgradeResourceState(r providers.UpgradeResourceStateRequ
return resp return resp
} }
state, err := msgpack.Unmarshal(protoResp.UpgradedState.Msgpack, resSchema.Block.ImpliedType()) state := cty.NullVal(resSchema.Block.ImpliedType())
if err != nil { if protoResp.UpgradedState != nil {
resp.Diagnostics = resp.Diagnostics.Append(err) state, err = msgpack.Unmarshal(protoResp.UpgradedState.Msgpack, resSchema.Block.ImpliedType())
return resp if err != nil {
resp.Diagnostics = resp.Diagnostics.Append(err)
return resp
}
} }
resp.UpgradedState = state resp.UpgradedState = state
@ -311,13 +314,16 @@ func (p *GRPCProvider) ReadResource(r providers.ReadResourceRequest) (resp provi
resp.Diagnostics = resp.Diagnostics.Append(convert.ProtoToDiagnostics(protoResp.Diagnostics)) resp.Diagnostics = resp.Diagnostics.Append(convert.ProtoToDiagnostics(protoResp.Diagnostics))
state, err := msgpack.Unmarshal(protoResp.NewState.Msgpack, resSchema.Block.ImpliedType()) state := cty.NullVal(resSchema.Block.ImpliedType())
if err != nil { if protoResp.NewState != nil {
resp.Diagnostics = resp.Diagnostics.Append(err) state, err = msgpack.Unmarshal(protoResp.NewState.Msgpack, resSchema.Block.ImpliedType())
return resp if err != nil {
resp.Diagnostics = resp.Diagnostics.Append(err)
return resp
}
} }
resp.NewState = state resp.NewState = state
return resp return resp
} }
@ -352,12 +358,14 @@ func (p *GRPCProvider) PlanResourceChange(r providers.PlanResourceChangeRequest)
resp.Diagnostics = resp.Diagnostics.Append(convert.ProtoToDiagnostics(protoResp.Diagnostics)) resp.Diagnostics = resp.Diagnostics.Append(convert.ProtoToDiagnostics(protoResp.Diagnostics))
state, err := msgpack.Unmarshal(protoResp.PlannedState.Msgpack, resSchema.Block.ImpliedType()) state := cty.NullVal(resSchema.Block.ImpliedType())
if err != nil { if protoResp.PlannedState != nil {
resp.Diagnostics = resp.Diagnostics.Append(err) state, err = msgpack.Unmarshal(protoResp.PlannedState.Msgpack, resSchema.Block.ImpliedType())
return resp if err != nil {
resp.Diagnostics = resp.Diagnostics.Append(err)
return resp
}
} }
resp.PlannedState = state resp.PlannedState = state
for _, p := range protoResp.RequiresReplace { for _, p := range protoResp.RequiresReplace {
@ -400,16 +408,15 @@ func (p *GRPCProvider) ApplyResourceChange(r providers.ApplyResourceChangeReques
resp.Private = protoResp.Private resp.Private = protoResp.Private
state := cty.NullVal(resSchema.Block.ImpliedType())
if protoResp.NewState != nil { if protoResp.NewState != nil {
state, err := msgpack.Unmarshal(protoResp.NewState.Msgpack, resSchema.Block.ImpliedType()) state, err = msgpack.Unmarshal(protoResp.NewState.Msgpack, resSchema.Block.ImpliedType())
if err != nil { if err != nil {
resp.Diagnostics = resp.Diagnostics.Append(err) resp.Diagnostics = resp.Diagnostics.Append(err)
return resp return resp
} }
resp.NewState = state
} else {
resp.NewState = cty.NullVal(resSchema.Block.ImpliedType())
} }
resp.NewState = state
return resp return resp
} }
@ -435,12 +442,15 @@ func (p *GRPCProvider) ImportResourceState(r providers.ImportResourceStateReques
TypeName: imported.TypeName, TypeName: imported.TypeName,
Private: imported.Private, Private: imported.Private,
} }
state, err := msgpack.Unmarshal(imported.State.Msgpack, resSchema.Block.ImpliedType())
if err != nil {
resp.Diagnostics = resp.Diagnostics.Append(err)
return resp
}
state := cty.NullVal(resSchema.Block.ImpliedType())
if imported.State != nil {
state, err = msgpack.Unmarshal(imported.State.Msgpack, resSchema.Block.ImpliedType())
if err != nil {
resp.Diagnostics = resp.Diagnostics.Append(err)
return resp
}
}
resource.State = state resource.State = state
resp.ImportedResources = append(resp.ImportedResources, resource) resp.ImportedResources = append(resp.ImportedResources, resource)
} }
@ -471,16 +481,17 @@ func (p *GRPCProvider) ReadDataSource(r providers.ReadDataSourceRequest) (resp p
resp.Diagnostics = resp.Diagnostics.Append(err) resp.Diagnostics = resp.Diagnostics.Append(err)
return resp return resp
} }
state := cty.NullVal(dataSchema.Block.ImpliedType())
state, err := msgpack.Unmarshal(protoResp.State.Msgpack, dataSchema.Block.ImpliedType()) if protoResp.State != nil {
if err != nil { state, err = msgpack.Unmarshal(protoResp.State.Msgpack, dataSchema.Block.ImpliedType())
resp.Diagnostics = resp.Diagnostics.Append(err) if err != nil {
return resp resp.Diagnostics = resp.Diagnostics.Append(err)
return resp
}
} }
resp.State = state resp.State = state
return resp
return resp
} }
// closing the grpc connection is final, and terraform will call it at the end of every phase. // closing the grpc connection is final, and terraform will call it at the end of every phase.