2014-07-09 20:13:05 +02:00
|
|
|
package rpc
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/rpc"
|
2014-07-10 01:54:34 +02:00
|
|
|
|
|
|
|
"github.com/hashicorp/terraform/terraform"
|
2014-07-09 20:13:05 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
// ResourceProvisioner is an implementation of terraform.ResourceProvisioner
|
|
|
|
// that communicates over RPC.
|
|
|
|
type ResourceProvisioner struct {
|
2014-10-04 18:54:03 +02:00
|
|
|
Broker *muxBroker
|
2014-07-09 20:13:05 +02:00
|
|
|
Client *rpc.Client
|
|
|
|
Name string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *ResourceProvisioner) Validate(c *terraform.ResourceConfig) ([]string, []error) {
|
|
|
|
var resp ResourceProvisionerValidateResponse
|
|
|
|
args := ResourceProvisionerValidateArgs{
|
|
|
|
Config: c,
|
|
|
|
}
|
|
|
|
|
|
|
|
err := p.Client.Call(p.Name+".Validate", &args, &resp)
|
|
|
|
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()
|
|
|
|
go acceptAndServe(p.Broker, id, "UIOutput", &UIOutputServer{
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
err := p.Client.Call(p.Name+".Apply", args, &resp)
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
type ResourceProvisionerValidateArgs struct {
|
|
|
|
Config *terraform.ResourceConfig
|
|
|
|
}
|
|
|
|
|
|
|
|
type ResourceProvisionerValidateResponse struct {
|
|
|
|
Warnings []string
|
|
|
|
Errors []*BasicError
|
|
|
|
}
|
|
|
|
|
|
|
|
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 {
|
|
|
|
Error *BasicError
|
|
|
|
}
|
|
|
|
|
|
|
|
// ResourceProvisionerServer is a net/rpc compatible structure for serving
|
|
|
|
// a ResourceProvisioner. This should not be used directly.
|
|
|
|
type ResourceProvisionerServer struct {
|
2014-10-04 18:54:03 +02:00
|
|
|
Broker *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{
|
|
|
|
Error: NewBasicError(err),
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
client := rpc.NewClient(conn)
|
|
|
|
defer client.Close()
|
|
|
|
|
|
|
|
output := &UIOutput{
|
|
|
|
Client: client,
|
|
|
|
Name: "UIOutput",
|
|
|
|
}
|
|
|
|
|
|
|
|
err = s.Provisioner.Apply(output, args.State, args.Config)
|
2014-07-09 20:13:05 +02:00
|
|
|
*result = ResourceProvisionerApplyResponse{
|
|
|
|
Error: NewBasicError(err),
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *ResourceProvisionerServer) Validate(
|
|
|
|
args *ResourceProvisionerValidateArgs,
|
|
|
|
reply *ResourceProvisionerValidateResponse) error {
|
|
|
|
warns, errs := s.Provisioner.Validate(args.Config)
|
|
|
|
berrs := make([]*BasicError, len(errs))
|
|
|
|
for i, err := range errs {
|
|
|
|
berrs[i] = NewBasicError(err)
|
|
|
|
}
|
|
|
|
*reply = ResourceProvisionerValidateResponse{
|
|
|
|
Warnings: warns,
|
|
|
|
Errors: berrs,
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|