From 1b85f3a565b136c10cd85a8dcdccf41894f21221 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Mon, 18 Aug 2014 15:41:12 -0700 Subject: [PATCH] helper/schema: SetDependencies --- helper/schema/resource_data.go | 36 +++++++++++++++++++++++++---- helper/schema/resource_data_test.go | 16 +++++++++++++ 2 files changed, 48 insertions(+), 4 deletions(-) diff --git a/helper/schema/resource_data.go b/helper/schema/resource_data.go index 5c30e31ba..5bb09ca5e 100644 --- a/helper/schema/resource_data.go +++ b/helper/schema/resource_data.go @@ -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 = ©State +} + func (d *ResourceData) get( k string, parts []string, diff --git a/helper/schema/resource_data_test.go b/helper/schema/resource_data_test.go index 0111a2ba1..4578d7574 100644 --- a/helper/schema/resource_data_test.go +++ b/helper/schema/resource_data_test.go @@ -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")