udpate communicator package to use new types

This commit is contained in:
James Bardin 2020-11-25 13:27:10 -05:00
parent 22a5641c79
commit d130add682
3 changed files with 30 additions and 21 deletions

View File

@ -9,16 +9,18 @@ import (
"time" "time"
"github.com/hashicorp/terraform/communicator/remote" "github.com/hashicorp/terraform/communicator/remote"
"github.com/hashicorp/terraform/communicator/shared"
"github.com/hashicorp/terraform/communicator/ssh" "github.com/hashicorp/terraform/communicator/ssh"
"github.com/hashicorp/terraform/communicator/winrm" "github.com/hashicorp/terraform/communicator/winrm"
"github.com/hashicorp/terraform/internal/legacy/terraform" "github.com/hashicorp/terraform/provisioners"
"github.com/zclconf/go-cty/cty"
) )
// Communicator is an interface that must be implemented by all communicators // Communicator is an interface that must be implemented by all communicators
// used for any of the provisioners // used for any of the provisioners
type Communicator interface { type Communicator interface {
// Connect is used to setup the connection // Connect is used to setup the connection
Connect(terraform.UIOutput) error Connect(provisioners.UIOutput) error
// Disconnect is used to terminate the connection // Disconnect is used to terminate the connection
Disconnect() error Disconnect() error
@ -43,13 +45,23 @@ type Communicator interface {
} }
// New returns a configured Communicator or an error if the connection type is not supported // New returns a configured Communicator or an error if the connection type is not supported
func New(s *terraform.InstanceState) (Communicator, error) { func New(v cty.Value) (Communicator, error) {
connType := s.Ephemeral.ConnInfo["type"] v, err := shared.ConnectionBlockSupersetSchema.CoerceValue(v)
if err != nil {
return nil, err
}
typeVal := v.GetAttr("type")
connType := ""
if !typeVal.IsNull() {
connType = typeVal.AsString()
}
switch connType { switch connType {
case "ssh", "": // The default connection type is ssh, so if connType is empty use ssh case "ssh", "": // The default connection type is ssh, so if connType is empty use ssh
return ssh.New(s) return ssh.New(v)
case "winrm": case "winrm":
return winrm.New(s) return winrm.New(v)
default: default:
return nil, fmt.Errorf("connection type '%s' not supported", connType) return nil, fmt.Errorf("connection type '%s' not supported", connType)
} }

View File

@ -8,7 +8,7 @@ import (
"time" "time"
"github.com/hashicorp/terraform/communicator/remote" "github.com/hashicorp/terraform/communicator/remote"
"github.com/hashicorp/terraform/internal/legacy/terraform" "github.com/hashicorp/terraform/provisioners"
) )
// MockCommunicator is an implementation of Communicator that can be used for tests. // MockCommunicator is an implementation of Communicator that can be used for tests.
@ -24,7 +24,7 @@ type MockCommunicator struct {
} }
// Connect implementation of communicator.Communicator interface // Connect implementation of communicator.Communicator interface
func (c *MockCommunicator) Connect(o terraform.UIOutput) error { func (c *MockCommunicator) Connect(o provisioners.UIOutput) error {
return nil return nil
} }

View File

@ -8,29 +8,26 @@ import (
"testing" "testing"
"time" "time"
"github.com/hashicorp/terraform/internal/legacy/terraform" "github.com/zclconf/go-cty/cty"
) )
func TestCommunicator_new(t *testing.T) { func TestCommunicator_new(t *testing.T) {
r := &terraform.InstanceState{ cfg := map[string]cty.Value{
Ephemeral: terraform.EphemeralState{ "type": cty.StringVal("telnet"),
ConnInfo: map[string]string{ "host": cty.StringVal("127.0.0.1"),
"type": "telnet",
"host": "127.0.0.1",
},
},
} }
if _, err := New(r); err == nil {
if _, err := New(cty.ObjectVal(cfg)); err == nil {
t.Fatalf("expected error with telnet") t.Fatalf("expected error with telnet")
} }
r.Ephemeral.ConnInfo["type"] = "ssh" cfg["type"] = cty.StringVal("ssh")
if _, err := New(r); err != nil { if _, err := New(cty.ObjectVal(cfg)); err != nil {
t.Fatalf("err: %v", err) t.Fatalf("err: %v", err)
} }
r.Ephemeral.ConnInfo["type"] = "winrm" cfg["type"] = cty.StringVal("winrm")
if _, err := New(r); err != nil { if _, err := New(cty.ObjectVal(cfg)); err != nil {
t.Fatalf("err: %v", err) t.Fatalf("err: %v", err)
} }
} }