Skip to content

Commit

Permalink
Fix #227 #232: Added a new Threshold attribute (#298)
Browse files Browse the repository at this point in the history
* feat: added threshold attribute

* test: implemented test case to verify the new threshold attribute

* chore: added MongoDB client branch alert_config temporaly, once it's merge we will change it

* chore: added missing verification

* test: added a import case to the threshold test case to verify the import behavior

* doc: updated resource documentation adding the Threshold attribute

* chore: added threshold attribute and the missing roles attribute on the alert configuration datasource

* test: added a test case on the alert configuration data source to verify the threshold attribute

* doc updated data source documentation adding the Threshold attribute

* chore: removed validations to leave the server to handle the errors

* docs: updated documentation

* chore: updated vendor files and dependecies

* chore: added validation to units attribute

* chore: updated documentation by requested changes
  • Loading branch information
PacoDw authored Aug 26, 2020
1 parent 280900d commit 10aee3c
Show file tree
Hide file tree
Showing 7 changed files with 413 additions and 149 deletions.
3 changes: 2 additions & 1 deletion go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,6 @@ github.com/go-ole/go-ole v1.2.1/go.mod h1:7FAglXiTm7HKlQRDeOQ6ZNUHidzCWXuZWq/1dT
github.com/go-sql-driver/mysql v1.4.0/go.mod h1:zAC/RDZ24gD3HViQzih4MyKcchzm+sOG5ZlKdlhCg5w=
github.com/go-sql-driver/mysql v1.5.0/go.mod h1:DCzpHaOWr8IXmIStZouvnhqoel9Qv2LBy8hT2VhHyBg=
github.com/go-stack/stack v1.8.0/go.mod h1:v0f6uXyyMGvRgIKkXu+yp6POWl0qKG85gN/melR3HDY=
github.com/go-test/deep v1.0.1/go.mod h1:wGDj63lr65AM2AQyKZd/NYHGb0R+1RLqB8NKt3aSFNA=
github.com/go-test/deep v1.0.3 h1:ZrJSEWsXzPOxaZnFteGEfooLba+ju3FYIbOrS+rQd68=
github.com/go-test/deep v1.0.3/go.mod h1:wGDj63lr65AM2AQyKZd/NYHGb0R+1RLqB8NKt3aSFNA=
github.com/go-test/deep v1.0.7 h1:/VSMRlnY/JSyqxQUzQLKVMAskpY/NZKFA5j2P+0pP2M=
Expand Down Expand Up @@ -606,6 +605,8 @@ go.mongodb.org/atlas v0.3.1-0.20200716160607-1b4e8b15eadf h1:uphQUeBtEbNSmlhg1MR
go.mongodb.org/atlas v0.3.1-0.20200716160607-1b4e8b15eadf/go.mod h1:xa/V3muNuVoReSG0y2pigUUnfPOx1cHF0ZV2uCE+c7I=
go.mongodb.org/atlas v0.4.1-0.20200817181428-cd1e61b39f75 h1:aXDdZDxIeD37cn0uVGQl+oHbT//g9OYybLF+R4u2NHk=
go.mongodb.org/atlas v0.4.1-0.20200817181428-cd1e61b39f75/go.mod h1:QlKvZKT43+R6lhHlaTy2E7Q/3AoAljMI6v5apfqslIs=
go.mongodb.org/atlas v0.4.1-0.20200819194203-09c49e85aa0d h1:5GrmSNMip4s758EBOchuRTLarSxpRMOclvUjo9Dez7M=
go.mongodb.org/atlas v0.4.1-0.20200819194203-09c49e85aa0d/go.mod h1:QlKvZKT43+R6lhHlaTy2E7Q/3AoAljMI6v5apfqslIs=
go.mongodb.org/atlas v0.4.1-0.20200820152733-8dc4a7c19a2b h1:AuAQZDrQLesdmz9mIPaIn07OJRoG4Vfm+M3xd31HGgo=
go.mongodb.org/atlas v0.4.1-0.20200820152733-8dc4a7c19a2b/go.mod h1:QlKvZKT43+R6lhHlaTy2E7Q/3AoAljMI6v5apfqslIs=
go.opencensus.io v0.21.0/go.mod h1:mSImk1erAIZhrmZN+AvHh14ztQfjbGwt4TtuofqLduU=
Expand Down
31 changes: 31 additions & 0 deletions mongodbatlas/data_source_mongodbatlas_alert_configuration.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,26 @@ func dataSourceMongoDBAtlasAlertConfiguration() *schema.Resource {
},
},
},
"threshold": {
Type: schema.TypeMap,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"operator": {
Type: schema.TypeString,
Computed: true,
},
"threshold": {
Type: schema.TypeFloat,
Computed: true,
},
"units": {
Type: schema.TypeString,
Computed: true,
},
},
},
},
"notification": {
Type: schema.TypeList,
Computed: true,
Expand Down Expand Up @@ -176,6 +196,13 @@ func dataSourceMongoDBAtlasAlertConfiguration() *schema.Resource {
Type: schema.TypeString,
Computed: true,
},
"roles": {
Type: schema.TypeList,
Optional: true,
Elem: &schema.Schema{
Type: schema.TypeString,
},
},
},
},
},
Expand Down Expand Up @@ -214,6 +241,10 @@ func dataSourceMongoDBAtlasAlertConfigurationRead(d *schema.ResourceData, meta i
return fmt.Errorf(errorAlertConfSetting, "metric_threshold", projectID, err)
}

if err := d.Set("threshold", flattenAlertConfigurationThreshold(alert.Threshold)); err != nil {
return fmt.Errorf(errorAlertConfSetting, "threshold", projectID, err)
}

if err := d.Set("notification", flattenAlertConfigurationNotifications(alert.Notifications)); err != nil {
return fmt.Errorf(errorAlertConfSetting, "notification", projectID, err)
}
Expand Down
59 changes: 59 additions & 0 deletions mongodbatlas/data_source_mongodbatlas_alert_configuration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,29 @@ func TestAccDataSourceMongoDBAtlaAlertConfiguration_basic(t *testing.T) {
})
}

func TestAccDataSourceMongoDBAtlaAlertConfiguration_withThreshold(t *testing.T) {
var (
alert = &matlas.AlertConfiguration{}
dataSourceName = "data.mongodbatlas_alert_configuration.test"
projectID = os.Getenv("MONGODB_ATLAS_PROJECT_ID")
)

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckMongoDBAtlasAlertConfigurationDestroy,
Steps: []resource.TestStep{
{
Config: testAccDSMongoDBAtlasAlertConfigurationConfigWithThreshold(projectID, true, 1),
Check: resource.ComposeTestCheckFunc(
testAccCheckMongoDBAtlasAlertConfigurationExists(dataSourceName, alert),
resource.TestCheckResourceAttrSet(dataSourceName, "project_id"),
),
},
},
})
}

func testAccDSMongoDBAtlasAlertConfiguration(projectID string) string {
return fmt.Sprintf(`
resource "mongodbatlas_alert_configuration" "test" {
Expand Down Expand Up @@ -68,3 +91,39 @@ func testAccDSMongoDBAtlasAlertConfiguration(projectID string) string {
}
`, projectID)
}

func testAccDSMongoDBAtlasAlertConfigurationConfigWithThreshold(projectID string, enabled bool, threshold float64) string {
return fmt.Sprintf(`
resource "mongodbatlas_alert_configuration" "test" {
project_id = "%s"
event_type = "REPLICATION_OPLOG_WINDOW_RUNNING_OUT"
enabled = "%t"
notification {
type_name = "GROUP"
interval_min = 5
delay_min = 0
sms_enabled = false
email_enabled = true
roles = ["GROUP_DATA_ACCESS_READ_ONLY", "GROUP_CLUSTER_MANAGER"]
}
matcher {
field_name = "HOSTNAME_AND_PORT"
operator = "EQUALS"
value = "SECONDARY"
}
threshold = {
operator = "LESS_THAN"
units = "HOURS"
threshold = %f
}
}
data "mongodbatlas_alert_configuration" "test" {
project_id = "${mongodbatlas_alert_configuration.test.project_id}"
alert_configuration_id = "${mongodbatlas_alert_configuration.test.alert_configuration_id}"
}
`, projectID, enabled, threshold)
}
117 changes: 78 additions & 39 deletions mongodbatlas/resource_mongodbatlas_alert_configuration.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"context"
"errors"
"fmt"
"log"
"reflect"
"strings"

Expand Down Expand Up @@ -68,17 +67,10 @@ func resourceMongoDBAtlasAlertConfiguration() *schema.Resource {
"field_name": {
Type: schema.TypeString,
Optional: true,
ValidateFunc: validation.StringInSlice([]string{
"TYPE_NAME", "HOSTNAME", "PORT", "HOSTNAME_AND_PORT",
"REPLICA_SET_NAME", "REPLICA_SET_NAME", "SHARD_NAME",
"CLUSTER_NAME", "CLUSTER_NAME", "SHARD_NAME"}, false),
},
"operator": {
Type: schema.TypeString,
Optional: true,
ValidateFunc: validation.StringInSlice([]string{
"EQUALS", "NOT_EQUALS", "CONTAINS", "NOT_CONTAINS",
"STARTS_WITH", "ENDS_WITH", "REGEX"}, false),
},
"value": {
Type: schema.TypeString,
Expand All @@ -95,17 +87,6 @@ func resourceMongoDBAtlasAlertConfiguration() *schema.Resource {
"metric_name": {
Type: schema.TypeString,
Optional: true,
ValidateFunc: validation.StringInSlice([]string{
"TYPE_NAME",
"HOSTNAME",
"PORT",
"HOSTNAME_AND_PORT",
"REPLICA_SET_NAME",
"REPLICA_SET_NAME",
"SHARD_NAME",
"CLUSTER_NAME",
"CLUSTER_NAME",
"SHARD_NAME"}, false),
},
"operator": {
Type: schema.TypeString,
Expand Down Expand Up @@ -144,6 +125,43 @@ func resourceMongoDBAtlasAlertConfiguration() *schema.Resource {
},
},
},
"threshold": {
Type: schema.TypeMap,
Optional: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"operator": {
Type: schema.TypeString,
Optional: true,
},
"threshold": {
Type: schema.TypeFloat,
Optional: true,
},
"units": {
Type: schema.TypeString,
Optional: true,
ValidateFunc: validation.StringInSlice([]string{
"RAW",
"BITS",
"BYTES",
"KILOBITS",
"KILOBYTES",
"MEGABITS",
"MEGABYTES",
"GIGABITS",
"GIGABYTES",
"TERABYTES",
"PETABYTES",
"MILLISECONDS",
"SECONDS",
"MINUTES",
"HOURS",
"DAYS"}, false),
},
},
},
},
"notification": {
Type: schema.TypeList,
Required: true,
Expand Down Expand Up @@ -222,20 +240,6 @@ func resourceMongoDBAtlasAlertConfiguration() *schema.Resource {
"type_name": {
Type: schema.TypeString,
Optional: true,
ValidateFunc: validation.StringInSlice([]string{
"DATADOG",
"EMAIL",
"FLOWDOCK",
"GROUP",
"OPS_GENIE",
"ORG",
"PAGER_DUTY",
"SLACK",
"SMS",
"TEAM",
"USER",
"VICTOR_OPS",
"WEBHOOK"}, false),
},
"username": {
Type: schema.TypeString,
Expand Down Expand Up @@ -273,6 +277,7 @@ func resourceMongoDBAtlasAlertConfigurationCreate(d *schema.ResourceData, meta i
Enabled: pointy.Bool(d.Get("enabled").(bool)),
Matchers: expandAlertConfigurationMatchers(d),
MetricThreshold: expandAlertConfigurationMetricThreshold(d),
Threshold: expandAlertConfigurationThreshold(d),
Notifications: expandAlertConfigurationNotification(d),
}

Expand Down Expand Up @@ -353,6 +358,10 @@ func resourceMongoDBAtlasAlertConfigurationUpdate(d *schema.ResourceData, meta i
req.MetricThreshold = expandAlertConfigurationMetricThreshold(d)
}

if d.HasChange("threshold") {
req.Threshold = expandAlertConfigurationThreshold(d)
}

if d.HasChange("notification") {
req.Notifications = expandAlertConfigurationNotification(d)
}
Expand Down Expand Up @@ -401,27 +410,31 @@ func resourceMongoDBAtlasAlertConfigurationImportState(d *schema.ResourceData, m
}

if err := d.Set("project_id", alert.GroupID); err != nil {
log.Printf(errorAlertConfSetting, "project_id", id, err)
return nil, fmt.Errorf(errorAlertConfSetting, "project_id", id, err)
}

if err := d.Set("event_type", alert.EventTypeName); err != nil {
log.Printf(errorAlertConfSetting, "event_type", id, err)
return nil, fmt.Errorf(errorAlertConfSetting, "event_type", id, err)
}

if err := d.Set("enabled", alert.Enabled); err != nil {
log.Printf(errorAlertConfSetting, "enabled", id, err)
return nil, fmt.Errorf(errorAlertConfSetting, "enabled", id, err)
}

if err := d.Set("matcher", flattenAlertConfigurationMatchers(alert.Matchers)); err != nil {
log.Printf(errorAlertConfSetting, "matcher", id, err)
return nil, fmt.Errorf(errorAlertConfSetting, "matcher", id, err)
}

if err := d.Set("metric_threshold", flattenAlertConfigurationMetricThreshold(alert.MetricThreshold)); err != nil {
log.Printf(errorAlertConfSetting, "metric_threshold", id, err)
return nil, fmt.Errorf(errorAlertConfSetting, "metric_threshold", id, err)
}

if err := d.Set("threshold", flattenAlertConfigurationThreshold(alert.Threshold)); err != nil {
return nil, fmt.Errorf(errorAlertConfSetting, "metric_threshold", id, err)
}

if err := d.Set("notification", flattenAlertConfigurationNotifications(alert.Notifications)); err != nil {
log.Printf(errorAlertConfSetting, "notification", id, err)
return nil, fmt.Errorf(errorAlertConfSetting, "notification", id, err)
}

d.SetId(encodeStateID(map[string]string{
Expand Down Expand Up @@ -480,6 +493,20 @@ func expandAlertConfigurationMetricThreshold(d *schema.ResourceData) *matlas.Met
return nil
}

func expandAlertConfigurationThreshold(d *schema.ResourceData) *matlas.Threshold {
if value, ok := d.GetOk("threshold"); ok {
v := value.(map[string]interface{})

return &matlas.Threshold{
Operator: cast.ToString(v["operator"]),
Units: cast.ToString(v["units"]),
Threshold: cast.ToFloat64(v["threshold"]),
}
}

return nil
}

func flattenAlertConfigurationMetricThreshold(m *matlas.MetricThreshold) map[string]interface{} {
if m != nil {
return map[string]interface{}{
Expand All @@ -494,6 +521,18 @@ func flattenAlertConfigurationMetricThreshold(m *matlas.MetricThreshold) map[str
return map[string]interface{}{}
}

func flattenAlertConfigurationThreshold(m *matlas.Threshold) map[string]interface{} {
if m != nil {
return map[string]interface{}{
"operator": m.Operator,
"units": m.Units,
"threshold": cast.ToString(m.Threshold),
}
}

return map[string]interface{}{}
}

func expandAlertConfigurationNotification(d *schema.ResourceData) []matlas.Notification {
notifications := make([]matlas.Notification, len(d.Get("notification").([]interface{})))

Expand Down
Loading

0 comments on commit 10aee3c

Please sign in to comment.