From 0bcbccf046fd2e0a96faa61773f68162b6a222be Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Tue, 16 Sep 2014 17:02:05 -0700 Subject: [PATCH] helper/resource: compiles, fails because Context doesn't work, probably --- helper/resource/map.go | 29 ++++++++++++++++------------- helper/resource/resource.go | 16 ++++++++-------- helper/resource/testing.go | 12 +++++++++--- helper/resource/testing_test.go | 11 ++++++----- 4 files changed, 39 insertions(+), 29 deletions(-) diff --git a/helper/resource/map.go b/helper/resource/map.go index 524f33c1c..979791828 100644 --- a/helper/resource/map.go +++ b/helper/resource/map.go @@ -31,12 +31,13 @@ func (m *Map) Validate( // Apply performs a create or update depending on the diff, and calls // the proper function on the matching Resource. func (m *Map) Apply( - s *terraform.ResourceState, + info *terraform.InstanceInfo, + s *terraform.InstanceState, d *terraform.ResourceDiff, - meta interface{}) (*terraform.ResourceState, error) { - r, ok := m.Mapping[s.Type] + meta interface{}) (*terraform.InstanceState, error) { + r, ok := m.Mapping[info.Type] if !ok { - return nil, fmt.Errorf("Unknown resource type: %s", s.Type) + return nil, fmt.Errorf("Unknown resource type: %s", info.Type) } if d.Destroy || d.RequiresNew() { @@ -57,7 +58,7 @@ func (m *Map) Apply( } } - var result *terraform.ResourceState + var result *terraform.InstanceState var err error if s.ID == "" { result, err = r.Create(s, d, meta) @@ -65,7 +66,7 @@ func (m *Map) Apply( if r.Update == nil { return s, fmt.Errorf( "Resource type '%s' doesn't support update", - s.Type) + info.Type) } result, err = r.Update(s, d, meta) @@ -83,12 +84,13 @@ func (m *Map) Apply( // Diff performs a diff on the proper resource type. func (m *Map) Diff( - s *terraform.ResourceState, + info *terraform.InstanceInfo, + s *terraform.InstanceState, c *terraform.ResourceConfig, meta interface{}) (*terraform.ResourceDiff, error) { - r, ok := m.Mapping[s.Type] + r, ok := m.Mapping[info.Type] if !ok { - return nil, fmt.Errorf("Unknown resource type: %s", s.Type) + return nil, fmt.Errorf("Unknown resource type: %s", info.Type) } return r.Diff(s, c, meta) @@ -101,16 +103,17 @@ func (m *Map) Diff( // // An error is returned if the resource isn't registered. func (m *Map) Refresh( - s *terraform.ResourceState, - meta interface{}) (*terraform.ResourceState, error) { + info *terraform.InstanceInfo, + s *terraform.InstanceState, + meta interface{}) (*terraform.InstanceState, error) { // If the resource isn't created, don't refresh. if s.ID == "" { return s, nil } - r, ok := m.Mapping[s.Type] + r, ok := m.Mapping[info.Type] if !ok { - return nil, fmt.Errorf("Unknown resource type: %s", s.Type) + return nil, fmt.Errorf("Unknown resource type: %s", info.Type) } return r.Refresh(s, meta) diff --git a/helper/resource/resource.go b/helper/resource/resource.go index 819ad1769..60a56a31d 100644 --- a/helper/resource/resource.go +++ b/helper/resource/resource.go @@ -17,33 +17,33 @@ type Resource struct { // CreateFunc is a function that creates a resource that didn't previously // exist. type CreateFunc func( - *terraform.ResourceState, + *terraform.InstanceState, *terraform.ResourceDiff, - interface{}) (*terraform.ResourceState, error) + interface{}) (*terraform.InstanceState, error) // DestroyFunc is a function that destroys a resource that previously // exists using the state. type DestroyFunc func( - *terraform.ResourceState, + *terraform.InstanceState, interface{}) error // DiffFunc is a function that performs a diff of a resource. type DiffFunc func( - *terraform.ResourceState, + *terraform.InstanceState, *terraform.ResourceConfig, interface{}) (*terraform.ResourceDiff, error) // RefreshFunc is a function that performs a refresh of a specific type // of resource. type RefreshFunc func( - *terraform.ResourceState, - interface{}) (*terraform.ResourceState, error) + *terraform.InstanceState, + interface{}) (*terraform.InstanceState, error) // UpdateFunc is a function that is called to update a resource that // previously existed. The difference between this and CreateFunc is that // the diff is guaranteed to only contain attributes that don't require // a new resource. type UpdateFunc func( - *terraform.ResourceState, + *terraform.InstanceState, *terraform.ResourceDiff, - interface{}) (*terraform.ResourceState, error) + interface{}) (*terraform.InstanceState, error) diff --git a/helper/resource/testing.go b/helper/resource/testing.go index be09e744d..2b0a2231f 100644 --- a/helper/resource/testing.go +++ b/helper/resource/testing.go @@ -244,18 +244,24 @@ func ComposeTestCheckFunc(fs ...TestCheckFunc) TestCheckFunc { func TestCheckResourceAttr(name, key, value string) TestCheckFunc { return func(s *terraform.State) error { - rs, ok := s.Resources[name] + ms := s.RootModule() + rs, ok := ms.Resources[name] if !ok { return fmt.Errorf("Not found: %s", name) } - if rs.Attributes[key] != value { + is := rs.Primary + if is == nil { + return fmt.Errorf("No primary instance: %s", name) + } + + if is.Attributes[key] != value { return fmt.Errorf( "%s: Attribute '%s' expected %#v, got %#v", name, key, value, - rs.Attributes[key]) + is.Attributes[key]) } return nil diff --git a/helper/resource/testing_test.go b/helper/resource/testing_test.go index 7a096149e..ad00f79cc 100644 --- a/helper/resource/testing_test.go +++ b/helper/resource/testing_test.go @@ -18,7 +18,7 @@ func init() { func TestTest(t *testing.T) { mp := testProvider() - mp.ApplyReturn = &terraform.ResourceState{ + mp.ApplyReturn = &terraform.InstanceState{ ID: "foo", } @@ -33,13 +33,14 @@ func TestTest(t *testing.T) { checkStepFn := func(s *terraform.State) error { checkStep = true - rs, ok := s.Resources["test_instance.foo"] + rs, ok := s.RootModule().Resources["test_instance.foo"] if !ok { t.Error("test_instance.foo is not present") return nil } - if rs.ID != "foo" { - t.Errorf("bad check ID: %s", rs.ID) + is := rs.Primary + if is.ID != "foo" { + t.Errorf("bad check ID: %s", is.ID) } return nil @@ -120,7 +121,7 @@ func TestTest_preCheck(t *testing.T) { func TestTest_stepError(t *testing.T) { mp := testProvider() - mp.ApplyReturn = &terraform.ResourceState{ + mp.ApplyReturn = &terraform.InstanceState{ ID: "foo", }