2016-01-25 03:10:52 +01:00
|
|
|
package plugin
|
2014-07-09 20:13:05 +02:00
|
|
|
|
|
|
|
import (
|
|
|
|
"net/rpc"
|
2014-07-10 01:54:34 +02:00
|
|
|
|
2016-01-25 03:10:52 +01:00
|
|
|
"github.com/hashicorp/go-plugin"
|
2018-08-08 22:57:31 +02:00
|
|
|
"github.com/hashicorp/terraform/configs/configschema"
|
2020-11-18 16:05:54 +01:00
|
|
|
"github.com/hashicorp/terraform/internal/legacy/terraform"
|
2014-07-09 20:13:05 +02:00
|
|
|
)
|
|
|
|
|
2016-01-25 03:10:52 +01:00
|
|
|
// ResourceProvisionerPlugin is the plugin.Plugin implementation.
|
|
|
|
type ResourceProvisionerPlugin struct {
|
2018-08-14 22:55:20 +02:00
|
|
|
ResourceProvisioner func() terraform.ResourceProvisioner
|
2016-01-25 03:10:52 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (p *ResourceProvisionerPlugin) Server(b *plugin.MuxBroker) (interface{}, error) {
|
2018-08-14 22:55:20 +02:00
|
|
|
return &ResourceProvisionerServer{
|
|
|
|
Broker: b,
|
|
|
|
Provisioner: p.ResourceProvisioner(),
|
|
|
|
}, nil
|
2016-01-25 03:10:52 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (p *ResourceProvisionerPlugin) Client(
|
|
|
|
b *plugin.MuxBroker, c *rpc.Client) (interface{}, error) {
|
|
|
|
return &ResourceProvisioner{Broker: b, Client: c}, nil
|
|
|
|
}
|
|
|
|
|
2014-07-09 20:13:05 +02:00
|
|
|
// ResourceProvisioner is an implementation of terraform.ResourceProvisioner
|
|
|
|
// that communicates over RPC.
|
|
|
|
type ResourceProvisioner struct {
|
2016-01-25 03:10:52 +01:00
|
|
|
Broker *plugin.MuxBroker
|
2014-07-09 20:13:05 +02:00
|
|
|
Client *rpc.Client
|
|
|
|
}
|
|
|
|
|
2018-08-08 22:57:31 +02:00
|
|
|
func (p *ResourceProvisioner) GetConfigSchema() (*configschema.Block, error) {
|
|
|
|
panic("not implemented")
|
|
|
|
}
|
|
|
|
|
2014-07-09 20:13:05 +02:00
|
|
|
func (p *ResourceProvisioner) Validate(c *terraform.ResourceConfig) ([]string, []error) {
|
|
|
|
var resp ResourceProvisionerValidateResponse
|
|
|
|
args := ResourceProvisionerValidateArgs{
|
|
|
|
Config: c,
|
|
|
|
}
|
|
|
|
|
2016-01-25 03:10:52 +01:00
|
|
|
err := p.Client.Call("Plugin.Validate", &args, &resp)
|
2014-07-09 20:13:05 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, []error{err}
|
|
|
|
}
|
|
|
|
|
|
|
|
var errs []error
|
|
|
|
if len(resp.Errors) > 0 {
|
|
|
|
errs = make([]error, len(resp.Errors))
|
|
|
|
for i, err := range resp.Errors {
|
|
|
|
errs[i] = err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return resp.Warnings, errs
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *ResourceProvisioner) Apply(
|
2014-10-04 18:54:03 +02:00
|
|
|
output terraform.UIOutput,
|
2014-09-17 01:48:33 +02:00
|
|
|
s *terraform.InstanceState,
|
2014-07-22 19:36:37 +02:00
|
|
|
c *terraform.ResourceConfig) error {
|
2014-10-04 18:54:03 +02:00
|
|
|
id := p.Broker.NextId()
|
2016-01-25 03:10:52 +01:00
|
|
|
go p.Broker.AcceptAndServe(id, &UIOutputServer{
|
2014-10-04 18:54:03 +02:00
|
|
|
UIOutput: output,
|
|
|
|
})
|
|
|
|
|
2014-07-09 20:13:05 +02:00
|
|
|
var resp ResourceProvisionerApplyResponse
|
|
|
|
args := &ResourceProvisionerApplyArgs{
|
2014-10-04 18:54:03 +02:00
|
|
|
OutputId: id,
|
|
|
|
State: s,
|
|
|
|
Config: c,
|
2014-07-09 20:13:05 +02:00
|
|
|
}
|
|
|
|
|
2016-01-25 03:10:52 +01:00
|
|
|
err := p.Client.Call("Plugin.Apply", args, &resp)
|
2014-07-09 20:13:05 +02:00
|
|
|
if err != nil {
|
2014-07-22 19:36:37 +02:00
|
|
|
return err
|
2014-07-09 20:13:05 +02:00
|
|
|
}
|
|
|
|
if resp.Error != nil {
|
|
|
|
err = resp.Error
|
|
|
|
}
|
|
|
|
|
2014-07-22 19:36:37 +02:00
|
|
|
return err
|
2014-07-09 20:13:05 +02:00
|
|
|
}
|
|
|
|
|
2016-12-22 20:38:26 +01:00
|
|
|
func (p *ResourceProvisioner) Stop() error {
|
|
|
|
var resp ResourceProvisionerStopResponse
|
|
|
|
err := p.Client.Call("Plugin.Stop", new(interface{}), &resp)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if resp.Error != nil {
|
|
|
|
err = resp.Error
|
|
|
|
}
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-06-19 21:52:50 +02:00
|
|
|
func (p *ResourceProvisioner) Close() error {
|
|
|
|
return p.Client.Close()
|
|
|
|
}
|
|
|
|
|
2014-07-09 20:13:05 +02:00
|
|
|
type ResourceProvisionerValidateArgs struct {
|
|
|
|
Config *terraform.ResourceConfig
|
|
|
|
}
|
|
|
|
|
|
|
|
type ResourceProvisionerValidateResponse struct {
|
|
|
|
Warnings []string
|
2016-01-25 03:10:52 +01:00
|
|
|
Errors []*plugin.BasicError
|
2014-07-09 20:13:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
type ResourceProvisionerApplyArgs struct {
|
2014-10-04 18:54:03 +02:00
|
|
|
OutputId uint32
|
|
|
|
State *terraform.InstanceState
|
|
|
|
Config *terraform.ResourceConfig
|
2014-07-09 20:13:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
type ResourceProvisionerApplyResponse struct {
|
2016-01-25 03:10:52 +01:00
|
|
|
Error *plugin.BasicError
|
2014-07-09 20:13:05 +02:00
|
|
|
}
|
|
|
|
|
2016-12-22 20:38:26 +01:00
|
|
|
type ResourceProvisionerStopResponse struct {
|
|
|
|
Error *plugin.BasicError
|
|
|
|
}
|
|
|
|
|
2014-07-09 20:13:05 +02:00
|
|
|
// ResourceProvisionerServer is a net/rpc compatible structure for serving
|
|
|
|
// a ResourceProvisioner. This should not be used directly.
|
|
|
|
type ResourceProvisionerServer struct {
|
2016-01-25 03:10:52 +01:00
|
|
|
Broker *plugin.MuxBroker
|
2014-07-09 20:13:05 +02:00
|
|
|
Provisioner terraform.ResourceProvisioner
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *ResourceProvisionerServer) Apply(
|
|
|
|
args *ResourceProvisionerApplyArgs,
|
|
|
|
result *ResourceProvisionerApplyResponse) error {
|
2014-10-04 18:54:03 +02:00
|
|
|
conn, err := s.Broker.Dial(args.OutputId)
|
|
|
|
if err != nil {
|
|
|
|
*result = ResourceProvisionerApplyResponse{
|
2016-01-25 03:10:52 +01:00
|
|
|
Error: plugin.NewBasicError(err),
|
2014-10-04 18:54:03 +02:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
client := rpc.NewClient(conn)
|
|
|
|
defer client.Close()
|
|
|
|
|
2016-01-25 03:10:52 +01:00
|
|
|
output := &UIOutput{Client: client}
|
2014-10-04 18:54:03 +02:00
|
|
|
|
|
|
|
err = s.Provisioner.Apply(output, args.State, args.Config)
|
2014-07-09 20:13:05 +02:00
|
|
|
*result = ResourceProvisionerApplyResponse{
|
2016-01-25 03:10:52 +01:00
|
|
|
Error: plugin.NewBasicError(err),
|
2014-07-09 20:13:05 +02:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *ResourceProvisionerServer) Validate(
|
|
|
|
args *ResourceProvisionerValidateArgs,
|
|
|
|
reply *ResourceProvisionerValidateResponse) error {
|
|
|
|
warns, errs := s.Provisioner.Validate(args.Config)
|
2016-01-25 03:10:52 +01:00
|
|
|
berrs := make([]*plugin.BasicError, len(errs))
|
2014-07-09 20:13:05 +02:00
|
|
|
for i, err := range errs {
|
2016-01-25 03:10:52 +01:00
|
|
|
berrs[i] = plugin.NewBasicError(err)
|
2014-07-09 20:13:05 +02:00
|
|
|
}
|
|
|
|
*reply = ResourceProvisionerValidateResponse{
|
|
|
|
Warnings: warns,
|
|
|
|
Errors: berrs,
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2016-12-22 20:38:26 +01:00
|
|
|
|
|
|
|
func (s *ResourceProvisionerServer) Stop(
|
|
|
|
_ interface{},
|
|
|
|
reply *ResourceProvisionerStopResponse) error {
|
|
|
|
err := s.Provisioner.Stop()
|
|
|
|
*reply = ResourceProvisionerStopResponse{
|
|
|
|
Error: plugin.NewBasicError(err),
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|