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"
"github.com/hashicorp/terraform/communicator/remote"
"github.com/hashicorp/terraform/communicator/shared"
"github.com/hashicorp/terraform/communicator/ssh"
"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
// used for any of the provisioners
type Communicator interface {
// Connect is used to setup the connection
Connect(terraform.UIOutput) error
Connect(provisioners.UIOutput) error
// Disconnect is used to terminate the connection
Disconnect() error
@ -43,13 +45,23 @@ type Communicator interface {
}
// New returns a configured Communicator or an error if the connection type is not supported
func New(s *terraform.InstanceState) (Communicator, error) {
connType := s.Ephemeral.ConnInfo["type"]
func New(v cty.Value) (Communicator, error) {
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 {
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":
return winrm.New(s)
return winrm.New(v)
default:
return nil, fmt.Errorf("connection type '%s' not supported", connType)
}

View File

@ -8,7 +8,7 @@ import (
"time"
"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.
@ -24,7 +24,7 @@ type MockCommunicator struct {
}
// Connect implementation of communicator.Communicator interface
func (c *MockCommunicator) Connect(o terraform.UIOutput) error {
func (c *MockCommunicator) Connect(o provisioners.UIOutput) error {
return nil
}

View File

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