From 47a682141a3d794f98035f4ef9fb0fe2014b764e Mon Sep 17 00:00:00 2001 From: James Bardin Date: Wed, 24 Feb 2021 13:25:58 -0500 Subject: [PATCH] remote provisioners require a connection config --- builtin/provisioners/file/resource_provisioner.go | 5 +++++ .../provisioners/file/resource_provisioner_test.go | 14 ++++++++++++++ .../remote-exec/resource_provisioner.go | 5 +++++ .../remote-exec/resource_provisioner_test.go | 13 +++++++++++++ 4 files changed, 37 insertions(+) diff --git a/builtin/provisioners/file/resource_provisioner.go b/builtin/provisioners/file/resource_provisioner.go index b96c03121..29ed85875 100644 --- a/builtin/provisioners/file/resource_provisioner.go +++ b/builtin/provisioners/file/resource_provisioner.go @@ -74,6 +74,11 @@ func (p *provisioner) ValidateProvisionerConfig(req provisioners.ValidateProvisi } func (p *provisioner) ProvisionResource(req provisioners.ProvisionResourceRequest) (resp provisioners.ProvisionResourceResponse) { + if req.Connection.IsNull() { + resp.Diagnostics = resp.Diagnostics.Append(errors.New("missing connection configuration for provisioner")) + return resp + } + comm, err := communicator.New(req.Connection) if err != nil { resp.Diagnostics = resp.Diagnostics.Append(err) diff --git a/builtin/provisioners/file/resource_provisioner_test.go b/builtin/provisioners/file/resource_provisioner_test.go index 080c76a74..52b99cb9d 100644 --- a/builtin/provisioners/file/resource_provisioner_test.go +++ b/builtin/provisioners/file/resource_provisioner_test.go @@ -1,6 +1,7 @@ package file import ( + "strings" "testing" "github.com/hashicorp/terraform/provisioners" @@ -102,3 +103,16 @@ func TestResourceProvisioner_StopClose(t *testing.T) { p.Stop() p.Close() } + +func TestResourceProvisioner_connectionRequired(t *testing.T) { + p := New() + resp := p.ProvisionResource(provisioners.ProvisionResourceRequest{}) + if !resp.Diagnostics.HasErrors() { + t.Fatal("expected error") + } + + got := resp.Diagnostics.Err().Error() + if !strings.Contains(got, "missing connection") { + t.Fatalf("expected 'missing connection' error: got %q", got) + } +} diff --git a/builtin/provisioners/remote-exec/resource_provisioner.go b/builtin/provisioners/remote-exec/resource_provisioner.go index 4311ba164..b3d42ba46 100644 --- a/builtin/provisioners/remote-exec/resource_provisioner.go +++ b/builtin/provisioners/remote-exec/resource_provisioner.go @@ -85,6 +85,11 @@ func (p *provisioner) ValidateProvisionerConfig(req provisioners.ValidateProvisi } func (p *provisioner) ProvisionResource(req provisioners.ProvisionResourceRequest) (resp provisioners.ProvisionResourceResponse) { + if req.Connection.IsNull() { + resp.Diagnostics = resp.Diagnostics.Append(errors.New("missing connection configuration for provisioner")) + return resp + } + comm, err := communicator.New(req.Connection) if err != nil { resp.Diagnostics = resp.Diagnostics.Append(err) diff --git a/builtin/provisioners/remote-exec/resource_provisioner_test.go b/builtin/provisioners/remote-exec/resource_provisioner_test.go index 16c115261..56bf23c81 100644 --- a/builtin/provisioners/remote-exec/resource_provisioner_test.go +++ b/builtin/provisioners/remote-exec/resource_provisioner_test.go @@ -261,3 +261,16 @@ func TestResourceProvisioner_StopClose(t *testing.T) { p.Stop() p.Close() } + +func TestResourceProvisioner_connectionRequired(t *testing.T) { + p := New() + resp := p.ProvisionResource(provisioners.ProvisionResourceRequest{}) + if !resp.Diagnostics.HasErrors() { + t.Fatal("expected error") + } + + got := resp.Diagnostics.Err().Error() + if !strings.Contains(got, "missing connection") { + t.Fatalf("expected 'missing connection' error: got %q", got) + } +}