-
Notifications
You must be signed in to change notification settings - Fork 256
/
resource_monitoring_monitored_project.go
408 lines (346 loc) · 14.7 KB
/
resource_monitoring_monitored_project.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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0
// ----------------------------------------------------------------------------
//
// *** AUTO GENERATED CODE *** Type: MMv1 ***
//
// ----------------------------------------------------------------------------
//
// 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 monitoring
import (
"context"
"fmt"
"log"
"net/http"
"reflect"
"strconv"
"strings"
"time"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-provider-google-beta/google-beta/tpgresource"
transport_tpg "github.com/hashicorp/terraform-provider-google-beta/google-beta/transport"
)
func ResourceMonitoringMonitoredProjectNameDiffSuppressFunc(k, old, new string, d tpgresource.TerraformResourceDataChange) bool {
// Don't suppress if values are empty strings
if old == "" || new == "" {
return false
}
oldShort := tpgresource.GetResourceNameFromSelfLink(old)
newShort := tpgresource.GetResourceNameFromSelfLink(new)
// Suppress if short names are equal
if oldShort == newShort {
return true
}
_, isOldNumErr := tpgresource.StringToFixed64(oldShort)
isOldNumber := isOldNumErr == nil
_, isNewNumErr := tpgresource.StringToFixed64(newShort)
isNewNumber := isNewNumErr == nil
// Suppress if comparing a project number to project id
return isOldNumber != isNewNumber
}
func resourceMonitoringMonitoredProjectNameDiffSuppress(k, old, new string, d *schema.ResourceData) bool {
return ResourceMonitoringMonitoredProjectNameDiffSuppressFunc(k, old, new, d)
}
func ResourceMonitoringMonitoredProject() *schema.Resource {
return &schema.Resource{
Create: resourceMonitoringMonitoredProjectCreate,
Read: resourceMonitoringMonitoredProjectRead,
Delete: resourceMonitoringMonitoredProjectDelete,
Importer: &schema.ResourceImporter{
State: resourceMonitoringMonitoredProjectImport,
},
Timeouts: &schema.ResourceTimeout{
Create: schema.DefaultTimeout(20 * time.Minute),
Delete: schema.DefaultTimeout(20 * time.Minute),
},
SchemaVersion: 1,
StateUpgraders: []schema.StateUpgrader{
{
Type: resourceMonitoringMonitoredProjectResourceV0().CoreConfigSchema().ImpliedType(),
Upgrade: ResourceMonitoringMonitoredProjectUpgradeV0,
Version: 0,
},
},
Schema: map[string]*schema.Schema{
"metrics_scope": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
DiffSuppressFunc: tpgresource.CompareResourceNames,
Description: `Required. The resource name of the existing Metrics Scope that will monitor this project. Example: locations/global/metricsScopes/{SCOPING_PROJECT_ID_OR_NUMBER}`,
},
"name": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
DiffSuppressFunc: resourceMonitoringMonitoredProjectNameDiffSuppress,
Description: `Immutable. The resource name of the 'MonitoredProject'. On input, the resource name includes the scoping project ID and monitored project ID. On output, it contains the equivalent project numbers. Example: 'locations/global/metricsScopes/{SCOPING_PROJECT_ID_OR_NUMBER}/projects/{MONITORED_PROJECT_ID_OR_NUMBER}'`,
},
"create_time": {
Type: schema.TypeString,
Computed: true,
Description: `Output only. The time when this 'MonitoredProject' was created.`,
},
},
UseJSONNumber: true,
}
}
func resourceMonitoringMonitoredProjectCreate(d *schema.ResourceData, meta interface{}) error {
config := meta.(*transport_tpg.Config)
userAgent, err := tpgresource.GenerateUserAgentString(d, config.UserAgent)
if err != nil {
return err
}
obj := make(map[string]interface{})
nameProp, err := expandMonitoringMonitoredProjectName(d.Get("name"), d, config)
if err != nil {
return err
} else if v, ok := d.GetOkExists("name"); !tpgresource.IsEmptyValue(reflect.ValueOf(nameProp)) && (ok || !reflect.DeepEqual(v, nameProp)) {
obj["name"] = nameProp
}
obj, err = resourceMonitoringMonitoredProjectEncoder(d, meta, obj)
if err != nil {
return err
}
url, err := tpgresource.ReplaceVars(d, config, "{{MonitoringBasePath}}v1/locations/global/metricsScopes/{{metrics_scope}}/projects")
if err != nil {
return err
}
log.Printf("[DEBUG] Creating new MonitoredProject: %#v", obj)
billingProject := ""
// err == nil indicates that the billing_project value was found
if bp, err := tpgresource.GetBillingProject(d, config); err == nil {
billingProject = bp
}
headers := make(http.Header)
res, err := transport_tpg.SendRequest(transport_tpg.SendRequestOptions{
Config: config,
Method: "POST",
Project: billingProject,
RawURL: url,
UserAgent: userAgent,
Body: obj,
Timeout: d.Timeout(schema.TimeoutCreate),
Headers: headers,
ErrorRetryPredicates: []transport_tpg.RetryErrorPredicateFunc{transport_tpg.IsMonitoringPermissionError},
})
if err != nil {
return fmt.Errorf("Error creating MonitoredProject: %s", err)
}
// Store the ID now
id, err := tpgresource.ReplaceVars(d, config, "locations/global/metricsScopes/{{metrics_scope}}/projects/{{name}}")
if err != nil {
return fmt.Errorf("Error constructing id: %s", err)
}
d.SetId(id)
log.Printf("[DEBUG] Finished creating MonitoredProject %q: %#v", d.Id(), res)
return resourceMonitoringMonitoredProjectRead(d, meta)
}
func resourceMonitoringMonitoredProjectRead(d *schema.ResourceData, meta interface{}) error {
config := meta.(*transport_tpg.Config)
userAgent, err := tpgresource.GenerateUserAgentString(d, config.UserAgent)
if err != nil {
return err
}
url, err := tpgresource.ReplaceVars(d, config, "{{MonitoringBasePath}}v1/locations/global/metricsScopes/{{metrics_scope}}")
if err != nil {
return err
}
billingProject := ""
// err == nil indicates that the billing_project value was found
if bp, err := tpgresource.GetBillingProject(d, config); err == nil {
billingProject = bp
}
headers := make(http.Header)
name := d.Get("name").(string)
name = tpgresource.GetResourceNameFromSelfLink(name)
d.Set("name", name)
metricsScope := d.Get("metrics_scope").(string)
metricsScope = tpgresource.GetResourceNameFromSelfLink(metricsScope)
d.Set("metrics_scope", metricsScope)
url, err = tpgresource.ReplaceVars(d, config, "{{MonitoringBasePath}}v1/locations/global/metricsScopes/{{metrics_scope}}")
if err != nil {
return err
}
res, err := transport_tpg.SendRequest(transport_tpg.SendRequestOptions{
Config: config,
Method: "GET",
Project: billingProject,
RawURL: url,
UserAgent: userAgent,
Headers: headers,
ErrorRetryPredicates: []transport_tpg.RetryErrorPredicateFunc{transport_tpg.IsMonitoringPermissionError},
})
if err != nil {
return transport_tpg.HandleNotFoundError(err, d, fmt.Sprintf("MonitoringMonitoredProject %q", d.Id()))
}
res, err = resourceMonitoringMonitoredProjectDecoder(d, meta, res)
if err != nil {
return err
}
if res == nil {
// Decoding the object has resulted in it being gone. It may be marked deleted
log.Printf("[DEBUG] Removing MonitoringMonitoredProject because it no longer exists.")
d.SetId("")
return nil
}
if err := d.Set("name", flattenMonitoringMonitoredProjectName(res["name"], d, config)); err != nil {
return fmt.Errorf("Error reading MonitoredProject: %s", err)
}
if err := d.Set("create_time", flattenMonitoringMonitoredProjectCreateTime(res["createTime"], d, config)); err != nil {
return fmt.Errorf("Error reading MonitoredProject: %s", err)
}
return nil
}
func resourceMonitoringMonitoredProjectDelete(d *schema.ResourceData, meta interface{}) error {
config := meta.(*transport_tpg.Config)
userAgent, err := tpgresource.GenerateUserAgentString(d, config.UserAgent)
if err != nil {
return err
}
billingProject := ""
url, err := tpgresource.ReplaceVars(d, config, "{{MonitoringBasePath}}v1/{{name}}")
if err != nil {
return err
}
var obj map[string]interface{}
// err == nil indicates that the billing_project value was found
if bp, err := tpgresource.GetBillingProject(d, config); err == nil {
billingProject = bp
}
headers := make(http.Header)
log.Printf("[DEBUG] Deleting MonitoredProject %q", d.Id())
res, err := transport_tpg.SendRequest(transport_tpg.SendRequestOptions{
Config: config,
Method: "DELETE",
Project: billingProject,
RawURL: url,
UserAgent: userAgent,
Body: obj,
Timeout: d.Timeout(schema.TimeoutDelete),
Headers: headers,
ErrorRetryPredicates: []transport_tpg.RetryErrorPredicateFunc{transport_tpg.IsMonitoringPermissionError},
})
if err != nil {
return transport_tpg.HandleNotFoundError(err, d, "MonitoredProject")
}
log.Printf("[DEBUG] Finished deleting MonitoredProject %q: %#v", d.Id(), res)
return nil
}
func resourceMonitoringMonitoredProjectImport(d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) {
name := d.Get("name").(string)
name = tpgresource.GetResourceNameFromSelfLink(name)
d.Set("name", name)
metricsScope := d.Get("metrics_scope").(string)
metricsScope = tpgresource.GetResourceNameFromSelfLink(metricsScope)
d.Set("metrics_scope", metricsScope)
config := meta.(*transport_tpg.Config)
if err := tpgresource.ParseImportId([]string{
"locations/global/metricsScopes/(?P<metrics_scope>[^/]+)/projects/(?P<name>[^/]+)",
"v1/locations/global/metricsScopes/(?P<metrics_scope>[^/]+)/projects/(?P<name>[^/]+)",
"(?P<metrics_scope>[^/]+)/(?P<name>[^/]+)",
}, d, config); err != nil {
return nil, err
}
// Replace import id for the resource id
id, err := tpgresource.ReplaceVars(d, config, "locations/global/metricsScopes/{{metrics_scope}}/projects/{{name}}")
if err != nil {
return nil, fmt.Errorf("Error constructing id: %s", err)
}
d.SetId(id)
return []*schema.ResourceData{d}, nil
}
func flattenMonitoringMonitoredProjectName(v interface{}, d *schema.ResourceData, config *transport_tpg.Config) interface{} {
return v
}
func flattenMonitoringMonitoredProjectCreateTime(v interface{}, d *schema.ResourceData, config *transport_tpg.Config) interface{} {
return v
}
func expandMonitoringMonitoredProjectName(v interface{}, d tpgresource.TerraformResourceData, config *transport_tpg.Config) (interface{}, error) {
return v, nil
}
func resourceMonitoringMonitoredProjectEncoder(d *schema.ResourceData, meta interface{}, obj map[string]interface{}) (map[string]interface{}, error) {
name := d.Get("name").(string)
log.Printf("[DEBUG] Encoded monitored project name: %s", name)
name = tpgresource.GetResourceNameFromSelfLink(name)
log.Printf("[DEBUG] Encoded monitored project resource name: %s", name)
d.Set("name", name)
metricsScope := d.Get("metrics_scope").(string)
log.Printf("[DEBUG] Encoded monitored project metricsScope: %s", metricsScope)
metricsScope = tpgresource.GetResourceNameFromSelfLink(metricsScope)
log.Printf("[DEBUG] Encoded monitored project metricsScope resource name: %s", metricsScope)
d.Set("metrics_scope", metricsScope)
obj["name"] = fmt.Sprintf("locations/global/metricsScopes/%s/projects/%s", metricsScope, name)
return obj, nil
}
func resourceMonitoringMonitoredProjectDecoder(d *schema.ResourceData, meta interface{}, res map[string]interface{}) (map[string]interface{}, error) {
// terraform resource config
config := meta.(*transport_tpg.Config)
// The API returns all monitored projects
monitoredProjects, _ := res["monitoredProjects"].([]interface{})
// Convert configured terraform monitored_project resource name to a ProjectNumber
expectedProject, configProjectErr := config.NewResourceManagerClient(config.UserAgent).Projects.Get(d.Get("name").(string)).Do()
if configProjectErr != nil {
return nil, configProjectErr
}
expectedProjectNumber := strconv.FormatInt(expectedProject.ProjectNumber, 10)
log.Printf("[DEBUG] Scanning for ProjectNumber: %s.", expectedProjectNumber)
// Iterate through the list of monitoredProjects to make sure one matches the configured monitored_project
for _, monitoredProjectRaw := range monitoredProjects {
if monitoredProjectRaw == nil {
continue
}
monitoredProject := monitoredProjectRaw.(map[string]interface{})
// MonitoredProject names have the format locations/global/metricsScopes/[metricScopeProjectNumber]/projects/[projectNumber]
monitoredProjectName := monitoredProject["name"]
// `res` contains the MonitoredProjects of the relevant metrics scope
log.Printf("[DEBUG] Matching ProjectNumbers: %s to %s.", expectedProjectNumber, monitoredProjectName)
if strings.HasSuffix(monitoredProjectName.(string), fmt.Sprintf("/%s", expectedProjectNumber)) {
// Match found - set response object name to match
res["name"] = monitoredProjectName
log.Printf("[DEBUG] Matched ProjectNumbers: %s and %s.", expectedProjectNumber, monitoredProjectName)
return res, nil
}
}
log.Printf("[DEBUG] MonitoringMonitoredProject couldn't be matched.")
return nil, nil
}
func resourceMonitoringMonitoredProjectResourceV0() *schema.Resource {
return &schema.Resource{
Schema: map[string]*schema.Schema{
"metrics_scope": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
DiffSuppressFunc: tpgresource.CompareResourceNames,
Description: `Required. The resource name of the existing Metrics Scope that will monitor this project. Example: locations/global/metricsScopes/{SCOPING_PROJECT_ID_OR_NUMBER}`,
},
"name": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
DiffSuppressFunc: tpgresource.CompareResourceNames,
Description: `Immutable. The resource name of the 'MonitoredProject'. On input, the resource name includes the scoping project ID and monitored project ID. On output, it contains the equivalent project numbers. Example: 'locations/global/metricsScopes/{SCOPING_PROJECT_ID_OR_NUMBER}/projects/{MONITORED_PROJECT_ID_OR_NUMBER}'`,
},
"create_time": {
Type: schema.TypeString,
Computed: true,
Description: `Output only. The time when this 'MonitoredProject' was created.`,
},
},
UseJSONNumber: true,
}
}
func ResourceMonitoringMonitoredProjectUpgradeV0(_ context.Context, rawState map[string]any, meta any) (map[string]any, error) {
log.Printf("[DEBUG] Attributes before migration: %#v", rawState)
rawState["id"] = strings.TrimPrefix(rawState["id"].(string), "v1/")
log.Printf("[DEBUG] Attributes after migration: %#v", rawState)
return rawState, nil
}