9 "github.com/zclconf/go-cty/cty"
11 plugin "github.com/hashicorp/go-plugin"
12 proto "github.com/hashicorp/terraform/internal/tfplugin5"
13 "github.com/hashicorp/terraform/plugin/convert"
14 "github.com/hashicorp/terraform/providers"
15 "github.com/hashicorp/terraform/version"
16 "github.com/zclconf/go-cty/cty/msgpack"
17 "google.golang.org/grpc"
20 // GRPCProviderPlugin implements plugin.GRPCPlugin for the go-plugin package.
21 type GRPCProviderPlugin struct {
23 GRPCProvider func() proto.ProviderServer
26 func (p *GRPCProviderPlugin) GRPCClient(ctx context.Context, broker *plugin.GRPCBroker, c *grpc.ClientConn) (interface{}, error) {
28 client: proto.NewProviderClient(c),
33 func (p *GRPCProviderPlugin) GRPCServer(broker *plugin.GRPCBroker, s *grpc.Server) error {
34 proto.RegisterProviderServer(s, p.GRPCProvider())
38 // GRPCProvider handles the client, or core side of the plugin rpc connection.
39 // The GRPCProvider methods are mostly a translation layer between the
40 // terraform provioders types and the grpc proto types, directly converting
42 type GRPCProvider struct {
43 // PluginClient provides a reference to the plugin.Client which controls the plugin process.
44 // This allows the GRPCProvider a way to shutdown the plugin process.
45 PluginClient *plugin.Client
47 // TestServer contains a grpc.Server to close when the GRPCProvider is being
48 // used in an end to end test of a provider.
49 TestServer *grpc.Server
51 // Proto client use to make the grpc service calls.
52 client proto.ProviderClient
54 // this context is created by the plugin package, and is canceled when the
55 // plugin process ends.
58 // schema stores the schema for this provider. This is used to properly
59 // serialize the state for requests.
61 schemas providers.GetSchemaResponse
64 // getSchema is used internally to get the saved provider schema. The schema
65 // should have already been fetched from the provider, but we have to
66 // synchronize access to avoid being called concurrently with GetSchema.
67 func (p *GRPCProvider) getSchema() providers.GetSchemaResponse {
69 // unlock inline in case GetSchema needs to be called
70 if p.schemas.Provider.Block != nil {
76 // the schema should have been fetched already, but give it another shot
77 // just in case things are being called out of order. This may happen for
79 schemas := p.GetSchema()
80 if schemas.Diagnostics.HasErrors() {
81 panic(schemas.Diagnostics.Err())
87 // getResourceSchema is a helper to extract the schema for a resource, and
88 // panics if the schema is not available.
89 func (p *GRPCProvider) getResourceSchema(name string) providers.Schema {
90 schema := p.getSchema()
91 resSchema, ok := schema.ResourceTypes[name]
93 panic("unknown resource type " + name)
98 // gettDatasourceSchema is a helper to extract the schema for a datasource, and
99 // panics if that schema is not available.
100 func (p *GRPCProvider) getDatasourceSchema(name string) providers.Schema {
101 schema := p.getSchema()
102 dataSchema, ok := schema.DataSources[name]
104 panic("unknown data source " + name)
109 func (p *GRPCProvider) GetSchema() (resp providers.GetSchemaResponse) {
110 log.Printf("[TRACE] GRPCProvider: GetSchema")
114 if p.schemas.Provider.Block != nil {
118 resp.ResourceTypes = make(map[string]providers.Schema)
119 resp.DataSources = make(map[string]providers.Schema)
121 // Some providers may generate quite large schemas, and the internal default
122 // grpc response size limit is 4MB. 64MB should cover most any use case, and
123 // if we get providers nearing that we may want to consider a finer-grained
124 // API to fetch individual resource schemas.
125 // Note: this option is marked as EXPERIMENTAL in the grpc API.
126 const maxRecvSize = 64 << 20
127 protoResp, err := p.client.GetSchema(p.ctx, new(proto.GetProviderSchema_Request), grpc.MaxRecvMsgSizeCallOption{MaxRecvMsgSize: maxRecvSize})
129 resp.Diagnostics = resp.Diagnostics.Append(err)
133 resp.Diagnostics = resp.Diagnostics.Append(convert.ProtoToDiagnostics(protoResp.Diagnostics))
135 if protoResp.Provider == nil {
136 resp.Diagnostics = resp.Diagnostics.Append(errors.New("missing provider schema"))
140 resp.Provider = convert.ProtoToProviderSchema(protoResp.Provider)
142 for name, res := range protoResp.ResourceSchemas {
143 resp.ResourceTypes[name] = convert.ProtoToProviderSchema(res)
146 for name, data := range protoResp.DataSourceSchemas {
147 resp.DataSources[name] = convert.ProtoToProviderSchema(data)
155 func (p *GRPCProvider) PrepareProviderConfig(r providers.PrepareProviderConfigRequest) (resp providers.PrepareProviderConfigResponse) {
156 log.Printf("[TRACE] GRPCProvider: PrepareProviderConfig")
158 schema := p.getSchema()
159 ty := schema.Provider.Block.ImpliedType()
161 mp, err := msgpack.Marshal(r.Config, ty)
163 resp.Diagnostics = resp.Diagnostics.Append(err)
167 protoReq := &proto.PrepareProviderConfig_Request{
168 Config: &proto.DynamicValue{Msgpack: mp},
171 protoResp, err := p.client.PrepareProviderConfig(p.ctx, protoReq)
173 resp.Diagnostics = resp.Diagnostics.Append(err)
177 config := cty.NullVal(ty)
178 if protoResp.PreparedConfig != nil {
179 config, err = msgpack.Unmarshal(protoResp.PreparedConfig.Msgpack, ty)
181 resp.Diagnostics = resp.Diagnostics.Append(err)
185 resp.PreparedConfig = config
187 resp.Diagnostics = resp.Diagnostics.Append(convert.ProtoToDiagnostics(protoResp.Diagnostics))
191 func (p *GRPCProvider) ValidateResourceTypeConfig(r providers.ValidateResourceTypeConfigRequest) (resp providers.ValidateResourceTypeConfigResponse) {
192 log.Printf("[TRACE] GRPCProvider: ValidateResourceTypeConfig")
193 resourceSchema := p.getResourceSchema(r.TypeName)
195 mp, err := msgpack.Marshal(r.Config, resourceSchema.Block.ImpliedType())
197 resp.Diagnostics = resp.Diagnostics.Append(err)
201 protoReq := &proto.ValidateResourceTypeConfig_Request{
202 TypeName: r.TypeName,
203 Config: &proto.DynamicValue{Msgpack: mp},
206 protoResp, err := p.client.ValidateResourceTypeConfig(p.ctx, protoReq)
208 resp.Diagnostics = resp.Diagnostics.Append(err)
212 resp.Diagnostics = resp.Diagnostics.Append(convert.ProtoToDiagnostics(protoResp.Diagnostics))
216 func (p *GRPCProvider) ValidateDataSourceConfig(r providers.ValidateDataSourceConfigRequest) (resp providers.ValidateDataSourceConfigResponse) {
217 log.Printf("[TRACE] GRPCProvider: ValidateDataSourceConfig")
219 dataSchema := p.getDatasourceSchema(r.TypeName)
221 mp, err := msgpack.Marshal(r.Config, dataSchema.Block.ImpliedType())
223 resp.Diagnostics = resp.Diagnostics.Append(err)
227 protoReq := &proto.ValidateDataSourceConfig_Request{
228 TypeName: r.TypeName,
229 Config: &proto.DynamicValue{Msgpack: mp},
232 protoResp, err := p.client.ValidateDataSourceConfig(p.ctx, protoReq)
234 resp.Diagnostics = resp.Diagnostics.Append(err)
237 resp.Diagnostics = resp.Diagnostics.Append(convert.ProtoToDiagnostics(protoResp.Diagnostics))
241 func (p *GRPCProvider) UpgradeResourceState(r providers.UpgradeResourceStateRequest) (resp providers.UpgradeResourceStateResponse) {
242 log.Printf("[TRACE] GRPCProvider: UpgradeResourceState")
244 resSchema := p.getResourceSchema(r.TypeName)
246 protoReq := &proto.UpgradeResourceState_Request{
247 TypeName: r.TypeName,
248 Version: int64(r.Version),
249 RawState: &proto.RawState{
250 Json: r.RawStateJSON,
251 Flatmap: r.RawStateFlatmap,
255 protoResp, err := p.client.UpgradeResourceState(p.ctx, protoReq)
257 resp.Diagnostics = resp.Diagnostics.Append(err)
260 resp.Diagnostics = resp.Diagnostics.Append(convert.ProtoToDiagnostics(protoResp.Diagnostics))
262 state := cty.NullVal(resSchema.Block.ImpliedType())
263 if protoResp.UpgradedState != nil {
264 state, err = msgpack.Unmarshal(protoResp.UpgradedState.Msgpack, resSchema.Block.ImpliedType())
266 resp.Diagnostics = resp.Diagnostics.Append(err)
271 resp.UpgradedState = state
275 func (p *GRPCProvider) Configure(r providers.ConfigureRequest) (resp providers.ConfigureResponse) {
276 log.Printf("[TRACE] GRPCProvider: Configure")
278 schema := p.getSchema()
282 // we don't have anything to marshal if there's no config
283 mp, err := msgpack.Marshal(r.Config, schema.Provider.Block.ImpliedType())
285 resp.Diagnostics = resp.Diagnostics.Append(err)
289 protoReq := &proto.Configure_Request{
290 TerraformVersion: version.Version,
291 Config: &proto.DynamicValue{
296 protoResp, err := p.client.Configure(p.ctx, protoReq)
298 resp.Diagnostics = resp.Diagnostics.Append(err)
301 resp.Diagnostics = resp.Diagnostics.Append(convert.ProtoToDiagnostics(protoResp.Diagnostics))
305 func (p *GRPCProvider) Stop() error {
306 log.Printf("[TRACE] GRPCProvider: Stop")
308 resp, err := p.client.Stop(p.ctx, new(proto.Stop_Request))
313 if resp.Error != "" {
314 return errors.New(resp.Error)
319 func (p *GRPCProvider) ReadResource(r providers.ReadResourceRequest) (resp providers.ReadResourceResponse) {
320 log.Printf("[TRACE] GRPCProvider: ReadResource")
322 resSchema := p.getResourceSchema(r.TypeName)
324 mp, err := msgpack.Marshal(r.PriorState, resSchema.Block.ImpliedType())
326 resp.Diagnostics = resp.Diagnostics.Append(err)
330 protoReq := &proto.ReadResource_Request{
331 TypeName: r.TypeName,
332 CurrentState: &proto.DynamicValue{Msgpack: mp},
335 protoResp, err := p.client.ReadResource(p.ctx, protoReq)
337 resp.Diagnostics = resp.Diagnostics.Append(err)
340 resp.Diagnostics = resp.Diagnostics.Append(convert.ProtoToDiagnostics(protoResp.Diagnostics))
342 state := cty.NullVal(resSchema.Block.ImpliedType())
343 if protoResp.NewState != nil {
344 state, err = msgpack.Unmarshal(protoResp.NewState.Msgpack, resSchema.Block.ImpliedType())
346 resp.Diagnostics = resp.Diagnostics.Append(err)
350 resp.NewState = state
355 func (p *GRPCProvider) PlanResourceChange(r providers.PlanResourceChangeRequest) (resp providers.PlanResourceChangeResponse) {
356 log.Printf("[TRACE] GRPCProvider: PlanResourceChange")
358 resSchema := p.getResourceSchema(r.TypeName)
360 priorMP, err := msgpack.Marshal(r.PriorState, resSchema.Block.ImpliedType())
362 resp.Diagnostics = resp.Diagnostics.Append(err)
366 configMP, err := msgpack.Marshal(r.Config, resSchema.Block.ImpliedType())
368 resp.Diagnostics = resp.Diagnostics.Append(err)
372 propMP, err := msgpack.Marshal(r.ProposedNewState, resSchema.Block.ImpliedType())
374 resp.Diagnostics = resp.Diagnostics.Append(err)
378 protoReq := &proto.PlanResourceChange_Request{
379 TypeName: r.TypeName,
380 PriorState: &proto.DynamicValue{Msgpack: priorMP},
381 Config: &proto.DynamicValue{Msgpack: configMP},
382 ProposedNewState: &proto.DynamicValue{Msgpack: propMP},
383 PriorPrivate: r.PriorPrivate,
386 protoResp, err := p.client.PlanResourceChange(p.ctx, protoReq)
388 resp.Diagnostics = resp.Diagnostics.Append(err)
391 resp.Diagnostics = resp.Diagnostics.Append(convert.ProtoToDiagnostics(protoResp.Diagnostics))
393 state := cty.NullVal(resSchema.Block.ImpliedType())
394 if protoResp.PlannedState != nil {
395 state, err = msgpack.Unmarshal(protoResp.PlannedState.Msgpack, resSchema.Block.ImpliedType())
397 resp.Diagnostics = resp.Diagnostics.Append(err)
401 resp.PlannedState = state
403 for _, p := range protoResp.RequiresReplace {
404 resp.RequiresReplace = append(resp.RequiresReplace, convert.AttributePathToPath(p))
407 resp.PlannedPrivate = protoResp.PlannedPrivate
409 resp.LegacyTypeSystem = protoResp.LegacyTypeSystem
414 func (p *GRPCProvider) ApplyResourceChange(r providers.ApplyResourceChangeRequest) (resp providers.ApplyResourceChangeResponse) {
415 log.Printf("[TRACE] GRPCProvider: ApplyResourceChange")
417 resSchema := p.getResourceSchema(r.TypeName)
419 priorMP, err := msgpack.Marshal(r.PriorState, resSchema.Block.ImpliedType())
421 resp.Diagnostics = resp.Diagnostics.Append(err)
424 plannedMP, err := msgpack.Marshal(r.PlannedState, resSchema.Block.ImpliedType())
426 resp.Diagnostics = resp.Diagnostics.Append(err)
429 configMP, err := msgpack.Marshal(r.Config, resSchema.Block.ImpliedType())
431 resp.Diagnostics = resp.Diagnostics.Append(err)
435 protoReq := &proto.ApplyResourceChange_Request{
436 TypeName: r.TypeName,
437 PriorState: &proto.DynamicValue{Msgpack: priorMP},
438 PlannedState: &proto.DynamicValue{Msgpack: plannedMP},
439 Config: &proto.DynamicValue{Msgpack: configMP},
440 PlannedPrivate: r.PlannedPrivate,
443 protoResp, err := p.client.ApplyResourceChange(p.ctx, protoReq)
445 resp.Diagnostics = resp.Diagnostics.Append(err)
448 resp.Diagnostics = resp.Diagnostics.Append(convert.ProtoToDiagnostics(protoResp.Diagnostics))
450 resp.Private = protoResp.Private
452 state := cty.NullVal(resSchema.Block.ImpliedType())
453 if protoResp.NewState != nil {
454 state, err = msgpack.Unmarshal(protoResp.NewState.Msgpack, resSchema.Block.ImpliedType())
456 resp.Diagnostics = resp.Diagnostics.Append(err)
460 resp.NewState = state
462 resp.LegacyTypeSystem = protoResp.LegacyTypeSystem
467 func (p *GRPCProvider) ImportResourceState(r providers.ImportResourceStateRequest) (resp providers.ImportResourceStateResponse) {
468 log.Printf("[TRACE] GRPCProvider: ImportResourceState")
470 protoReq := &proto.ImportResourceState_Request{
471 TypeName: r.TypeName,
475 protoResp, err := p.client.ImportResourceState(p.ctx, protoReq)
477 resp.Diagnostics = resp.Diagnostics.Append(err)
480 resp.Diagnostics = resp.Diagnostics.Append(convert.ProtoToDiagnostics(protoResp.Diagnostics))
482 for _, imported := range protoResp.ImportedResources {
483 resource := providers.ImportedResource{
484 TypeName: imported.TypeName,
485 Private: imported.Private,
488 resSchema := p.getResourceSchema(resource.TypeName)
489 state := cty.NullVal(resSchema.Block.ImpliedType())
490 if imported.State != nil {
491 state, err = msgpack.Unmarshal(imported.State.Msgpack, resSchema.Block.ImpliedType())
493 resp.Diagnostics = resp.Diagnostics.Append(err)
497 resource.State = state
498 resp.ImportedResources = append(resp.ImportedResources, resource)
504 func (p *GRPCProvider) ReadDataSource(r providers.ReadDataSourceRequest) (resp providers.ReadDataSourceResponse) {
505 log.Printf("[TRACE] GRPCProvider: ReadDataSource")
507 dataSchema := p.getDatasourceSchema(r.TypeName)
509 config, err := msgpack.Marshal(r.Config, dataSchema.Block.ImpliedType())
511 resp.Diagnostics = resp.Diagnostics.Append(err)
515 protoReq := &proto.ReadDataSource_Request{
516 TypeName: r.TypeName,
517 Config: &proto.DynamicValue{
522 protoResp, err := p.client.ReadDataSource(p.ctx, protoReq)
524 resp.Diagnostics = resp.Diagnostics.Append(err)
527 resp.Diagnostics = resp.Diagnostics.Append(convert.ProtoToDiagnostics(protoResp.Diagnostics))
529 state := cty.NullVal(dataSchema.Block.ImpliedType())
530 if protoResp.State != nil {
531 state, err = msgpack.Unmarshal(protoResp.State.Msgpack, dataSchema.Block.ImpliedType())
533 resp.Diagnostics = resp.Diagnostics.Append(err)
542 // closing the grpc connection is final, and terraform will call it at the end of every phase.
543 func (p *GRPCProvider) Close() error {
544 log.Printf("[TRACE] GRPCProvider: Close")
546 // Make sure to stop the server if we're not running within go-plugin.
547 if p.TestServer != nil {
551 // Check this since it's not automatically inserted during plugin creation.
552 // It's currently only inserted by the command package, because that is
553 // where the factory is built and is the only point with access to the
555 if p.PluginClient == nil {
556 log.Println("[DEBUG] provider has no plugin.Client")
560 p.PluginClient.Kill()