forked from GoogleCloudPlatform/magic-modules
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathresource_monitoring_dashboard.go
214 lines (175 loc) · 5.9 KB
/
resource_monitoring_dashboard.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
package google
import (
"fmt"
"reflect"
"time"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/structure"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
)
func monitoringDashboardDiffSuppress(k, old, new string, d *schema.ResourceData) bool {
computedFields := []string{"etag", "name"}
oldMap, err := structure.ExpandJsonFromString(old)
if err != nil {
return false
}
newMap, err := structure.ExpandJsonFromString(new)
if err != nil {
return false
}
for _, f := range computedFields {
delete(oldMap, f)
delete(newMap, f)
}
return reflect.DeepEqual(oldMap, newMap)
}
func resourceMonitoringDashboard() *schema.Resource {
return &schema.Resource{
Create: resourceMonitoringDashboardCreate,
Read: resourceMonitoringDashboardRead,
Update: resourceMonitoringDashboardUpdate,
Delete: resourceMonitoringDashboardDelete,
Importer: &schema.ResourceImporter{
State: resourceMonitoringDashboardImport,
},
Timeouts: &schema.ResourceTimeout{
Create: schema.DefaultTimeout(4 * time.Minute),
Update: schema.DefaultTimeout(4 * time.Minute),
Delete: schema.DefaultTimeout(4 * time.Minute),
},
Schema: map[string]*schema.Schema{
"dashboard_json": {
Type: schema.TypeString,
Required: true,
ValidateFunc: validation.StringIsJSON,
DiffSuppressFunc: monitoringDashboardDiffSuppress,
StateFunc: func(v interface{}) string {
json, _ := structure.NormalizeJsonString(v)
return json
},
Description: `The JSON representation of a dashboard, following the format at https://cloud.google.com/monitoring/api/ref_v3/rest/v1/projects.dashboards.`,
},
"project": {
Type: schema.TypeString,
Optional: true,
Computed: true,
ForceNew: true,
Description: `The ID of the project in which the resource belongs. If it is not provided, the provider project is used.`,
},
},
UseJSONNumber: true,
}
}
func resourceMonitoringDashboardCreate(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
userAgent, err := generateUserAgentString(d, config.userAgent)
if err != nil {
return err
}
obj, err := structure.ExpandJsonFromString(d.Get("dashboard_json").(string))
if err != nil {
return err
}
project, err := getProject(d, config)
if err != nil {
return err
}
url, err := replaceVars(d, config, "{{MonitoringBasePath}}v1/projects/{{project}}/dashboards")
if err != nil {
return err
}
res, err := sendRequestWithTimeout(config, "POST", project, url, userAgent, obj, d.Timeout(schema.TimeoutCreate), isMonitoringConcurrentEditError)
if err != nil {
return fmt.Errorf("Error creating Dashboard: %s", err)
}
name, ok := res["name"]
if !ok {
return fmt.Errorf("Create response didn't contain critical fields. Create may not have succeeded.")
}
d.SetId(name.(string))
return resourceMonitoringDashboardRead(d, config)
}
func resourceMonitoringDashboardRead(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
userAgent, err := generateUserAgentString(d, config.userAgent)
if err != nil {
return err
}
url := config.MonitoringBasePath + "v1/" + d.Id()
project, err := getProject(d, config)
if err != nil {
return err
}
res, err := sendRequest(config, "GET", project, url, userAgent, nil, isMonitoringConcurrentEditError)
if err != nil {
return handleNotFoundError(err, d, fmt.Sprintf("MonitoringDashboard %q", d.Id()))
}
if err := d.Set("project", project); err != nil {
return fmt.Errorf("Error setting Dashboard: %s", err)
}
str, err := structure.FlattenJsonToString(res)
if err != nil {
return fmt.Errorf("Error reading Dashboard: %s", err)
}
if err = d.Set("dashboard_json", str); err != nil {
return fmt.Errorf("Error reading Dashboard: %s", err)
}
return nil
}
func resourceMonitoringDashboardUpdate(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
userAgent, err := generateUserAgentString(d, config.userAgent)
if err != nil {
return err
}
o, n := d.GetChange("dashboard_json")
oObj, err := structure.ExpandJsonFromString(o.(string))
if err != nil {
return err
}
nObj, err := structure.ExpandJsonFromString(n.(string))
if err != nil {
return err
}
nObj["etag"] = oObj["etag"]
project, err := getProject(d, config)
if err != nil {
return err
}
url := config.MonitoringBasePath + "v1/" + d.Id()
_, err = sendRequestWithTimeout(config, "PATCH", project, url, userAgent, nObj, d.Timeout(schema.TimeoutUpdate), isMonitoringConcurrentEditError)
if err != nil {
return fmt.Errorf("Error updating Dashboard %q: %s", d.Id(), err)
}
return resourceMonitoringDashboardRead(d, config)
}
func resourceMonitoringDashboardDelete(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
userAgent, err := generateUserAgentString(d, config.userAgent)
if err != nil {
return err
}
url := config.MonitoringBasePath + "v1/" + d.Id()
project, err := getProject(d, config)
if err != nil {
return err
}
_, err = sendRequestWithTimeout(config, "DELETE", project, url, userAgent, nil, d.Timeout(schema.TimeoutDelete), isMonitoringConcurrentEditError)
if err != nil {
return handleNotFoundError(err, d, fmt.Sprintf("MonitoringDashboard %q", d.Id()))
}
return nil
}
func resourceMonitoringDashboardImport(d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) {
config := meta.(*Config)
// current import_formats can't import fields with forward slashes in their value
parts, err := getImportIdQualifiers([]string{"projects/(?P<project>[^/]+)/dashboards/(?P<id>[^/]+)", "(?P<id>[^/]+)"}, d, config, d.Id())
if err != nil {
return nil, err
}
if err := d.Set("project", parts["project"]); err != nil {
return nil, fmt.Errorf("Error setting project: %s", err)
}
d.SetId(fmt.Sprintf("projects/%s/dashboards/%s", parts["project"], parts["id"]))
return []*schema.ResourceData{d}, nil
}