opentofu/plugin/resource_provisioner.go

146 lines
3.4 KiB
Go
Raw Normal View History

package plugin
2014-07-09 13:13:05 -05:00
import (
"net/rpc"
2014-07-09 18:54:34 -05:00
"github.com/hashicorp/go-plugin"
2014-07-09 18:54:34 -05:00
"github.com/hashicorp/terraform/terraform"
2014-07-09 13:13:05 -05:00
)
// ResourceProvisionerPlugin is the plugin.Plugin implementation.
type ResourceProvisionerPlugin struct {
F func() terraform.ResourceProvisioner
}
func (p *ResourceProvisionerPlugin) Server(b *plugin.MuxBroker) (interface{}, error) {
return &ResourceProvisionerServer{Broker: b, Provisioner: p.F()}, nil
}
func (p *ResourceProvisionerPlugin) Client(
b *plugin.MuxBroker, c *rpc.Client) (interface{}, error) {
return &ResourceProvisioner{Broker: b, Client: c}, nil
}
2014-07-09 13:13:05 -05:00
// ResourceProvisioner is an implementation of terraform.ResourceProvisioner
// that communicates over RPC.
type ResourceProvisioner struct {
Broker *plugin.MuxBroker
2014-07-09 13:13:05 -05:00
Client *rpc.Client
}
func (p *ResourceProvisioner) Validate(c *terraform.ResourceConfig) ([]string, []error) {
var resp ResourceProvisionerValidateResponse
args := ResourceProvisionerValidateArgs{
Config: c,
}
err := p.Client.Call("Plugin.Validate", &args, &resp)
2014-07-09 13:13:05 -05: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 11:54:03 -05:00
output terraform.UIOutput,
2014-09-16 18:48:33 -05:00
s *terraform.InstanceState,
2014-07-22 12:36:37 -05:00
c *terraform.ResourceConfig) error {
2014-10-04 11:54:03 -05:00
id := p.Broker.NextId()
go p.Broker.AcceptAndServe(id, &UIOutputServer{
2014-10-04 11:54:03 -05:00
UIOutput: output,
})
2014-07-09 13:13:05 -05:00
var resp ResourceProvisionerApplyResponse
args := &ResourceProvisionerApplyArgs{
2014-10-04 11:54:03 -05:00
OutputId: id,
State: s,
Config: c,
2014-07-09 13:13:05 -05:00
}
err := p.Client.Call("Plugin.Apply", args, &resp)
2014-07-09 13:13:05 -05:00
if err != nil {
2014-07-22 12:36:37 -05:00
return err
2014-07-09 13:13:05 -05:00
}
if resp.Error != nil {
err = resp.Error
}
2014-07-22 12:36:37 -05:00
return err
2014-07-09 13:13:05 -05:00
}
func (p *ResourceProvisioner) Close() error {
return p.Client.Close()
}
2014-07-09 13:13:05 -05:00
type ResourceProvisionerValidateArgs struct {
Config *terraform.ResourceConfig
}
type ResourceProvisionerValidateResponse struct {
Warnings []string
Errors []*plugin.BasicError
2014-07-09 13:13:05 -05:00
}
type ResourceProvisionerApplyArgs struct {
2014-10-04 11:54:03 -05:00
OutputId uint32
State *terraform.InstanceState
Config *terraform.ResourceConfig
2014-07-09 13:13:05 -05:00
}
type ResourceProvisionerApplyResponse struct {
Error *plugin.BasicError
2014-07-09 13:13:05 -05:00
}
// ResourceProvisionerServer is a net/rpc compatible structure for serving
// a ResourceProvisioner. This should not be used directly.
type ResourceProvisionerServer struct {
Broker *plugin.MuxBroker
2014-07-09 13:13:05 -05:00
Provisioner terraform.ResourceProvisioner
}
func (s *ResourceProvisionerServer) Apply(
args *ResourceProvisionerApplyArgs,
result *ResourceProvisionerApplyResponse) error {
2014-10-04 11:54:03 -05:00
conn, err := s.Broker.Dial(args.OutputId)
if err != nil {
*result = ResourceProvisionerApplyResponse{
Error: plugin.NewBasicError(err),
2014-10-04 11:54:03 -05:00
}
return nil
}
client := rpc.NewClient(conn)
defer client.Close()
output := &UIOutput{Client: client}
2014-10-04 11:54:03 -05:00
err = s.Provisioner.Apply(output, args.State, args.Config)
2014-07-09 13:13:05 -05:00
*result = ResourceProvisionerApplyResponse{
Error: plugin.NewBasicError(err),
2014-07-09 13:13:05 -05:00
}
return nil
}
func (s *ResourceProvisionerServer) Validate(
args *ResourceProvisionerValidateArgs,
reply *ResourceProvisionerValidateResponse) error {
warns, errs := s.Provisioner.Validate(args.Config)
berrs := make([]*plugin.BasicError, len(errs))
2014-07-09 13:13:05 -05:00
for i, err := range errs {
berrs[i] = plugin.NewBasicError(err)
2014-07-09 13:13:05 -05:00
}
*reply = ResourceProvisionerValidateResponse{
Warnings: warns,
Errors: berrs,
}
return nil
}