diff --git a/plugin/plugin_test.go b/plugin/plugin_test.go index 038bc3917..7e5dcfcb1 100644 --- a/plugin/plugin_test.go +++ b/plugin/plugin_test.go @@ -60,6 +60,12 @@ func TestHelperProcess(*testing.T) { log.Printf("[ERR] %s", err) os.Exit(1) } + case "resource-provisioner": + err := Serve(new(terraform.MockResourceProvisioner)) + if err != nil { + log.Printf("[ERR] %s", err) + os.Exit(1) + } case "invalid-rpc-address": fmt.Println("lolinvalid") case "mock": diff --git a/plugin/resource_provisioner.go b/plugin/resource_provisioner.go new file mode 100644 index 000000000..6d8fd39db --- /dev/null +++ b/plugin/resource_provisioner.go @@ -0,0 +1,35 @@ +package plugin + +import ( + "os/exec" + + tfrpc "github.com/hashicorp/terraform/rpc" + "github.com/hashicorp/terraform/terraform" +) + +// ResourceProvisionerFactory returns a Terraform ResourceProvisionerFactory +// that executes a plugin and connects to it. +func ResourceProvisionerFactory(cmd *exec.Cmd) terraform.ResourceProvisionerFactory { + return func() (terraform.ResourceProvisioner, error) { + config := &ClientConfig{ + Cmd: cmd, + Managed: true, + } + + client := NewClient(config) + rpcClient, err := client.Client() + if err != nil { + return nil, err + } + + rpcName, err := client.Service() + if err != nil { + return nil, err + } + + return &tfrpc.ResourceProvisioner{ + Client: rpcClient, + Name: rpcName, + }, nil + } +} diff --git a/plugin/resource_provisioner_test.go b/plugin/resource_provisioner_test.go new file mode 100644 index 000000000..2ca37c7d9 --- /dev/null +++ b/plugin/resource_provisioner_test.go @@ -0,0 +1,23 @@ +package plugin + +import ( + "testing" +) + +func TestResourceProvisioner(t *testing.T) { + c := NewClient(&ClientConfig{Cmd: helperProcess("resource-provisioner")}) + defer c.Kill() + + _, err := c.Client() + if err != nil { + t.Fatalf("should not have error: %s", err) + } + + service, err := c.Service() + if err != nil { + t.Fatalf("err: %s", err) + } + if service == "" { + t.Fatal("service should not be blank") + } +}