rpc: implement provisioner update

This commit is contained in:
Mitchell Hashimoto 2014-10-04 09:54:03 -07:00
parent 8b129babe5
commit 02d01141ca
5 changed files with 42 additions and 11 deletions

View File

@ -101,6 +101,7 @@ func (c *Client) ResourceProvisioner() (terraform.ResourceProvisioner, error) {
}
return &ResourceProvisioner{
Broker: c.broker,
Client: rpc.NewClient(conn),
Name: "ResourceProvisioner",
}, nil

View File

@ -60,9 +60,10 @@ func TestClient_ResourceProvisioner(t *testing.T) {
}
// Apply
output := &terraform.MockUIOutput{}
state := &terraform.InstanceState{}
conf := &terraform.ResourceConfig{}
err = provisioner.Apply(state, conf)
err = provisioner.Apply(output, state, conf)
if !p.ApplyCalled {
t.Fatal("apply should be called")
}

View File

@ -9,6 +9,7 @@ import (
// ResourceProvisioner is an implementation of terraform.ResourceProvisioner
// that communicates over RPC.
type ResourceProvisioner struct {
Broker *muxBroker
Client *rpc.Client
Name string
}
@ -36,10 +37,17 @@ func (p *ResourceProvisioner) Validate(c *terraform.ResourceConfig) ([]string, [
}
func (p *ResourceProvisioner) Apply(
output terraform.UIOutput,
s *terraform.InstanceState,
c *terraform.ResourceConfig) error {
id := p.Broker.NextId()
go acceptAndServe(p.Broker, id, "UIOutput", &UIOutputServer{
UIOutput: output,
})
var resp ResourceProvisionerApplyResponse
args := &ResourceProvisionerApplyArgs{
OutputId: id,
State: s,
Config: c,
}
@ -65,6 +73,7 @@ type ResourceProvisionerValidateResponse struct {
}
type ResourceProvisionerApplyArgs struct {
OutputId uint32
State *terraform.InstanceState
Config *terraform.ResourceConfig
}
@ -76,13 +85,29 @@ type ResourceProvisionerApplyResponse struct {
// ResourceProvisionerServer is a net/rpc compatible structure for serving
// a ResourceProvisioner. This should not be used directly.
type ResourceProvisionerServer struct {
Broker *muxBroker
Provisioner terraform.ResourceProvisioner
}
func (s *ResourceProvisionerServer) Apply(
args *ResourceProvisionerApplyArgs,
result *ResourceProvisionerApplyResponse) error {
err := s.Provisioner.Apply(args.State, args.Config)
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)
*result = ResourceProvisionerApplyResponse{
Error: NewBasicError(err),
}

View File

@ -13,18 +13,21 @@ func TestResourceProvisioner_impl(t *testing.T) {
}
func TestResourceProvisioner_apply(t *testing.T) {
p := new(terraform.MockResourceProvisioner)
client, server := testClientServer(t)
name, err := Register(server, p)
client, server := testNewClientServer(t)
defer client.Close()
p := server.ProvisionerFunc().(*terraform.MockResourceProvisioner)
provisioner, err := client.ResourceProvisioner()
if err != nil {
t.Fatalf("err: %s", err)
}
provisioner := &ResourceProvisioner{Client: client, Name: name}
// Apply
output := &terraform.MockUIOutput{}
state := &terraform.InstanceState{}
conf := &terraform.ResourceConfig{}
err = provisioner.Apply(state, conf)
err = provisioner.Apply(output, state, conf)
if !p.ApplyCalled {
t.Fatal("apply should be called")
}

View File

@ -118,6 +118,7 @@ func (d *dispenseServer) ResourceProvisioner(
}
serve(conn, "ResourceProvisioner", &ResourceProvisionerServer{
Broker: d.broker,
Provisioner: d.ProvisionerFunc(),
})
}()