terraform/builtin/providers/test/resource_map.go

78 lines
1.9 KiB
Go
Raw Normal View History

2019-01-23 22:33:19 +01:00
package test
import (
"fmt"
2019-08-07 01:58:58 +02:00
"github.com/hashicorp/terraform/configs/hcl2shim"
2019-01-23 22:33:19 +01:00
"github.com/hashicorp/terraform/helper/schema"
)
func testResourceMap() *schema.Resource {
return &schema.Resource{
Create: testResourceMapCreate,
Read: testResourceMapRead,
Update: testResourceMapUpdate,
Delete: testResourceMapDelete,
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
},
"map_of_three": {
Type: schema.TypeMap,
Optional: true,
Elem: &schema.Schema{Type: schema.TypeString},
ValidateFunc: func(v interface{}, _ string) ([]string, []error) {
errs := []error{}
for k, v := range v.(map[string]interface{}) {
if v == hcl2shim.UnknownVariableValue {
errs = append(errs, fmt.Errorf("unknown value in ValidateFunc: %q=%q", k, v))
}
}
return nil, errs
},
2019-01-23 22:33:19 +01:00
},
"map_values": {
Type: schema.TypeMap,
Optional: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
"computed_map": {
Type: schema.TypeMap,
Computed: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
2019-01-23 22:33:19 +01:00
},
}
}
func testResourceMapCreate(d *schema.ResourceData, meta interface{}) error {
// make sure all elements are passed to the map
m := d.Get("map_of_three").(map[string]interface{})
if len(m) != 3 {
return fmt.Errorf("expected 3 map values, got %#v\n", m)
}
d.SetId("testId")
return testResourceMapRead(d, meta)
2019-01-23 22:33:19 +01:00
}
func testResourceMapRead(d *schema.ResourceData, meta interface{}) error {
var computedMap map[string]interface{}
if v, ok := d.GetOk("map_values"); ok {
computedMap = v.(map[string]interface{})
}
d.Set("computed_map", computedMap)
2019-01-23 22:33:19 +01:00
return nil
}
func testResourceMapUpdate(d *schema.ResourceData, meta interface{}) error {
return testResourceMapRead(d, meta)
2019-01-23 22:33:19 +01:00
}
func testResourceMapDelete(d *schema.ResourceData, meta interface{}) error {
d.SetId("")
return nil
}