@@ -4,8 +4,12 @@ import (
4
4
"context"
5
5
"fmt"
6
6
7
+ "github.com/hashicorp/hcl/v2/hclwrite"
7
8
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
8
9
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
10
+ "github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
11
+ "github.com/zclconf/go-cty/cty"
12
+ matlas "go.mongodb.org/atlas/mongodbatlas"
9
13
)
10
14
11
15
func dataSourceMongoDBAtlasAlertConfiguration () * schema.Resource {
@@ -245,6 +249,27 @@ func dataSourceMongoDBAtlasAlertConfiguration() *schema.Resource {
245
249
},
246
250
},
247
251
},
252
+ "output" : {
253
+ Type : schema .TypeList ,
254
+ Optional : true ,
255
+ Elem : & schema.Resource {
256
+ Schema : map [string ]* schema.Schema {
257
+ "type" : {
258
+ Type : schema .TypeString ,
259
+ Required : true ,
260
+ ValidateFunc : validation .StringInSlice ([]string {"resource_hcl" , "resource_import" }, false ),
261
+ },
262
+ "label" : {
263
+ Type : schema .TypeString ,
264
+ Optional : true ,
265
+ },
266
+ "value" : {
267
+ Type : schema .TypeString ,
268
+ Computed : true ,
269
+ },
270
+ },
271
+ },
272
+ },
248
273
},
249
274
}
250
275
}
@@ -296,10 +321,199 @@ func dataSourceMongoDBAtlasAlertConfigurationRead(ctx context.Context, d *schema
296
321
return diag .FromErr (fmt .Errorf (errorAlertConfSetting , "notification" , projectID , err ))
297
322
}
298
323
324
+ if dOutput := d .Get ("output" ); dOutput != nil {
325
+ if err := d .Set ("output" , computeAlertConfigurationOutput (alert , dOutput .([]interface {}), alert .EventTypeName )); err != nil {
326
+ return diag .FromErr (fmt .Errorf (errorAlertConfSetting , "output" , projectID , err ))
327
+ }
328
+ }
329
+
299
330
d .SetId (encodeStateID (map [string ]string {
300
331
"id" : alert .ID ,
301
332
"project_id" : projectID ,
302
333
}))
303
334
304
335
return nil
305
336
}
337
+
338
+ func computeAlertConfigurationOutput (alert * matlas.AlertConfiguration , outputConfigurations []interface {}, defaultLabel string ) []map [string ]interface {} {
339
+ output := make ([]map [string ]interface {}, 0 )
340
+
341
+ for i := 0 ; i < len (outputConfigurations ); i ++ {
342
+ config := outputConfigurations [i ].(map [string ]interface {})
343
+ var o = map [string ]interface {}{
344
+ "type" : config ["type" ],
345
+ }
346
+
347
+ if label , ok := o ["label" ]; ok {
348
+ o ["label" ] = label
349
+ } else {
350
+ o ["label" ] = defaultLabel
351
+ }
352
+
353
+ if outputValue := outputAlertConfiguration (alert , o ["type" ].(string ), o ["label" ].(string )); outputValue != "" {
354
+ o ["value" ] = outputValue
355
+ }
356
+
357
+ output = append (output , o )
358
+ }
359
+
360
+ return output
361
+ }
362
+
363
+ func outputAlertConfiguration (alert * matlas.AlertConfiguration , outputType , resourceLabel string ) string {
364
+ if outputType == "resource_hcl" {
365
+ return outputAlertConfigurationResourceHcl (resourceLabel , alert )
366
+ }
367
+ if outputType == "resource_import" {
368
+ return outputAlertConfigurationResourceImport (resourceLabel , alert )
369
+ }
370
+
371
+ return ""
372
+ }
373
+
374
+ func outputAlertConfigurationResourceHcl (label string , alert * matlas.AlertConfiguration ) string {
375
+ f := hclwrite .NewEmptyFile ()
376
+ root := f .Body ()
377
+ resource := root .AppendNewBlock ("resource" , []string {"mongodbatlas_alert_configuration" , label }).Body ()
378
+
379
+ resource .SetAttributeValue ("project_id" , cty .StringVal (alert .GroupID ))
380
+ resource .SetAttributeValue ("event_type" , cty .StringVal (alert .EventTypeName ))
381
+
382
+ if alert .Enabled != nil {
383
+ resource .SetAttributeValue ("enabled" , cty .BoolVal (* alert .Enabled ))
384
+ }
385
+
386
+ for _ , matcher := range alert .Matchers {
387
+ values := convertMatcherToCtyValues (matcher )
388
+
389
+ appendBlockWithCtyValues (resource , "matcher" , []string {}, values )
390
+ }
391
+
392
+ if alert .MetricThreshold != nil {
393
+ values := convertMetricThresholdToCtyValues (* alert .MetricThreshold )
394
+
395
+ appendBlockWithCtyValues (resource , "metric_threshold_config" , []string {}, values )
396
+ }
397
+
398
+ if alert .Threshold != nil {
399
+ values := convertThresholdToCtyValues (* alert .Threshold )
400
+
401
+ appendBlockWithCtyValues (resource , "threshold_config" , []string {}, values )
402
+ }
403
+
404
+ for i := 0 ; i < len (alert .Notifications ); i ++ {
405
+ values := convertNotificationToCtyValues (& alert .Notifications [i ])
406
+
407
+ appendBlockWithCtyValues (resource , "notification" , []string {}, values )
408
+ }
409
+
410
+ return string (f .Bytes ())
411
+ }
412
+
413
+ func outputAlertConfigurationResourceImport (label string , alert * matlas.AlertConfiguration ) string {
414
+ return fmt .Sprintf ("terraform import mongodbatlas_alert_configuration.%s %s-%s\n " , label , alert .GroupID , alert .ID )
415
+ }
416
+
417
+ func convertMatcherToCtyValues (matcher matlas.Matcher ) map [string ]cty.Value {
418
+ return map [string ]cty.Value {
419
+ "field_name" : cty .StringVal (matcher .FieldName ),
420
+ "operator" : cty .StringVal (matcher .Operator ),
421
+ "value" : cty .StringVal (matcher .Value ),
422
+ }
423
+ }
424
+
425
+ func convertMetricThresholdToCtyValues (metric matlas.MetricThreshold ) map [string ]cty.Value {
426
+ return map [string ]cty.Value {
427
+ "metric_name" : cty .StringVal (metric .MetricName ),
428
+ "operator" : cty .StringVal (metric .Operator ),
429
+ "threshold" : cty .NumberFloatVal (metric .Threshold ),
430
+ "units" : cty .StringVal (metric .Units ),
431
+ "mode" : cty .StringVal (metric .Mode ),
432
+ }
433
+ }
434
+
435
+ func convertThresholdToCtyValues (threshold matlas.Threshold ) map [string ]cty.Value {
436
+ return map [string ]cty.Value {
437
+ "operator" : cty .StringVal (threshold .Operator ),
438
+ "units" : cty .StringVal (threshold .Units ),
439
+ "threshold" : cty .NumberFloatVal (threshold .Threshold ),
440
+ }
441
+ }
442
+
443
+ func convertNotificationToCtyValues (notification * matlas.Notification ) map [string ]cty.Value {
444
+ values := map [string ]cty.Value {}
445
+
446
+ if notification .ChannelName != "" {
447
+ values ["channel_name" ] = cty .StringVal (notification .ChannelName )
448
+ }
449
+
450
+ if notification .DatadogRegion != "" {
451
+ values ["datadog_region" ] = cty .StringVal (notification .DatadogRegion )
452
+ }
453
+
454
+ if notification .EmailAddress != "" {
455
+ values ["email_address" ] = cty .StringVal (notification .EmailAddress )
456
+ }
457
+
458
+ if notification .FlowName != "" {
459
+ values ["flow_name" ] = cty .StringVal (notification .FlowName )
460
+ }
461
+
462
+ if notification .IntervalMin > 0 {
463
+ values ["interval_min" ] = cty .NumberIntVal (int64 (notification .IntervalMin ))
464
+ }
465
+
466
+ if notification .MobileNumber != "" {
467
+ values ["mobile_number" ] = cty .StringVal (notification .MobileNumber )
468
+ }
469
+
470
+ if notification .OpsGenieRegion != "" {
471
+ values ["ops_genie_region" ] = cty .StringVal (notification .OpsGenieRegion )
472
+ }
473
+
474
+ if notification .OrgName != "" {
475
+ values ["org_name" ] = cty .StringVal (notification .OrgName )
476
+ }
477
+
478
+ if notification .TeamID != "" {
479
+ values ["team_id" ] = cty .StringVal (notification .TeamID )
480
+ }
481
+
482
+ if notification .TeamName != "" {
483
+ values ["team_name" ] = cty .StringVal (notification .TeamName )
484
+ }
485
+
486
+ if notification .TypeName != "" {
487
+ values ["type_name" ] = cty .StringVal (notification .TypeName )
488
+ }
489
+
490
+ if notification .Username != "" {
491
+ values ["username" ] = cty .StringVal (notification .Username )
492
+ }
493
+
494
+ if notification .DelayMin != nil && * notification .DelayMin > 0 {
495
+ values ["delay_min" ] = cty .NumberIntVal (int64 (* notification .DelayMin ))
496
+ }
497
+
498
+ if notification .EmailEnabled != nil && * notification .EmailEnabled {
499
+ values ["email_enabled" ] = cty .BoolVal (* notification .EmailEnabled )
500
+ }
501
+
502
+ if notification .SMSEnabled != nil && * notification .SMSEnabled {
503
+ values ["sms_enabled" ] = cty .BoolVal (* notification .SMSEnabled )
504
+ }
505
+
506
+ if len (notification .Roles ) > 0 {
507
+ roles := make ([]cty.Value , 0 )
508
+
509
+ for _ , r := range notification .Roles {
510
+ if r != "" {
511
+ roles = append (roles , cty .StringVal (r ))
512
+ }
513
+ }
514
+
515
+ values ["roles" ] = cty .TupleVal (roles )
516
+ }
517
+
518
+ return values
519
+ }
0 commit comments