Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] core: Ensure that DataSourcesMap is handled during apply w/destroy #6913

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion helper/schema/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,15 @@ 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)
}
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