|
| 1 | +// ---------------------------------------------------------------------------- |
| 2 | +// |
| 3 | +// *** AUTO GENERATED CODE *** AUTO GENERATED CODE *** |
| 4 | +// |
| 5 | +// ---------------------------------------------------------------------------- |
| 6 | +// |
| 7 | +// This file is automatically generated by Magic Modules and manual |
| 8 | +// changes will be clobbered when the file is regenerated. |
| 9 | +// |
| 10 | +// Please read more about how to change this file in |
| 11 | +// .github/CONTRIBUTING.md. |
| 12 | +// |
| 13 | +// ---------------------------------------------------------------------------- |
| 14 | + |
| 15 | +package google |
| 16 | + |
| 17 | +import ( |
| 18 | + "fmt" |
| 19 | + "log" |
| 20 | + "reflect" |
| 21 | + |
| 22 | + "github.com/hashicorp/terraform/helper/schema" |
| 23 | +) |
| 24 | + |
| 25 | +func resourceMonitoringGroup() *schema.Resource { |
| 26 | + return &schema.Resource{ |
| 27 | + Create: resourceMonitoringGroupCreate, |
| 28 | + Read: resourceMonitoringGroupRead, |
| 29 | + Update: resourceMonitoringGroupUpdate, |
| 30 | + Delete: resourceMonitoringGroupDelete, |
| 31 | + |
| 32 | + Importer: &schema.ResourceImporter{ |
| 33 | + State: resourceMonitoringGroupImport, |
| 34 | + }, |
| 35 | + |
| 36 | + Schema: map[string]*schema.Schema{ |
| 37 | + "display_name": { |
| 38 | + Type: schema.TypeString, |
| 39 | + Required: true, |
| 40 | + }, |
| 41 | + "filter": { |
| 42 | + Type: schema.TypeString, |
| 43 | + Required: true, |
| 44 | + }, |
| 45 | + "is_cluster": { |
| 46 | + Type: schema.TypeBool, |
| 47 | + Optional: true, |
| 48 | + }, |
| 49 | + "parent_name": { |
| 50 | + Type: schema.TypeString, |
| 51 | + Optional: true, |
| 52 | + DiffSuppressFunc: compareSelfLinkRelativePaths, |
| 53 | + }, |
| 54 | + "name": { |
| 55 | + Type: schema.TypeString, |
| 56 | + Computed: true, |
| 57 | + }, |
| 58 | + "project": { |
| 59 | + Type: schema.TypeString, |
| 60 | + Optional: true, |
| 61 | + Computed: true, |
| 62 | + ForceNew: true, |
| 63 | + }, |
| 64 | + }, |
| 65 | + } |
| 66 | +} |
| 67 | + |
| 68 | +func resourceMonitoringGroupCreate(d *schema.ResourceData, meta interface{}) error { |
| 69 | + config := meta.(*Config) |
| 70 | + |
| 71 | + obj := make(map[string]interface{}) |
| 72 | + parentNameProp, err := expandMonitoringGroupParentName(d.Get("parent_name"), d, config) |
| 73 | + if err != nil { |
| 74 | + return err |
| 75 | + } else if v, ok := d.GetOkExists("parent_name"); !isEmptyValue(reflect.ValueOf(parentNameProp)) && (ok || !reflect.DeepEqual(v, parentNameProp)) { |
| 76 | + obj["parentName"] = parentNameProp |
| 77 | + } |
| 78 | + isClusterProp, err := expandMonitoringGroupIsCluster(d.Get("is_cluster"), d, config) |
| 79 | + if err != nil { |
| 80 | + return err |
| 81 | + } else if v, ok := d.GetOkExists("is_cluster"); !isEmptyValue(reflect.ValueOf(isClusterProp)) && (ok || !reflect.DeepEqual(v, isClusterProp)) { |
| 82 | + obj["isCluster"] = isClusterProp |
| 83 | + } |
| 84 | + displayNameProp, err := expandMonitoringGroupDisplayName(d.Get("display_name"), d, config) |
| 85 | + if err != nil { |
| 86 | + return err |
| 87 | + } else if v, ok := d.GetOkExists("display_name"); !isEmptyValue(reflect.ValueOf(displayNameProp)) && (ok || !reflect.DeepEqual(v, displayNameProp)) { |
| 88 | + obj["displayName"] = displayNameProp |
| 89 | + } |
| 90 | + filterProp, err := expandMonitoringGroupFilter(d.Get("filter"), d, config) |
| 91 | + if err != nil { |
| 92 | + return err |
| 93 | + } else if v, ok := d.GetOkExists("filter"); !isEmptyValue(reflect.ValueOf(filterProp)) && (ok || !reflect.DeepEqual(v, filterProp)) { |
| 94 | + obj["filter"] = filterProp |
| 95 | + } |
| 96 | + |
| 97 | + lockName, err := replaceVars(d, config, "stackdriver/groups/{{project}}") |
| 98 | + if err != nil { |
| 99 | + return err |
| 100 | + } |
| 101 | + mutexKV.Lock(lockName) |
| 102 | + defer mutexKV.Unlock(lockName) |
| 103 | + |
| 104 | + url, err := replaceVars(d, config, "https://monitoring.googleapis.com/v3/projects/{{project}}/groups") |
| 105 | + if err != nil { |
| 106 | + return err |
| 107 | + } |
| 108 | + |
| 109 | + log.Printf("[DEBUG] Creating new Group: %#v", obj) |
| 110 | + res, err := sendRequest(config, "POST", url, obj) |
| 111 | + if err != nil { |
| 112 | + return fmt.Errorf("Error creating Group: %s", err) |
| 113 | + } |
| 114 | + |
| 115 | + // Store the ID now |
| 116 | + id, err := replaceVars(d, config, "{{name}}") |
| 117 | + if err != nil { |
| 118 | + return fmt.Errorf("Error constructing id: %s", err) |
| 119 | + } |
| 120 | + d.SetId(id) |
| 121 | + |
| 122 | + log.Printf("[DEBUG] Finished creating Group %q: %#v", d.Id(), res) |
| 123 | + |
| 124 | + // `name` is autogenerated from the api so needs to be set post-create |
| 125 | + name, ok := res["name"] |
| 126 | + if !ok { |
| 127 | + return fmt.Errorf("Create response didn't contain critical fields. Create may not have succeeded.") |
| 128 | + } |
| 129 | + d.Set("name", name.(string)) |
| 130 | + d.SetId(name.(string)) |
| 131 | + |
| 132 | + return resourceMonitoringGroupRead(d, meta) |
| 133 | +} |
| 134 | + |
| 135 | +func resourceMonitoringGroupRead(d *schema.ResourceData, meta interface{}) error { |
| 136 | + config := meta.(*Config) |
| 137 | + |
| 138 | + url, err := replaceVars(d, config, "https://monitoring.googleapis.com/v3/{{name}}") |
| 139 | + if err != nil { |
| 140 | + return err |
| 141 | + } |
| 142 | + |
| 143 | + res, err := sendRequest(config, "GET", url, nil) |
| 144 | + if err != nil { |
| 145 | + return handleNotFoundError(err, d, fmt.Sprintf("MonitoringGroup %q", d.Id())) |
| 146 | + } |
| 147 | + |
| 148 | + if err := d.Set("parent_name", flattenMonitoringGroupParentName(res["parentName"])); err != nil { |
| 149 | + return fmt.Errorf("Error reading Group: %s", err) |
| 150 | + } |
| 151 | + if err := d.Set("name", flattenMonitoringGroupName(res["name"])); err != nil { |
| 152 | + return fmt.Errorf("Error reading Group: %s", err) |
| 153 | + } |
| 154 | + if err := d.Set("is_cluster", flattenMonitoringGroupIsCluster(res["isCluster"])); err != nil { |
| 155 | + return fmt.Errorf("Error reading Group: %s", err) |
| 156 | + } |
| 157 | + if err := d.Set("display_name", flattenMonitoringGroupDisplayName(res["displayName"])); err != nil { |
| 158 | + return fmt.Errorf("Error reading Group: %s", err) |
| 159 | + } |
| 160 | + if err := d.Set("filter", flattenMonitoringGroupFilter(res["filter"])); err != nil { |
| 161 | + return fmt.Errorf("Error reading Group: %s", err) |
| 162 | + } |
| 163 | + project, err := getProject(d, config) |
| 164 | + if err != nil { |
| 165 | + return err |
| 166 | + } |
| 167 | + if err := d.Set("project", project); err != nil { |
| 168 | + return fmt.Errorf("Error reading Group: %s", err) |
| 169 | + } |
| 170 | + |
| 171 | + return nil |
| 172 | +} |
| 173 | + |
| 174 | +func resourceMonitoringGroupUpdate(d *schema.ResourceData, meta interface{}) error { |
| 175 | + config := meta.(*Config) |
| 176 | + |
| 177 | + obj := make(map[string]interface{}) |
| 178 | + parentNameProp, err := expandMonitoringGroupParentName(d.Get("parent_name"), d, config) |
| 179 | + if err != nil { |
| 180 | + return err |
| 181 | + } else if v, ok := d.GetOkExists("parent_name"); !isEmptyValue(reflect.ValueOf(v)) && (ok || !reflect.DeepEqual(v, parentNameProp)) { |
| 182 | + obj["parentName"] = parentNameProp |
| 183 | + } |
| 184 | + isClusterProp, err := expandMonitoringGroupIsCluster(d.Get("is_cluster"), d, config) |
| 185 | + if err != nil { |
| 186 | + return err |
| 187 | + } else if v, ok := d.GetOkExists("is_cluster"); !isEmptyValue(reflect.ValueOf(v)) && (ok || !reflect.DeepEqual(v, isClusterProp)) { |
| 188 | + obj["isCluster"] = isClusterProp |
| 189 | + } |
| 190 | + displayNameProp, err := expandMonitoringGroupDisplayName(d.Get("display_name"), d, config) |
| 191 | + if err != nil { |
| 192 | + return err |
| 193 | + } else if v, ok := d.GetOkExists("display_name"); !isEmptyValue(reflect.ValueOf(v)) && (ok || !reflect.DeepEqual(v, displayNameProp)) { |
| 194 | + obj["displayName"] = displayNameProp |
| 195 | + } |
| 196 | + filterProp, err := expandMonitoringGroupFilter(d.Get("filter"), d, config) |
| 197 | + if err != nil { |
| 198 | + return err |
| 199 | + } else if v, ok := d.GetOkExists("filter"); !isEmptyValue(reflect.ValueOf(v)) && (ok || !reflect.DeepEqual(v, filterProp)) { |
| 200 | + obj["filter"] = filterProp |
| 201 | + } |
| 202 | + |
| 203 | + lockName, err := replaceVars(d, config, "stackdriver/groups/{{project}}") |
| 204 | + if err != nil { |
| 205 | + return err |
| 206 | + } |
| 207 | + mutexKV.Lock(lockName) |
| 208 | + defer mutexKV.Unlock(lockName) |
| 209 | + |
| 210 | + url, err := replaceVars(d, config, "https://monitoring.googleapis.com/v3/{{name}}") |
| 211 | + if err != nil { |
| 212 | + return err |
| 213 | + } |
| 214 | + |
| 215 | + log.Printf("[DEBUG] Updating Group %q: %#v", d.Id(), obj) |
| 216 | + _, err = sendRequest(config, "PUT", url, obj) |
| 217 | + |
| 218 | + if err != nil { |
| 219 | + return fmt.Errorf("Error updating Group %q: %s", d.Id(), err) |
| 220 | + } |
| 221 | + |
| 222 | + return resourceMonitoringGroupRead(d, meta) |
| 223 | +} |
| 224 | + |
| 225 | +func resourceMonitoringGroupDelete(d *schema.ResourceData, meta interface{}) error { |
| 226 | + config := meta.(*Config) |
| 227 | + |
| 228 | + lockName, err := replaceVars(d, config, "stackdriver/groups/{{project}}") |
| 229 | + if err != nil { |
| 230 | + return err |
| 231 | + } |
| 232 | + mutexKV.Lock(lockName) |
| 233 | + defer mutexKV.Unlock(lockName) |
| 234 | + |
| 235 | + url, err := replaceVars(d, config, "https://monitoring.googleapis.com/v3/{{name}}") |
| 236 | + if err != nil { |
| 237 | + return err |
| 238 | + } |
| 239 | + |
| 240 | + var obj map[string]interface{} |
| 241 | + log.Printf("[DEBUG] Deleting Group %q", d.Id()) |
| 242 | + res, err := sendRequest(config, "DELETE", url, obj) |
| 243 | + if err != nil { |
| 244 | + return handleNotFoundError(err, d, "Group") |
| 245 | + } |
| 246 | + |
| 247 | + log.Printf("[DEBUG] Finished deleting Group %q: %#v", d.Id(), res) |
| 248 | + return nil |
| 249 | +} |
| 250 | + |
| 251 | +func resourceMonitoringGroupImport(d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) { |
| 252 | + |
| 253 | + config := meta.(*Config) |
| 254 | + |
| 255 | + // current import_formats can't import id's with forward slashes in them. |
| 256 | + parseImportId([]string{"(?P<name>.+)"}, d, config) |
| 257 | + |
| 258 | + return []*schema.ResourceData{d}, nil |
| 259 | +} |
| 260 | + |
| 261 | +func flattenMonitoringGroupParentName(v interface{}) interface{} { |
| 262 | + return v |
| 263 | +} |
| 264 | + |
| 265 | +func flattenMonitoringGroupName(v interface{}) interface{} { |
| 266 | + return v |
| 267 | +} |
| 268 | + |
| 269 | +func flattenMonitoringGroupIsCluster(v interface{}) interface{} { |
| 270 | + return v |
| 271 | +} |
| 272 | + |
| 273 | +func flattenMonitoringGroupDisplayName(v interface{}) interface{} { |
| 274 | + return v |
| 275 | +} |
| 276 | + |
| 277 | +func flattenMonitoringGroupFilter(v interface{}) interface{} { |
| 278 | + return v |
| 279 | +} |
| 280 | + |
| 281 | +func expandMonitoringGroupParentName(v interface{}, d *schema.ResourceData, config *Config) (interface{}, error) { |
| 282 | + return v, nil |
| 283 | +} |
| 284 | + |
| 285 | +func expandMonitoringGroupIsCluster(v interface{}, d *schema.ResourceData, config *Config) (interface{}, error) { |
| 286 | + return v, nil |
| 287 | +} |
| 288 | + |
| 289 | +func expandMonitoringGroupDisplayName(v interface{}, d *schema.ResourceData, config *Config) (interface{}, error) { |
| 290 | + return v, nil |
| 291 | +} |
| 292 | + |
| 293 | +func expandMonitoringGroupFilter(v interface{}, d *schema.ResourceData, config *Config) (interface{}, error) { |
| 294 | + return v, nil |
| 295 | +} |
0 commit comments