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

don't apply unchanged attributes from legacy diffs #19253

Merged
merged 2 commits into from
Nov 2, 2018
Merged
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
1 change: 1 addition & 0 deletions builtin/providers/test/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ func Provider() terraform.ResourceProvider {
"test_resource_timeout": testResourceTimeout(),
"test_resource_diff_suppress": testResourceDiffSuppress(),
"test_resource_force_new": testResourceForceNew(),
"test_resource_nested": testResourceNested(),
},
DataSourcesMap: map[string]*schema.Resource{
"test_data_source": testDataSource(),
Expand Down
61 changes: 61 additions & 0 deletions builtin/providers/test/resource_nested.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package test

import (
"fmt"
"math/rand"

"github.com/hashicorp/terraform/helper/schema"
)

func testResourceNested() *schema.Resource {
return &schema.Resource{
Create: testResourceNestedCreate,
Read: testResourceNestedRead,
Delete: testResourceNestedDelete,

Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},

Schema: map[string]*schema.Schema{
"optional": {
Type: schema.TypeBool,
Optional: true,
ForceNew: true,
},
"nested": {
Type: schema.TypeSet,
Optional: true,
ForceNew: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"string": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
},
"optional": {
Type: schema.TypeBool,
Optional: true,
ForceNew: true,
},
},
},
},
},
}
}

func testResourceNestedCreate(d *schema.ResourceData, meta interface{}) error {
d.SetId(fmt.Sprintf("%x", rand.Int63()))
return testResourceNestedRead(d, meta)
}

func testResourceNestedRead(d *schema.ResourceData, meta interface{}) error {
return nil
}

func testResourceNestedDelete(d *schema.ResourceData, meta interface{}) error {
d.SetId("")
return nil
}
104 changes: 104 additions & 0 deletions builtin/providers/test/resource_nested_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
package test

import (
"errors"
"strings"
"testing"

"github.com/hashicorp/terraform/addrs"
"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/terraform"
)

func TestResourceNested_basic(t *testing.T) {
resource.UnitTest(t, resource.TestCase{
Providers: testAccProviders,
CheckDestroy: testAccCheckResourceDestroy,
Steps: []resource.TestStep{
resource.TestStep{
Config: strings.TrimSpace(`
resource "test_resource_nested" "foo" {
nested {
string = "val"
}
}
`),
},
},
})
}

func TestResourceNested_addRemove(t *testing.T) {
var id string
checkFunc := func(s *terraform.State) error {
root := s.ModuleByPath(addrs.RootModuleInstance)
res := root.Resources["test_resource_nested.foo"]
if res.Primary.ID == id {
return errors.New("expected new resource")
}
id = res.Primary.ID
return nil
}
resource.UnitTest(t, resource.TestCase{
Providers: testAccProviders,
CheckDestroy: testAccCheckResourceDestroy,
Steps: []resource.TestStep{
resource.TestStep{
Config: strings.TrimSpace(`
resource "test_resource_nested" "foo" {
}
`),
Check: checkFunc,
},
resource.TestStep{
Config: strings.TrimSpace(`
resource "test_resource_nested" "foo" {
nested {
string = "val"
}
}
`),
Check: checkFunc,
},
resource.TestStep{
Config: strings.TrimSpace(`
resource "test_resource_nested" "foo" {
optional = true
nested {
string = "val"
}
}
`),
Check: checkFunc,
},
resource.TestStep{
Config: strings.TrimSpace(`
resource "test_resource_nested" "foo" {
nested {
string = "val"
}
}
`),
Check: checkFunc,
},
resource.TestStep{
Config: strings.TrimSpace(`
resource "test_resource_nested" "foo" {
nested {
string = "val"
optional = true
}
}
`),
Check: checkFunc,
},
resource.TestStep{
Config: strings.TrimSpace(`
resource "test_resource_nested" "foo" {
}
`),
Check: checkFunc,
},
},
})
}
5 changes: 5 additions & 0 deletions terraform/diff.go
Original file line number Diff line number Diff line change
Expand Up @@ -457,6 +457,11 @@ func (d *InstanceDiff) ApplyToValue(base cty.Value, schema *configschema.Block)
continue
}

// sometimes helper/schema gives us values that aren't really a diff
if diff.Old == diff.New {
continue
}

attrs[attr] = diff.New
}

Expand Down