helper/schema: SetDependencies

This commit is contained in:
Mitchell Hashimoto 2014-08-18 15:41:12 -07:00
parent 4e1d8869fe
commit 1b85f3a565
2 changed files with 48 additions and 4 deletions

View File

@ -5,6 +5,7 @@ import (
"reflect" "reflect"
"strconv" "strconv"
"strings" "strings"
"sync"
"github.com/hashicorp/terraform/terraform" "github.com/hashicorp/terraform/terraform"
"github.com/mitchellh/mapstructure" "github.com/mitchellh/mapstructure"
@ -29,6 +30,7 @@ type ResourceData struct {
setMap map[string]string setMap map[string]string
newState *terraform.ResourceState newState *terraform.ResourceState
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.
@ -93,14 +95,30 @@ func (d *ResourceData) Id() string {
return result return result
} }
// Dependencies returns the dependencies in this state.
func (d *ResourceData) Dependencies() []terraform.ResourceDependency {
if d.newState != nil {
return d.newState.Dependencies
}
if d.state != nil {
return d.state.Dependencies
}
return nil
}
// SetId sets the ID of the resource. If the value is blank, then the // SetId sets the ID of the resource. If the value is blank, then the
// resource is destroyed. // resource is destroyed.
func (d *ResourceData) SetId(v string) { func (d *ResourceData) SetId(v string) {
if d.newState == nil { d.once.Do(d.init)
d.newState = new(terraform.ResourceState) d.newState.ID = v
} }
d.newState.ID = v // SetDependencies sets the dependencies of a resource.
func (d *ResourceData) SetDependencies(ds []terraform.ResourceDependency) {
d.once.Do(d.init)
d.newState.Dependencies = ds
} }
// State returns the new ResourceState after the diff and any Set // State returns the new ResourceState after the diff and any Set
@ -109,10 +127,20 @@ func (d *ResourceData) State() *terraform.ResourceState {
var result terraform.ResourceState var result terraform.ResourceState
result.ID = d.Id() result.ID = d.Id()
result.Attributes = d.stateObject("", d.schema) result.Attributes = d.stateObject("", d.schema)
result.Dependencies = d.Dependencies()
return &result return &result
} }
func (d *ResourceData) init() {
var copyState terraform.ResourceState
if d.state != nil {
copyState = *d.state
}
d.newState = &copyState
}
func (d *ResourceData) get( func (d *ResourceData) get(
k string, k string,
parts []string, parts []string,

View File

@ -1192,6 +1192,22 @@ func TestResourceDataState(t *testing.T) {
} }
} }
func TestResourceDataSetDependencies(t *testing.T) {
d := &ResourceData{}
d.SetDependencies([]terraform.ResourceDependency{
terraform.ResourceDependency{ID: "foo"},
})
expected := []terraform.ResourceDependency{
terraform.ResourceDependency{ID: "foo"},
}
actual := d.State()
if !reflect.DeepEqual(actual.Dependencies, expected) {
t.Fatalf("bad: %#v", actual)
}
}
func TestResourceDataSetId(t *testing.T) { func TestResourceDataSetId(t *testing.T) {
d := &ResourceData{} d := &ResourceData{}
d.SetId("foo") d.SetId("foo")