github.com/handlerbot/terraform@v0.10.0-beta1.0.20180726153736-26b68d98f9cb/builtin/providers/test/resource.go (about) 1 package test 2 3 import ( 4 "fmt" 5 6 "github.com/hashicorp/terraform/helper/schema" 7 ) 8 9 func testResource() *schema.Resource { 10 return &schema.Resource{ 11 Create: testResourceCreate, 12 Read: testResourceRead, 13 Update: testResourceUpdate, 14 Delete: testResourceDelete, 15 16 Importer: &schema.ResourceImporter{ 17 State: schema.ImportStatePassthrough, 18 }, 19 20 Schema: map[string]*schema.Schema{ 21 "required": { 22 Type: schema.TypeString, 23 Required: true, 24 }, 25 "optional": { 26 Type: schema.TypeString, 27 Optional: true, 28 }, 29 "optional_bool": { 30 Type: schema.TypeBool, 31 Optional: true, 32 }, 33 "optional_force_new": { 34 Type: schema.TypeString, 35 Optional: true, 36 ForceNew: true, 37 }, 38 "optional_computed_map": { 39 Type: schema.TypeMap, 40 Optional: true, 41 Computed: true, 42 }, 43 "optional_computed_force_new": { 44 Type: schema.TypeString, 45 Optional: true, 46 Computed: true, 47 ForceNew: true, 48 }, 49 "computed_read_only": { 50 Type: schema.TypeString, 51 Computed: true, 52 }, 53 "computed_from_required": { 54 Type: schema.TypeString, 55 Computed: true, 56 ForceNew: true, 57 }, 58 "computed_read_only_force_new": { 59 Type: schema.TypeString, 60 Computed: true, 61 ForceNew: true, 62 }, 63 "computed_list": { 64 Type: schema.TypeList, 65 Computed: true, 66 Elem: &schema.Schema{ 67 Type: schema.TypeString, 68 }, 69 }, 70 "set": { 71 Type: schema.TypeSet, 72 Optional: true, 73 Elem: &schema.Schema{ 74 Type: schema.TypeString, 75 }, 76 Set: schema.HashString, 77 }, 78 "computed_set": { 79 Type: schema.TypeSet, 80 Computed: true, 81 Elem: &schema.Schema{ 82 Type: schema.TypeString, 83 }, 84 Set: schema.HashString, 85 }, 86 "map": { 87 Type: schema.TypeMap, 88 Optional: true, 89 }, 90 "optional_map": { 91 Type: schema.TypeMap, 92 Optional: true, 93 }, 94 "required_map": { 95 Type: schema.TypeMap, 96 Required: true, 97 }, 98 "map_that_look_like_set": { 99 Type: schema.TypeMap, 100 Optional: true, 101 Elem: &schema.Schema{ 102 Type: schema.TypeString, 103 }, 104 }, 105 "computed_map": { 106 Type: schema.TypeMap, 107 Computed: true, 108 }, 109 "list": { 110 Type: schema.TypeList, 111 Optional: true, 112 Elem: &schema.Schema{ 113 Type: schema.TypeString, 114 }, 115 }, 116 "list_of_map": { 117 Type: schema.TypeList, 118 Optional: true, 119 Elem: &schema.Schema{ 120 Type: schema.TypeMap, 121 Elem: &schema.Schema{ 122 Type: schema.TypeString, 123 }, 124 }, 125 }, 126 }, 127 } 128 } 129 130 func testResourceCreate(d *schema.ResourceData, meta interface{}) error { 131 d.SetId("testId") 132 133 // Required must make it through to Create 134 if _, ok := d.GetOk("required"); !ok { 135 return fmt.Errorf("Missing attribute 'required', but it's required!") 136 } 137 if _, ok := d.GetOk("required_map"); !ok { 138 return fmt.Errorf("Missing attribute 'required_map', but it's required!") 139 } 140 141 d.Set("computed_from_required", d.Get("required")) 142 143 return testResourceRead(d, meta) 144 } 145 146 func testResourceRead(d *schema.ResourceData, meta interface{}) error { 147 d.Set("computed_read_only", "value_from_api") 148 d.Set("computed_read_only_force_new", "value_from_api") 149 if _, ok := d.GetOk("optional_computed_map"); !ok { 150 d.Set("optional_computed_map", map[string]string{}) 151 } 152 d.Set("computed_map", map[string]string{"key1": "value1"}) 153 d.Set("computed_list", []string{"listval1", "listval2"}) 154 d.Set("computed_set", []string{"setval1", "setval2"}) 155 return nil 156 } 157 158 func testResourceUpdate(d *schema.ResourceData, meta interface{}) error { 159 return nil 160 } 161 162 func testResourceDelete(d *schema.ResourceData, meta interface{}) error { 163 d.SetId("") 164 return nil 165 }