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