terraform/builtin/providers/test/resource_computed_set.go

75 lines
1.6 KiB
Go

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
}