add computed set test with CustomizeDiff

This commit is contained in:
James Bardin 2019-02-01 17:07:56 -05:00
parent 4c99864dad
commit 89c1ba099f
3 changed files with 127 additions and 0 deletions

View File

@ -31,6 +31,7 @@ func Provider() terraform.ResourceProvider {
"test_resource_defaults": testResourceDefaults(),
"test_resource_list": testResourceList(),
"test_resource_map": testResourceMap(),
"test_resource_computed_set": testResourceComputedSet(),
},
DataSourcesMap: map[string]*schema.Resource{
"test_data_source": testDataSource(),

View File

@ -0,0 +1,74 @@
package test
import (
"fmt"
"math/rand"
"github.com/hashicorp/terraform/helper/schema"
)
func testResourceComputedSet() *schema.Resource {
return &schema.Resource{
Create: testResourceComputedSetCreate,
Read: testResourceComputedSetRead,
Delete: testResourceComputedSetDelete,
Update: testResourceComputedSetUpdate,
CustomizeDiff: func(d *schema.ResourceDiff, _ interface{}) error {
o, n := d.GetChange("set_count")
if o != n {
d.SetNewComputed("string_set")
}
return nil
},
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},
Schema: map[string]*schema.Schema{
"set_count": {
Type: schema.TypeInt,
Optional: true,
},
"string_set": {
Type: schema.TypeSet,
Computed: true,
Elem: &schema.Schema{
Type: schema.TypeString,
},
Set: schema.HashString,
},
},
}
}
func testResourceComputedSetCreate(d *schema.ResourceData, meta interface{}) error {
d.SetId(fmt.Sprintf("%x", rand.Int63()))
return testResourceComputedSetRead(d, meta)
}
func testResourceComputedSetRead(d *schema.ResourceData, meta interface{}) error {
count := 3
v, ok := d.GetOk("set_count")
if ok {
count = v.(int)
}
var set []interface{}
for i := 0; i < count; i++ {
set = append(set, fmt.Sprintf("%d", i))
}
d.Set("string_set", schema.NewSet(schema.HashString, set))
return nil
}
func testResourceComputedSetUpdate(d *schema.ResourceData, meta interface{}) error {
return testResourceComputedSetRead(d, meta)
}
func testResourceComputedSetDelete(d *schema.ResourceData, meta interface{}) error {
d.SetId("")
return nil
}

View File

@ -0,0 +1,52 @@
package test
import (
"strings"
"testing"
"github.com/hashicorp/terraform/helper/resource"
)
func TestResourceComputedSet_update(t *testing.T) {
resource.UnitTest(t, resource.TestCase{
Providers: testAccProviders,
CheckDestroy: testAccCheckResourceDestroy,
Steps: []resource.TestStep{
resource.TestStep{
Config: strings.TrimSpace(`
resource "test_resource_computed_set" "foo" {
}
`),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(
"test_resource_computed_set.foo", "string_set.#", "3",
),
),
},
resource.TestStep{
Config: strings.TrimSpace(`
resource "test_resource_computed_set" "foo" {
set_count = 5
}
`),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(
"test_resource_computed_set.foo", "string_set.#", "5",
),
),
},
resource.TestStep{
Config: strings.TrimSpace(`
resource "test_resource_computed_set" "foo" {
set_count = 2
}
`),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(
"test_resource_computed_set.foo", "string_set.#", "2",
),
),
},
},
})
}