terraform: Remove ResourceConnectionInfo, use raw map

This commit is contained in:
Armon Dadgar 2014-07-15 11:30:21 -07:00
parent de8ee65b2b
commit 104b28e19e
8 changed files with 55 additions and 59 deletions

View File

@ -89,8 +89,8 @@ func resource_aws_instance_create(
instance = instanceRaw.(*ec2.Instance) instance = instanceRaw.(*ec2.Instance)
// Initialize the connection info // Initialize the connection info
rs.ConnInfo.Raw["type"] = "ssh" rs.ConnInfo["type"] = "ssh"
rs.ConnInfo.Raw["host"] = instance.PublicIpAddress rs.ConnInfo["host"] = instance.PublicIpAddress
// Set our attributes // Set our attributes
rs, err = resource_aws_instance_update_state(rs, instance) rs, err = resource_aws_instance_update_state(rs, instance)

View File

@ -102,9 +102,8 @@ func (p *ResourceProvisioner) Validate(c *terraform.ResourceConfig) (ws []string
// verifySSH is used to verify the ConnInfo is usable by remote-exec // verifySSH is used to verify the ConnInfo is usable by remote-exec
func (p *ResourceProvisioner) verifySSH(s *terraform.ResourceState) error { func (p *ResourceProvisioner) verifySSH(s *terraform.ResourceState) error {
connType := s.ConnInfo.Raw["type"] connType := s.ConnInfo["type"]
switch connType { switch connType {
case nil:
case "": case "":
case "ssh": case "ssh":
default: default:
@ -125,7 +124,7 @@ func (p *ResourceProvisioner) sshConfig(s *terraform.ResourceState) (*SSHConfig,
if err != nil { if err != nil {
return nil, err return nil, err
} }
if err := dec.Decode(s.ConnInfo.Raw); err != nil { if err := dec.Decode(s.ConnInfo); err != nil {
return nil, err return nil, err
} }
if sshConf.User == "" { if sshConf.User == "" {

View File

@ -44,16 +44,14 @@ func TestResourceProvider_Validate_bad(t *testing.T) {
func TestResourceProvider_verifySSH(t *testing.T) { func TestResourceProvider_verifySSH(t *testing.T) {
p := new(ResourceProvisioner) p := new(ResourceProvisioner)
r := &terraform.ResourceState{ r := &terraform.ResourceState{
ConnInfo: &terraform.ResourceConnectionInfo{ ConnInfo: map[string]string{
Raw: map[string]interface{}{ "type": "telnet",
"type": "telnet",
},
}, },
} }
if err := p.verifySSH(r); err == nil { if err := p.verifySSH(r); err == nil {
t.Fatalf("expected error with telnet") t.Fatalf("expected error with telnet")
} }
r.ConnInfo.Raw["type"] = "ssh" r.ConnInfo["type"] = "ssh"
if err := p.verifySSH(r); err != nil { if err := p.verifySSH(r); err != nil {
t.Fatalf("err: %v", err) t.Fatalf("err: %v", err)
} }
@ -62,16 +60,14 @@ func TestResourceProvider_verifySSH(t *testing.T) {
func TestResourceProvider_sshConfig(t *testing.T) { func TestResourceProvider_sshConfig(t *testing.T) {
p := new(ResourceProvisioner) p := new(ResourceProvisioner)
r := &terraform.ResourceState{ r := &terraform.ResourceState{
ConnInfo: &terraform.ResourceConnectionInfo{ ConnInfo: map[string]string{
Raw: map[string]interface{}{ "type": "ssh",
"type": "ssh", "user": "root",
"user": "root", "password": "supersecret",
"password": "supersecret", "key_file": "/my/key/file.pem",
"key_file": "/my/key/file.pem", "host": "127.0.0.1",
"host": "127.0.0.1", "port": "22",
"port": "22", "timeout": "30s",
"timeout": "30s",
},
}, },
} }

View File

@ -383,8 +383,9 @@ func TestApply_state(t *testing.T) {
originalState := &terraform.State{ originalState := &terraform.State{
Resources: map[string]*terraform.ResourceState{ Resources: map[string]*terraform.ResourceState{
"test_instance.foo": &terraform.ResourceState{ "test_instance.foo": &terraform.ResourceState{
ID: "bar", ID: "bar",
Type: "test_instance", Type: "test_instance",
ConnInfo: make(map[string]string),
}, },
}, },
} }

View File

@ -3,6 +3,7 @@ package terraform
import ( import (
"fmt" "fmt"
"log" "log"
"strconv"
"strings" "strings"
"sync" "sync"
"sync/atomic" "sync/atomic"
@ -469,10 +470,7 @@ func (c *Context) applyWalkFn() depgraph.WalkFunc {
// If we do not have any connection info, initialize // If we do not have any connection info, initialize
if r.State.ConnInfo == nil { if r.State.ConnInfo == nil {
r.State.ConnInfo = &ResourceConnectionInfo{} r.State.ConnInfo = make(map[string]string)
}
if r.State.ConnInfo.Raw == nil {
r.State.ConnInfo.Raw = make(map[string]interface{})
} }
// Remove any output values from the diff // Remove any output values from the diff
@ -584,16 +582,33 @@ func (c *Context) applyProvisioners(r *Resource, rs *ResourceState) (*ResourceSt
} }
// Merge the connection information // Merge the connection information
overlay := make(map[string]interface{}) overlay := make(map[string]string)
if origConnInfo != nil { if origConnInfo != nil {
for k, v := range origConnInfo.Raw { for k, v := range origConnInfo {
overlay[k] = v overlay[k] = v
} }
} }
for k, v := range connInfo.Config { for k, v := range connInfo.Config {
overlay[k] = v switch vt := v.(type) {
case string:
overlay[k] = vt
case int64:
overlay[k] = strconv.FormatInt(vt, 10)
case int32:
overlay[k] = strconv.FormatInt(int64(vt), 10)
case int:
overlay[k] = strconv.FormatInt(int64(vt), 10)
case float32:
overlay[k] = strconv.FormatFloat(float64(vt), 'f', 3, 32)
case float64:
overlay[k] = strconv.FormatFloat(vt, 'f', 3, 64)
case bool:
overlay[k] = strconv.FormatBool(vt)
default:
overlay[k] = fmt.Sprintf("%v", vt)
}
} }
rs.ConnInfo = &ResourceConnectionInfo{Raw: overlay} rs.ConnInfo = overlay
// Invoke the Provisioner // Invoke the Provisioner
rs, err = prov.Provisioner.Apply(rs, prov.Config) rs, err = prov.Provisioner.Apply(rs, prov.Config)

View File

@ -513,31 +513,29 @@ func TestContextApply_Provisioner_ConnInfo(t *testing.T) {
pr := testProvisioner() pr := testProvisioner()
p.ApplyFn = func(s *ResourceState, d *ResourceDiff) (*ResourceState, error) { p.ApplyFn = func(s *ResourceState, d *ResourceDiff) (*ResourceState, error) {
if s.ConnInfo == nil || s.ConnInfo.Raw == nil { if s.ConnInfo == nil {
t.Fatalf("ConnInfo not initialized") t.Fatalf("ConnInfo not initialized")
} }
result, _ := testApplyFn(s, d) result, _ := testApplyFn(s, d)
result.ConnInfo = &ResourceConnectionInfo{ result.ConnInfo = map[string]string{
Raw: map[string]interface{}{ "type": "ssh",
"type": "ssh", "host": "127.0.0.1",
"host": "127.0.0.1", "port": "22",
"port": 22,
},
} }
return result, nil return result, nil
} }
p.DiffFn = testDiffFn p.DiffFn = testDiffFn
pr.ApplyFn = func(rs *ResourceState, c *ResourceConfig) (*ResourceState, error) { pr.ApplyFn = func(rs *ResourceState, c *ResourceConfig) (*ResourceState, error) {
conn := rs.ConnInfo.Raw conn := rs.ConnInfo
if conn["type"] != "telnet" { if conn["type"] != "telnet" {
t.Fatalf("Bad: %#v", conn) t.Fatalf("Bad: %#v", conn)
} }
if conn["host"] != "127.0.0.1" { if conn["host"] != "127.0.0.1" {
t.Fatalf("Bad: %#v", conn) t.Fatalf("Bad: %#v", conn)
} }
if conn["port"] != 2222 { if conn["port"] != "2222" {
t.Fatalf("Bad: %#v", conn) t.Fatalf("Bad: %#v", conn)
} }
if conn["user"] != "superuser" { if conn["user"] != "superuser" {

View File

@ -145,14 +145,14 @@ func (s *State) String() string {
// that should not be serialized. This is only used temporarily // that should not be serialized. This is only used temporarily
// and is restored into the state. // and is restored into the state.
type sensitiveState struct { type sensitiveState struct {
ConnInfo map[string]*ResourceConnectionInfo ConnInfo map[string]map[string]string
once sync.Once once sync.Once
} }
func (s *sensitiveState) init() { func (s *sensitiveState) init() {
s.once.Do(func() { s.once.Do(func() {
s.ConnInfo = make(map[string]*ResourceConnectionInfo) s.ConnInfo = make(map[string]map[string]string)
}) })
} }
@ -245,17 +245,6 @@ func WriteState(d *State, dst io.Writer) error {
return err return err
} }
// ResourceConnectionInfo holds addresses, credentials and configuration
// information require to connect to a resource. This is populated
// by a provider so that provisioners can connect and run on the
// resource.
type ResourceConnectionInfo struct {
// Raw is used to store any relevant keys for the given 'type'
// so that a provisioner can connect to the resource. This could
// contain credentials or address information.
Raw map[string]interface{}
}
// ResourceState holds the state of a resource that is used so that // ResourceState holds the state of a resource that is used so that
// a provider can find and manage an existing resource as well as for // a provider can find and manage an existing resource as well as for
// storing attributes that are uesd to populate variables of child // storing attributes that are uesd to populate variables of child
@ -288,7 +277,7 @@ type ResourceState struct {
// ConnInfo is used for the providers to export information which is // ConnInfo is used for the providers to export information which is
// used to connect to the resource for provisioning. For example, // used to connect to the resource for provisioning. For example,
// this could contain SSH or WinRM credentials. // this could contain SSH or WinRM credentials.
ConnInfo *ResourceConnectionInfo ConnInfo map[string]string
// Extra information that the provider can store about a resource. // Extra information that the provider can store about a resource.
// This data is opaque, never shown to the user, and is sent back to // This data is opaque, never shown to the user, and is sent back to

View File

@ -98,12 +98,10 @@ func TestReadWriteState(t *testing.T) {
Resources: map[string]*ResourceState{ Resources: map[string]*ResourceState{
"foo": &ResourceState{ "foo": &ResourceState{
ID: "bar", ID: "bar",
ConnInfo: &ResourceConnectionInfo{ ConnInfo: map[string]string{
Raw: map[string]interface{}{ "type": "ssh",
"type": "ssh", "user": "root",
"user": "root", "password": "supersecret",
"password": "supersecret",
},
}, },
}, },
}, },