private and timeout handling in grpc_provider

Load private data for read, so the resource can get it's configured
timeouts if they exist.

Ensure PlanResourceChange returns the saved private data when there is
an empty diff.

Handle the timeout decoding into Meta in the PlanResourceChange, so that
it's always there for later operations.
This commit is contained in:
James Bardin 2019-06-19 22:44:38 -04:00
parent c4874aa5b3
commit 9365a2d97d
1 changed files with 28 additions and 6 deletions

View File

@ -492,7 +492,12 @@ func (s *GRPCProviderServer) Configure(_ context.Context, req *proto.Configure_R
} }
func (s *GRPCProviderServer) ReadResource(_ context.Context, req *proto.ReadResource_Request) (*proto.ReadResource_Response, error) { func (s *GRPCProviderServer) ReadResource(_ context.Context, req *proto.ReadResource_Request) (*proto.ReadResource_Response, error) {
resp := &proto.ReadResource_Response{} resp := &proto.ReadResource_Response{
// helper/schema did previously handle private data during refresh, but
// core is now going to expect this to be maintained in order to
// persist it in the state.
Private: req.Private,
}
res := s.provider.ResourcesMap[req.TypeName] res := s.provider.ResourcesMap[req.TypeName]
schemaBlock := s.getResourceSchemaBlock(req.TypeName) schemaBlock := s.getResourceSchemaBlock(req.TypeName)
@ -509,6 +514,15 @@ func (s *GRPCProviderServer) ReadResource(_ context.Context, req *proto.ReadReso
return resp, nil return resp, nil
} }
private := make(map[string]interface{})
if len(req.Private) > 0 {
if err := json.Unmarshal(req.Private, &private); err != nil {
resp.Diagnostics = convert.AppendProtoDiag(resp.Diagnostics, err)
return resp, nil
}
}
instanceState.Meta = private
newInstanceState, err := res.RefreshWithoutUpgrade(instanceState, s.provider.Meta()) newInstanceState, err := res.RefreshWithoutUpgrade(instanceState, s.provider.Meta())
if err != nil { if err != nil {
resp.Diagnostics = convert.AppendProtoDiag(resp.Diagnostics, err) resp.Diagnostics = convert.AppendProtoDiag(resp.Diagnostics, err)
@ -551,11 +565,6 @@ func (s *GRPCProviderServer) ReadResource(_ context.Context, req *proto.ReadReso
Msgpack: newStateMP, Msgpack: newStateMP,
} }
// helper/schema did previously handle private data during refresh, but
// core is now going to expect this to be maintained in order to
// persist it in the state.
resp.Private = req.Private
return resp, nil return resp, nil
} }
@ -645,6 +654,7 @@ func (s *GRPCProviderServer) PlanResourceChange(_ context.Context, req *proto.Pl
// description that _shows_ there are no changes. This is always the // description that _shows_ there are no changes. This is always the
// prior state, because we force a diff above if this is a new instance. // prior state, because we force a diff above if this is a new instance.
resp.PlannedState = req.PriorState resp.PlannedState = req.PriorState
resp.PlannedPrivate = req.PriorPrivate
return resp, nil return resp, nil
} }
@ -705,6 +715,18 @@ func (s *GRPCProviderServer) PlanResourceChange(_ context.Context, req *proto.Pl
Msgpack: plannedMP, Msgpack: plannedMP,
} }
// encode any timeouts into the diff Meta
t := &schema.ResourceTimeout{}
if err := t.ConfigDecode(res, cfg); err != nil {
resp.Diagnostics = convert.AppendProtoDiag(resp.Diagnostics, err)
return resp, nil
}
if err := t.DiffEncode(diff); err != nil {
resp.Diagnostics = convert.AppendProtoDiag(resp.Diagnostics, err)
return resp, nil
}
// Now we need to store any NewExtra values, which are where any actual // Now we need to store any NewExtra values, which are where any actual
// StateFunc modified config fields are hidden. // StateFunc modified config fields are hidden.
privateMap := diff.Meta privateMap := diff.Meta