2018-08-08 23:02:24 +02:00
|
|
|
package plugin
|
|
|
|
|
|
|
|
import (
|
|
|
|
"log"
|
|
|
|
|
|
|
|
"github.com/hashicorp/terraform/helper/schema"
|
2018-08-15 18:03:39 +02:00
|
|
|
"github.com/hashicorp/terraform/plugin/convert"
|
2018-08-08 23:02:24 +02:00
|
|
|
"github.com/hashicorp/terraform/plugin/proto"
|
|
|
|
"github.com/hashicorp/terraform/terraform"
|
|
|
|
"github.com/zclconf/go-cty/cty"
|
2018-08-15 18:03:39 +02:00
|
|
|
ctyconvert "github.com/zclconf/go-cty/cty/convert"
|
2018-08-08 23:02:24 +02:00
|
|
|
"github.com/zclconf/go-cty/cty/msgpack"
|
|
|
|
context "golang.org/x/net/context"
|
|
|
|
)
|
|
|
|
|
2018-08-14 22:39:30 +02:00
|
|
|
// NewGRPCProvisionerServerShim wraps a terraform.ResourceProvisioner in a
|
|
|
|
// proto.ProvisionerServer implementation. If the provided provisioner is not a
|
|
|
|
// *schema.Provisioner, this will return nil,
|
|
|
|
func NewGRPCProvisionerServerShim(p terraform.ResourceProvisioner) *GRPCProvisionerServer {
|
|
|
|
sp, ok := p.(*schema.Provisioner)
|
|
|
|
if !ok {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return &GRPCProvisionerServer{
|
|
|
|
provisioner: sp,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-08 23:02:24 +02:00
|
|
|
type GRPCProvisionerServer struct {
|
|
|
|
provisioner *schema.Provisioner
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *GRPCProvisionerServer) GetSchema(_ context.Context, req *proto.GetProvisionerSchema_Request) (*proto.GetProvisionerSchema_Response, error) {
|
|
|
|
resp := &proto.GetProvisionerSchema_Response{}
|
|
|
|
|
|
|
|
resp.Provisioner = &proto.Schema{
|
2018-08-15 18:03:39 +02:00
|
|
|
Block: convert.ConfigSchemaToProto(schema.InternalMap(s.provisioner.Schema).CoreConfigSchema()),
|
2018-08-08 23:02:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return resp, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *GRPCProvisionerServer) ValidateProvisionerConfig(_ context.Context, req *proto.ValidateProvisionerConfig_Request) (*proto.ValidateProvisionerConfig_Response, error) {
|
|
|
|
resp := &proto.ValidateProvisionerConfig_Response{}
|
|
|
|
|
|
|
|
cfgSchema := schema.InternalMap(s.provisioner.Schema).CoreConfigSchema()
|
|
|
|
|
|
|
|
configVal, err := msgpack.Unmarshal(req.Config.Msgpack, cfgSchema.ImpliedType())
|
|
|
|
if err != nil {
|
2018-08-15 18:03:39 +02:00
|
|
|
resp.Diagnostics = convert.AppendProtoDiag(resp.Diagnostics, err)
|
2018-08-08 23:02:24 +02:00
|
|
|
return resp, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
config := terraform.NewResourceConfigShimmed(configVal, cfgSchema)
|
|
|
|
|
|
|
|
warns, errs := s.provisioner.Validate(config)
|
2018-08-15 18:03:39 +02:00
|
|
|
resp.Diagnostics = convert.AppendProtoDiag(resp.Diagnostics, convert.WarnsAndErrsToProto(warns, errs))
|
2018-08-08 23:02:24 +02:00
|
|
|
|
|
|
|
return resp, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// stringMapFromValue converts a cty.Value to a map[stirng]string.
|
|
|
|
// This will panic if the val is not a cty.Map(cty.String).
|
|
|
|
func stringMapFromValue(val cty.Value) map[string]string {
|
|
|
|
m := map[string]string{}
|
|
|
|
if val.IsNull() || !val.IsKnown() {
|
|
|
|
return m
|
|
|
|
}
|
|
|
|
|
|
|
|
for it := val.ElementIterator(); it.Next(); {
|
|
|
|
ak, av := it.Element()
|
|
|
|
name := ak.AsString()
|
|
|
|
|
|
|
|
if !av.IsKnown() || av.IsNull() {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2018-08-15 18:03:39 +02:00
|
|
|
av, _ = ctyconvert.Convert(av, cty.String)
|
2018-08-08 23:02:24 +02:00
|
|
|
m[name] = av.AsString()
|
|
|
|
}
|
|
|
|
|
|
|
|
return m
|
|
|
|
}
|
|
|
|
|
|
|
|
// uiOutput implements the terraform.UIOutput interface to adapt the grpc
|
|
|
|
// stream to the legacy Provisioner.Apply method.
|
|
|
|
type uiOutput struct {
|
|
|
|
srv proto.Provisioner_ProvisionResourceServer
|
|
|
|
}
|
|
|
|
|
|
|
|
func (o uiOutput) Output(s string) {
|
|
|
|
err := o.srv.Send(&proto.ProvisionResource_Response{
|
|
|
|
Output: s,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("[ERROR] %s", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *GRPCProvisionerServer) ProvisionResource(req *proto.ProvisionResource_Request, srv proto.Provisioner_ProvisionResourceServer) error {
|
|
|
|
// We send back a diagnostics over the stream if there was a
|
|
|
|
// provisioner-side problem.
|
|
|
|
srvResp := &proto.ProvisionResource_Response{}
|
|
|
|
|
|
|
|
cfgSchema := schema.InternalMap(s.provisioner.Schema).CoreConfigSchema()
|
|
|
|
cfgVal, err := msgpack.Unmarshal(req.Config.Msgpack, cfgSchema.ImpliedType())
|
|
|
|
if err != nil {
|
2018-08-15 18:03:39 +02:00
|
|
|
srvResp.Diagnostics = convert.AppendProtoDiag(srvResp.Diagnostics, err)
|
2018-08-08 23:02:24 +02:00
|
|
|
srv.Send(srvResp)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
resourceConfig := terraform.NewResourceConfigShimmed(cfgVal, cfgSchema)
|
|
|
|
|
|
|
|
connVal, err := msgpack.Unmarshal(req.Connection.Msgpack, cty.Map(cty.String))
|
|
|
|
if err != nil {
|
2018-08-15 18:03:39 +02:00
|
|
|
srvResp.Diagnostics = convert.AppendProtoDiag(srvResp.Diagnostics, err)
|
2018-08-08 23:02:24 +02:00
|
|
|
srv.Send(srvResp)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
conn := stringMapFromValue(connVal)
|
|
|
|
|
|
|
|
instanceState := &terraform.InstanceState{
|
|
|
|
Ephemeral: terraform.EphemeralState{
|
|
|
|
ConnInfo: conn,
|
|
|
|
},
|
2018-10-08 18:25:16 +02:00
|
|
|
Meta: make(map[string]interface{}),
|
2018-08-08 23:02:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
err = s.provisioner.Apply(uiOutput{srv}, instanceState, resourceConfig)
|
|
|
|
if err != nil {
|
2018-08-15 18:03:39 +02:00
|
|
|
srvResp.Diagnostics = convert.AppendProtoDiag(srvResp.Diagnostics, err)
|
2018-08-08 23:02:24 +02:00
|
|
|
srv.Send(srvResp)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *GRPCProvisionerServer) Stop(_ context.Context, req *proto.Stop_Request) (*proto.Stop_Response, error) {
|
|
|
|
resp := &proto.Stop_Response{}
|
|
|
|
|
|
|
|
err := s.provisioner.Stop()
|
|
|
|
if err != nil {
|
|
|
|
resp.Error = err.Error()
|
|
|
|
}
|
|
|
|
|
|
|
|
return resp, nil
|
|
|
|
}
|