helper/resource: compiles, fails because Context doesn't work, probably

This commit is contained in:
Mitchell Hashimoto 2014-09-16 17:02:05 -07:00
parent 90dd00ee3c
commit 0bcbccf046
4 changed files with 39 additions and 29 deletions

View File

@ -31,12 +31,13 @@ func (m *Map) Validate(
// Apply performs a create or update depending on the diff, and calls // Apply performs a create or update depending on the diff, and calls
// the proper function on the matching Resource. // the proper function on the matching Resource.
func (m *Map) Apply( func (m *Map) Apply(
s *terraform.ResourceState, info *terraform.InstanceInfo,
s *terraform.InstanceState,
d *terraform.ResourceDiff, d *terraform.ResourceDiff,
meta interface{}) (*terraform.ResourceState, error) { meta interface{}) (*terraform.InstanceState, error) {
r, ok := m.Mapping[s.Type] r, ok := m.Mapping[info.Type]
if !ok { 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() { if d.Destroy || d.RequiresNew() {
@ -57,7 +58,7 @@ func (m *Map) Apply(
} }
} }
var result *terraform.ResourceState var result *terraform.InstanceState
var err error var err error
if s.ID == "" { if s.ID == "" {
result, err = r.Create(s, d, meta) result, err = r.Create(s, d, meta)
@ -65,7 +66,7 @@ func (m *Map) Apply(
if r.Update == nil { if r.Update == nil {
return s, fmt.Errorf( return s, fmt.Errorf(
"Resource type '%s' doesn't support update", "Resource type '%s' doesn't support update",
s.Type) info.Type)
} }
result, err = r.Update(s, d, meta) result, err = r.Update(s, d, meta)
@ -83,12 +84,13 @@ func (m *Map) Apply(
// Diff performs a diff on the proper resource type. // Diff performs a diff on the proper resource type.
func (m *Map) Diff( func (m *Map) Diff(
s *terraform.ResourceState, info *terraform.InstanceInfo,
s *terraform.InstanceState,
c *terraform.ResourceConfig, c *terraform.ResourceConfig,
meta interface{}) (*terraform.ResourceDiff, error) { meta interface{}) (*terraform.ResourceDiff, error) {
r, ok := m.Mapping[s.Type] r, ok := m.Mapping[info.Type]
if !ok { 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) return r.Diff(s, c, meta)
@ -101,16 +103,17 @@ func (m *Map) Diff(
// //
// An error is returned if the resource isn't registered. // An error is returned if the resource isn't registered.
func (m *Map) Refresh( func (m *Map) Refresh(
s *terraform.ResourceState, info *terraform.InstanceInfo,
meta interface{}) (*terraform.ResourceState, error) { s *terraform.InstanceState,
meta interface{}) (*terraform.InstanceState, error) {
// If the resource isn't created, don't refresh. // If the resource isn't created, don't refresh.
if s.ID == "" { if s.ID == "" {
return s, nil return s, nil
} }
r, ok := m.Mapping[s.Type] r, ok := m.Mapping[info.Type]
if !ok { 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) return r.Refresh(s, meta)

View File

@ -17,33 +17,33 @@ type Resource struct {
// CreateFunc is a function that creates a resource that didn't previously // CreateFunc is a function that creates a resource that didn't previously
// exist. // exist.
type CreateFunc func( type CreateFunc func(
*terraform.ResourceState, *terraform.InstanceState,
*terraform.ResourceDiff, *terraform.ResourceDiff,
interface{}) (*terraform.ResourceState, error) interface{}) (*terraform.InstanceState, error)
// DestroyFunc is a function that destroys a resource that previously // DestroyFunc is a function that destroys a resource that previously
// exists using the state. // exists using the state.
type DestroyFunc func( type DestroyFunc func(
*terraform.ResourceState, *terraform.InstanceState,
interface{}) error interface{}) error
// DiffFunc is a function that performs a diff of a resource. // DiffFunc is a function that performs a diff of a resource.
type DiffFunc func( type DiffFunc func(
*terraform.ResourceState, *terraform.InstanceState,
*terraform.ResourceConfig, *terraform.ResourceConfig,
interface{}) (*terraform.ResourceDiff, error) interface{}) (*terraform.ResourceDiff, error)
// RefreshFunc is a function that performs a refresh of a specific type // RefreshFunc is a function that performs a refresh of a specific type
// of resource. // of resource.
type RefreshFunc func( type RefreshFunc func(
*terraform.ResourceState, *terraform.InstanceState,
interface{}) (*terraform.ResourceState, error) interface{}) (*terraform.InstanceState, error)
// UpdateFunc is a function that is called to update a resource that // UpdateFunc is a function that is called to update a resource that
// previously existed. The difference between this and CreateFunc is that // previously existed. The difference between this and CreateFunc is that
// the diff is guaranteed to only contain attributes that don't require // the diff is guaranteed to only contain attributes that don't require
// a new resource. // a new resource.
type UpdateFunc func( type UpdateFunc func(
*terraform.ResourceState, *terraform.InstanceState,
*terraform.ResourceDiff, *terraform.ResourceDiff,
interface{}) (*terraform.ResourceState, error) interface{}) (*terraform.InstanceState, error)

View File

@ -244,18 +244,24 @@ func ComposeTestCheckFunc(fs ...TestCheckFunc) TestCheckFunc {
func TestCheckResourceAttr(name, key, value string) TestCheckFunc { func TestCheckResourceAttr(name, key, value string) TestCheckFunc {
return func(s *terraform.State) error { return func(s *terraform.State) error {
rs, ok := s.Resources[name] ms := s.RootModule()
rs, ok := ms.Resources[name]
if !ok { if !ok {
return fmt.Errorf("Not found: %s", name) 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( return fmt.Errorf(
"%s: Attribute '%s' expected %#v, got %#v", "%s: Attribute '%s' expected %#v, got %#v",
name, name,
key, key,
value, value,
rs.Attributes[key]) is.Attributes[key])
} }
return nil return nil

View File

@ -18,7 +18,7 @@ func init() {
func TestTest(t *testing.T) { func TestTest(t *testing.T) {
mp := testProvider() mp := testProvider()
mp.ApplyReturn = &terraform.ResourceState{ mp.ApplyReturn = &terraform.InstanceState{
ID: "foo", ID: "foo",
} }
@ -33,13 +33,14 @@ func TestTest(t *testing.T) {
checkStepFn := func(s *terraform.State) error { checkStepFn := func(s *terraform.State) error {
checkStep = true checkStep = true
rs, ok := s.Resources["test_instance.foo"] rs, ok := s.RootModule().Resources["test_instance.foo"]
if !ok { if !ok {
t.Error("test_instance.foo is not present") t.Error("test_instance.foo is not present")
return nil return nil
} }
if rs.ID != "foo" { is := rs.Primary
t.Errorf("bad check ID: %s", rs.ID) if is.ID != "foo" {
t.Errorf("bad check ID: %s", is.ID)
} }
return nil return nil
@ -120,7 +121,7 @@ func TestTest_preCheck(t *testing.T) {
func TestTest_stepError(t *testing.T) { func TestTest_stepError(t *testing.T) {
mp := testProvider() mp := testProvider()
mp.ApplyReturn = &terraform.ResourceState{ mp.ApplyReturn = &terraform.InstanceState{
ID: "foo", ID: "foo",
} }