Skip to content

Commit 2fb30bd

Browse files
committed
Got tests passing, added documentation for alert_configuration data_source
1 parent 4f974d8 commit 2fb30bd

File tree

5 files changed

+109
-4
lines changed

5 files changed

+109
-4
lines changed

examples/atlas-alert-configurations/alert-configurations-data.tf

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,3 +27,10 @@ output "alert_resources" {
2727
output "alert_imports" {
2828
value = join("", local.alert_imports)
2929
}
30+
31+
data "mongodbatlas_alert_configurations" "test" {
32+
project_id = "%s"
33+
list_options {
34+
page_num = 0
35+
}
36+
}

mongodbatlas/data_source_mongodbatlas_alert_configuration.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -322,7 +322,7 @@ func dataSourceMongoDBAtlasAlertConfigurationRead(ctx context.Context, d *schema
322322
}
323323

324324
if dOutput := d.Get("output"); dOutput != nil {
325-
if err := d.Set("output", computeAlertConfigurationOutput(alert, dOutput.([]map[string]interface{}), alert.EventTypeName)); err != nil {
325+
if err := d.Set("output", computeAlertConfigurationOutput(alert, dOutput.([]interface{}), alert.EventTypeName)); err != nil {
326326
return diag.FromErr(fmt.Errorf(errorAlertConfSetting, "output", projectID, err))
327327
}
328328
}
@@ -335,10 +335,11 @@ func dataSourceMongoDBAtlasAlertConfigurationRead(ctx context.Context, d *schema
335335
return nil
336336
}
337337

338-
func computeAlertConfigurationOutput(alert *matlas.AlertConfiguration, outputConfigurations []map[string]interface{}, defaultLabel string) []map[string]interface{} {
338+
func computeAlertConfigurationOutput(alert *matlas.AlertConfiguration, outputConfigurations []interface{}, defaultLabel string) []map[string]interface{} {
339339
output := make([]map[string]interface{}, 0)
340340

341-
for _, config := range outputConfigurations {
341+
for i := 0; i < len(outputConfigurations); i++ {
342+
config := outputConfigurations[i].(map[string]interface{})
342343
var o = map[string]interface{}{
343344
"type": config["type"],
344345
}

mongodbatlas/data_source_mongodbatlas_alert_configurations.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ func dataSourceMongoDBAtlasAlertConfigurationsRead(ctx context.Context, d *schem
117117
}
118118

119119
func flattenAlertConfigurations(ctx context.Context, conn *matlas.Client, alerts []matlas.AlertConfiguration, d *schema.ResourceData) []map[string]interface{} {
120-
var outputConfigurations []map[string]interface{}
120+
var outputConfigurations []interface{}
121121

122122
results := make([]map[string]interface{}, 0)
123123

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
package mongodbatlas
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"os"
7+
"testing"
8+
9+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
10+
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
11+
matlas "go.mongodb.org/atlas/mongodbatlas"
12+
)
13+
14+
func TestAccConfigDSAlertConfigurations_basic(t *testing.T) {
15+
var (
16+
dataSourceName = "data.mongodbatlas_alert_configurations.test"
17+
projectID = os.Getenv("MONGODB_ATLAS_PROJECT_ID")
18+
)
19+
20+
resource.Test(t, resource.TestCase{
21+
PreCheck: func() { testAccPreCheck(t) },
22+
ProviderFactories: testAccProviderFactories,
23+
Steps: []resource.TestStep{
24+
{
25+
Config: testAccDSMongoDBAtlasAlertConfiguration(projectID),
26+
Check: resource.ComposeTestCheckFunc(
27+
testAccCheckMongoDBAtlasAlertConfigurationsCount(dataSourceName),
28+
resource.TestCheckResourceAttrSet(dataSourceName, "project_id"),
29+
),
30+
},
31+
},
32+
})
33+
}
34+
35+
func testAccDSMongoDBAtlasAlertConfigurations(projectID string) string {
36+
return fmt.Sprintf(`
37+
data "mongodbatlas_alert_configurations" "test" {
38+
project_id = "%s"
39+
40+
list_options {
41+
page_num = 0
42+
}
43+
}
44+
`, projectID)
45+
}
46+
47+
func testAccCheckMongoDBAtlasAlertConfigurationsCount(resourceName string) resource.TestCheckFunc {
48+
return func(s *terraform.State) error {
49+
conn := testAccProvider.Meta().(*MongoDBClient).Atlas
50+
51+
rs, ok := s.RootModule().Resources[resourceName]
52+
if !ok {
53+
return fmt.Errorf("not found: %s", resourceName)
54+
}
55+
56+
if rs.Primary.ID == "" {
57+
return fmt.Errorf("no ID is set")
58+
}
59+
60+
ids := decodeStateID(rs.Primary.ID)
61+
projectID := ids["project_id"]
62+
63+
alertResp, _, err := conn.AlertConfigurations.List(context.Background(), projectID, &matlas.ListOptions{
64+
PageNum: 0,
65+
ItemsPerPage: 100,
66+
IncludeCount: true,
67+
})
68+
69+
if err != nil {
70+
return fmt.Errorf("the Alert Configurations List for project (%s) could not be read", projectID)
71+
}
72+
73+
if len(rs.Primary.Attributes["results"]) != len(alertResp) {
74+
return fmt.Errorf("%s results count did not match that of current Alert Configurations", resourceName)
75+
}
76+
77+
return nil
78+
}
79+
}

website/docs/d/alert_configuration.html.markdown

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,24 @@ data "mongodbatlas_alert_configuration" "test" {
8787
}
8888
```
8989

90+
Utilize data_source to generate resource hcl and import statement. Useful if you have a specific alert_configuration_id and are looking to manage it as is in state. To import all alerts, refer to the documentation on [data_source_mongodbatlas_alert_configurations](https://registry.terraform.io/providers/mongodb/mongodbatlas/latest/docs/data-sources/alert_configurations)
91+
```
92+
data "mongodbatlas_alert_configuration" "test" {
93+
project_id = var.project_id
94+
alert_configuration_id = var.alert_configuration_id
95+
96+
output {
97+
type = "resource_hcl"
98+
label = "test"
99+
}
100+
101+
output {
102+
type = "resource_import"
103+
label = "test"
104+
}
105+
}
106+
```
107+
90108
## Argument Reference
91109

92110
* `project_id` - (Required) The ID of the project where the alert configuration will create.

0 commit comments

Comments
 (0)