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},
|
2019-05-24 22:13:29 +02:00
|
|
|
ValidateFunc: func(v interface{}, _ string) ([]string, []error) {
|
|
|
|
errs := []error{}
|
|
|
|
for k, v := range v.(map[string]interface{}) {
|
2019-07-18 04:41:24 +02:00
|
|
|
if v == hcl2shim.UnknownVariableValue {
|
2019-05-24 22:13:29 +02:00
|
|
|
errs = append(errs, fmt.Errorf("unknown value in ValidateFunc: %q=%q", k, v))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil, errs
|
|
|
|
},
|
2019-01-23 22:33:19 +01:00
|
|
|
},
|
2019-04-23 18:31:19 +02: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")
|
2019-04-23 18:31:19 +02:00
|
|
|
return testResourceMapRead(d, meta)
|
2019-01-23 22:33:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func testResourceMapRead(d *schema.ResourceData, meta interface{}) error {
|
2019-04-23 18:31:19 +02:00
|
|
|
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 {
|
2019-04-23 18:31:19 +02:00
|
|
|
return testResourceMapRead(d, meta)
|
2019-01-23 22:33:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func testResourceMapDelete(d *schema.ResourceData, meta interface{}) error {
|
|
|
|
d.SetId("")
|
|
|
|
return nil
|
|
|
|
}
|