/
resource_huaweicloud_dws_alarm_subscription.go
288 lines (252 loc) · 9.98 KB
/
resource_huaweicloud_dws_alarm_subscription.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
// ---------------------------------------------------------------
// *** AUTO GENERATED CODE ***
// @Product DWS
// ---------------------------------------------------------------
package dws
import (
"context"
"encoding/json"
"fmt"
"strings"
"github.com/hashicorp/go-multierror"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/jmespath/go-jmespath"
"github.com/chnsz/golangsdk"
"github.com/chnsz/golangsdk/pagination"
"github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/common"
"github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/config"
"github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/utils"
)
// @API DWS GET /v2/{project_id}/alarm-subs
// @API DWS POST /v2/{project_id}/alarm-subs
// @API DWS PUT /v2/{project_id}/alarm-subs/{alarm_sub_id}
// @API DWS DELETE /v2/{project_id}/alarm-subs/{alarm_sub_id}
func ResourceDwsAlarmSubs() *schema.Resource {
return &schema.Resource{
CreateContext: resourceDwsAlarmSubsCreate,
UpdateContext: resourceDwsAlarmSubsUpdate,
ReadContext: resourceDwsAlarmSubsRead,
DeleteContext: resourceDwsAlarmSubsDelete,
Importer: &schema.ResourceImporter{
StateContext: schema.ImportStatePassthroughContext,
},
Schema: map[string]*schema.Schema{
"region": {
Type: schema.TypeString,
Optional: true,
Computed: true,
ForceNew: true,
},
"name": {
Type: schema.TypeString,
Required: true,
Description: `The name of the alarm subscription.`,
},
"enable": {
Type: schema.TypeInt,
Required: true,
Description: `whether the alarm subscription is enabled.`,
},
"notification_target": {
Type: schema.TypeString,
Required: true,
Description: `The notification target.`,
},
"notification_target_name": {
Type: schema.TypeString,
Required: true,
Description: `The name of notification target.`,
},
"notification_target_type": {
Type: schema.TypeString,
Required: true,
Description: `The type of notification target. Currently only **SMN** is supported.`,
},
"time_zone": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
Description: `The time_zone of the alarm subscription.`,
},
"alarm_level": {
Type: schema.TypeString,
Optional: true,
Computed: true,
Description: `The level of alarm. separate multiple alarm levels with commas (,).
`,
},
},
}
}
func resourceDwsAlarmSubsCreate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
cfg := meta.(*config.Config)
region := cfg.GetRegion(d)
// createDwsAlarmSubs: create a DWS alarm subscription.
var (
createDwsAlarmSubsHttpUrl = "v2/{project_id}/alarm-subs"
createDwsAlarmSubsProduct = "dws"
)
createDwsAlarmSubsClient, err := cfg.NewServiceClient(createDwsAlarmSubsProduct, region)
if err != nil {
return diag.Errorf("error creating DWS Client: %s", err)
}
createDwsAlarmSubsPath := createDwsAlarmSubsClient.Endpoint + createDwsAlarmSubsHttpUrl
createDwsAlarmSubsPath = strings.ReplaceAll(createDwsAlarmSubsPath, "{project_id}", createDwsAlarmSubsClient.ProjectID)
createDwsAlarmSubsOpt := golangsdk.RequestOpts{
MoreHeaders: requestOpts.MoreHeaders,
KeepResponseBody: true,
}
createDwsAlarmSubsOpt.JSONBody = utils.RemoveNil(buildCreateDwsAlarmSubsBodyParams(d))
createDwsAlarmSubsResp, err := createDwsAlarmSubsClient.Request("POST", createDwsAlarmSubsPath, &createDwsAlarmSubsOpt)
if err != nil {
return diag.Errorf("error creating DWS alarm subscription: %s", err)
}
createDwsAlarmSubsRespBody, err := utils.FlattenResponse(createDwsAlarmSubsResp)
if err != nil {
return diag.FromErr(err)
}
id, err := jmespath.Search("id", createDwsAlarmSubsRespBody)
if err != nil {
return diag.Errorf("error creating DWS alarm subscription: ID is not found in API response")
}
d.SetId(id.(string))
return resourceDwsAlarmSubsRead(ctx, d, meta)
}
func buildCreateDwsAlarmSubsBodyParams(d *schema.ResourceData) map[string]interface{} {
bodyParams := map[string]interface{}{
"name": utils.ValueIngoreEmpty(d.Get("name")),
"alarm_level": utils.ValueIngoreEmpty(d.Get("alarm_level")),
"enable": d.Get("enable"),
"notification_target": utils.ValueIngoreEmpty(d.Get("notification_target")),
"notification_target_name": utils.ValueIngoreEmpty(d.Get("notification_target_name")),
"notification_target_type": utils.ValueIngoreEmpty(d.Get("notification_target_type")),
"time_zone": utils.ValueIngoreEmpty(d.Get("time_zone")),
}
return bodyParams
}
func resourceDwsAlarmSubsRead(_ context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
cfg := meta.(*config.Config)
region := cfg.GetRegion(d)
var mErr *multierror.Error
// getDwsAlarmSubs: Query the DWS alarm subscription.
var (
getDwsAlarmSubsHttpUrl = "v2/{project_id}/alarm-subs"
getDwsAlarmSubsProduct = "dws"
)
getDwsAlarmSubsClient, err := cfg.NewServiceClient(getDwsAlarmSubsProduct, region)
if err != nil {
return diag.Errorf("error creating DWS Client: %s", err)
}
getDwsAlarmSubsPath := getDwsAlarmSubsClient.Endpoint + getDwsAlarmSubsHttpUrl
getDwsAlarmSubsPath = strings.ReplaceAll(getDwsAlarmSubsPath, "{project_id}", getDwsAlarmSubsClient.ProjectID)
getDwsAlarmSubsResp, err := pagination.ListAllItems(
getDwsAlarmSubsClient,
"offset",
getDwsAlarmSubsPath,
&pagination.QueryOpts{MarkerField: ""})
if err != nil {
return common.CheckDeletedDiag(d, err, "error retrieving DWS alarm subscription")
}
getDwsAlarmSubsRespJson, err := json.Marshal(getDwsAlarmSubsResp)
if err != nil {
return diag.FromErr(err)
}
var getDwsAlarmSubsRespBody interface{}
err = json.Unmarshal(getDwsAlarmSubsRespJson, &getDwsAlarmSubsRespBody)
if err != nil {
return diag.FromErr(err)
}
jsonPath := fmt.Sprintf("alarm_subscriptions[?id=='%s']|[0]", d.Id())
rawData := utils.PathSearch(jsonPath, getDwsAlarmSubsRespBody, nil)
if rawData == nil {
return common.CheckDeletedDiag(d, golangsdk.ErrDefault404{}, "no data found")
}
mErr = multierror.Append(
mErr,
d.Set("region", region),
d.Set("name", utils.PathSearch("name", rawData, nil)),
d.Set("alarm_level", utils.PathSearch("alarm_level", rawData, nil)),
d.Set("enable", utils.PathSearch("enable", rawData, nil)),
d.Set("notification_target", utils.PathSearch("notification_target", rawData, nil)),
d.Set("notification_target_name", utils.PathSearch("notification_target_name", rawData, nil)),
d.Set("notification_target_type", utils.PathSearch("notification_target_type", rawData, nil)),
d.Set("time_zone", utils.PathSearch("time_zone", rawData, nil)),
)
return diag.FromErr(mErr.ErrorOrNil())
}
func resourceDwsAlarmSubsUpdate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
cfg := meta.(*config.Config)
region := cfg.GetRegion(d)
updateDwsAlarmSubsChanges := []string{
"name",
"alarm_level",
"enable",
"notification_target",
"notification_target_name",
"notification_target_type",
}
if d.HasChanges(updateDwsAlarmSubsChanges...) {
// updateDwsAlarmSubs: update the DWS alarm subscription.
var (
updateDwsAlarmSubsHttpUrl = "v2/{project_id}/alarm-subs/{alarm_sub_id}"
updateDwsAlarmSubsProduct = "dws"
)
updateDwsAlarmSubsClient, err := cfg.NewServiceClient(updateDwsAlarmSubsProduct, region)
if err != nil {
return diag.Errorf("error creating DWS Client: %s", err)
}
updateDwsAlarmSubsPath := updateDwsAlarmSubsClient.Endpoint + updateDwsAlarmSubsHttpUrl
updateDwsAlarmSubsPath = strings.ReplaceAll(updateDwsAlarmSubsPath, "{project_id}", updateDwsAlarmSubsClient.ProjectID)
updateDwsAlarmSubsPath = strings.ReplaceAll(updateDwsAlarmSubsPath, "{alarm_sub_id}", d.Id())
updateDwsAlarmSubsOpt := golangsdk.RequestOpts{
MoreHeaders: requestOpts.MoreHeaders,
KeepResponseBody: true,
}
updateDwsAlarmSubsOpt.JSONBody = utils.RemoveNil(buildUpdateDwsAlarmSubsBodyParams(d))
_, err = updateDwsAlarmSubsClient.Request("PUT", updateDwsAlarmSubsPath, &updateDwsAlarmSubsOpt)
if err != nil {
return diag.Errorf("error updating DWS alarm subscription: %s", err)
}
}
return resourceDwsAlarmSubsRead(ctx, d, meta)
}
func buildUpdateDwsAlarmSubsBodyParams(d *schema.ResourceData) map[string]interface{} {
bodyParams := map[string]interface{}{
"name": utils.ValueIngoreEmpty(d.Get("name")),
"alarm_level": utils.ValueIngoreEmpty(d.Get("alarm_level")),
"enable": d.Get("enable"),
"notification_target": utils.ValueIngoreEmpty(d.Get("notification_target")),
"notification_target_name": utils.ValueIngoreEmpty(d.Get("notification_target_name")),
"notification_target_type": utils.ValueIngoreEmpty(d.Get("notification_target_type")),
}
return bodyParams
}
func resourceDwsAlarmSubsDelete(_ context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
cfg := meta.(*config.Config)
region := cfg.GetRegion(d)
// deleteDwsAlarmSubs: delete DWS alarm subscription
var (
deleteDwsAlarmSubsHttpUrl = "v2/{project_id}/alarm-subs/{alarm_sub_id}"
deleteDwsAlarmSubsProduct = "dws"
)
deleteDwsAlarmSubsClient, err := cfg.NewServiceClient(deleteDwsAlarmSubsProduct, region)
if err != nil {
return diag.Errorf("error creating DWS Client: %s", err)
}
deleteDwsAlarmSubsPath := deleteDwsAlarmSubsClient.Endpoint + deleteDwsAlarmSubsHttpUrl
deleteDwsAlarmSubsPath = strings.ReplaceAll(deleteDwsAlarmSubsPath, "{project_id}", deleteDwsAlarmSubsClient.ProjectID)
deleteDwsAlarmSubsPath = strings.ReplaceAll(deleteDwsAlarmSubsPath, "{alarm_sub_id}", d.Id())
deleteDwsAlarmSubsOpt := golangsdk.RequestOpts{
MoreHeaders: requestOpts.MoreHeaders,
KeepResponseBody: true,
OkCodes: []int{
200,
},
}
_, err = deleteDwsAlarmSubsClient.Request("DELETE", deleteDwsAlarmSubsPath, &deleteDwsAlarmSubsOpt)
if err != nil {
return diag.Errorf("error deleting DWS alarm subscription: %s", err)
}
return nil
}