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"
"strconv"
"strings"
"sync"
"github.com/hashicorp/terraform/terraform"
"github.com/mitchellh/mapstructure"
@ -29,6 +30,7 @@ type ResourceData struct {
setMap map[string]string
newState *terraform.ResourceState
once sync.Once
}
// Get returns the data for the given key, or nil if the key doesn't exist.
@ -93,26 +95,52 @@ func (d *ResourceData) Id() string {
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
// resource is destroyed.
func (d *ResourceData) SetId(v string) {
if d.newState == nil {
d.newState = new(terraform.ResourceState)
}
d.once.Do(d.init)
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
// calls.
func (d *ResourceData) State() *terraform.ResourceState {
var result terraform.ResourceState
result.ID = d.Id()
result.Attributes = d.stateObject("", d.schema)
result.Dependencies = d.Dependencies()
return &result
}
func (d *ResourceData) init() {
var copyState terraform.ResourceState
if d.state != nil {
copyState = *d.state
}
d.newState = &copyState
}
func (d *ResourceData) get(
k 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) {
d := &ResourceData{}
d.SetId("foo")