From 2e894c4ef74381a81dc292c0395564f6e6ed36d3 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Thu, 22 Dec 2016 11:38:26 -0800 Subject: [PATCH] plugin: add ResourceProvisioner.Stop API --- plugin/resource_provisioner.go | 28 +++++++++++++++ plugin/resource_provisioner_test.go | 55 +++++++++++++++++++++++++++++ 2 files changed, 83 insertions(+) diff --git a/plugin/resource_provisioner.go b/plugin/resource_provisioner.go index 982309580..8fce9d8ae 100644 --- a/plugin/resource_provisioner.go +++ b/plugin/resource_provisioner.go @@ -77,6 +77,19 @@ func (p *ResourceProvisioner) Apply( return err } +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 +} + func (p *ResourceProvisioner) Close() error { return p.Client.Close() } @@ -100,6 +113,10 @@ type ResourceProvisionerApplyResponse struct { Error *plugin.BasicError } +type ResourceProvisionerStopResponse struct { + Error *plugin.BasicError +} + // ResourceProvisionerServer is a net/rpc compatible structure for serving // a ResourceProvisioner. This should not be used directly. type ResourceProvisionerServer struct { @@ -143,3 +160,14 @@ func (s *ResourceProvisionerServer) Validate( } return nil } + +func (s *ResourceProvisionerServer) Stop( + _ interface{}, + reply *ResourceProvisionerStopResponse) error { + err := s.Provisioner.Stop() + *reply = ResourceProvisionerStopResponse{ + Error: plugin.NewBasicError(err), + } + + return nil +} diff --git a/plugin/resource_provisioner_test.go b/plugin/resource_provisioner_test.go index 073c8d2b7..70ae1ee74 100644 --- a/plugin/resource_provisioner_test.go +++ b/plugin/resource_provisioner_test.go @@ -14,6 +14,61 @@ func TestResourceProvisioner_impl(t *testing.T) { var _ terraform.ResourceProvisioner = new(ResourceProvisioner) } +func TestResourceProvisioner_stop(t *testing.T) { + // Create a mock provider + p := new(terraform.MockResourceProvisioner) + client, _ := plugin.TestPluginRPCConn(t, pluginMap(&ServeOpts{ + ProvisionerFunc: testProvisionerFixed(p), + })) + defer client.Close() + + // Request the provider + raw, err := client.Dispense(ProvisionerPluginName) + if err != nil { + t.Fatalf("err: %s", err) + } + provider := raw.(terraform.ResourceProvisioner) + + // Stop + e := provider.Stop() + if !p.StopCalled { + t.Fatal("stop should be called") + } + if e != nil { + t.Fatalf("bad: %#v", e) + } +} + +func TestResourceProvisioner_stopErrors(t *testing.T) { + p := new(terraform.MockResourceProvisioner) + p.StopReturnError = errors.New("foo") + + // Create a mock provider + client, _ := plugin.TestPluginRPCConn(t, pluginMap(&ServeOpts{ + ProvisionerFunc: testProvisionerFixed(p), + })) + defer client.Close() + + // Request the provider + raw, err := client.Dispense(ProvisionerPluginName) + if err != nil { + t.Fatalf("err: %s", err) + } + provider := raw.(terraform.ResourceProvisioner) + + // Stop + e := provider.Stop() + if !p.StopCalled { + t.Fatal("stop should be called") + } + if e == nil { + t.Fatal("should have error") + } + if e.Error() != "foo" { + t.Fatalf("bad: %s", e) + } +} + func TestResourceProvisioner_apply(t *testing.T) { // Create a mock provider p := new(terraform.MockResourceProvisioner)