Skip to content

Commit

Permalink
core: Ensure that DataSourcesMap is handled during apply w/destroy
Browse files Browse the repository at this point in the history
During accpeptance tests of some of the first data sources (see
hashicorp#6881 and hashicorp#6911),
"unknown resource type" errors were coming up. It looks like
DataSourcesMap was not being taken into account in
*schema.Provider.Apply(), causing this error.

Also, added a skip in *schema.Resource.Apply() if
*schema.Resource.Destroy() is not defined, as data sources do not define
this, fixing the first error resulted in a nil pointer dereference.
  • Loading branch information
Chris Marchesi committed May 28, 2016
1 parent 10cc8b8 commit 4695251
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 6 deletions.
14 changes: 12 additions & 2 deletions helper/schema/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,9 +168,19 @@ func (p *Provider) Apply(
info *terraform.InstanceInfo,
s *terraform.InstanceState,
d *terraform.InstanceDiff) (*terraform.InstanceState, error) {
r, ok := p.ResourcesMap[info.Type]
m := p.ResourcesMap
for k, v := range p.DataSourcesMap {
if _, ok := m[k]; ok {
panic(fmt.Errorf("Data source %s is also a resource. This is a bug and should be reported.", k))
}
m[k] = v
}

r, ok := m[info.Type]
if !ok {
return nil, fmt.Errorf("unknown resource type: %s", info.Type)
if !ok {
return nil, fmt.Errorf("unknown resource type: %s", info.Type)
}
}

return r.Apply(s, d, p.meta)
Expand Down
9 changes: 5 additions & 4 deletions helper/schema/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,11 +133,12 @@ func (r *Resource) Apply(

if d.Destroy || d.RequiresNew() {
if s.ID != "" {
// Destroy the resource since it is created
if err := r.Delete(data, meta); err != nil {
return r.recordCurrentSchemaVersion(data.State()), err
if r.Delete != nil {
// Destroy the resource since it is created
if err := r.Delete(data, meta); err != nil {
return r.recordCurrentSchemaVersion(data.State()), err
}
}

// Make sure the ID is gone.
data.SetId("")
}
Expand Down
30 changes: 30 additions & 0 deletions helper/schema/resource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,36 @@ func TestResourceApply_destroy(t *testing.T) {
}
}

func TestResourceApply_destroyDataSource(t *testing.T) {
// data sources do not have a destroy function, so we need to test that a
// destroy operation will properly skip over it if it is not defined.
r := &Resource{
Schema: map[string]*Schema{
"foo": &Schema{
Type: TypeInt,
Optional: true,
},
},
}

s := &terraform.InstanceState{
ID: "bar",
}

d := &terraform.InstanceDiff{
Destroy: true,
}

actual, err := r.Apply(s, d, nil)
if err != nil {
t.Fatalf("err: %s", err)
}

if actual != nil {
t.Fatalf("bad: %#v", actual)
}
}

func TestResourceApply_destroyCreate(t *testing.T) {
r := &Resource{
Schema: map[string]*Schema{
Expand Down

0 comments on commit 4695251

Please sign in to comment.