2023-05-02 10:33:06 -05:00
|
|
|
// Copyright (c) HashiCorp, Inc.
|
|
|
|
// SPDX-License-Identifier: MPL-2.0
|
|
|
|
|
2018-08-08 15:57:31 -05:00
|
|
|
package plugin
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"errors"
|
2022-05-03 09:22:55 -05:00
|
|
|
"fmt"
|
2018-08-08 15:57:31 -05:00
|
|
|
"sync"
|
|
|
|
|
2018-08-27 19:34:14 -05:00
|
|
|
"github.com/zclconf/go-cty/cty"
|
|
|
|
|
2018-08-14 15:55:20 -05:00
|
|
|
plugin "github.com/hashicorp/go-plugin"
|
2023-08-17 07:45:11 -05:00
|
|
|
"github.com/placeholderplaceholderplaceholder/opentf/internal/addrs"
|
|
|
|
"github.com/placeholderplaceholderplaceholder/opentf/internal/logging"
|
|
|
|
"github.com/placeholderplaceholderplaceholder/opentf/internal/plugin/convert"
|
|
|
|
"github.com/placeholderplaceholderplaceholder/opentf/internal/providers"
|
|
|
|
proto "github.com/placeholderplaceholderplaceholder/opentf/internal/tfplugin5"
|
2020-10-16 14:05:49 -05:00
|
|
|
ctyjson "github.com/zclconf/go-cty/cty/json"
|
2018-08-08 15:57:31 -05:00
|
|
|
"github.com/zclconf/go-cty/cty/msgpack"
|
|
|
|
"google.golang.org/grpc"
|
|
|
|
)
|
|
|
|
|
2020-10-22 16:28:54 -05:00
|
|
|
var logger = logging.HCLogger()
|
|
|
|
|
2018-08-14 15:55:20 -05:00
|
|
|
// GRPCProviderPlugin implements plugin.GRPCPlugin for the go-plugin package.
|
|
|
|
type GRPCProviderPlugin struct {
|
|
|
|
plugin.Plugin
|
|
|
|
GRPCProvider func() proto.ProviderServer
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *GRPCProviderPlugin) GRPCClient(ctx context.Context, broker *plugin.GRPCBroker, c *grpc.ClientConn) (interface{}, error) {
|
|
|
|
return &GRPCProvider{
|
|
|
|
client: proto.NewProviderClient(c),
|
|
|
|
ctx: ctx,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *GRPCProviderPlugin) GRPCServer(broker *plugin.GRPCBroker, s *grpc.Server) error {
|
|
|
|
proto.RegisterProviderServer(s, p.GRPCProvider())
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-08-08 15:57:31 -05:00
|
|
|
// GRPCProvider handles the client, or core side of the plugin rpc connection.
|
|
|
|
// The GRPCProvider methods are mostly a translation layer between the
|
2021-02-18 09:13:43 -06:00
|
|
|
// terraform providers types and the grpc proto types, directly converting
|
2018-08-08 15:57:31 -05:00
|
|
|
// between the two.
|
|
|
|
type GRPCProvider struct {
|
2018-10-02 15:06:12 -05:00
|
|
|
// PluginClient provides a reference to the plugin.Client which controls the plugin process.
|
|
|
|
// This allows the GRPCProvider a way to shutdown the plugin process.
|
|
|
|
PluginClient *plugin.Client
|
|
|
|
|
2019-04-29 13:14:04 -05:00
|
|
|
// TestServer contains a grpc.Server to close when the GRPCProvider is being
|
2018-10-15 20:15:08 -05:00
|
|
|
// used in an end to end test of a provider.
|
2019-04-29 13:14:04 -05:00
|
|
|
TestServer *grpc.Server
|
2018-10-15 20:15:08 -05:00
|
|
|
|
2023-07-03 11:37:50 -05:00
|
|
|
// Addr uniquely identifies the type of provider.
|
|
|
|
// Normally executed providers will have this set during initialization,
|
|
|
|
// but it may not always be available for alternative execute modes.
|
|
|
|
Addr addrs.Provider
|
|
|
|
|
2018-10-02 15:06:12 -05:00
|
|
|
// Proto client use to make the grpc service calls.
|
2018-08-08 15:57:31 -05:00
|
|
|
client proto.ProviderClient
|
|
|
|
|
|
|
|
// this context is created by the plugin package, and is canceled when the
|
|
|
|
// plugin process ends.
|
|
|
|
ctx context.Context
|
|
|
|
|
|
|
|
// schema stores the schema for this provider. This is used to properly
|
2023-07-06 15:48:06 -05:00
|
|
|
// serialize the requests for schemas.
|
|
|
|
mu sync.Mutex
|
|
|
|
schema providers.GetProviderSchemaResponse
|
2018-08-08 15:57:31 -05:00
|
|
|
}
|
|
|
|
|
2021-02-18 09:13:43 -06:00
|
|
|
func (p *GRPCProvider) GetProviderSchema() (resp providers.GetProviderSchemaResponse) {
|
|
|
|
logger.Trace("GRPCProvider: GetProviderSchema")
|
2018-08-08 15:57:31 -05:00
|
|
|
p.mu.Lock()
|
|
|
|
defer p.mu.Unlock()
|
|
|
|
|
2023-07-03 11:37:50 -05:00
|
|
|
// check the global cache if we can
|
2023-07-18 12:43:21 -05:00
|
|
|
if !p.Addr.IsZero() && resp.ServerCapabilities.GetProviderSchemaOptional {
|
2023-07-03 11:37:50 -05:00
|
|
|
if resp, ok := providers.SchemaCache.Get(p.Addr); ok {
|
|
|
|
return resp
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-18 12:43:21 -05:00
|
|
|
// If the local cache is non-zero, we know this instance has called
|
|
|
|
// GetProviderSchema at least once and we can return early.
|
2023-07-06 15:48:06 -05:00
|
|
|
if p.schema.Provider.Block != nil {
|
|
|
|
return p.schema
|
2018-08-08 15:57:31 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
resp.ResourceTypes = make(map[string]providers.Schema)
|
|
|
|
resp.DataSources = make(map[string]providers.Schema)
|
|
|
|
|
2019-04-02 14:43:48 -05:00
|
|
|
// Some providers may generate quite large schemas, and the internal default
|
|
|
|
// grpc response size limit is 4MB. 64MB should cover most any use case, and
|
|
|
|
// if we get providers nearing that we may want to consider a finer-grained
|
|
|
|
// API to fetch individual resource schemas.
|
2022-06-02 17:03:35 -05:00
|
|
|
// Note: this option is marked as EXPERIMENTAL in the grpc API. We keep
|
|
|
|
// this for compatibility, but recent providers all set the max message
|
|
|
|
// size much higher on the server side, which is the supported method for
|
|
|
|
// determining payload size.
|
2019-04-02 14:43:48 -05:00
|
|
|
const maxRecvSize = 64 << 20
|
|
|
|
protoResp, err := p.client.GetSchema(p.ctx, new(proto.GetProviderSchema_Request), grpc.MaxRecvMsgSizeCallOption{MaxRecvMsgSize: maxRecvSize})
|
2018-08-08 15:57:31 -05:00
|
|
|
if err != nil {
|
2020-10-22 16:28:54 -05:00
|
|
|
resp.Diagnostics = resp.Diagnostics.Append(grpcErr(err))
|
2018-08-08 15:57:31 -05:00
|
|
|
return resp
|
|
|
|
}
|
|
|
|
|
2018-10-06 13:26:07 -05:00
|
|
|
resp.Diagnostics = resp.Diagnostics.Append(convert.ProtoToDiagnostics(protoResp.Diagnostics))
|
|
|
|
|
2022-06-03 13:27:55 -05:00
|
|
|
if resp.Diagnostics.HasErrors() {
|
|
|
|
return resp
|
|
|
|
}
|
|
|
|
|
2018-08-08 15:57:31 -05:00
|
|
|
if protoResp.Provider == nil {
|
|
|
|
resp.Diagnostics = resp.Diagnostics.Append(errors.New("missing provider schema"))
|
|
|
|
return resp
|
|
|
|
}
|
|
|
|
|
2018-08-15 11:03:39 -05:00
|
|
|
resp.Provider = convert.ProtoToProviderSchema(protoResp.Provider)
|
2020-03-05 18:53:24 -06:00
|
|
|
if protoResp.ProviderMeta == nil {
|
2020-10-22 16:28:54 -05:00
|
|
|
logger.Debug("No provider meta schema returned")
|
2020-03-05 18:53:24 -06:00
|
|
|
} else {
|
|
|
|
resp.ProviderMeta = convert.ProtoToProviderSchema(protoResp.ProviderMeta)
|
|
|
|
}
|
2018-08-08 15:57:31 -05:00
|
|
|
|
|
|
|
for name, res := range protoResp.ResourceSchemas {
|
2018-08-15 11:03:39 -05:00
|
|
|
resp.ResourceTypes[name] = convert.ProtoToProviderSchema(res)
|
2018-08-08 15:57:31 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
for name, data := range protoResp.DataSourceSchemas {
|
2018-08-15 11:03:39 -05:00
|
|
|
resp.DataSources[name] = convert.ProtoToProviderSchema(data)
|
2018-08-08 15:57:31 -05:00
|
|
|
}
|
|
|
|
|
2022-07-06 12:47:30 -05:00
|
|
|
if protoResp.ServerCapabilities != nil {
|
|
|
|
resp.ServerCapabilities.PlanDestroy = protoResp.ServerCapabilities.PlanDestroy
|
2023-07-06 15:48:06 -05:00
|
|
|
resp.ServerCapabilities.GetProviderSchemaOptional = protoResp.ServerCapabilities.GetProviderSchemaOptional
|
2022-06-02 17:03:35 -05:00
|
|
|
}
|
|
|
|
|
2023-07-03 11:37:50 -05:00
|
|
|
// set the global cache if we can
|
2023-07-18 12:43:21 -05:00
|
|
|
if !p.Addr.IsZero() {
|
2023-07-06 15:48:06 -05:00
|
|
|
providers.SchemaCache.Set(p.Addr, resp)
|
|
|
|
}
|
2018-08-08 15:57:31 -05:00
|
|
|
|
2023-07-18 12:43:21 -05:00
|
|
|
// always store this here in the client for providers that are not able to
|
|
|
|
// use GetProviderSchemaOptional
|
|
|
|
p.schema = resp
|
|
|
|
|
2018-08-08 15:57:31 -05:00
|
|
|
return resp
|
|
|
|
}
|
|
|
|
|
2021-02-18 09:13:43 -06:00
|
|
|
func (p *GRPCProvider) ValidateProviderConfig(r providers.ValidateProviderConfigRequest) (resp providers.ValidateProviderConfigResponse) {
|
|
|
|
logger.Trace("GRPCProvider: ValidateProviderConfig")
|
2018-08-24 19:10:26 -05:00
|
|
|
|
2023-07-03 11:37:50 -05:00
|
|
|
schema := p.GetProviderSchema()
|
2022-06-02 17:03:35 -05:00
|
|
|
if schema.Diagnostics.HasErrors() {
|
|
|
|
resp.Diagnostics = schema.Diagnostics
|
|
|
|
return resp
|
|
|
|
}
|
|
|
|
|
2018-10-17 20:20:40 -05:00
|
|
|
ty := schema.Provider.Block.ImpliedType()
|
|
|
|
|
|
|
|
mp, err := msgpack.Marshal(r.Config, ty)
|
2018-08-08 15:57:31 -05:00
|
|
|
if err != nil {
|
|
|
|
resp.Diagnostics = resp.Diagnostics.Append(err)
|
|
|
|
return resp
|
|
|
|
}
|
|
|
|
|
2018-10-17 20:20:40 -05:00
|
|
|
protoReq := &proto.PrepareProviderConfig_Request{
|
2018-08-08 15:57:31 -05:00
|
|
|
Config: &proto.DynamicValue{Msgpack: mp},
|
|
|
|
}
|
|
|
|
|
2018-10-17 20:20:40 -05:00
|
|
|
protoResp, err := p.client.PrepareProviderConfig(p.ctx, protoReq)
|
2018-08-08 15:57:31 -05:00
|
|
|
if err != nil {
|
2020-10-22 16:28:54 -05:00
|
|
|
resp.Diagnostics = resp.Diagnostics.Append(grpcErr(err))
|
2018-08-08 15:57:31 -05:00
|
|
|
return resp
|
|
|
|
}
|
2018-10-06 13:26:07 -05:00
|
|
|
|
2020-10-16 14:05:49 -05:00
|
|
|
config, err := decodeDynamicValue(protoResp.PreparedConfig, ty)
|
|
|
|
if err != nil {
|
|
|
|
resp.Diagnostics = resp.Diagnostics.Append(err)
|
|
|
|
return resp
|
2018-10-17 20:20:40 -05:00
|
|
|
}
|
|
|
|
resp.PreparedConfig = config
|
|
|
|
|
2018-08-15 11:03:39 -05:00
|
|
|
resp.Diagnostics = resp.Diagnostics.Append(convert.ProtoToDiagnostics(protoResp.Diagnostics))
|
2018-08-08 15:57:31 -05:00
|
|
|
return resp
|
|
|
|
}
|
|
|
|
|
2021-02-18 09:13:43 -06:00
|
|
|
func (p *GRPCProvider) ValidateResourceConfig(r providers.ValidateResourceConfigRequest) (resp providers.ValidateResourceConfigResponse) {
|
|
|
|
logger.Trace("GRPCProvider: ValidateResourceConfig")
|
2022-05-03 09:22:55 -05:00
|
|
|
|
2023-07-03 11:37:50 -05:00
|
|
|
schema := p.GetProviderSchema()
|
2022-06-02 17:03:35 -05:00
|
|
|
if schema.Diagnostics.HasErrors() {
|
|
|
|
resp.Diagnostics = schema.Diagnostics
|
|
|
|
return resp
|
|
|
|
}
|
|
|
|
|
|
|
|
resourceSchema, ok := schema.ResourceTypes[r.TypeName]
|
|
|
|
if !ok {
|
|
|
|
resp.Diagnostics = resp.Diagnostics.Append(fmt.Errorf("unknown resource type %q", r.TypeName))
|
2022-05-03 09:22:55 -05:00
|
|
|
return resp
|
|
|
|
}
|
2018-08-08 15:57:31 -05:00
|
|
|
|
|
|
|
mp, err := msgpack.Marshal(r.Config, resourceSchema.Block.ImpliedType())
|
|
|
|
if err != nil {
|
|
|
|
resp.Diagnostics = resp.Diagnostics.Append(err)
|
|
|
|
return resp
|
|
|
|
}
|
|
|
|
|
|
|
|
protoReq := &proto.ValidateResourceTypeConfig_Request{
|
|
|
|
TypeName: r.TypeName,
|
|
|
|
Config: &proto.DynamicValue{Msgpack: mp},
|
|
|
|
}
|
|
|
|
|
|
|
|
protoResp, err := p.client.ValidateResourceTypeConfig(p.ctx, protoReq)
|
|
|
|
if err != nil {
|
2020-10-22 16:28:54 -05:00
|
|
|
resp.Diagnostics = resp.Diagnostics.Append(grpcErr(err))
|
2018-08-08 15:57:31 -05:00
|
|
|
return resp
|
|
|
|
}
|
|
|
|
|
2018-08-15 11:03:39 -05:00
|
|
|
resp.Diagnostics = resp.Diagnostics.Append(convert.ProtoToDiagnostics(protoResp.Diagnostics))
|
2018-08-08 15:57:31 -05:00
|
|
|
return resp
|
|
|
|
}
|
|
|
|
|
2021-02-24 11:04:28 -06:00
|
|
|
func (p *GRPCProvider) ValidateDataResourceConfig(r providers.ValidateDataResourceConfigRequest) (resp providers.ValidateDataResourceConfigResponse) {
|
|
|
|
logger.Trace("GRPCProvider: ValidateDataResourceConfig")
|
2018-08-24 19:10:26 -05:00
|
|
|
|
2023-07-03 11:37:50 -05:00
|
|
|
schema := p.GetProviderSchema()
|
2022-06-02 17:03:35 -05:00
|
|
|
if schema.Diagnostics.HasErrors() {
|
|
|
|
resp.Diagnostics = schema.Diagnostics
|
|
|
|
return resp
|
|
|
|
}
|
|
|
|
|
|
|
|
dataSchema, ok := schema.DataSources[r.TypeName]
|
|
|
|
if !ok {
|
|
|
|
resp.Diagnostics = resp.Diagnostics.Append(fmt.Errorf("unknown data source %q", r.TypeName))
|
2022-05-03 09:22:55 -05:00
|
|
|
return resp
|
|
|
|
}
|
2018-08-08 15:57:31 -05:00
|
|
|
|
|
|
|
mp, err := msgpack.Marshal(r.Config, dataSchema.Block.ImpliedType())
|
|
|
|
if err != nil {
|
|
|
|
resp.Diagnostics = resp.Diagnostics.Append(err)
|
|
|
|
return resp
|
|
|
|
}
|
|
|
|
|
|
|
|
protoReq := &proto.ValidateDataSourceConfig_Request{
|
|
|
|
TypeName: r.TypeName,
|
|
|
|
Config: &proto.DynamicValue{Msgpack: mp},
|
|
|
|
}
|
|
|
|
|
|
|
|
protoResp, err := p.client.ValidateDataSourceConfig(p.ctx, protoReq)
|
|
|
|
if err != nil {
|
2020-10-22 16:28:54 -05:00
|
|
|
resp.Diagnostics = resp.Diagnostics.Append(grpcErr(err))
|
2018-08-08 15:57:31 -05:00
|
|
|
return resp
|
|
|
|
}
|
2018-08-15 11:03:39 -05:00
|
|
|
resp.Diagnostics = resp.Diagnostics.Append(convert.ProtoToDiagnostics(protoResp.Diagnostics))
|
2018-08-08 15:57:31 -05:00
|
|
|
return resp
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *GRPCProvider) UpgradeResourceState(r providers.UpgradeResourceStateRequest) (resp providers.UpgradeResourceStateResponse) {
|
2020-10-22 16:28:54 -05:00
|
|
|
logger.Trace("GRPCProvider: UpgradeResourceState")
|
2018-08-24 19:10:26 -05:00
|
|
|
|
2023-07-03 11:37:50 -05:00
|
|
|
schema := p.GetProviderSchema()
|
2022-06-02 17:03:35 -05:00
|
|
|
if schema.Diagnostics.HasErrors() {
|
|
|
|
resp.Diagnostics = schema.Diagnostics
|
|
|
|
return resp
|
|
|
|
}
|
|
|
|
|
|
|
|
resSchema, ok := schema.ResourceTypes[r.TypeName]
|
|
|
|
if !ok {
|
|
|
|
resp.Diagnostics = resp.Diagnostics.Append(fmt.Errorf("unknown resource type %q", r.TypeName))
|
2022-05-03 09:22:55 -05:00
|
|
|
return resp
|
|
|
|
}
|
2018-08-08 15:57:31 -05:00
|
|
|
|
|
|
|
protoReq := &proto.UpgradeResourceState_Request{
|
|
|
|
TypeName: r.TypeName,
|
|
|
|
Version: int64(r.Version),
|
|
|
|
RawState: &proto.RawState{
|
|
|
|
Json: r.RawStateJSON,
|
|
|
|
Flatmap: r.RawStateFlatmap,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
protoResp, err := p.client.UpgradeResourceState(p.ctx, protoReq)
|
|
|
|
if err != nil {
|
2020-10-22 16:28:54 -05:00
|
|
|
resp.Diagnostics = resp.Diagnostics.Append(grpcErr(err))
|
2018-08-08 15:57:31 -05:00
|
|
|
return resp
|
|
|
|
}
|
2018-10-06 13:26:07 -05:00
|
|
|
resp.Diagnostics = resp.Diagnostics.Append(convert.ProtoToDiagnostics(protoResp.Diagnostics))
|
2018-08-08 15:57:31 -05:00
|
|
|
|
2020-10-16 14:05:49 -05:00
|
|
|
ty := resSchema.Block.ImpliedType()
|
|
|
|
resp.UpgradedState = cty.NullVal(ty)
|
|
|
|
if protoResp.UpgradedState == nil {
|
|
|
|
return resp
|
2018-08-08 15:57:31 -05:00
|
|
|
}
|
|
|
|
|
2020-10-16 14:05:49 -05:00
|
|
|
state, err := decodeDynamicValue(protoResp.UpgradedState, ty)
|
|
|
|
if err != nil {
|
|
|
|
resp.Diagnostics = resp.Diagnostics.Append(err)
|
|
|
|
return resp
|
|
|
|
}
|
2018-08-08 15:57:31 -05:00
|
|
|
resp.UpgradedState = state
|
2020-10-16 14:05:49 -05:00
|
|
|
|
2018-08-08 15:57:31 -05:00
|
|
|
return resp
|
|
|
|
}
|
|
|
|
|
2021-02-18 09:13:43 -06:00
|
|
|
func (p *GRPCProvider) ConfigureProvider(r providers.ConfigureProviderRequest) (resp providers.ConfigureProviderResponse) {
|
|
|
|
logger.Trace("GRPCProvider: ConfigureProvider")
|
2018-08-24 19:10:26 -05:00
|
|
|
|
2023-07-03 11:37:50 -05:00
|
|
|
schema := p.GetProviderSchema()
|
2022-06-02 17:03:35 -05:00
|
|
|
if schema.Diagnostics.HasErrors() {
|
|
|
|
resp.Diagnostics = schema.Diagnostics
|
|
|
|
return resp
|
|
|
|
}
|
2018-08-08 15:57:31 -05:00
|
|
|
|
|
|
|
var mp []byte
|
|
|
|
|
|
|
|
// we don't have anything to marshal if there's no config
|
|
|
|
mp, err := msgpack.Marshal(r.Config, schema.Provider.Block.ImpliedType())
|
|
|
|
if err != nil {
|
|
|
|
resp.Diagnostics = resp.Diagnostics.Append(err)
|
|
|
|
return resp
|
|
|
|
}
|
|
|
|
|
|
|
|
protoReq := &proto.Configure_Request{
|
2019-08-30 06:12:41 -05:00
|
|
|
TerraformVersion: r.TerraformVersion,
|
2018-08-08 15:57:31 -05:00
|
|
|
Config: &proto.DynamicValue{
|
|
|
|
Msgpack: mp,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
protoResp, err := p.client.Configure(p.ctx, protoReq)
|
|
|
|
if err != nil {
|
2020-10-22 16:28:54 -05:00
|
|
|
resp.Diagnostics = resp.Diagnostics.Append(grpcErr(err))
|
2018-08-08 15:57:31 -05:00
|
|
|
return resp
|
|
|
|
}
|
2018-08-15 11:03:39 -05:00
|
|
|
resp.Diagnostics = resp.Diagnostics.Append(convert.ProtoToDiagnostics(protoResp.Diagnostics))
|
2018-08-08 15:57:31 -05:00
|
|
|
return resp
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *GRPCProvider) Stop() error {
|
2020-10-22 16:28:54 -05:00
|
|
|
logger.Trace("GRPCProvider: Stop")
|
2018-08-24 19:10:26 -05:00
|
|
|
|
2018-08-08 15:57:31 -05:00
|
|
|
resp, err := p.client.Stop(p.ctx, new(proto.Stop_Request))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if resp.Error != "" {
|
|
|
|
return errors.New(resp.Error)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *GRPCProvider) ReadResource(r providers.ReadResourceRequest) (resp providers.ReadResourceResponse) {
|
2020-10-22 16:28:54 -05:00
|
|
|
logger.Trace("GRPCProvider: ReadResource")
|
2018-08-24 19:10:26 -05:00
|
|
|
|
2023-07-03 11:37:50 -05:00
|
|
|
schema := p.GetProviderSchema()
|
2022-06-02 17:03:35 -05:00
|
|
|
if schema.Diagnostics.HasErrors() {
|
|
|
|
resp.Diagnostics = schema.Diagnostics
|
|
|
|
return resp
|
|
|
|
}
|
|
|
|
|
|
|
|
resSchema, ok := schema.ResourceTypes[r.TypeName]
|
|
|
|
if !ok {
|
|
|
|
resp.Diagnostics = resp.Diagnostics.Append(fmt.Errorf("unknown resource type " + r.TypeName))
|
2022-05-03 09:22:55 -05:00
|
|
|
return resp
|
|
|
|
}
|
2018-08-08 15:57:31 -05:00
|
|
|
|
2022-06-02 17:03:35 -05:00
|
|
|
metaSchema := schema.ProviderMeta
|
|
|
|
|
2018-08-08 15:57:31 -05:00
|
|
|
mp, err := msgpack.Marshal(r.PriorState, resSchema.Block.ImpliedType())
|
|
|
|
if err != nil {
|
|
|
|
resp.Diagnostics = resp.Diagnostics.Append(err)
|
|
|
|
return resp
|
|
|
|
}
|
|
|
|
|
|
|
|
protoReq := &proto.ReadResource_Request{
|
|
|
|
TypeName: r.TypeName,
|
|
|
|
CurrentState: &proto.DynamicValue{Msgpack: mp},
|
2019-06-03 17:08:26 -05:00
|
|
|
Private: r.Private,
|
2018-08-08 15:57:31 -05:00
|
|
|
}
|
|
|
|
|
2020-03-05 18:53:24 -06:00
|
|
|
if metaSchema.Block != nil {
|
|
|
|
metaMP, err := msgpack.Marshal(r.ProviderMeta, metaSchema.Block.ImpliedType())
|
|
|
|
if err != nil {
|
|
|
|
resp.Diagnostics = resp.Diagnostics.Append(err)
|
|
|
|
return resp
|
|
|
|
}
|
|
|
|
protoReq.ProviderMeta = &proto.DynamicValue{Msgpack: metaMP}
|
|
|
|
}
|
|
|
|
|
2018-08-08 15:57:31 -05:00
|
|
|
protoResp, err := p.client.ReadResource(p.ctx, protoReq)
|
|
|
|
if err != nil {
|
2020-10-22 16:28:54 -05:00
|
|
|
resp.Diagnostics = resp.Diagnostics.Append(grpcErr(err))
|
2018-08-08 15:57:31 -05:00
|
|
|
return resp
|
|
|
|
}
|
2018-08-15 11:03:39 -05:00
|
|
|
resp.Diagnostics = resp.Diagnostics.Append(convert.ProtoToDiagnostics(protoResp.Diagnostics))
|
2018-08-08 15:57:31 -05:00
|
|
|
|
2020-10-16 14:05:49 -05:00
|
|
|
state, err := decodeDynamicValue(protoResp.NewState, resSchema.Block.ImpliedType())
|
|
|
|
if err != nil {
|
|
|
|
resp.Diagnostics = resp.Diagnostics.Append(err)
|
|
|
|
return resp
|
2018-08-08 15:57:31 -05:00
|
|
|
}
|
|
|
|
resp.NewState = state
|
2019-06-03 17:08:26 -05:00
|
|
|
resp.Private = protoResp.Private
|
2018-10-03 12:45:12 -05:00
|
|
|
|
2018-08-08 15:57:31 -05:00
|
|
|
return resp
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *GRPCProvider) PlanResourceChange(r providers.PlanResourceChangeRequest) (resp providers.PlanResourceChangeResponse) {
|
2020-10-22 16:28:54 -05:00
|
|
|
logger.Trace("GRPCProvider: PlanResourceChange")
|
2018-08-24 19:10:26 -05:00
|
|
|
|
2023-07-03 11:37:50 -05:00
|
|
|
schema := p.GetProviderSchema()
|
2022-06-02 17:03:35 -05:00
|
|
|
if schema.Diagnostics.HasErrors() {
|
|
|
|
resp.Diagnostics = schema.Diagnostics
|
|
|
|
return resp
|
|
|
|
}
|
|
|
|
|
|
|
|
resSchema, ok := schema.ResourceTypes[r.TypeName]
|
|
|
|
if !ok {
|
|
|
|
resp.Diagnostics = resp.Diagnostics.Append(fmt.Errorf("unknown resource type %q", r.TypeName))
|
|
|
|
return resp
|
|
|
|
}
|
|
|
|
|
|
|
|
metaSchema := schema.ProviderMeta
|
2022-07-06 12:47:30 -05:00
|
|
|
capabilities := schema.ServerCapabilities
|
2022-06-02 17:03:35 -05:00
|
|
|
|
|
|
|
// If the provider doesn't support planning a destroy operation, we can
|
|
|
|
// return immediately.
|
|
|
|
if r.ProposedNewState.IsNull() && !capabilities.PlanDestroy {
|
|
|
|
resp.PlannedState = r.ProposedNewState
|
|
|
|
resp.PlannedPrivate = r.PriorPrivate
|
2022-05-03 09:22:55 -05:00
|
|
|
return resp
|
|
|
|
}
|
2018-08-08 15:57:31 -05:00
|
|
|
|
|
|
|
priorMP, err := msgpack.Marshal(r.PriorState, resSchema.Block.ImpliedType())
|
|
|
|
if err != nil {
|
|
|
|
resp.Diagnostics = resp.Diagnostics.Append(err)
|
|
|
|
return resp
|
|
|
|
}
|
2018-12-03 11:47:31 -06:00
|
|
|
|
|
|
|
configMP, err := msgpack.Marshal(r.Config, resSchema.Block.ImpliedType())
|
|
|
|
if err != nil {
|
|
|
|
resp.Diagnostics = resp.Diagnostics.Append(err)
|
|
|
|
return resp
|
|
|
|
}
|
|
|
|
|
2018-08-08 15:57:31 -05:00
|
|
|
propMP, err := msgpack.Marshal(r.ProposedNewState, resSchema.Block.ImpliedType())
|
|
|
|
if err != nil {
|
|
|
|
resp.Diagnostics = resp.Diagnostics.Append(err)
|
|
|
|
return resp
|
|
|
|
}
|
|
|
|
|
|
|
|
protoReq := &proto.PlanResourceChange_Request{
|
|
|
|
TypeName: r.TypeName,
|
|
|
|
PriorState: &proto.DynamicValue{Msgpack: priorMP},
|
2018-12-03 11:47:31 -06:00
|
|
|
Config: &proto.DynamicValue{Msgpack: configMP},
|
2018-08-08 15:57:31 -05:00
|
|
|
ProposedNewState: &proto.DynamicValue{Msgpack: propMP},
|
|
|
|
PriorPrivate: r.PriorPrivate,
|
|
|
|
}
|
|
|
|
|
2020-03-05 18:53:24 -06:00
|
|
|
if metaSchema.Block != nil {
|
|
|
|
metaMP, err := msgpack.Marshal(r.ProviderMeta, metaSchema.Block.ImpliedType())
|
|
|
|
if err != nil {
|
|
|
|
resp.Diagnostics = resp.Diagnostics.Append(err)
|
|
|
|
return resp
|
|
|
|
}
|
|
|
|
protoReq.ProviderMeta = &proto.DynamicValue{Msgpack: metaMP}
|
|
|
|
}
|
|
|
|
|
2018-08-08 15:57:31 -05:00
|
|
|
protoResp, err := p.client.PlanResourceChange(p.ctx, protoReq)
|
|
|
|
if err != nil {
|
2020-10-22 16:28:54 -05:00
|
|
|
resp.Diagnostics = resp.Diagnostics.Append(grpcErr(err))
|
2018-08-08 15:57:31 -05:00
|
|
|
return resp
|
|
|
|
}
|
2018-08-15 11:03:39 -05:00
|
|
|
resp.Diagnostics = resp.Diagnostics.Append(convert.ProtoToDiagnostics(protoResp.Diagnostics))
|
2018-08-08 15:57:31 -05:00
|
|
|
|
2020-10-16 14:05:49 -05:00
|
|
|
state, err := decodeDynamicValue(protoResp.PlannedState, resSchema.Block.ImpliedType())
|
|
|
|
if err != nil {
|
|
|
|
resp.Diagnostics = resp.Diagnostics.Append(err)
|
|
|
|
return resp
|
2018-08-08 15:57:31 -05:00
|
|
|
}
|
|
|
|
resp.PlannedState = state
|
|
|
|
|
|
|
|
for _, p := range protoResp.RequiresReplace {
|
2018-08-15 11:03:39 -05:00
|
|
|
resp.RequiresReplace = append(resp.RequiresReplace, convert.AttributePathToPath(p))
|
2018-08-08 15:57:31 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
resp.PlannedPrivate = protoResp.PlannedPrivate
|
|
|
|
|
2019-02-08 22:32:44 -06:00
|
|
|
resp.LegacyTypeSystem = protoResp.LegacyTypeSystem
|
|
|
|
|
2018-08-08 15:57:31 -05:00
|
|
|
return resp
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *GRPCProvider) ApplyResourceChange(r providers.ApplyResourceChangeRequest) (resp providers.ApplyResourceChangeResponse) {
|
2020-10-22 16:28:54 -05:00
|
|
|
logger.Trace("GRPCProvider: ApplyResourceChange")
|
2018-08-24 19:10:26 -05:00
|
|
|
|
2023-07-03 11:37:50 -05:00
|
|
|
schema := p.GetProviderSchema()
|
2022-06-02 17:03:35 -05:00
|
|
|
if schema.Diagnostics.HasErrors() {
|
|
|
|
resp.Diagnostics = schema.Diagnostics
|
|
|
|
return resp
|
|
|
|
}
|
|
|
|
|
|
|
|
resSchema, ok := schema.ResourceTypes[r.TypeName]
|
|
|
|
if !ok {
|
|
|
|
resp.Diagnostics = resp.Diagnostics.Append(fmt.Errorf("unknown resource type %q", r.TypeName))
|
2022-05-03 09:22:55 -05:00
|
|
|
return resp
|
|
|
|
}
|
2018-08-08 15:57:31 -05:00
|
|
|
|
2022-06-02 17:03:35 -05:00
|
|
|
metaSchema := schema.ProviderMeta
|
|
|
|
|
2018-08-08 15:57:31 -05:00
|
|
|
priorMP, err := msgpack.Marshal(r.PriorState, resSchema.Block.ImpliedType())
|
|
|
|
if err != nil {
|
|
|
|
resp.Diagnostics = resp.Diagnostics.Append(err)
|
|
|
|
return resp
|
|
|
|
}
|
|
|
|
plannedMP, err := msgpack.Marshal(r.PlannedState, resSchema.Block.ImpliedType())
|
|
|
|
if err != nil {
|
|
|
|
resp.Diagnostics = resp.Diagnostics.Append(err)
|
|
|
|
return resp
|
|
|
|
}
|
2019-01-04 12:47:04 -06:00
|
|
|
configMP, err := msgpack.Marshal(r.Config, resSchema.Block.ImpliedType())
|
|
|
|
if err != nil {
|
|
|
|
resp.Diagnostics = resp.Diagnostics.Append(err)
|
|
|
|
return resp
|
|
|
|
}
|
2018-08-08 15:57:31 -05:00
|
|
|
|
|
|
|
protoReq := &proto.ApplyResourceChange_Request{
|
|
|
|
TypeName: r.TypeName,
|
|
|
|
PriorState: &proto.DynamicValue{Msgpack: priorMP},
|
|
|
|
PlannedState: &proto.DynamicValue{Msgpack: plannedMP},
|
2019-01-04 12:47:04 -06:00
|
|
|
Config: &proto.DynamicValue{Msgpack: configMP},
|
2018-08-08 15:57:31 -05:00
|
|
|
PlannedPrivate: r.PlannedPrivate,
|
|
|
|
}
|
|
|
|
|
2020-03-05 18:53:24 -06:00
|
|
|
if metaSchema.Block != nil {
|
|
|
|
metaMP, err := msgpack.Marshal(r.ProviderMeta, metaSchema.Block.ImpliedType())
|
|
|
|
if err != nil {
|
|
|
|
resp.Diagnostics = resp.Diagnostics.Append(err)
|
|
|
|
return resp
|
|
|
|
}
|
|
|
|
protoReq.ProviderMeta = &proto.DynamicValue{Msgpack: metaMP}
|
|
|
|
}
|
|
|
|
|
2018-08-08 15:57:31 -05:00
|
|
|
protoResp, err := p.client.ApplyResourceChange(p.ctx, protoReq)
|
|
|
|
if err != nil {
|
2020-10-22 16:28:54 -05:00
|
|
|
resp.Diagnostics = resp.Diagnostics.Append(grpcErr(err))
|
2018-08-08 15:57:31 -05:00
|
|
|
return resp
|
|
|
|
}
|
2018-10-06 13:26:07 -05:00
|
|
|
resp.Diagnostics = resp.Diagnostics.Append(convert.ProtoToDiagnostics(protoResp.Diagnostics))
|
2018-08-08 15:57:31 -05:00
|
|
|
|
|
|
|
resp.Private = protoResp.Private
|
|
|
|
|
2020-10-16 14:05:49 -05:00
|
|
|
state, err := decodeDynamicValue(protoResp.NewState, resSchema.Block.ImpliedType())
|
|
|
|
if err != nil {
|
|
|
|
resp.Diagnostics = resp.Diagnostics.Append(err)
|
|
|
|
return resp
|
2018-08-08 15:57:31 -05:00
|
|
|
}
|
2018-10-03 12:45:12 -05:00
|
|
|
resp.NewState = state
|
2018-08-08 15:57:31 -05:00
|
|
|
|
2019-02-05 16:28:58 -06:00
|
|
|
resp.LegacyTypeSystem = protoResp.LegacyTypeSystem
|
|
|
|
|
2018-08-08 15:57:31 -05:00
|
|
|
return resp
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *GRPCProvider) ImportResourceState(r providers.ImportResourceStateRequest) (resp providers.ImportResourceStateResponse) {
|
2020-10-22 16:28:54 -05:00
|
|
|
logger.Trace("GRPCProvider: ImportResourceState")
|
2018-08-24 19:10:26 -05:00
|
|
|
|
2023-07-03 11:37:50 -05:00
|
|
|
schema := p.GetProviderSchema()
|
2022-06-02 17:03:35 -05:00
|
|
|
if schema.Diagnostics.HasErrors() {
|
|
|
|
resp.Diagnostics = schema.Diagnostics
|
|
|
|
return resp
|
|
|
|
}
|
|
|
|
|
2018-08-08 15:57:31 -05:00
|
|
|
protoReq := &proto.ImportResourceState_Request{
|
|
|
|
TypeName: r.TypeName,
|
|
|
|
Id: r.ID,
|
|
|
|
}
|
|
|
|
|
|
|
|
protoResp, err := p.client.ImportResourceState(p.ctx, protoReq)
|
|
|
|
if err != nil {
|
2020-10-22 16:28:54 -05:00
|
|
|
resp.Diagnostics = resp.Diagnostics.Append(grpcErr(err))
|
2018-08-08 15:57:31 -05:00
|
|
|
return resp
|
|
|
|
}
|
2018-10-06 13:26:07 -05:00
|
|
|
resp.Diagnostics = resp.Diagnostics.Append(convert.ProtoToDiagnostics(protoResp.Diagnostics))
|
2018-08-08 15:57:31 -05:00
|
|
|
|
|
|
|
for _, imported := range protoResp.ImportedResources {
|
|
|
|
resource := providers.ImportedResource{
|
|
|
|
TypeName: imported.TypeName,
|
|
|
|
Private: imported.Private,
|
|
|
|
}
|
|
|
|
|
2022-06-02 17:03:35 -05:00
|
|
|
resSchema, ok := schema.ResourceTypes[r.TypeName]
|
|
|
|
if !ok {
|
|
|
|
resp.Diagnostics = resp.Diagnostics.Append(fmt.Errorf("unknown resource type %q", r.TypeName))
|
|
|
|
continue
|
2022-05-03 09:22:55 -05:00
|
|
|
}
|
|
|
|
|
2020-10-16 14:05:49 -05:00
|
|
|
state, err := decodeDynamicValue(imported.State, resSchema.Block.ImpliedType())
|
|
|
|
if err != nil {
|
|
|
|
resp.Diagnostics = resp.Diagnostics.Append(err)
|
|
|
|
return resp
|
2018-10-03 12:45:12 -05:00
|
|
|
}
|
2018-08-08 15:57:31 -05:00
|
|
|
resource.State = state
|
|
|
|
resp.ImportedResources = append(resp.ImportedResources, resource)
|
|
|
|
}
|
|
|
|
|
|
|
|
return resp
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *GRPCProvider) ReadDataSource(r providers.ReadDataSourceRequest) (resp providers.ReadDataSourceResponse) {
|
2020-10-22 16:28:54 -05:00
|
|
|
logger.Trace("GRPCProvider: ReadDataSource")
|
2018-08-24 19:10:26 -05:00
|
|
|
|
2023-07-03 11:37:50 -05:00
|
|
|
schema := p.GetProviderSchema()
|
2022-06-02 17:03:35 -05:00
|
|
|
if schema.Diagnostics.HasErrors() {
|
|
|
|
resp.Diagnostics = schema.Diagnostics
|
2022-05-03 09:22:55 -05:00
|
|
|
return resp
|
|
|
|
}
|
2018-08-08 15:57:31 -05:00
|
|
|
|
2022-06-02 17:03:35 -05:00
|
|
|
dataSchema, ok := schema.DataSources[r.TypeName]
|
|
|
|
if !ok {
|
|
|
|
schema.Diagnostics = schema.Diagnostics.Append(fmt.Errorf("unknown data source %q", r.TypeName))
|
|
|
|
}
|
|
|
|
|
|
|
|
metaSchema := schema.ProviderMeta
|
|
|
|
|
2018-08-08 15:57:31 -05:00
|
|
|
config, err := msgpack.Marshal(r.Config, dataSchema.Block.ImpliedType())
|
|
|
|
if err != nil {
|
|
|
|
resp.Diagnostics = resp.Diagnostics.Append(err)
|
|
|
|
return resp
|
|
|
|
}
|
|
|
|
|
|
|
|
protoReq := &proto.ReadDataSource_Request{
|
|
|
|
TypeName: r.TypeName,
|
|
|
|
Config: &proto.DynamicValue{
|
|
|
|
Msgpack: config,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2020-03-05 18:53:24 -06:00
|
|
|
if metaSchema.Block != nil {
|
|
|
|
metaMP, err := msgpack.Marshal(r.ProviderMeta, metaSchema.Block.ImpliedType())
|
|
|
|
if err != nil {
|
|
|
|
resp.Diagnostics = resp.Diagnostics.Append(err)
|
|
|
|
return resp
|
|
|
|
}
|
|
|
|
protoReq.ProviderMeta = &proto.DynamicValue{Msgpack: metaMP}
|
|
|
|
}
|
|
|
|
|
2018-08-08 15:57:31 -05:00
|
|
|
protoResp, err := p.client.ReadDataSource(p.ctx, protoReq)
|
|
|
|
if err != nil {
|
2020-10-22 16:28:54 -05:00
|
|
|
resp.Diagnostics = resp.Diagnostics.Append(grpcErr(err))
|
2018-08-08 15:57:31 -05:00
|
|
|
return resp
|
|
|
|
}
|
2018-10-06 13:26:07 -05:00
|
|
|
resp.Diagnostics = resp.Diagnostics.Append(convert.ProtoToDiagnostics(protoResp.Diagnostics))
|
|
|
|
|
2020-10-16 14:05:49 -05:00
|
|
|
state, err := decodeDynamicValue(protoResp.State, dataSchema.Block.ImpliedType())
|
|
|
|
if err != nil {
|
|
|
|
resp.Diagnostics = resp.Diagnostics.Append(err)
|
|
|
|
return resp
|
2018-08-08 15:57:31 -05:00
|
|
|
}
|
|
|
|
resp.State = state
|
|
|
|
|
2018-10-03 12:45:12 -05:00
|
|
|
return resp
|
2018-08-08 15:57:31 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// closing the grpc connection is final, and terraform will call it at the end of every phase.
|
|
|
|
func (p *GRPCProvider) Close() error {
|
2020-10-22 16:28:54 -05:00
|
|
|
logger.Trace("GRPCProvider: Close")
|
2018-10-16 13:31:27 -05:00
|
|
|
|
2019-04-29 13:14:04 -05:00
|
|
|
// Make sure to stop the server if we're not running within go-plugin.
|
|
|
|
if p.TestServer != nil {
|
|
|
|
p.TestServer.Stop()
|
2018-10-16 13:31:27 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Check this since it's not automatically inserted during plugin creation.
|
|
|
|
// It's currently only inserted by the command package, because that is
|
|
|
|
// where the factory is built and is the only point with access to the
|
|
|
|
// plugin.Client.
|
2018-10-02 15:06:12 -05:00
|
|
|
if p.PluginClient == nil {
|
2020-10-22 16:28:54 -05:00
|
|
|
logger.Debug("provider has no plugin.Client")
|
2018-10-02 15:06:12 -05:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
p.PluginClient.Kill()
|
2018-08-08 15:57:31 -05:00
|
|
|
return nil
|
|
|
|
}
|
2020-10-16 14:05:49 -05:00
|
|
|
|
|
|
|
// Decode a DynamicValue from either the JSON or MsgPack encoding.
|
|
|
|
func decodeDynamicValue(v *proto.DynamicValue, ty cty.Type) (cty.Value, error) {
|
|
|
|
// always return a valid value
|
2020-10-19 08:31:37 -05:00
|
|
|
var err error
|
2020-10-16 14:05:49 -05:00
|
|
|
res := cty.NullVal(ty)
|
|
|
|
if v == nil {
|
|
|
|
return res, nil
|
|
|
|
}
|
|
|
|
|
2020-10-19 08:31:37 -05:00
|
|
|
switch {
|
|
|
|
case len(v.Msgpack) > 0:
|
|
|
|
res, err = msgpack.Unmarshal(v.Msgpack, ty)
|
|
|
|
case len(v.Json) > 0:
|
|
|
|
res, err = ctyjson.Unmarshal(v.Json, ty)
|
2020-10-16 14:05:49 -05:00
|
|
|
}
|
2020-10-19 08:31:37 -05:00
|
|
|
return res, err
|
2020-10-16 14:05:49 -05:00
|
|
|
}
|