2018-08-08 22:57:31 +02:00
|
|
|
package plugin
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"errors"
|
2018-10-16 03:15:08 +02:00
|
|
|
"io"
|
2018-08-25 02:10:26 +02:00
|
|
|
"log"
|
2018-08-08 22:57:31 +02:00
|
|
|
"sync"
|
|
|
|
|
2018-08-28 02:34:14 +02:00
|
|
|
"github.com/zclconf/go-cty/cty"
|
|
|
|
|
2018-08-14 22:55:20 +02:00
|
|
|
plugin "github.com/hashicorp/go-plugin"
|
2018-11-19 18:39:16 +01:00
|
|
|
proto "github.com/hashicorp/terraform/internal/tfplugin5"
|
2018-08-15 18:03:39 +02:00
|
|
|
"github.com/hashicorp/terraform/plugin/convert"
|
2018-08-08 22:57:31 +02:00
|
|
|
"github.com/hashicorp/terraform/providers"
|
|
|
|
"github.com/hashicorp/terraform/version"
|
|
|
|
"github.com/zclconf/go-cty/cty/msgpack"
|
|
|
|
"google.golang.org/grpc"
|
|
|
|
)
|
|
|
|
|
2018-08-14 22:55:20 +02: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 22:57:31 +02:00
|
|
|
// GRPCProvider handles the client, or core side of the plugin rpc connection.
|
|
|
|
// The GRPCProvider methods are mostly a translation layer between the
|
|
|
|
// terraform provioders types and the grpc proto types, directly converting
|
|
|
|
// between the two.
|
|
|
|
type GRPCProvider struct {
|
2018-10-02 22:06:12 +02: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
|
|
|
|
|
2018-10-16 03:15:08 +02:00
|
|
|
// TestListener contains a net.Conn to close when the GRPCProvider is being
|
|
|
|
// used in an end to end test of a provider.
|
|
|
|
TestListener io.Closer
|
|
|
|
|
2018-10-02 22:06:12 +02:00
|
|
|
// Proto client use to make the grpc service calls.
|
2018-08-08 22:57:31 +02: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
|
|
|
|
// serialize the state for requests.
|
|
|
|
mu sync.Mutex
|
|
|
|
schemas providers.GetSchemaResponse
|
|
|
|
}
|
|
|
|
|
|
|
|
// getSchema is used internally to get the saved provider schema. The schema
|
|
|
|
// should have already been fetched from the provider, but we have to
|
|
|
|
// synchronize access to avoid being called concurrently with GetSchema.
|
|
|
|
func (p *GRPCProvider) getSchema() providers.GetSchemaResponse {
|
|
|
|
p.mu.Lock()
|
|
|
|
// unlock inline in case GetSchema needs to be called
|
|
|
|
if p.schemas.Provider.Block != nil {
|
2018-08-25 02:10:06 +02:00
|
|
|
p.mu.Unlock()
|
2018-08-08 22:57:31 +02:00
|
|
|
return p.schemas
|
|
|
|
}
|
|
|
|
p.mu.Unlock()
|
|
|
|
|
|
|
|
// the schema should have been fetched already, but give it another shot
|
|
|
|
// just in case things are being called out of order. This may happen for
|
|
|
|
// tests.
|
|
|
|
schemas := p.GetSchema()
|
|
|
|
if schemas.Diagnostics.HasErrors() {
|
|
|
|
panic(schemas.Diagnostics.Err())
|
|
|
|
}
|
|
|
|
|
|
|
|
return schemas
|
|
|
|
}
|
|
|
|
|
|
|
|
// getResourceSchema is a helper to extract the schema for a resource, and
|
|
|
|
// panics if the schema is not available.
|
|
|
|
func (p *GRPCProvider) getResourceSchema(name string) providers.Schema {
|
|
|
|
schema := p.getSchema()
|
|
|
|
resSchema, ok := schema.ResourceTypes[name]
|
|
|
|
if !ok {
|
|
|
|
panic("unknown resource type " + name)
|
|
|
|
}
|
|
|
|
return resSchema
|
|
|
|
}
|
|
|
|
|
|
|
|
// gettDatasourceSchema is a helper to extract the schema for a datasource, and
|
|
|
|
// panics if that schema is not available.
|
|
|
|
func (p *GRPCProvider) getDatasourceSchema(name string) providers.Schema {
|
|
|
|
schema := p.getSchema()
|
|
|
|
dataSchema, ok := schema.DataSources[name]
|
|
|
|
if !ok {
|
|
|
|
panic("unknown data source " + name)
|
|
|
|
}
|
|
|
|
return dataSchema
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *GRPCProvider) GetSchema() (resp providers.GetSchemaResponse) {
|
2018-08-25 02:10:26 +02:00
|
|
|
log.Printf("[TRACE] GRPCProvider: GetSchema")
|
2018-08-08 22:57:31 +02:00
|
|
|
p.mu.Lock()
|
|
|
|
defer p.mu.Unlock()
|
|
|
|
|
|
|
|
if p.schemas.Provider.Block != nil {
|
|
|
|
return p.schemas
|
|
|
|
}
|
|
|
|
|
|
|
|
resp.ResourceTypes = make(map[string]providers.Schema)
|
|
|
|
resp.DataSources = make(map[string]providers.Schema)
|
|
|
|
|
|
|
|
protoResp, err := p.client.GetSchema(p.ctx, new(proto.GetProviderSchema_Request))
|
|
|
|
if err != nil {
|
|
|
|
resp.Diagnostics = resp.Diagnostics.Append(err)
|
|
|
|
return resp
|
|
|
|
}
|
|
|
|
|
2018-10-06 20:26:07 +02:00
|
|
|
resp.Diagnostics = resp.Diagnostics.Append(convert.ProtoToDiagnostics(protoResp.Diagnostics))
|
|
|
|
|
2018-08-08 22:57:31 +02:00
|
|
|
if protoResp.Provider == nil {
|
|
|
|
resp.Diagnostics = resp.Diagnostics.Append(errors.New("missing provider schema"))
|
|
|
|
return resp
|
|
|
|
}
|
|
|
|
|
2018-08-15 18:03:39 +02:00
|
|
|
resp.Provider = convert.ProtoToProviderSchema(protoResp.Provider)
|
2018-08-08 22:57:31 +02:00
|
|
|
|
|
|
|
for name, res := range protoResp.ResourceSchemas {
|
2018-08-15 18:03:39 +02:00
|
|
|
resp.ResourceTypes[name] = convert.ProtoToProviderSchema(res)
|
2018-08-08 22:57:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
for name, data := range protoResp.DataSourceSchemas {
|
2018-08-15 18:03:39 +02:00
|
|
|
resp.DataSources[name] = convert.ProtoToProviderSchema(data)
|
2018-08-08 22:57:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
p.schemas = resp
|
|
|
|
|
|
|
|
return resp
|
|
|
|
}
|
|
|
|
|
2018-10-18 03:20:40 +02:00
|
|
|
func (p *GRPCProvider) PrepareProviderConfig(r providers.PrepareProviderConfigRequest) (resp providers.PrepareProviderConfigResponse) {
|
|
|
|
log.Printf("[TRACE] GRPCProvider: PrepareProviderConfig")
|
2018-08-25 02:10:26 +02:00
|
|
|
|
2018-08-08 22:57:31 +02:00
|
|
|
schema := p.getSchema()
|
2018-10-18 03:20:40 +02:00
|
|
|
ty := schema.Provider.Block.ImpliedType()
|
|
|
|
|
|
|
|
mp, err := msgpack.Marshal(r.Config, ty)
|
2018-08-08 22:57:31 +02:00
|
|
|
if err != nil {
|
|
|
|
resp.Diagnostics = resp.Diagnostics.Append(err)
|
|
|
|
return resp
|
|
|
|
}
|
|
|
|
|
2018-10-18 03:20:40 +02:00
|
|
|
protoReq := &proto.PrepareProviderConfig_Request{
|
2018-08-08 22:57:31 +02:00
|
|
|
Config: &proto.DynamicValue{Msgpack: mp},
|
|
|
|
}
|
|
|
|
|
2018-10-18 03:20:40 +02:00
|
|
|
protoResp, err := p.client.PrepareProviderConfig(p.ctx, protoReq)
|
2018-08-08 22:57:31 +02:00
|
|
|
if err != nil {
|
|
|
|
resp.Diagnostics = resp.Diagnostics.Append(err)
|
|
|
|
return resp
|
|
|
|
}
|
2018-10-06 20:26:07 +02:00
|
|
|
|
2018-10-18 03:20:40 +02:00
|
|
|
config := cty.NullVal(ty)
|
|
|
|
if protoResp.PreparedConfig != nil {
|
|
|
|
config, err = msgpack.Unmarshal(protoResp.PreparedConfig.Msgpack, ty)
|
|
|
|
if err != nil {
|
|
|
|
resp.Diagnostics = resp.Diagnostics.Append(err)
|
|
|
|
return resp
|
|
|
|
}
|
|
|
|
}
|
|
|
|
resp.PreparedConfig = config
|
|
|
|
|
2018-08-15 18:03:39 +02:00
|
|
|
resp.Diagnostics = resp.Diagnostics.Append(convert.ProtoToDiagnostics(protoResp.Diagnostics))
|
2018-08-08 22:57:31 +02:00
|
|
|
return resp
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *GRPCProvider) ValidateResourceTypeConfig(r providers.ValidateResourceTypeConfigRequest) (resp providers.ValidateResourceTypeConfigResponse) {
|
2018-08-25 02:10:26 +02:00
|
|
|
log.Printf("[TRACE] GRPCProvider: ValidateResourceTypeConfig")
|
|
|
|
|
2018-08-08 22:57:31 +02:00
|
|
|
resourceSchema := p.getResourceSchema(r.TypeName)
|
|
|
|
|
|
|
|
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 {
|
|
|
|
resp.Diagnostics = resp.Diagnostics.Append(err)
|
|
|
|
return resp
|
|
|
|
}
|
|
|
|
|
2018-08-15 18:03:39 +02:00
|
|
|
resp.Diagnostics = resp.Diagnostics.Append(convert.ProtoToDiagnostics(protoResp.Diagnostics))
|
2018-08-08 22:57:31 +02:00
|
|
|
return resp
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *GRPCProvider) ValidateDataSourceConfig(r providers.ValidateDataSourceConfigRequest) (resp providers.ValidateDataSourceConfigResponse) {
|
2018-08-25 02:10:26 +02:00
|
|
|
log.Printf("[TRACE] GRPCProvider: ValidateDataSourceConfig")
|
|
|
|
|
2018-08-08 22:57:31 +02:00
|
|
|
dataSchema := p.getDatasourceSchema(r.TypeName)
|
|
|
|
|
|
|
|
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 {
|
|
|
|
resp.Diagnostics = resp.Diagnostics.Append(err)
|
|
|
|
return resp
|
|
|
|
}
|
2018-08-15 18:03:39 +02:00
|
|
|
resp.Diagnostics = resp.Diagnostics.Append(convert.ProtoToDiagnostics(protoResp.Diagnostics))
|
2018-08-08 22:57:31 +02:00
|
|
|
return resp
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *GRPCProvider) UpgradeResourceState(r providers.UpgradeResourceStateRequest) (resp providers.UpgradeResourceStateResponse) {
|
2018-08-25 02:10:26 +02:00
|
|
|
log.Printf("[TRACE] GRPCProvider: UpgradeResourceState")
|
|
|
|
|
2018-08-08 22:57:31 +02:00
|
|
|
resSchema := p.getResourceSchema(r.TypeName)
|
|
|
|
|
|
|
|
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 {
|
|
|
|
resp.Diagnostics = resp.Diagnostics.Append(err)
|
|
|
|
return resp
|
|
|
|
}
|
2018-10-06 20:26:07 +02:00
|
|
|
resp.Diagnostics = resp.Diagnostics.Append(convert.ProtoToDiagnostics(protoResp.Diagnostics))
|
2018-08-08 22:57:31 +02:00
|
|
|
|
2018-10-03 19:45:12 +02:00
|
|
|
state := cty.NullVal(resSchema.Block.ImpliedType())
|
|
|
|
if protoResp.UpgradedState != nil {
|
|
|
|
state, err = msgpack.Unmarshal(protoResp.UpgradedState.Msgpack, resSchema.Block.ImpliedType())
|
|
|
|
if err != nil {
|
|
|
|
resp.Diagnostics = resp.Diagnostics.Append(err)
|
|
|
|
return resp
|
|
|
|
}
|
2018-08-08 22:57:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
resp.UpgradedState = state
|
|
|
|
return resp
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *GRPCProvider) Configure(r providers.ConfigureRequest) (resp providers.ConfigureResponse) {
|
2018-08-25 02:10:26 +02:00
|
|
|
log.Printf("[TRACE] GRPCProvider: Configure")
|
|
|
|
|
2018-08-08 22:57:31 +02:00
|
|
|
schema := p.getSchema()
|
|
|
|
|
|
|
|
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{
|
|
|
|
TerraformVersion: version.Version,
|
|
|
|
Config: &proto.DynamicValue{
|
|
|
|
Msgpack: mp,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
protoResp, err := p.client.Configure(p.ctx, protoReq)
|
|
|
|
if err != nil {
|
|
|
|
resp.Diagnostics = resp.Diagnostics.Append(err)
|
|
|
|
return resp
|
|
|
|
}
|
2018-08-15 18:03:39 +02:00
|
|
|
resp.Diagnostics = resp.Diagnostics.Append(convert.ProtoToDiagnostics(protoResp.Diagnostics))
|
2018-08-08 22:57:31 +02:00
|
|
|
return resp
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *GRPCProvider) Stop() error {
|
2018-08-25 02:10:26 +02:00
|
|
|
log.Printf("[TRACE] GRPCProvider: Stop")
|
|
|
|
|
2018-08-08 22:57:31 +02: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) {
|
2018-08-25 02:10:26 +02:00
|
|
|
log.Printf("[TRACE] GRPCProvider: ReadResource")
|
|
|
|
|
2018-08-08 22:57:31 +02:00
|
|
|
resSchema := p.getResourceSchema(r.TypeName)
|
|
|
|
|
|
|
|
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},
|
|
|
|
}
|
|
|
|
|
|
|
|
protoResp, err := p.client.ReadResource(p.ctx, protoReq)
|
|
|
|
if err != nil {
|
|
|
|
resp.Diagnostics = resp.Diagnostics.Append(err)
|
|
|
|
return resp
|
|
|
|
}
|
2018-08-15 18:03:39 +02:00
|
|
|
resp.Diagnostics = resp.Diagnostics.Append(convert.ProtoToDiagnostics(protoResp.Diagnostics))
|
2018-08-08 22:57:31 +02:00
|
|
|
|
2018-10-03 19:45:12 +02:00
|
|
|
state := cty.NullVal(resSchema.Block.ImpliedType())
|
|
|
|
if protoResp.NewState != nil {
|
|
|
|
state, err = msgpack.Unmarshal(protoResp.NewState.Msgpack, resSchema.Block.ImpliedType())
|
|
|
|
if err != nil {
|
|
|
|
resp.Diagnostics = resp.Diagnostics.Append(err)
|
|
|
|
return resp
|
|
|
|
}
|
2018-08-08 22:57:31 +02:00
|
|
|
}
|
|
|
|
resp.NewState = state
|
2018-10-03 19:45:12 +02:00
|
|
|
|
2018-08-08 22:57:31 +02:00
|
|
|
return resp
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *GRPCProvider) PlanResourceChange(r providers.PlanResourceChangeRequest) (resp providers.PlanResourceChangeResponse) {
|
2018-08-25 02:10:26 +02:00
|
|
|
log.Printf("[TRACE] GRPCProvider: PlanResourceChange")
|
|
|
|
|
2018-08-08 22:57:31 +02:00
|
|
|
resSchema := p.getResourceSchema(r.TypeName)
|
|
|
|
|
|
|
|
priorMP, err := msgpack.Marshal(r.PriorState, resSchema.Block.ImpliedType())
|
|
|
|
if err != nil {
|
|
|
|
resp.Diagnostics = resp.Diagnostics.Append(err)
|
|
|
|
return resp
|
|
|
|
}
|
2018-12-03 18:47:31 +01:00
|
|
|
|
|
|
|
configMP, err := msgpack.Marshal(r.Config, resSchema.Block.ImpliedType())
|
|
|
|
if err != nil {
|
|
|
|
resp.Diagnostics = resp.Diagnostics.Append(err)
|
|
|
|
return resp
|
|
|
|
}
|
|
|
|
|
2018-08-08 22:57:31 +02: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 18:47:31 +01:00
|
|
|
Config: &proto.DynamicValue{Msgpack: configMP},
|
2018-08-08 22:57:31 +02:00
|
|
|
ProposedNewState: &proto.DynamicValue{Msgpack: propMP},
|
|
|
|
PriorPrivate: r.PriorPrivate,
|
|
|
|
}
|
|
|
|
|
|
|
|
protoResp, err := p.client.PlanResourceChange(p.ctx, protoReq)
|
|
|
|
if err != nil {
|
|
|
|
resp.Diagnostics = resp.Diagnostics.Append(err)
|
|
|
|
return resp
|
|
|
|
}
|
2018-08-15 18:03:39 +02:00
|
|
|
resp.Diagnostics = resp.Diagnostics.Append(convert.ProtoToDiagnostics(protoResp.Diagnostics))
|
2018-08-08 22:57:31 +02:00
|
|
|
|
2018-10-03 19:45:12 +02:00
|
|
|
state := cty.NullVal(resSchema.Block.ImpliedType())
|
|
|
|
if protoResp.PlannedState != nil {
|
|
|
|
state, err = msgpack.Unmarshal(protoResp.PlannedState.Msgpack, resSchema.Block.ImpliedType())
|
|
|
|
if err != nil {
|
|
|
|
resp.Diagnostics = resp.Diagnostics.Append(err)
|
|
|
|
return resp
|
|
|
|
}
|
2018-08-08 22:57:31 +02:00
|
|
|
}
|
|
|
|
resp.PlannedState = state
|
|
|
|
|
|
|
|
for _, p := range protoResp.RequiresReplace {
|
2018-08-15 18:03:39 +02:00
|
|
|
resp.RequiresReplace = append(resp.RequiresReplace, convert.AttributePathToPath(p))
|
2018-08-08 22:57:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
resp.PlannedPrivate = protoResp.PlannedPrivate
|
|
|
|
|
|
|
|
return resp
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *GRPCProvider) ApplyResourceChange(r providers.ApplyResourceChangeRequest) (resp providers.ApplyResourceChangeResponse) {
|
2018-08-25 02:10:26 +02:00
|
|
|
log.Printf("[TRACE] GRPCProvider: ApplyResourceChange")
|
|
|
|
|
2018-08-08 22:57:31 +02:00
|
|
|
resSchema := p.getResourceSchema(r.TypeName)
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
protoReq := &proto.ApplyResourceChange_Request{
|
|
|
|
TypeName: r.TypeName,
|
|
|
|
PriorState: &proto.DynamicValue{Msgpack: priorMP},
|
|
|
|
PlannedState: &proto.DynamicValue{Msgpack: plannedMP},
|
|
|
|
PlannedPrivate: r.PlannedPrivate,
|
|
|
|
}
|
|
|
|
|
|
|
|
protoResp, err := p.client.ApplyResourceChange(p.ctx, protoReq)
|
|
|
|
if err != nil {
|
|
|
|
resp.Diagnostics = resp.Diagnostics.Append(err)
|
|
|
|
return resp
|
|
|
|
}
|
2018-10-06 20:26:07 +02:00
|
|
|
resp.Diagnostics = resp.Diagnostics.Append(convert.ProtoToDiagnostics(protoResp.Diagnostics))
|
2018-08-08 22:57:31 +02:00
|
|
|
|
|
|
|
resp.Private = protoResp.Private
|
|
|
|
|
2018-10-03 19:45:12 +02:00
|
|
|
state := cty.NullVal(resSchema.Block.ImpliedType())
|
2018-08-28 02:34:14 +02:00
|
|
|
if protoResp.NewState != nil {
|
2018-10-03 19:45:12 +02:00
|
|
|
state, err = msgpack.Unmarshal(protoResp.NewState.Msgpack, resSchema.Block.ImpliedType())
|
2018-08-28 02:34:14 +02:00
|
|
|
if err != nil {
|
|
|
|
resp.Diagnostics = resp.Diagnostics.Append(err)
|
|
|
|
return resp
|
|
|
|
}
|
2018-08-08 22:57:31 +02:00
|
|
|
}
|
2018-10-03 19:45:12 +02:00
|
|
|
resp.NewState = state
|
2018-08-08 22:57:31 +02:00
|
|
|
|
|
|
|
return resp
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *GRPCProvider) ImportResourceState(r providers.ImportResourceStateRequest) (resp providers.ImportResourceStateResponse) {
|
2018-08-25 02:10:26 +02:00
|
|
|
log.Printf("[TRACE] GRPCProvider: ImportResourceState")
|
|
|
|
|
2018-08-08 22:57:31 +02:00
|
|
|
resSchema := p.getResourceSchema(r.TypeName)
|
|
|
|
|
|
|
|
protoReq := &proto.ImportResourceState_Request{
|
|
|
|
TypeName: r.TypeName,
|
|
|
|
Id: r.ID,
|
|
|
|
}
|
|
|
|
|
|
|
|
protoResp, err := p.client.ImportResourceState(p.ctx, protoReq)
|
|
|
|
if err != nil {
|
|
|
|
resp.Diagnostics = resp.Diagnostics.Append(err)
|
|
|
|
return resp
|
|
|
|
}
|
2018-10-06 20:26:07 +02:00
|
|
|
resp.Diagnostics = resp.Diagnostics.Append(convert.ProtoToDiagnostics(protoResp.Diagnostics))
|
2018-08-08 22:57:31 +02:00
|
|
|
|
|
|
|
for _, imported := range protoResp.ImportedResources {
|
|
|
|
resource := providers.ImportedResource{
|
|
|
|
TypeName: imported.TypeName,
|
|
|
|
Private: imported.Private,
|
|
|
|
}
|
|
|
|
|
2018-10-03 19:45:12 +02:00
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
2018-08-08 22:57:31 +02:00
|
|
|
resource.State = state
|
|
|
|
resp.ImportedResources = append(resp.ImportedResources, resource)
|
|
|
|
}
|
|
|
|
|
|
|
|
return resp
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *GRPCProvider) ReadDataSource(r providers.ReadDataSourceRequest) (resp providers.ReadDataSourceResponse) {
|
2018-08-25 02:10:26 +02:00
|
|
|
log.Printf("[TRACE] GRPCProvider: ReadDataSource")
|
|
|
|
|
2018-08-08 22:57:31 +02:00
|
|
|
dataSchema := p.getDatasourceSchema(r.TypeName)
|
|
|
|
|
|
|
|
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,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
protoResp, err := p.client.ReadDataSource(p.ctx, protoReq)
|
|
|
|
if err != nil {
|
|
|
|
resp.Diagnostics = resp.Diagnostics.Append(err)
|
|
|
|
return resp
|
|
|
|
}
|
2018-10-06 20:26:07 +02:00
|
|
|
resp.Diagnostics = resp.Diagnostics.Append(convert.ProtoToDiagnostics(protoResp.Diagnostics))
|
|
|
|
|
2018-10-03 19:45:12 +02:00
|
|
|
state := cty.NullVal(dataSchema.Block.ImpliedType())
|
|
|
|
if protoResp.State != nil {
|
|
|
|
state, err = msgpack.Unmarshal(protoResp.State.Msgpack, dataSchema.Block.ImpliedType())
|
|
|
|
if err != nil {
|
|
|
|
resp.Diagnostics = resp.Diagnostics.Append(err)
|
|
|
|
return resp
|
|
|
|
}
|
2018-08-08 22:57:31 +02:00
|
|
|
}
|
|
|
|
resp.State = state
|
|
|
|
|
2018-10-03 19:45:12 +02:00
|
|
|
return resp
|
2018-08-08 22:57:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// closing the grpc connection is final, and terraform will call it at the end of every phase.
|
|
|
|
func (p *GRPCProvider) Close() error {
|
2018-12-07 20:22:00 +01:00
|
|
|
log.Printf("[TRACE] GRPCProvider: Close")
|
2018-10-16 20:31:27 +02:00
|
|
|
|
|
|
|
// close the remote listener if we're running within a test
|
|
|
|
if p.TestListener != nil {
|
|
|
|
p.TestListener.Close()
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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 22:06:12 +02:00
|
|
|
if p.PluginClient == nil {
|
|
|
|
log.Println("[DEBUG] provider has no plugin.Client")
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
p.PluginClient.Kill()
|
2018-08-08 22:57:31 +02:00
|
|
|
return nil
|
|
|
|
}
|