Skip to content

Commit 5cdc377

Browse files
feat: Update Alert Configuration (#1546)
Co-authored-by: Rakhul S Prakash <rakhul.s.prakash@peerislands.io>
1 parent 300fa5c commit 5cdc377

9 files changed

Lines changed: 147 additions & 64 deletions

File tree

cfn-resources/alert-configuration/cmd/resource/model.go

Lines changed: 15 additions & 12 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

cfn-resources/alert-configuration/cmd/resource/resource.go

Lines changed: 48 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ import (
2121
"reflect"
2222
"strings"
2323

24-
admin20231115014 "go.mongodb.org/atlas-sdk/v20231115014/admin"
24+
"go.mongodb.org/atlas-sdk/v20250312013/admin"
2525

2626
"github.com/aws-cloudformation/cloudformation-cli-go-plugin/cfn/handler"
2727
"github.com/aws/aws-sdk-go-v2/aws"
@@ -63,7 +63,7 @@ func Create(req handler.Request, prevModel *Model, currentModel *Model) (handler
6363
if peErr != nil {
6464
return *peErr, nil
6565
}
66-
atlasV2 := client.Atlas20231115014
66+
atlasV2 := client.AtlasSDK
6767

6868
if currentModel.Id != nil && *currentModel.Id != "" {
6969
_, _ = logger.Warnf("resource already exists for Id: %s", *currentModel.Id)
@@ -78,19 +78,20 @@ func Create(req handler.Request, prevModel *Model, currentModel *Model) (handler
7878
return progressevents.GetFailedEventByCode(err.Error(), string(types.HandlerErrorCodeInvalidRequest)), err
7979
}
8080

81-
alertConfigRequest := admin20231115014.GroupAlertsConfig{
82-
GroupId: currentModel.ProjectId,
83-
EventTypeName: currentModel.EventTypeName,
84-
Enabled: currentModel.Enabled,
85-
Matchers: expandAlertConfigurationMatchers(currentModel.Matchers),
86-
MetricThreshold: expandAlertConfigurationMetricThresholdConfig(currentModel),
87-
Threshold: expandAlertConfigurationThreshold(currentModel.Threshold),
88-
Notifications: notifications,
81+
alertConfigRequest := admin.GroupAlertsConfig{
82+
GroupId: currentModel.ProjectId,
83+
EventTypeName: currentModel.EventTypeName,
84+
Enabled: currentModel.Enabled,
85+
Matchers: expandAlertConfigurationMatchers(currentModel.Matchers),
86+
MetricThreshold: expandAlertConfigurationMetricThresholdConfig(currentModel),
87+
Threshold: expandAlertConfigurationThreshold(currentModel.Threshold),
88+
Notifications: notifications,
89+
SeverityOverride: currentModel.SeverityOverride,
8990
}
9091

9192
projectID := *currentModel.ProjectId
9293
var res *http.Response
93-
alertConfig, res, err := atlasV2.AlertConfigurationsApi.CreateAlertConfiguration(context.Background(), projectID, &alertConfigRequest).Execute()
94+
alertConfig, res, err := atlasV2.AlertConfigurationsApi.CreateAlertConfig(context.Background(), projectID, &alertConfigRequest).Execute()
9495
defer res.Body.Close()
9596
if err != nil {
9697
return progressevents.GetFailedEventByResponse(err.Error(), res), nil
@@ -120,7 +121,7 @@ func Read(req handler.Request, prevModel *Model, currentModel *Model) (handler.P
120121
if peErr != nil {
121122
return *peErr, nil
122123
}
123-
atlasV2 := client.Atlas20231115014
124+
atlasV2 := client.AtlasSDK
124125

125126
if !isExist(currentModel, atlasV2) {
126127
_, _ = logger.Warnf("resource not exist for Id: %s", *currentModel.Id)
@@ -130,7 +131,7 @@ func Read(req handler.Request, prevModel *Model, currentModel *Model) (handler.P
130131
HandlerErrorCode: string(types.HandlerErrorCodeNotFound)}, nil
131132
}
132133

133-
alertConfig, resp, err := atlasV2.AlertConfigurationsApi.GetAlertConfiguration(context.Background(), *currentModel.ProjectId, *currentModel.Id).Execute()
134+
alertConfig, resp, err := atlasV2.AlertConfigurationsApi.GetAlertConfig(context.Background(), *currentModel.ProjectId, *currentModel.Id).Execute()
134135
defer resp.Body.Close()
135136
if err != nil {
136137
return progressevents.GetFailedEventByResponse(err.Error(), resp), nil
@@ -160,7 +161,7 @@ func Update(req handler.Request, prevModel *Model, currentModel *Model) (handler
160161
if peErr != nil {
161162
return *peErr, nil
162163
}
163-
atlasV2 := client.Atlas20231115014
164+
atlasV2 := client.AtlasSDK
164165

165166
if !isExist(currentModel, atlasV2) {
166167
_, _ = logger.Warnf("resource not exist for Id: %s", *currentModel.Id)
@@ -174,7 +175,7 @@ func Update(req handler.Request, prevModel *Model, currentModel *Model) (handler
174175
// server returns an error 500
175176
projectID := *currentModel.ProjectId
176177
id := *currentModel.Id
177-
alertReq, res, err := atlasV2.AlertConfigurationsApi.GetAlertConfiguration(context.Background(), projectID, id).Execute()
178+
alertReq, res, err := atlasV2.AlertConfigurationsApi.GetAlertConfig(context.Background(), projectID, id).Execute()
178179
if err != nil {
179180
return progressevents.GetFailedEventByResponse(err.Error(), res), nil
180181
}
@@ -184,15 +185,15 @@ func Update(req handler.Request, prevModel *Model, currentModel *Model) (handler
184185
// Removing the computed attributes to recreate the original request
185186
alertReq.Created = nil
186187
alertReq.Updated = nil
187-
var alertModel *admin20231115014.GroupAlertsConfig
188+
var alertModel *admin.GroupAlertsConfig
188189

189190
// Cannot enable/disable ONLY via update (if only send enable as changed field server returns a 500 error)
190191
// so have to use different method to change enabled.
191-
if reflect.DeepEqual(alertReq, &admin20231115014.GroupAlertsConfig{Enabled: aws.Bool(true)}) ||
192-
reflect.DeepEqual(alertReq, &admin20231115014.GroupAlertsConfig{Enabled: aws.Bool(false)}) {
193-
alertModel, res, err = atlasV2.AlertConfigurationsApi.ToggleAlertConfiguration(context.Background(), projectID, id, &admin20231115014.AlertsToggle{Enabled: alertReq.Enabled}).Execute()
192+
if reflect.DeepEqual(alertReq, &admin.GroupAlertsConfig{Enabled: aws.Bool(true)}) ||
193+
reflect.DeepEqual(alertReq, &admin.GroupAlertsConfig{Enabled: aws.Bool(false)}) {
194+
alertModel, res, err = atlasV2.AlertConfigurationsApi.ToggleAlertConfig(context.Background(), projectID, id, &admin.AlertsToggle{Enabled: alertReq.Enabled}).Execute()
194195
} else {
195-
alertModel, res, err = atlasV2.AlertConfigurationsApi.UpdateAlertConfiguration(context.Background(), projectID, id, alertReq).Execute()
196+
alertModel, res, err = atlasV2.AlertConfigurationsApi.UpdateAlertConfig(context.Background(), projectID, id, alertReq).Execute()
196197
}
197198

198199
if err != nil {
@@ -224,7 +225,7 @@ func Delete(req handler.Request, prevModel *Model, currentModel *Model) (handler
224225
if peErr != nil {
225226
return *peErr, nil
226227
}
227-
atlasV2 := client.Atlas20231115014
228+
atlasV2 := client.AtlasSDK
228229

229230
if !isExist(currentModel, atlasV2) {
230231
_, _ = logger.Warnf("resource not exist for Id: %s", *currentModel.Id)
@@ -234,7 +235,7 @@ func Delete(req handler.Request, prevModel *Model, currentModel *Model) (handler
234235
HandlerErrorCode: string(types.HandlerErrorCodeNotFound)}, nil
235236
}
236237

237-
res, err := atlasV2.AlertConfigurationsApi.DeleteAlertConfiguration(context.Background(), *currentModel.ProjectId, *currentModel.Id).Execute()
238+
res, err := atlasV2.AlertConfigurationsApi.DeleteAlertConfig(context.Background(), *currentModel.ProjectId, *currentModel.Id).Execute()
238239

239240
if err != nil {
240241
_, _ = logger.Warnf("Delete - error: %+v", err)
@@ -253,15 +254,15 @@ func List(req handler.Request, prevModel *Model, currentModel *Model) (handler.P
253254
HandlerErrorCode: string(types.HandlerErrorCodeNotFound)}, nil
254255
}
255256

256-
func isExist(currentModel *Model, client *admin20231115014.APIClient) bool {
257-
alert, _, err := client.AlertConfigurationsApi.GetAlertConfiguration(context.Background(), *currentModel.ProjectId, *currentModel.Id).Execute()
257+
func isExist(currentModel *Model, client *admin.APIClient) bool {
258+
alert, _, err := client.AlertConfigurationsApi.GetAlertConfig(context.Background(), *currentModel.ProjectId, *currentModel.Id).Execute()
258259
return err == nil && alert != nil
259260
}
260261

261-
func expandAlertConfigurationMatchers(matchers []Matcher) *[]admin20231115014.StreamsMatcher {
262-
mts := make([]admin20231115014.StreamsMatcher, 0)
262+
func expandAlertConfigurationMatchers(matchers []Matcher) *[]admin.StreamsMatcher {
263+
mts := make([]admin.StreamsMatcher, 0)
263264
for ind := range matchers {
264-
mMatcher := admin20231115014.NewStreamsMatcher(
265+
mMatcher := admin.NewStreamsMatcher(
265266
cast.ToString(matchers[ind].FieldName),
266267
cast.ToString(matchers[ind].Operator),
267268
cast.ToString(matchers[ind].Value),
@@ -271,12 +272,12 @@ func expandAlertConfigurationMatchers(matchers []Matcher) *[]admin20231115014.St
271272
return &mts
272273
}
273274

274-
func expandAlertConfigurationMetricThresholdConfig(currentModel *Model) *admin20231115014.ServerlessMetricThreshold {
275+
func expandAlertConfigurationMetricThresholdConfig(currentModel *Model) *admin.FlexClusterMetricThreshold {
275276
threshold := currentModel.MetricThreshold
276277
if threshold == nil {
277278
return nil
278279
}
279-
return &admin20231115014.ServerlessMetricThreshold{
280+
return &admin.FlexClusterMetricThreshold{
280281
MetricName: cast.ToString(threshold.MetricName),
281282
Operator: threshold.Operator,
282283
Threshold: threshold.Threshold,
@@ -285,19 +286,19 @@ func expandAlertConfigurationMetricThresholdConfig(currentModel *Model) *admin20
285286
}
286287
}
287288

288-
func expandAlertConfigurationThreshold(threshold *IntegerThresholdView) *admin20231115014.GreaterThanRawThreshold {
289+
func expandAlertConfigurationThreshold(threshold *IntegerThresholdView) *admin.StreamProcessorMetricThreshold {
289290
if threshold == nil {
290291
return nil
291292
}
292-
return &admin20231115014.GreaterThanRawThreshold{
293+
return &admin.StreamProcessorMetricThreshold{
293294
Operator: threshold.Operator,
294-
Threshold: util.Pointer(int(*threshold.Threshold)),
295+
Threshold: threshold.Threshold,
295296
Units: threshold.Units,
296297
}
297298
}
298299

299-
func expandAlertConfigurationNotification(notificationList []NotificationView) (*[]admin20231115014.AlertsNotificationRootForGroup, error) {
300-
notifications := make([]admin20231115014.AlertsNotificationRootForGroup, 0)
300+
func expandAlertConfigurationNotification(notificationList []NotificationView) (*[]admin.AlertsNotificationRootForGroup, error) {
301+
notifications := make([]admin.AlertsNotificationRootForGroup, 0)
301302

302303
for ind := range notificationList {
303304
if notificationList[ind].IntervalMin != nil && *notificationList[ind].IntervalMin > cast.ToFloat64(0) {
@@ -309,7 +310,7 @@ func expandAlertConfigurationNotification(notificationList []NotificationView) (
309310
}
310311

311312
for ind := range notificationList {
312-
notification := admin20231115014.AlertsNotificationRootForGroup{
313+
notification := admin.AlertsNotificationRootForGroup{
313314
ApiToken: notificationList[ind].ApiToken,
314315
ChannelName: notificationList[ind].ChannelName,
315316
DatadogApiKey: notificationList[ind].DatadogApiKey,
@@ -330,15 +331,17 @@ func expandAlertConfigurationNotification(notificationList []NotificationView) (
330331
VictorOpsRoutingKey: notificationList[ind].VictorOpsRoutingKey,
331332
Roles: &notificationList[ind].Roles,
332333
DelayMin: notificationList[ind].DelayMin,
334+
NotifierId: notificationList[ind].NotifierId,
335+
IntegrationId: notificationList[ind].IntegrationId,
333336
}
334337
notifications = append(notifications, notification)
335338
}
336339
return &notifications, nil
337340
}
338341

339-
func convertToMongoModel(reqModel *admin20231115014.GroupAlertsConfig, currentModel *Model) *admin20231115014.GroupAlertsConfig {
342+
func convertToMongoModel(reqModel *admin.GroupAlertsConfig, currentModel *Model) *admin.GroupAlertsConfig {
340343
if reqModel == nil {
341-
reqModel = &admin20231115014.GroupAlertsConfig{}
344+
reqModel = &admin.GroupAlertsConfig{}
342345
}
343346

344347
// Only change the updated fields
@@ -360,10 +363,13 @@ func convertToMongoModel(reqModel *admin20231115014.GroupAlertsConfig, currentMo
360363
if currentModel.Notifications != nil {
361364
reqModel.Notifications, _ = expandAlertConfigurationNotification(currentModel.Notifications)
362365
}
366+
if currentModel.SeverityOverride != nil {
367+
reqModel.SeverityOverride = currentModel.SeverityOverride
368+
}
363369
return reqModel
364370
}
365371

366-
func convertToUIModel(alertConfig *admin20231115014.GroupAlertsConfig, currentModel *Model) *Model {
372+
func convertToUIModel(alertConfig *admin.GroupAlertsConfig, currentModel *Model) *Model {
367373
currentModel.Id = alertConfig.Id
368374
if alertConfig.Created != nil {
369375
currentModel.Created = util.TimePtrToStringPtr(alertConfig.Created)
@@ -377,5 +383,9 @@ func convertToUIModel(alertConfig *admin20231115014.GroupAlertsConfig, currentMo
377383
currentModel.Enabled = alertConfig.Enabled
378384
}
379385

386+
if alertConfig.SeverityOverride != nil {
387+
currentModel.SeverityOverride = alertConfig.SeverityOverride
388+
}
389+
380390
return currentModel
381391
}

cfn-resources/alert-configuration/docs/README.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ To declare this entity in your AWS CloudFormation template, use the following sy
2020
"<a href="#notifications" title="Notifications">Notifications</a>" : <i>[ <a href="notificationview.md">NotificationView</a>, ... ]</i>,
2121
"<a href="#threshold" title="Threshold">Threshold</a>" : <i><a href="integerthresholdview.md">IntegerThresholdView</a></i>,
2222
"<a href="#typename" title="TypeName">TypeName</a>" : <i>String</i>,
23+
"<a href="#severityoverride" title="SeverityOverride">SeverityOverride</a>" : <i>String</i>
2324
}
2425
}
2526
</pre>
@@ -39,6 +40,7 @@ Properties:
3940
- <a href="notificationview.md">NotificationView</a></i>
4041
<a href="#threshold" title="Threshold">Threshold</a>: <i><a href="integerthresholdview.md">IntegerThresholdView</a></i>
4142
<a href="#typename" title="TypeName">TypeName</a>: <i>String</i>
43+
<a href="#severityoverride" title="SeverityOverride">SeverityOverride</a>: <i>String</i>
4244
</pre>
4345

4446
## Properties
@@ -125,6 +127,16 @@ _Type_: String
125127

126128
_Update requires_: [Replacement](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/using-cfn-updating-stacks-update-behaviors.html#update-replacement)
127129

130+
#### SeverityOverride
131+
132+
Degree of seriousness given to this alert. This value overrides the default severity level for the alert.
133+
134+
_Required_: No
135+
136+
_Type_: String
137+
138+
_Update requires_: [Replacement](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/using-cfn-updating-stacks-update-behaviors.html#update-replacement)
139+
128140
## Return Values
129141

130142
### Fn::GetAtt

cfn-resources/alert-configuration/docs/notificationview.md

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,9 @@ To declare this entity in your AWS CloudFormation template, use the following sy
3434
"<a href="#victoropsapikey" title="VictorOpsApiKey">VictorOpsApiKey</a>" : <i>String</i>,
3535
"<a href="#victoropsroutingkey" title="VictorOpsRoutingKey">VictorOpsRoutingKey</a>" : <i>String</i>,
3636
"<a href="#webhooksecret" title="WebhookSecret">WebhookSecret</a>" : <i>String</i>,
37-
"<a href="#webhookurl" title="WebhookUrl">WebhookUrl</a>" : <i>String</i>
37+
"<a href="#webhookurl" title="WebhookUrl">WebhookUrl</a>" : <i>String</i>,
38+
"<a href="#notifierid" title="NotifierId">NotifierId</a>" : <i>String</i>,
39+
"<a href="#integrationid" title="IntegrationId">IntegrationId</a>" : <i>String</i>
3840
}
3941
</pre>
4042

@@ -69,6 +71,8 @@ To declare this entity in your AWS CloudFormation template, use the following sy
6971
<a href="#victoropsroutingkey" title="VictorOpsRoutingKey">VictorOpsRoutingKey</a>: <i>String</i>
7072
<a href="#webhooksecret" title="WebhookSecret">WebhookSecret</a>: <i>String</i>
7173
<a href="#webhookurl" title="WebhookUrl">WebhookUrl</a>: <i>String</i>
74+
<a href="#notifierid" title="NotifierId">NotifierId</a>: <i>String</i>
75+
<a href="#integrationid" title="IntegrationId">IntegrationId</a>: <i>String</i>
7276
</pre>
7377

7478
## Properties
@@ -390,3 +394,35 @@ _Type_: String
390394

391395
_Update requires_: [No interruption](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/using-cfn-updating-stacks-update-behaviors.html#update-no-interrupt)
392396

397+
#### NotifierId
398+
399+
Unique 24-hexadecimal digit string that identifies the notifier to use for this alert configuration.
400+
401+
_Required_: No
402+
403+
_Type_: String
404+
405+
_Minimum Length_: <code>24</code>
406+
407+
_Maximum Length_: <code>24</code>
408+
409+
_Pattern_: <code>^([a-f0-9]{24})$</code>
410+
411+
_Update requires_: [No interruption](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/using-cfn-updating-stacks-update-behaviors.html#update-no-interrupt)
412+
413+
#### IntegrationId
414+
415+
Unique 24-hexadecimal digit string that identifies the third party integration to use for this alert configuration.
416+
417+
_Required_: No
418+
419+
_Type_: String
420+
421+
_Minimum Length_: <code>24</code>
422+
423+
_Maximum Length_: <code>24</code>
424+
425+
_Pattern_: <code>^([a-f0-9]{24})$</code>
426+
427+
_Update requires_: [No interruption](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/using-cfn-updating-stacks-update-behaviors.html#update-no-interrupt)
428+

0 commit comments

Comments
 (0)