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)
// Initialize the connection info
rs.ConnInfo.Raw["type"] = "ssh"
rs.ConnInfo.Raw["host"] = instance.PublicIpAddress
rs.ConnInfo["type"] = "ssh"
rs.ConnInfo["host"] = instance.PublicIpAddress
// Set our attributes
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
func (p *ResourceProvisioner) verifySSH(s *terraform.ResourceState) error {
connType := s.ConnInfo.Raw["type"]
connType := s.ConnInfo["type"]
switch connType {
case nil:
case "":
case "ssh":
default:
@ -125,7 +124,7 @@ func (p *ResourceProvisioner) sshConfig(s *terraform.ResourceState) (*SSHConfig,
if err != nil {
return nil, err
}
if err := dec.Decode(s.ConnInfo.Raw); err != nil {
if err := dec.Decode(s.ConnInfo); err != nil {
return nil, err
}
if sshConf.User == "" {

View File

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

View File

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

View File

@ -3,6 +3,7 @@ package terraform
import (
"fmt"
"log"
"strconv"
"strings"
"sync"
"sync/atomic"
@ -469,10 +470,7 @@ func (c *Context) applyWalkFn() depgraph.WalkFunc {
// If we do not have any connection info, initialize
if r.State.ConnInfo == nil {
r.State.ConnInfo = &ResourceConnectionInfo{}
}
if r.State.ConnInfo.Raw == nil {
r.State.ConnInfo.Raw = make(map[string]interface{})
r.State.ConnInfo = make(map[string]string)
}
// Remove any output values from the diff
@ -584,16 +582,33 @@ func (c *Context) applyProvisioners(r *Resource, rs *ResourceState) (*ResourceSt
}
// Merge the connection information
overlay := make(map[string]interface{})
overlay := make(map[string]string)
if origConnInfo != nil {
for k, v := range origConnInfo.Raw {
for k, v := range origConnInfo {
overlay[k] = v
}
}
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
rs, err = prov.Provisioner.Apply(rs, prov.Config)

View File

@ -513,31 +513,29 @@ func TestContextApply_Provisioner_ConnInfo(t *testing.T) {
pr := testProvisioner()
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")
}
result, _ := testApplyFn(s, d)
result.ConnInfo = &ResourceConnectionInfo{
Raw: map[string]interface{}{
"type": "ssh",
"host": "127.0.0.1",
"port": 22,
},
result.ConnInfo = map[string]string{
"type": "ssh",
"host": "127.0.0.1",
"port": "22",
}
return result, nil
}
p.DiffFn = testDiffFn
pr.ApplyFn = func(rs *ResourceState, c *ResourceConfig) (*ResourceState, error) {
conn := rs.ConnInfo.Raw
conn := rs.ConnInfo
if conn["type"] != "telnet" {
t.Fatalf("Bad: %#v", conn)
}
if conn["host"] != "127.0.0.1" {
t.Fatalf("Bad: %#v", conn)
}
if conn["port"] != 2222 {
if conn["port"] != "2222" {
t.Fatalf("Bad: %#v", conn)
}
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
// and is restored into the state.
type sensitiveState struct {
ConnInfo map[string]*ResourceConnectionInfo
ConnInfo map[string]map[string]string
once sync.Once
}
func (s *sensitiveState) init() {
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
}
// 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
// a provider can find and manage an existing resource as well as for
// 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
// used to connect to the resource for provisioning. For example,
// this could contain SSH or WinRM credentials.
ConnInfo *ResourceConnectionInfo
ConnInfo map[string]string
// Extra information that the provider can store about a resource.
// 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{
"foo": &ResourceState{
ID: "bar",
ConnInfo: &ResourceConnectionInfo{
Raw: map[string]interface{}{
"type": "ssh",
"user": "root",
"password": "supersecret",
},
ConnInfo: map[string]string{
"type": "ssh",
"user": "root",
"password": "supersecret",
},
},
},