@@ -10,8 +10,9 @@ import (
10
10
"github.com/hashicorp/terraform-plugin-framework/datasource/schema"
11
11
"github.com/hashicorp/terraform-plugin-framework/schema/validator"
12
12
"github.com/hashicorp/terraform-plugin-framework/types"
13
+ "github.com/mongodb/terraform-provider-mongodbatlas/mongodbatlas/util"
13
14
"github.com/zclconf/go-cty/cty"
14
- matlas "go.mongodb.org/atlas/mongodbatlas "
15
+ "go.mongodb.org/atlas-sdk/v20230201006/admin "
15
16
)
16
17
17
18
var _ datasource.DataSource = & AlertConfigurationDS {}
@@ -244,7 +245,6 @@ func (d *AlertConfigurationDS) Schema(ctx context.Context, req datasource.Schema
244
245
245
246
func (d * AlertConfigurationDS ) Read (ctx context.Context , req datasource.ReadRequest , resp * datasource.ReadResponse ) {
246
247
var alertConfigurationConfig tfAlertConfigurationDSModel
247
- conn := d .client .Atlas
248
248
249
249
resp .Diagnostics .Append (req .Config .Get (ctx , & alertConfigurationConfig )... )
250
250
if resp .Diagnostics .HasError () {
@@ -257,23 +257,23 @@ func (d *AlertConfigurationDS) Read(ctx context.Context, req datasource.ReadRequ
257
257
alertID := getEncodedID (alertConfigurationConfig .AlertConfigurationID .ValueString (), encodedIDKeyAlertID )
258
258
outputs := alertConfigurationConfig .Output
259
259
260
- alert , _ , err := conn .AlertConfigurations .GetAnAlertConfig (ctx , projectID , alertID )
260
+ connV2 := d .client .AtlasV2
261
+ alert , _ , err := connV2 .AlertConfigurationsApi .GetAlertConfiguration (ctx , projectID , alertID ).Execute ()
261
262
if err != nil {
262
263
resp .Diagnostics .AddError (errorReadAlertConf , err .Error ())
263
264
return
264
265
}
265
266
266
267
resultAlertConfigModel := newTFAlertConfigurationDSModel (alert , projectID )
267
- computedOutputs := computeAlertConfigurationOutput (alert , outputs , alert .EventTypeName )
268
- resultAlertConfigModel .Output = computedOutputs
268
+ resultAlertConfigModel .Output = computeAlertConfigurationOutput (alert , outputs , * alert .EventTypeName )
269
269
270
270
// setting initial value for backwards compatibility, but setting the alert_configuration resource id here is not consistent with the resource
271
271
resultAlertConfigModel .AlertConfigurationID = alertConfigurationConfig .AlertConfigurationID
272
272
273
273
resp .Diagnostics .Append (resp .State .Set (ctx , & resultAlertConfigModel )... )
274
274
}
275
275
276
- func computeAlertConfigurationOutput (alert * matlas. AlertConfiguration , definedOutputs []tfAlertConfigurationOutputModel , defaultLabel string ) []tfAlertConfigurationOutputModel {
276
+ func computeAlertConfigurationOutput (alert * admin. GroupAlertsConfig , definedOutputs []tfAlertConfigurationOutputModel , defaultLabel string ) []tfAlertConfigurationOutputModel {
277
277
resultOutputs := make ([]tfAlertConfigurationOutputModel , len (definedOutputs ))
278
278
for i , defined := range definedOutputs {
279
279
resultOutput := tfAlertConfigurationOutputModel {}
@@ -291,26 +291,26 @@ func computeAlertConfigurationOutput(alert *matlas.AlertConfiguration, definedOu
291
291
return resultOutputs
292
292
}
293
293
294
- func newTFAlertConfigurationDSModel (apiRespConfig * matlas. AlertConfiguration , projectID string ) tfAlertConfigurationDSModel {
294
+ func newTFAlertConfigurationDSModel (apiRespConfig * admin. GroupAlertsConfig , projectID string ) tfAlertConfigurationDSModel {
295
295
return tfAlertConfigurationDSModel {
296
296
ID : types .StringValue (encodeStateID (map [string ]string {
297
- encodedIDKeyAlertID : apiRespConfig .ID ,
297
+ encodedIDKeyAlertID : * apiRespConfig .Id ,
298
298
encodedIDKeyProjectID : projectID ,
299
299
})),
300
300
ProjectID : types .StringValue (projectID ),
301
- AlertConfigurationID : types .StringValue (apiRespConfig .ID ),
302
- EventType : types .StringValue (apiRespConfig .EventTypeName ),
303
- Created : types .StringValue ( apiRespConfig .Created ),
304
- Updated : types .StringValue ( apiRespConfig .Updated ),
301
+ AlertConfigurationID : types .StringValue (* apiRespConfig .Id ),
302
+ EventType : types .StringValue (* apiRespConfig .EventTypeName ),
303
+ Created : types .StringPointerValue ( util . TimePtrToStringPtr ( apiRespConfig .Created ) ),
304
+ Updated : types .StringPointerValue ( util . TimePtrToStringPtr ( apiRespConfig .Updated ) ),
305
305
Enabled : types .BoolPointerValue (apiRespConfig .Enabled ),
306
- MetricThresholdConfig : newTFMetricThresholdConfigModel (apiRespConfig .MetricThreshold , []tfMetricThresholdConfigModel {}),
307
- ThresholdConfig : newTFThresholdConfigModel (apiRespConfig .Threshold , []tfThresholdConfigModel {}),
308
- Notification : newTFNotificationModelList (apiRespConfig .Notifications , []tfNotificationModel {}),
309
- Matcher : newTFMatcherModelList (apiRespConfig .Matchers , []tfMatcherModel {}),
306
+ MetricThresholdConfig : newTFMetricThresholdConfigModelV2 (apiRespConfig .MetricThreshold , []tfMetricThresholdConfigModel {}),
307
+ ThresholdConfig : newTFThresholdConfigModelV2 (apiRespConfig .Threshold , []tfThresholdConfigModel {}),
308
+ Notification : newTFNotificationModelListV2 (apiRespConfig .Notifications , []tfNotificationModel {}),
309
+ Matcher : newTFMatcherModelListV2 (apiRespConfig .Matchers , []tfMatcherModel {}),
310
310
}
311
311
}
312
312
313
- func outputAlertConfiguration (alert * matlas. AlertConfiguration , outputType , resourceLabel string ) string {
313
+ func outputAlertConfiguration (alert * admin. GroupAlertsConfig , outputType , resourceLabel string ) string {
314
314
if outputType == "resource_hcl" {
315
315
return outputAlertConfigurationResourceHcl (resourceLabel , alert )
316
316
}
@@ -321,124 +321,119 @@ func outputAlertConfiguration(alert *matlas.AlertConfiguration, outputType, reso
321
321
return ""
322
322
}
323
323
324
- func outputAlertConfigurationResourceHcl (label string , alert * matlas. AlertConfiguration ) string {
324
+ func outputAlertConfigurationResourceHcl (label string , alert * admin. GroupAlertsConfig ) string {
325
325
f := hclwrite .NewEmptyFile ()
326
326
root := f .Body ()
327
327
resource := root .AppendNewBlock ("resource" , []string {"mongodbatlas_alert_configuration" , label }).Body ()
328
328
329
- resource .SetAttributeValue ("project_id" , cty .StringVal (alert .GroupID ))
330
- resource .SetAttributeValue ("event_type" , cty .StringVal (alert .EventTypeName ))
329
+ resource .SetAttributeValue ("project_id" , cty .StringVal (* alert .GroupId ))
330
+ resource .SetAttributeValue ("event_type" , cty .StringVal (* alert .EventTypeName ))
331
331
332
332
if alert .Enabled != nil {
333
333
resource .SetAttributeValue ("enabled" , cty .BoolVal (* alert .Enabled ))
334
334
}
335
335
336
336
for _ , matcher := range alert .Matchers {
337
- values := convertMatcherToCtyValues (matcher )
338
-
339
- appendBlockWithCtyValues (resource , "matcher" , []string {}, values )
337
+ appendBlockWithCtyValues (resource , "matcher" , []string {}, convertMatcherToCtyValues (matcher ))
340
338
}
341
339
342
340
if alert .MetricThreshold != nil {
343
- values := convertMetricThresholdToCtyValues (* alert .MetricThreshold )
344
-
345
- appendBlockWithCtyValues (resource , "metric_threshold_config" , []string {}, values )
341
+ appendBlockWithCtyValues (resource , "metric_threshold_config" , []string {}, convertMetricThresholdToCtyValues (* alert .MetricThreshold ))
346
342
}
347
343
348
344
if alert .Threshold != nil {
349
- values := convertThresholdToCtyValues (* alert .Threshold )
350
-
351
- appendBlockWithCtyValues (resource , "threshold_config" , []string {}, values )
345
+ appendBlockWithCtyValues (resource , "threshold_config" , []string {}, convertThresholdToCtyValues (alert .Threshold ))
352
346
}
353
347
354
348
for i := 0 ; i < len (alert .Notifications ); i ++ {
355
- values := convertNotificationToCtyValues (& alert .Notifications [i ])
356
-
357
- appendBlockWithCtyValues (resource , "notification" , []string {}, values )
349
+ appendBlockWithCtyValues (resource , "notification" , []string {}, convertNotificationToCtyValues (& alert .Notifications [i ]))
358
350
}
359
351
360
352
return string (f .Bytes ())
361
353
}
362
354
363
- func outputAlertConfigurationResourceImport (label string , alert * matlas. AlertConfiguration ) string {
364
- return fmt .Sprintf ("terraform import mongodbatlas_alert_configuration.%s %s-%s\n " , label , alert .GroupID , alert .ID )
355
+ func outputAlertConfigurationResourceImport (label string , alert * admin. GroupAlertsConfig ) string {
356
+ return fmt .Sprintf ("terraform import mongodbatlas_alert_configuration.%s %s-%s\n " , label , * alert .GroupId , * alert .Id )
365
357
}
366
358
367
- func convertMatcherToCtyValues (matcher matlas.Matcher ) map [string ]cty.Value {
359
+ func convertMatcherToCtyValues (matcher map [string ]interface {}) map [string ]cty.Value {
360
+ fieldName , _ := matcher ["fieldName" ].(string )
361
+ operator , _ := matcher ["operator" ].(string )
362
+ value , _ := matcher ["value" ].(string )
368
363
return map [string ]cty.Value {
369
- "field_name" : cty .StringVal (matcher . FieldName ),
370
- "operator" : cty .StringVal (matcher . Operator ),
371
- "value" : cty .StringVal (matcher . Value ),
364
+ "field_name" : cty .StringVal (fieldName ),
365
+ "operator" : cty .StringVal (operator ),
366
+ "value" : cty .StringVal (value ),
372
367
}
373
368
}
374
369
375
- func convertMetricThresholdToCtyValues (metric matlas.MetricThreshold ) map [string ]cty.Value {
370
+ func convertMetricThresholdToCtyValues (metric admin.ServerlessMetricThreshold ) map [string ]cty.Value {
371
+ var t float64
372
+ if metric .Threshold != nil {
373
+ t = * metric .Threshold
374
+ }
376
375
return map [string ]cty.Value {
377
- "metric_name" : cty . StringVal (metric .MetricName ),
378
- "operator" : cty . StringVal (metric .Operator ),
379
- "threshold" : cty .NumberFloatVal (metric . Threshold ),
380
- "units" : cty . StringVal (metric .Units ),
381
- "mode" : cty . StringVal (metric .Mode ),
376
+ "metric_name" : ctyStringPtrVal (metric .MetricName ),
377
+ "operator" : ctyStringPtrVal (metric .Operator ),
378
+ "threshold" : cty .NumberFloatVal (t ),
379
+ "units" : ctyStringPtrVal (metric .Units ),
380
+ "mode" : ctyStringPtrVal (metric .Mode ),
382
381
}
383
382
}
384
383
385
- func convertThresholdToCtyValues (threshold matlas.Threshold ) map [string ]cty.Value {
384
+ func convertThresholdToCtyValues (threshold * admin.GreaterThanRawThreshold ) map [string ]cty.Value {
385
+ var t int
386
+ if threshold .Threshold != nil {
387
+ t = * threshold .Threshold
388
+ }
386
389
return map [string ]cty.Value {
387
- "operator" : cty . StringVal (threshold .Operator ),
388
- "units" : cty . StringVal (threshold .Units ),
389
- "threshold" : cty .NumberFloatVal (threshold . Threshold ),
390
+ "operator" : ctyStringPtrVal (threshold .Operator ),
391
+ "units" : ctyStringPtrVal (threshold .Units ),
392
+ "threshold" : cty .NumberFloatVal (float64 ( t )), // int in new SDK but keeping float64 for backward compatibility
390
393
}
391
394
}
392
395
393
- func convertNotificationToCtyValues (notification * matlas. Notification ) map [string ]cty.Value {
396
+ func convertNotificationToCtyValues (notification * admin. AlertsNotificationRootForGroup ) map [string ]cty.Value {
394
397
values := map [string ]cty.Value {}
395
398
396
- if notification .ChannelName != "" {
397
- values ["channel_name" ] = cty .StringVal (notification .ChannelName )
399
+ if util . IsStringPresent ( notification .ChannelName ) {
400
+ values ["channel_name" ] = cty .StringVal (* notification .ChannelName )
398
401
}
399
402
400
- if notification .DatadogRegion != "" {
401
- values ["datadog_region" ] = cty .StringVal (notification .DatadogRegion )
403
+ if util . IsStringPresent ( notification .DatadogRegion ) {
404
+ values ["datadog_region" ] = cty .StringVal (* notification .DatadogRegion )
402
405
}
403
406
404
- if notification .EmailAddress != "" {
405
- values ["email_address" ] = cty .StringVal (notification .EmailAddress )
407
+ if util . IsStringPresent ( notification .EmailAddress ) {
408
+ values ["email_address" ] = cty .StringVal (* notification .EmailAddress )
406
409
}
407
410
408
- if notification .FlowName != "" {
409
- values ["flow_name " ] = cty .StringVal ( notification .FlowName )
411
+ if notification .IntervalMin != nil && * notification . IntervalMin > 0 {
412
+ values ["interval_min " ] = cty .NumberIntVal ( int64 ( * notification .IntervalMin ) )
410
413
}
411
414
412
- if notification .IntervalMin > 0 {
413
- values ["interval_min " ] = cty .NumberIntVal ( int64 ( notification .IntervalMin ) )
415
+ if util . IsStringPresent ( notification .MobileNumber ) {
416
+ values ["mobile_number " ] = cty .StringVal ( * notification .MobileNumber )
414
417
}
415
418
416
- if notification .MobileNumber != "" {
417
- values ["mobile_number " ] = cty .StringVal (notification .MobileNumber )
419
+ if util . IsStringPresent ( notification .OpsGenieRegion ) {
420
+ values ["ops_genie_region " ] = cty .StringVal (* notification .OpsGenieRegion )
418
421
}
419
422
420
- if notification .OpsGenieRegion != "" {
421
- values ["ops_genie_region " ] = cty .StringVal (notification .OpsGenieRegion )
423
+ if util . IsStringPresent ( notification .TeamId ) {
424
+ values ["team_id " ] = cty .StringVal (* notification .TeamId )
422
425
}
423
426
424
- if notification .OrgName != "" {
425
- values ["org_name " ] = cty .StringVal (notification .OrgName )
427
+ if util . IsStringPresent ( notification .TeamName ) {
428
+ values ["team_name " ] = cty .StringVal (* notification .TeamName )
426
429
}
427
430
428
- if notification .TeamID != "" {
429
- values ["team_id " ] = cty .StringVal (notification .TeamID )
431
+ if util . IsStringPresent ( notification .TypeName ) {
432
+ values ["type_name " ] = cty .StringVal (* notification .TypeName )
430
433
}
431
434
432
- if notification .TeamName != "" {
433
- values ["team_name" ] = cty .StringVal (notification .TeamName )
434
- }
435
-
436
- if notification .TypeName != "" {
437
- values ["type_name" ] = cty .StringVal (notification .TypeName )
438
- }
439
-
440
- if notification .Username != "" {
441
- values ["username" ] = cty .StringVal (notification .Username )
435
+ if util .IsStringPresent (notification .Username ) {
436
+ values ["username" ] = cty .StringVal (* notification .Username )
442
437
}
443
438
444
439
if notification .DelayMin != nil && * notification .DelayMin > 0 {
@@ -449,8 +444,8 @@ func convertNotificationToCtyValues(notification *matlas.Notification) map[strin
449
444
values ["email_enabled" ] = cty .BoolVal (* notification .EmailEnabled )
450
445
}
451
446
452
- if notification .SMSEnabled != nil && * notification .SMSEnabled {
453
- values ["sms_enabled" ] = cty .BoolVal (* notification .SMSEnabled )
447
+ if notification .SmsEnabled != nil && * notification .SmsEnabled {
448
+ values ["sms_enabled" ] = cty .BoolVal (* notification .SmsEnabled )
454
449
}
455
450
456
451
if len (notification .Roles ) > 0 {
@@ -467,3 +462,10 @@ func convertNotificationToCtyValues(notification *matlas.Notification) map[strin
467
462
468
463
return values
469
464
}
465
+
466
+ func ctyStringPtrVal (ptr * string ) cty.Value {
467
+ if ptr == nil {
468
+ return cty .StringVal ("" )
469
+ }
470
+ return cty .StringVal (* ptr )
471
+ }
0 commit comments