Merge pull request #220 from hashicorp/f-aws-instance-schema

aws_instance to helper/schema, and fixes for helper/schema
This commit is contained in:
Mitchell Hashimoto 2014-08-22 12:22:18 -07:00
commit 2aa2a0f883
10 changed files with 695 additions and 238 deletions

View File

@ -17,6 +17,7 @@ func Provider() *schema.Provider {
return &schema.Provider{ return &schema.Provider{
ResourcesMap: map[string]*schema.Resource{ ResourcesMap: map[string]*schema.Resource{
"aws_eip": resourceAwsEip(), "aws_eip": resourceAwsEip(),
"aws_instance": resourceAwsInstance(),
"aws_security_group": resourceAwsSecurityGroup(), "aws_security_group": resourceAwsSecurityGroup(),
}, },
} }

View File

@ -5,68 +5,160 @@ import (
"encoding/hex" "encoding/hex"
"fmt" "fmt"
"log" "log"
"strconv"
"strings" "strings"
"time" "time"
"github.com/hashicorp/terraform/flatmap" "github.com/hashicorp/terraform/helper/hashcode"
"github.com/hashicorp/terraform/helper/diff"
"github.com/hashicorp/terraform/helper/resource" "github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/helper/schema"
"github.com/hashicorp/terraform/terraform" "github.com/hashicorp/terraform/terraform"
"github.com/mitchellh/goamz/ec2" "github.com/mitchellh/goamz/ec2"
) )
func resource_aws_instance_create( /*
s *terraform.ResourceState, PreProcess: map[string]diff.PreProcessFunc{
d *terraform.ResourceDiff, "user_data": func(v string) string {
meta interface{}) (*terraform.ResourceState, error) { hash := sha1.Sum([]byte(v))
return hex.EncodeToString(hash[:])
},
},
}
*/
func resourceAwsInstance() *schema.Resource {
return &schema.Resource{
Create: resourceAwsInstanceCreate,
Read: resourceAwsInstanceRead,
Update: resourceAwsInstanceUpdate,
Delete: resourceAwsInstanceDelete,
Schema: map[string]*schema.Schema{
"ami": &schema.Schema{
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"associate_public_ip_address": &schema.Schema{
Type: schema.TypeBool,
Optional: true,
ForceNew: true,
},
"availability_zone": &schema.Schema{
Type: schema.TypeString,
Optional: true,
Computed: true,
ForceNew: true,
},
"instance_type": &schema.Schema{
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"key_name": &schema.Schema{
Type: schema.TypeString,
Optional: true,
ForceNew: true,
Computed: true,
},
"subnet_id": &schema.Schema{
Type: schema.TypeString,
Optional: true,
Computed: true,
ForceNew: true,
},
"private_ip": &schema.Schema{
Type: schema.TypeString,
Optional: true,
Computed: true,
},
"source_dest_check": &schema.Schema{
Type: schema.TypeBool,
Optional: true,
},
"user_data": &schema.Schema{
Type: schema.TypeString,
Optional: true,
ForceNew: true,
StateFunc: func(v interface{}) string {
hash := sha1.Sum([]byte(v.(string)))
return hex.EncodeToString(hash[:])
},
},
"security_groups": &schema.Schema{
Type: schema.TypeSet,
Optional: true,
Elem: &schema.Schema{Type: schema.TypeString},
Set: func(v interface{}) int {
return hashcode.String(v.(string))
},
},
"public_dns": &schema.Schema{
Type: schema.TypeString,
Computed: true,
},
"public_ip": &schema.Schema{
Type: schema.TypeString,
Computed: true,
},
"private_dns": &schema.Schema{
Type: schema.TypeString,
Computed: true,
},
},
}
}
func resourceAwsInstanceCreate(d *schema.ResourceData, meta interface{}) error {
p := meta.(*ResourceProvider) p := meta.(*ResourceProvider)
ec2conn := p.ec2conn ec2conn := p.ec2conn
// Merge the diff into the state so that we have all the attributes
// properly.
rs := s.MergeDiff(d)
delete(rs.Attributes, "source_dest_check")
// Figure out user data // Figure out user data
userData := "" userData := ""
if attr, ok := d.Attributes["user_data"]; ok { if v := d.Get("user_data"); v != nil {
userData = attr.NewExtra.(string) userData = v.(string)
} }
associatePublicIPAddress := false associatePublicIPAddress := false
if rs.Attributes["associate_public_ip_address"] == "true" { if v := d.Get("associate_public_ip_addresss"); v != nil {
associatePublicIPAddress = true associatePublicIPAddress = v.(bool)
} }
// Build the creation struct // Build the creation struct
runOpts := &ec2.RunInstances{ runOpts := &ec2.RunInstances{
ImageId: rs.Attributes["ami"], ImageId: d.Get("ami").(string),
AvailZone: rs.Attributes["availability_zone"], AvailZone: d.Get("availability_zone").(string),
InstanceType: rs.Attributes["instance_type"], InstanceType: d.Get("instance_type").(string),
KeyName: rs.Attributes["key_name"], KeyName: d.Get("key_name").(string),
SubnetId: rs.Attributes["subnet_id"], SubnetId: d.Get("subnet_id").(string),
PrivateIPAddress: rs.Attributes["private_ip"], PrivateIPAddress: d.Get("private_ip").(string),
AssociatePublicIpAddress: associatePublicIPAddress, AssociatePublicIpAddress: associatePublicIPAddress,
UserData: []byte(userData), UserData: []byte(userData),
} }
if raw := flatmap.Expand(rs.Attributes, "security_groups"); raw != nil {
if sgs, ok := raw.([]interface{}); ok {
for _, sg := range sgs {
str, ok := sg.(string)
if !ok {
continue
}
var g ec2.SecurityGroup if v := d.Get("security_groups"); v != nil {
if runOpts.SubnetId != "" { for _, v := range v.(*schema.Set).List() {
g.Id = str str := v.(string)
} else {
g.Name = str
}
runOpts.SecurityGroups = append(runOpts.SecurityGroups, g) var g ec2.SecurityGroup
if runOpts.SubnetId != "" {
g.Id = str
} else {
g.Name = str
} }
runOpts.SecurityGroups = append(runOpts.SecurityGroups, g)
} }
} }
@ -74,14 +166,14 @@ func resource_aws_instance_create(
log.Printf("[DEBUG] Run configuration: %#v", runOpts) log.Printf("[DEBUG] Run configuration: %#v", runOpts)
runResp, err := ec2conn.RunInstances(runOpts) runResp, err := ec2conn.RunInstances(runOpts)
if err != nil { if err != nil {
return nil, fmt.Errorf("Error launching source instance: %s", err) return fmt.Errorf("Error launching source instance: %s", err)
} }
instance := &runResp.Instances[0] instance := &runResp.Instances[0]
log.Printf("[INFO] Instance ID: %s", instance.InstanceId) log.Printf("[INFO] Instance ID: %s", instance.InstanceId)
// Store the resulting ID so we can look this up later // Store the resulting ID so we can look this up later
rs.ID = instance.InstanceId d.SetId(instance.InstanceId)
// Wait for the instance to become running so we can get some attributes // Wait for the instance to become running so we can get some attributes
// that aren't available until later. // that aren't available until later.
@ -99,9 +191,8 @@ func resource_aws_instance_create(
} }
instanceRaw, err := stateConf.WaitForState() instanceRaw, err := stateConf.WaitForState()
if err != nil { if err != nil {
return rs, fmt.Errorf( return fmt.Errorf(
"Error waiting for instance (%s) to become ready: %s", "Error waiting for instance (%s) to become ready: %s",
instance.InstanceId, err) instance.InstanceId, err)
} }
@ -109,213 +200,157 @@ func resource_aws_instance_create(
instance = instanceRaw.(*ec2.Instance) instance = instanceRaw.(*ec2.Instance)
// Initialize the connection info // Initialize the connection info
rs.ConnInfo["type"] = "ssh" d.SetConnInfo(map[string]string{
rs.ConnInfo["host"] = instance.PublicIpAddress "type": "ssh",
"host": instance.PublicIpAddress,
})
// Set our attributes // Set our attributes
rs, err = resource_aws_instance_update_state(rs, instance) if err := resourceAwsInstanceRead(d, meta); err != nil {
if err != nil { return err
return rs, err
} }
// Update if we need to // Update if we need to
return resource_aws_instance_update(rs, d, meta) return resourceAwsInstanceUpdate(d, meta)
} }
func resource_aws_instance_update( func resourceAwsInstanceUpdate(d *schema.ResourceData, meta interface{}) error {
s *terraform.ResourceState,
d *terraform.ResourceDiff,
meta interface{}) (*terraform.ResourceState, error) {
p := meta.(*ResourceProvider) p := meta.(*ResourceProvider)
ec2conn := p.ec2conn ec2conn := p.ec2conn
rs := s.MergeDiff(d)
modify := false modify := false
opts := new(ec2.ModifyInstance) opts := new(ec2.ModifyInstance)
if attr, ok := d.Attributes["source_dest_check"]; ok { if d.HasChange("source_dest_check") {
modify = true opts.SourceDestCheck = d.Get("source_dest_check").(bool)
opts.SourceDestCheck = attr.New != "" && attr.New != "false"
opts.SetSourceDestCheck = true opts.SetSourceDestCheck = true
rs.Attributes["source_dest_check"] = strconv.FormatBool(
opts.SourceDestCheck)
} }
if modify { if modify {
log.Printf("[INFO] Modifing instance %s: %#v", s.ID, opts) log.Printf("[INFO] Modifing instance %s: %#v", d.Id(), opts)
if _, err := ec2conn.ModifyInstance(s.ID, opts); err != nil { if _, err := ec2conn.ModifyInstance(d.Id(), opts); err != nil {
return s, err return err
} }
// TODO(mitchellh): wait for the attributes we modified to // TODO(mitchellh): wait for the attributes we modified to
// persist the change... // persist the change...
} }
return rs, nil return nil
} }
func resource_aws_instance_destroy( func resourceAwsInstanceDelete(d *schema.ResourceData, meta interface{}) error {
s *terraform.ResourceState,
meta interface{}) error {
p := meta.(*ResourceProvider) p := meta.(*ResourceProvider)
ec2conn := p.ec2conn ec2conn := p.ec2conn
log.Printf("[INFO] Terminating instance: %s", s.ID) log.Printf("[INFO] Terminating instance: %s", d.Id())
if _, err := ec2conn.TerminateInstances([]string{s.ID}); err != nil { if _, err := ec2conn.TerminateInstances([]string{d.Id()}); err != nil {
return fmt.Errorf("Error terminating instance: %s", err) return fmt.Errorf("Error terminating instance: %s", err)
} }
log.Printf( log.Printf(
"[DEBUG] Waiting for instance (%s) to become terminated", "[DEBUG] Waiting for instance (%s) to become terminated",
s.ID) d.Id())
stateConf := &resource.StateChangeConf{ stateConf := &resource.StateChangeConf{
Pending: []string{"pending", "running", "shutting-down", "stopped", "stopping"}, Pending: []string{"pending", "running", "shutting-down", "stopped", "stopping"},
Target: "terminated", Target: "terminated",
Refresh: InstanceStateRefreshFunc(ec2conn, s.ID), Refresh: InstanceStateRefreshFunc(ec2conn, d.Id()),
Timeout: 10 * time.Minute, Timeout: 10 * time.Minute,
Delay: 10 * time.Second, Delay: 10 * time.Second,
MinTimeout: 3 * time.Second, MinTimeout: 3 * time.Second,
} }
_, err := stateConf.WaitForState() _, err := stateConf.WaitForState()
if err != nil { if err != nil {
return fmt.Errorf( return fmt.Errorf(
"Error waiting for instance (%s) to terminate: %s", "Error waiting for instance (%s) to terminate: %s",
s.ID, err) d.Id(), err)
} }
d.SetId("")
return nil return nil
} }
func resource_aws_instance_diff( func resourceAwsInstanceRead(d *schema.ResourceData, meta interface{}) error {
s *terraform.ResourceState,
c *terraform.ResourceConfig,
meta interface{}) (*terraform.ResourceDiff, error) {
b := &diff.ResourceBuilder{
Attrs: map[string]diff.AttrType{
"ami": diff.AttrTypeCreate,
"availability_zone": diff.AttrTypeCreate,
"instance_type": diff.AttrTypeCreate,
"key_name": diff.AttrTypeCreate,
"private_ip": diff.AttrTypeCreate,
"security_groups": diff.AttrTypeCreate,
"subnet_id": diff.AttrTypeCreate,
"source_dest_check": diff.AttrTypeUpdate,
"user_data": diff.AttrTypeCreate,
"associate_public_ip_address": diff.AttrTypeCreate,
},
ComputedAttrs: []string{
"availability_zone",
"key_name",
"public_dns",
"public_ip",
"private_dns",
"private_ip",
"security_groups",
"subnet_id",
},
PreProcess: map[string]diff.PreProcessFunc{
"user_data": func(v string) string {
hash := sha1.Sum([]byte(v))
return hex.EncodeToString(hash[:])
},
},
}
return b.Diff(s, c)
}
func resource_aws_instance_refresh(
s *terraform.ResourceState,
meta interface{}) (*terraform.ResourceState, error) {
p := meta.(*ResourceProvider) p := meta.(*ResourceProvider)
ec2conn := p.ec2conn ec2conn := p.ec2conn
resp, err := ec2conn.Instances([]string{s.ID}, ec2.NewFilter()) resp, err := ec2conn.Instances([]string{d.Id()}, ec2.NewFilter())
if err != nil { if err != nil {
// If the instance was not found, return nil so that we can show // If the instance was not found, return nil so that we can show
// that the instance is gone. // that the instance is gone.
if ec2err, ok := err.(*ec2.Error); ok && ec2err.Code == "InvalidInstanceID.NotFound" { if ec2err, ok := err.(*ec2.Error); ok && ec2err.Code == "InvalidInstanceID.NotFound" {
return nil, nil d.SetId("")
return nil
} }
// Some other error, report it // Some other error, report it
return s, err return err
} }
// If nothing was found, then return no state // If nothing was found, then return no state
if len(resp.Reservations) == 0 { if len(resp.Reservations) == 0 {
return nil, nil d.SetId("")
return nil
} }
instance := &resp.Reservations[0].Instances[0] instance := &resp.Reservations[0].Instances[0]
// If the instance is terminated, then it is gone // If the instance is terminated, then it is gone
if instance.State.Name == "terminated" { if instance.State.Name == "terminated" {
return nil, nil d.SetId("")
return nil
} }
return resource_aws_instance_update_state(s, instance) d.Set("availability_zone", instance.AvailZone)
} d.Set("key_name", instance.KeyName)
d.Set("public_dns", instance.DNSName)
d.Set("public_ip", instance.PublicIpAddress)
d.Set("private_dns", instance.PrivateDNSName)
d.Set("private_ip", instance.PrivateIpAddress)
d.Set("subnet_id", instance.SubnetId)
func resource_aws_instance_update_state( var deps []terraform.ResourceDependency
s *terraform.ResourceState,
instance *ec2.Instance) (*terraform.ResourceState, error) {
s.Attributes["availability_zone"] = instance.AvailZone
s.Attributes["key_name"] = instance.KeyName
s.Attributes["public_dns"] = instance.DNSName
s.Attributes["public_ip"] = instance.PublicIpAddress
s.Attributes["private_dns"] = instance.PrivateDNSName
s.Attributes["private_ip"] = instance.PrivateIpAddress
s.Attributes["subnet_id"] = instance.SubnetId
s.Dependencies = nil
// Extract the existing security groups // Determine whether we're referring to security groups with
useID := false // IDs or names. We use a heuristic to figure this out. By default,
if raw := flatmap.Expand(s.Attributes, "security_groups"); raw != nil { // we use IDs if we're in a VPC. However, if we previously had an
if sgs, ok := raw.([]interface{}); ok { // all-name list of security groups, we use names. Or, if we had any
for _, sg := range sgs { // IDs, we use IDs.
str, ok := sg.(string) useID := instance.SubnetId != ""
if !ok { if v := d.Get("security_groups"); v != nil {
continue match := false
} for _, v := range v.(*schema.Set).List() {
if strings.HasPrefix(v.(string), "sg-") {
if strings.HasPrefix(str, "sg-") { match = true
useID = true break
break
}
} }
} }
useID = match
} }
// Build up the security groups // Build up the security groups
sgs := make([]string, len(instance.SecurityGroups)) sgs := make([]string, len(instance.SecurityGroups))
for i, sg := range instance.SecurityGroups { for i, sg := range instance.SecurityGroups {
if instance.SubnetId != "" && useID { if useID {
sgs[i] = sg.Id sgs[i] = sg.Id
} else { } else {
sgs[i] = sg.Name sgs[i] = sg.Name
} }
s.Dependencies = append(s.Dependencies, deps = append(deps, terraform.ResourceDependency{ID: sg.Id})
terraform.ResourceDependency{ID: sg.Id},
)
} }
flatmap.Map(s.Attributes).Merge(flatmap.Flatten(map[string]interface{}{ d.Set("security_groups", sgs)
"security_groups": sgs,
}))
// If we're in a VPC, we depend on the subnet
if instance.SubnetId != "" { if instance.SubnetId != "" {
s.Dependencies = append(s.Dependencies, deps = append(deps, terraform.ResourceDependency{ID: instance.SubnetId})
terraform.ResourceDependency{ID: instance.SubnetId},
)
} }
return s, nil d.SetDependencies(deps)
return nil
} }
// InstanceStateRefreshFunc returns a resource.StateRefreshFunc that is used to watch // InstanceStateRefreshFunc returns a resource.StateRefreshFunc that is used to watch

View File

@ -3,8 +3,8 @@ package aws
import ( import (
"bytes" "bytes"
"fmt" "fmt"
"sort"
"log" "log"
"sort"
"time" "time"
"github.com/hashicorp/terraform/helper/hashcode" "github.com/hashicorp/terraform/helper/hashcode"

View File

@ -47,14 +47,6 @@ func init() {
Refresh: resource_aws_elb_refresh, Refresh: resource_aws_elb_refresh,
}, },
"aws_instance": resource.Resource{
Create: resource_aws_instance_create,
Destroy: resource_aws_instance_destroy,
Diff: resource_aws_instance_diff,
Refresh: resource_aws_instance_refresh,
Update: resource_aws_instance_update,
},
"aws_internet_gateway": resource.Resource{ "aws_internet_gateway": resource.Resource{
Create: resource_aws_internet_gateway_create, Create: resource_aws_internet_gateway_create,
Destroy: resource_aws_internet_gateway_destroy, Destroy: resource_aws_internet_gateway_destroy,

View File

@ -24,9 +24,9 @@ const UnknownVariableValue = "74D93920-ED26-11E3-AC10-0800200C9A66"
// RawConfig supports a query-like interface to request // RawConfig supports a query-like interface to request
// information from deep within the structure. // information from deep within the structure.
type RawConfig struct { type RawConfig struct {
Raw map[string]interface{} Raw map[string]interface{}
Interpolations []Interpolation Interpolations []Interpolation
Variables map[string]InterpolatedVariable Variables map[string]InterpolatedVariable
config map[string]interface{} config map[string]interface{}
unknownKeys []string unknownKeys []string

View File

@ -108,7 +108,7 @@ func (r *Resource) Refresh(
err = r.Read(data, meta) err = r.Read(data, meta)
state := data.State() state := data.State()
if state.ID == "" { if state != nil && state.ID == "" {
state = nil state = nil
} }

View File

@ -23,6 +23,17 @@ const (
getSourceSet getSourceSet
) )
// getResult is the internal structure that is generated when a Get
// is called that contains some extra data that might be used.
type getResult struct {
Value interface{}
ValueProcessed interface{}
Exists bool
Schema *Schema
}
var getResultEmpty getResult
// ResourceData is used to query and set the attributes of a resource. // ResourceData is used to query and set the attributes of a resource.
type ResourceData struct { type ResourceData struct {
schema map[string]*Schema schema map[string]*Schema
@ -36,25 +47,42 @@ type ResourceData struct {
once sync.Once once sync.Once
} }
// Get returns the data for the given key, or nil if the key doesn't exist. // Get returns the data for the given key, or nil if the key doesn't exist
// in the schema.
// //
// The type of the data returned will be according to the schema specified. // If the key does exist in the schema but doesn't exist in the configuration,
// Primitives will be their respective types in Go, lists will always be // then the default value for that type will be returned. For strings, this is
// []interface{}, and sub-resources will be map[string]interface{}. // "", for numbers it is 0, etc.
//
// If you also want to test if something is set at all, use GetOk.
func (d *ResourceData) Get(key string) interface{} { func (d *ResourceData) Get(key string) interface{} {
var parts []string v, _ := d.GetOk(key)
if key != "" { return v
parts = strings.Split(key, ".")
}
return d.getObject("", parts, d.schema, getSourceSet)
} }
// GetChange returns the old and new value for a given key. // GetChange returns the old and new value for a given key.
// //
// If there is no change, then old and new will simply be the same. // If there is no change, then old and new will simply be the same.
func (d *ResourceData) GetChange(key string) (interface{}, interface{}) { func (d *ResourceData) GetChange(key string) (interface{}, interface{}) {
return d.getChange(key, getSourceConfig, getSourceDiff) o, n := d.getChange(key, getSourceConfig, getSourceDiff)
return o.Value, n.Value
}
// GetOk returns the data for the given key and whether or not the key
// existed or not in the configuration. The second boolean result will also
// be false if a key is given that isn't in the schema at all.
func (d *ResourceData) GetOk(key string) (interface{}, bool) {
r := d.getRaw(key)
return r.Value, r.Exists
}
func (d *ResourceData) getRaw(key string) getResult {
var parts []string
if key != "" {
parts = strings.Split(key, ".")
}
return d.getObject("", parts, d.schema, getSourceSet)
} }
// HasChange returns whether or not the given key has been changed. // HasChange returns whether or not the given key has been changed.
@ -91,6 +119,19 @@ func (d *ResourceData) Id() string {
return result return result
} }
// ConnInfo returns the connection info for this resource.
func (d *ResourceData) ConnInfo() map[string]string {
if d.newState != nil {
return d.newState.ConnInfo
}
if d.state != nil {
return d.state.ConnInfo
}
return nil
}
// Dependencies returns the dependencies in this state. // Dependencies returns the dependencies in this state.
func (d *ResourceData) Dependencies() []terraform.ResourceDependency { func (d *ResourceData) Dependencies() []terraform.ResourceDependency {
if d.newState != nil { if d.newState != nil {
@ -111,6 +152,12 @@ func (d *ResourceData) SetId(v string) {
d.newState.ID = v d.newState.ID = v
} }
// SetConnInfo sets the connection info for a resource.
func (d *ResourceData) SetConnInfo(v map[string]string) {
d.once.Do(d.init)
d.newState.ConnInfo = v
}
// SetDependencies sets the dependencies of a resource. // SetDependencies sets the dependencies of a resource.
func (d *ResourceData) SetDependencies(ds []terraform.ResourceDependency) { func (d *ResourceData) SetDependencies(ds []terraform.ResourceDependency) {
d.once.Do(d.init) d.once.Do(d.init)
@ -122,7 +169,15 @@ func (d *ResourceData) SetDependencies(ds []terraform.ResourceDependency) {
func (d *ResourceData) State() *terraform.ResourceState { func (d *ResourceData) State() *terraform.ResourceState {
var result terraform.ResourceState var result terraform.ResourceState
result.ID = d.Id() result.ID = d.Id()
// If we have no ID, then this resource doesn't exist and we just
// return nil.
if result.ID == "" {
return nil
}
result.Attributes = d.stateObject("", d.schema) result.Attributes = d.stateObject("", d.schema)
result.ConnInfo = d.ConnInfo()
result.Dependencies = d.Dependencies() result.Dependencies = d.Dependencies()
if v := d.Id(); v != "" { if v := d.Id(); v != "" {
@ -144,15 +199,21 @@ func (d *ResourceData) init() {
func (d *ResourceData) diffChange(k string) (interface{}, interface{}, bool) { func (d *ResourceData) diffChange(k string) (interface{}, interface{}, bool) {
// Get the change between the state and the config. // Get the change between the state and the config.
o, n := d.getChange(k, getSourceState, getSourceConfig) o, n := d.getChange(k, getSourceState, getSourceConfig)
if !o.Exists {
o.Value = nil
}
if !n.Exists {
n.Value = nil
}
// Return the old, new, and whether there is a change // Return the old, new, and whether there is a change
return o, n, !reflect.DeepEqual(o, n) return o.Value, n.Value, !reflect.DeepEqual(o.Value, n.Value)
} }
func (d *ResourceData) getChange( func (d *ResourceData) getChange(
key string, key string,
oldLevel getSource, oldLevel getSource,
newLevel getSource) (interface{}, interface{}) { newLevel getSource) (getResult, getResult) {
var parts, parts2 []string var parts, parts2 []string
if key != "" { if key != "" {
parts = strings.Split(key, ".") parts = strings.Split(key, ".")
@ -168,7 +229,7 @@ func (d *ResourceData) get(
k string, k string,
parts []string, parts []string,
schema *Schema, schema *Schema,
source getSource) interface{} { source getSource) getResult {
switch schema.Type { switch schema.Type {
case TypeList: case TypeList:
return d.getList(k, parts, schema, source) return d.getList(k, parts, schema, source)
@ -191,24 +252,25 @@ func (d *ResourceData) getSet(
k string, k string,
parts []string, parts []string,
schema *Schema, schema *Schema,
source getSource) interface{} { source getSource) getResult {
s := &Set{F: schema.Set} s := &Set{F: schema.Set}
result := getResult{Schema: schema, Value: s}
raw := d.getList(k, nil, schema, source) raw := d.getList(k, nil, schema, source)
if raw == nil { if !raw.Exists {
if len(parts) > 0 { if len(parts) > 0 {
return d.getList(k, parts, schema, source) return d.getList(k, parts, schema, source)
} }
return s return result
} }
list := raw.([]interface{}) list := raw.Value.([]interface{})
if len(list) == 0 { if len(list) == 0 {
if len(parts) > 0 { if len(parts) > 0 {
return d.getList(k, parts, schema, source) return d.getList(k, parts, schema, source)
} }
return s return result
} }
// This is a reverse map of hash code => index in config used to // This is a reverse map of hash code => index in config used to
@ -242,12 +304,12 @@ func (d *ResourceData) getSet(
index := parts[0] index := parts[0]
indexInt, err := strconv.ParseInt(index, 0, 0) indexInt, err := strconv.ParseInt(index, 0, 0)
if err != nil { if err != nil {
return nil return getResultEmpty
} }
codes := s.listCode() codes := s.listCode()
if int(indexInt) >= len(codes) { if int(indexInt) >= len(codes) {
return nil return getResultEmpty
} }
code := codes[indexInt] code := codes[indexInt]
realIndex := indexMap[code] realIndex := indexMap[code]
@ -256,17 +318,19 @@ func (d *ResourceData) getSet(
return d.getList(k, parts, schema, source) return d.getList(k, parts, schema, source)
} }
return s result.Exists = true
return result
} }
func (d *ResourceData) getMap( func (d *ResourceData) getMap(
k string, k string,
parts []string, parts []string,
schema *Schema, schema *Schema,
source getSource) interface{} { source getSource) getResult {
elemSchema := &Schema{Type: TypeString} elemSchema := &Schema{Type: TypeString}
result := make(map[string]interface{}) result := make(map[string]interface{})
resultSet := false
prefix := k + "." prefix := k + "."
if d.state != nil && source >= getSourceState { if d.state != nil && source >= getSourceState {
@ -276,7 +340,8 @@ func (d *ResourceData) getMap(
} }
single := k[len(prefix):] single := k[len(prefix):]
result[single] = d.getPrimitive(k, nil, elemSchema, source) result[single] = d.getPrimitive(k, nil, elemSchema, source).Value
resultSet = true
} }
} }
@ -284,6 +349,7 @@ func (d *ResourceData) getMap(
// For config, we always set the result to exactly what was requested // For config, we always set the result to exactly what was requested
if m, ok := d.config.Get(k); ok { if m, ok := d.config.Get(k); ok {
result = m.(map[string]interface{}) result = m.(map[string]interface{})
resultSet = true
} else { } else {
result = nil result = nil
} }
@ -294,13 +360,14 @@ func (d *ResourceData) getMap(
if !strings.HasPrefix(k, prefix) { if !strings.HasPrefix(k, prefix) {
continue continue
} }
resultSet = true
single := k[len(prefix):] single := k[len(prefix):]
if v.NewRemoved { if v.NewRemoved {
delete(result, single) delete(result, single)
} else { } else {
result[single] = d.getPrimitive(k, nil, elemSchema, source) result[single] = d.getPrimitive(k, nil, elemSchema, source).Value
} }
} }
} }
@ -311,6 +378,8 @@ func (d *ResourceData) getMap(
if !strings.HasPrefix(k, prefix) { if !strings.HasPrefix(k, prefix) {
continue continue
} }
resultSet = true
if !cleared { if !cleared {
// We clear the results if they are in the set map // We clear the results if they are in the set map
result = make(map[string]interface{}) result = make(map[string]interface{})
@ -318,30 +387,35 @@ func (d *ResourceData) getMap(
} }
single := k[len(prefix):] single := k[len(prefix):]
result[single] = d.getPrimitive(k, nil, elemSchema, source) result[single] = d.getPrimitive(k, nil, elemSchema, source).Value
} }
} }
// If we're requesting a specific element, return that // If we're requesting a specific element, return that
var resultValue interface{} = result
if len(parts) > 0 { if len(parts) > 0 {
return result[parts[0]] resultValue = result[parts[0]]
} }
return result return getResult{
Value: resultValue,
Exists: resultSet,
Schema: schema,
}
} }
func (d *ResourceData) getObject( func (d *ResourceData) getObject(
k string, k string,
parts []string, parts []string,
schema map[string]*Schema, schema map[string]*Schema,
source getSource) interface{} { source getSource) getResult {
if len(parts) > 0 { if len(parts) > 0 {
// We're requesting a specific key in an object // We're requesting a specific key in an object
key := parts[0] key := parts[0]
parts = parts[1:] parts = parts[1:]
s, ok := schema[key] s, ok := schema[key]
if !ok { if !ok {
return nil return getResultEmpty
} }
if k != "" { if k != "" {
@ -356,17 +430,23 @@ func (d *ResourceData) getObject(
// Get the entire object // Get the entire object
result := make(map[string]interface{}) result := make(map[string]interface{})
for field, _ := range schema { for field, _ := range schema {
result[field] = d.getObject(k, []string{field}, schema, source) result[field] = d.getObject(k, []string{field}, schema, source).Value
} }
return result return getResult{
Value: result,
Exists: true,
Schema: &Schema{
Elem: schema,
},
}
} }
func (d *ResourceData) getList( func (d *ResourceData) getList(
k string, k string,
parts []string, parts []string,
schema *Schema, schema *Schema,
source getSource) interface{} { source getSource) getResult {
if len(parts) > 0 { if len(parts) > 0 {
// We still have parts left over meaning we're accessing an // We still have parts left over meaning we're accessing an
// element of this list. // element of this list.
@ -376,12 +456,7 @@ func (d *ResourceData) getList(
// Special case if we're accessing the count of the list // Special case if we're accessing the count of the list
if idx == "#" { if idx == "#" {
schema := &Schema{Type: TypeInt} schema := &Schema{Type: TypeInt}
result := d.get(k+".#", parts, schema, source) return d.get(k+".#", parts, schema, source)
if result == nil {
result = 0
}
return result
} }
key := fmt.Sprintf("%s.%s", k, idx) key := fmt.Sprintf("%s.%s", k, idx)
@ -394,23 +469,27 @@ func (d *ResourceData) getList(
} }
// Get the entire list. // Get the entire list.
result := make( count := d.getList(k, []string{"#"}, schema, source)
[]interface{}, result := make([]interface{}, count.Value.(int))
d.getList(k, []string{"#"}, schema, source).(int))
for i, _ := range result { for i, _ := range result {
is := strconv.FormatInt(int64(i), 10) is := strconv.FormatInt(int64(i), 10)
result[i] = d.getList(k, []string{is}, schema, source) result[i] = d.getList(k, []string{is}, schema, source).Value
} }
return result return getResult{
Value: result,
Exists: count.Exists,
Schema: schema,
}
} }
func (d *ResourceData) getPrimitive( func (d *ResourceData) getPrimitive(
k string, k string,
parts []string, parts []string,
schema *Schema, schema *Schema,
source getSource) interface{} { source getSource) getResult {
var result string var result string
var resultProcessed interface{}
var resultSet bool var resultSet bool
if d.state != nil && source >= getSourceState { if d.state != nil && source >= getSourceState {
result, resultSet = d.state.Attributes[k] result, resultSet = d.state.Attributes[k]
@ -435,6 +514,17 @@ func (d *ResourceData) getPrimitive(
attrD, ok := d.diff.Attributes[k] attrD, ok := d.diff.Attributes[k]
if ok && !attrD.NewComputed { if ok && !attrD.NewComputed {
result = attrD.New result = attrD.New
if attrD.NewExtra != nil {
// If NewExtra != nil, then we have processed data as the New,
// so we store that but decode the unprocessed data into result
resultProcessed = result
err := mapstructure.WeakDecode(attrD.NewExtra, &result)
if err != nil {
panic(err)
}
}
resultSet = true resultSet = true
} }
} }
@ -447,13 +537,15 @@ func (d *ResourceData) getPrimitive(
} }
if !resultSet { if !resultSet {
return nil result = ""
} }
var resultValue interface{}
switch schema.Type { switch schema.Type {
case TypeBool: case TypeBool:
if result == "" { if result == "" {
return false resultValue = false
break
} }
v, err := strconv.ParseBool(result) v, err := strconv.ParseBool(result)
@ -461,13 +553,14 @@ func (d *ResourceData) getPrimitive(
panic(err) panic(err)
} }
return v resultValue = v
case TypeString: case TypeString:
// Use the value as-is. We just put this case here to be explicit. // Use the value as-is. We just put this case here to be explicit.
return result resultValue = result
case TypeInt: case TypeInt:
if result == "" { if result == "" {
return 0 resultValue = 0
break
} }
v, err := strconv.ParseInt(result, 0, 0) v, err := strconv.ParseInt(result, 0, 0)
@ -475,10 +568,17 @@ func (d *ResourceData) getPrimitive(
panic(err) panic(err)
} }
return int(v) resultValue = int(v)
default: default:
panic(fmt.Sprintf("Unknown type: %s", schema.Type)) panic(fmt.Sprintf("Unknown type: %s", schema.Type))
} }
return getResult{
Value: resultValue,
ValueProcessed: resultProcessed,
Exists: resultSet,
Schema: schema,
}
} }
func (d *ResourceData) set( func (d *ResourceData) set(
@ -700,10 +800,10 @@ func (d *ResourceData) stateList(
prefix string, prefix string,
schema *Schema) map[string]string { schema *Schema) map[string]string {
countRaw := d.get(prefix, []string{"#"}, schema, getSourceSet) countRaw := d.get(prefix, []string{"#"}, schema, getSourceSet)
if countRaw == nil { if !countRaw.Exists {
return nil return nil
} }
count := countRaw.(int) count := countRaw.Value.(int)
result := make(map[string]string) result := make(map[string]string)
if count > 0 { if count > 0 {
@ -732,13 +832,13 @@ func (d *ResourceData) stateMap(
prefix string, prefix string,
schema *Schema) map[string]string { schema *Schema) map[string]string {
v := d.getMap(prefix, nil, schema, getSourceSet) v := d.getMap(prefix, nil, schema, getSourceSet)
if v == nil { if !v.Exists {
return nil return nil
} }
elemSchema := &Schema{Type: TypeString} elemSchema := &Schema{Type: TypeString}
result := make(map[string]string) result := make(map[string]string)
for mk, _ := range v.(map[string]interface{}) { for mk, _ := range v.Value.(map[string]interface{}) {
mp := fmt.Sprintf("%s.%s", prefix, mk) mp := fmt.Sprintf("%s.%s", prefix, mk)
for k, v := range d.stateSingle(mp, elemSchema) { for k, v := range d.stateSingle(mp, elemSchema) {
result[k] = v result[k] = v
@ -769,11 +869,16 @@ func (d *ResourceData) stateObject(
func (d *ResourceData) statePrimitive( func (d *ResourceData) statePrimitive(
prefix string, prefix string,
schema *Schema) map[string]string { schema *Schema) map[string]string {
v := d.Get(prefix) raw := d.getRaw(prefix)
if v == nil { if !raw.Exists {
return nil return nil
} }
v := raw.Value
if raw.ValueProcessed != nil {
v = raw.ValueProcessed
}
var vs string var vs string
switch schema.Type { switch schema.Type {
case TypeBool: case TypeBool:
@ -795,11 +900,11 @@ func (d *ResourceData) stateSet(
prefix string, prefix string,
schema *Schema) map[string]string { schema *Schema) map[string]string {
raw := d.get(prefix, nil, schema, getSourceSet) raw := d.get(prefix, nil, schema, getSourceSet)
if raw == nil { if !raw.Exists {
return nil return nil
} }
set := raw.(*Set) set := raw.Value.(*Set)
list := set.List() list := set.List()
result := make(map[string]string) result := make(map[string]string)
result[prefix+".#"] = strconv.FormatInt(int64(len(list)), 10) result[prefix+".#"] = strconv.FormatInt(int64(len(list)), 10)

View File

@ -37,9 +37,8 @@ func TestResourceDataGet(t *testing.T) {
}, },
}, },
Key: "availability_zone", Key: "availability_zone",
Value: "",
Value: nil,
}, },
{ {
@ -69,6 +68,32 @@ func TestResourceDataGet(t *testing.T) {
Value: "foo", Value: "foo",
}, },
{
Schema: map[string]*Schema{
"availability_zone": &Schema{
Type: TypeString,
Optional: true,
Computed: true,
ForceNew: true,
},
},
State: nil,
Diff: &terraform.ResourceDiff{
Attributes: map[string]*terraform.ResourceAttrDiff{
"availability_zone": &terraform.ResourceAttrDiff{
Old: "",
New: "foo!",
NewExtra: "foo",
},
},
},
Key: "availability_zone",
Value: "foo",
},
{ {
Schema: map[string]*Schema{ Schema: map[string]*Schema{
"availability_zone": &Schema{ "availability_zone": &Schema{
@ -524,7 +549,7 @@ func TestResourceDataGetChange(t *testing.T) {
Key: "availability_zone", Key: "availability_zone",
OldValue: nil, OldValue: "",
NewValue: "foo", NewValue: "foo",
}, },
@ -577,6 +602,169 @@ func TestResourceDataGetChange(t *testing.T) {
} }
} }
func TestResourceDataGetOk(t *testing.T) {
cases := []struct {
Schema map[string]*Schema
State *terraform.ResourceState
Diff *terraform.ResourceDiff
Key string
Value interface{}
Ok bool
}{
/*
* Primitives
*/
{
Schema: map[string]*Schema{
"availability_zone": &Schema{
Type: TypeString,
Optional: true,
Computed: true,
ForceNew: true,
},
},
State: nil,
Diff: &terraform.ResourceDiff{
Attributes: map[string]*terraform.ResourceAttrDiff{
"availability_zone": &terraform.ResourceAttrDiff{
Old: "",
New: "",
},
},
},
Key: "availability_zone",
Value: "",
Ok: true,
},
{
Schema: map[string]*Schema{
"availability_zone": &Schema{
Type: TypeString,
Optional: true,
Computed: true,
ForceNew: true,
},
},
State: nil,
Diff: nil,
Key: "availability_zone",
Value: "",
Ok: false,
},
/*
* Lists
*/
{
Schema: map[string]*Schema{
"ports": &Schema{
Type: TypeList,
Optional: true,
Elem: &Schema{Type: TypeInt},
},
},
State: nil,
Diff: nil,
Key: "ports",
Value: []interface{}{},
Ok: false,
},
/*
* Map
*/
{
Schema: map[string]*Schema{
"ports": &Schema{
Type: TypeMap,
Optional: true,
},
},
State: nil,
Diff: nil,
Key: "ports",
Value: map[string]interface{}{},
Ok: false,
},
/*
* Set
*/
{
Schema: map[string]*Schema{
"ports": &Schema{
Type: TypeSet,
Optional: true,
Elem: &Schema{Type: TypeInt},
Set: func(a interface{}) int { return a.(int) },
},
},
State: nil,
Diff: nil,
Key: "ports",
Value: []interface{}{},
Ok: false,
},
{
Schema: map[string]*Schema{
"ports": &Schema{
Type: TypeSet,
Optional: true,
Elem: &Schema{Type: TypeInt},
Set: func(a interface{}) int { return a.(int) },
},
},
State: nil,
Diff: nil,
Key: "ports.0",
Value: 0,
Ok: false,
},
}
for i, tc := range cases {
d, err := schemaMap(tc.Schema).Data(tc.State, tc.Diff)
if err != nil {
t.Fatalf("err: %s", err)
}
v, ok := d.GetOk(tc.Key)
if s, ok := v.(*Set); ok {
v = s.List()
}
if !reflect.DeepEqual(v, tc.Value) {
t.Fatalf("Bad: %d\n\n%#v", i, v)
}
if ok != tc.Ok {
t.Fatalf("Bad: %d\n\n%#v", i, ok)
}
}
}
func TestResourceDataHasChange(t *testing.T) { func TestResourceDataHasChange(t *testing.T) {
cases := []struct { cases := []struct {
Schema map[string]*Schema Schema map[string]*Schema
@ -771,7 +959,7 @@ func TestResourceDataSet(t *testing.T) {
Err: true, Err: true,
GetKey: "availability_zone", GetKey: "availability_zone",
GetValue: nil, GetValue: "",
}, },
// List of primitives, set element // List of primitives, set element
@ -1268,6 +1456,36 @@ func TestResourceDataState(t *testing.T) {
}, },
}, },
// Basic primitive with StateFunc set
{
Schema: map[string]*Schema{
"availability_zone": &Schema{
Type: TypeString,
Optional: true,
Computed: true,
StateFunc: func(interface{}) string { return "" },
},
},
State: nil,
Diff: &terraform.ResourceDiff{
Attributes: map[string]*terraform.ResourceAttrDiff{
"availability_zone": &terraform.ResourceAttrDiff{
Old: "",
New: "foo",
NewExtra: "foo!",
},
},
},
Result: &terraform.ResourceState{
Attributes: map[string]string{
"availability_zone": "foo",
},
},
},
// List // List
{ {
Schema: map[string]*Schema{ Schema: map[string]*Schema{
@ -1524,15 +1742,47 @@ func TestResourceDataState(t *testing.T) {
} }
} }
// Set an ID so that the state returned is not nil
idSet := false
if d.Id() == "" {
idSet = true
d.SetId("foo")
}
actual := d.State() actual := d.State()
// If we set an ID, then undo what we did so the comparison works
if actual != nil && idSet {
actual.ID = ""
delete(actual.Attributes, "id")
}
if !reflect.DeepEqual(actual, tc.Result) { if !reflect.DeepEqual(actual, tc.Result) {
t.Fatalf("Bad: %d\n\n%#v", i, actual) t.Fatalf("Bad: %d\n\n%#v", i, actual)
} }
} }
} }
func TestResourceDataSetConnInfo(t *testing.T) {
d := &ResourceData{}
d.SetId("foo")
d.SetConnInfo(map[string]string{
"foo": "bar",
})
expected := map[string]string{
"foo": "bar",
}
actual := d.State()
if !reflect.DeepEqual(actual.ConnInfo, expected) {
t.Fatalf("bad: %#v", actual)
}
}
func TestResourceDataSetDependencies(t *testing.T) { func TestResourceDataSetDependencies(t *testing.T) {
d := &ResourceData{} d := &ResourceData{}
d.SetId("foo")
d.SetDependencies([]terraform.ResourceDependency{ d.SetDependencies([]terraform.ResourceDependency{
terraform.ResourceDependency{ID: "foo"}, terraform.ResourceDependency{ID: "foo"},
}) })
@ -1564,7 +1814,7 @@ func TestResourceDataSetId_clear(t *testing.T) {
d.SetId("") d.SetId("")
actual := d.State() actual := d.State()
if actual.ID != "" { if actual != nil {
t.Fatalf("bad: %#v", actual) t.Fatalf("bad: %#v", actual)
} }
} }

View File

@ -41,8 +41,14 @@ type Schema struct {
// //
// If ForceNew is true, then a change in this resource necessitates // If ForceNew is true, then a change in this resource necessitates
// the creation of a new resource. // the creation of a new resource.
Computed bool //
ForceNew bool // StateFunc is a function called to change the value of this before
// storing it in the state (and likewise before comparing for diffs).
// The use for this is for example with large strings, you may want
// to simply store the hash of it.
Computed bool
ForceNew bool
StateFunc SchemaStateFunc
// The following fields are only set for a TypeList or TypeSet Type. // The following fields are only set for a TypeList or TypeSet Type.
// //
@ -66,7 +72,11 @@ type Schema struct {
// SchemaSetFunc is a function that must return a unique ID for the given // SchemaSetFunc is a function that must return a unique ID for the given
// element. This unique ID is used to store the element in a hash. // element. This unique ID is used to store the element in a hash.
type SchemaSetFunc func(a interface{}) int type SchemaSetFunc func(interface{}) int
// SchemaStateFunc is a function used to convert some type to a string
// to be stored in the state.
type SchemaStateFunc func(interface{}) string
func (s *Schema) finalizeDiff( func (s *Schema) finalizeDiff(
d *terraform.ResourceAttrDiff) *terraform.ResourceAttrDiff { d *terraform.ResourceAttrDiff) *terraform.ResourceAttrDiff {
@ -376,8 +386,13 @@ func (m schemaMap) diffString(
schema *Schema, schema *Schema,
diff *terraform.ResourceDiff, diff *terraform.ResourceDiff,
d *ResourceData) error { d *ResourceData) error {
var originalN interface{}
var os, ns string var os, ns string
o, n, _ := d.diffChange(k) o, n, _ := d.diffChange(k)
if schema.StateFunc != nil {
originalN = n
n = schema.StateFunc(n)
}
if err := mapstructure.WeakDecode(o, &os); err != nil { if err := mapstructure.WeakDecode(o, &os); err != nil {
return fmt.Errorf("%s: %s", k, err) return fmt.Errorf("%s: %s", k, err)
} }
@ -386,9 +401,14 @@ func (m schemaMap) diffString(
} }
if os == ns { if os == ns {
// They're the same value, return no diff as long as we're not // They're the same value. If there old value is not blank or we
// computing a new value. // have an ID, then return right away since we're already setup.
if os != "" || !schema.Computed { if os != "" || d.Id() != "" {
return nil
}
// Otherwise, only continue if we're computed
if !schema.Computed {
return nil return nil
} }
} }
@ -401,6 +421,7 @@ func (m schemaMap) diffString(
diff.Attributes[k] = schema.finalizeDiff(&terraform.ResourceAttrDiff{ diff.Attributes[k] = schema.finalizeDiff(&terraform.ResourceAttrDiff{
Old: os, Old: os,
New: ns, New: ns,
NewExtra: originalN,
NewRemoved: removed, NewRemoved: removed,
}) })

View File

@ -76,6 +76,59 @@ func TestSchemaMap_Diff(t *testing.T) {
Err: false, Err: false,
}, },
{
Schema: map[string]*Schema{
"availability_zone": &Schema{
Type: TypeString,
Optional: true,
Computed: true,
ForceNew: true,
},
},
State: &terraform.ResourceState{
ID: "foo",
},
Config: map[string]interface{}{},
Diff: nil,
Err: false,
},
// String with StateFunc
{
Schema: map[string]*Schema{
"availability_zone": &Schema{
Type: TypeString,
Optional: true,
Computed: true,
StateFunc: func(a interface{}) string {
return a.(string) + "!"
},
},
},
State: nil,
Config: map[string]interface{}{
"availability_zone": "foo",
},
Diff: &terraform.ResourceDiff{
Attributes: map[string]*terraform.ResourceAttrDiff{
"availability_zone": &terraform.ResourceAttrDiff{
Old: "",
New: "foo!",
NewExtra: "foo",
},
},
},
Err: false,
},
/* /*
* Int decode * Int decode
*/ */