diff --git a/CHANGELOG.md b/CHANGELOG.md index f0fbcb85..6fc5ab1c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,8 @@ +## 0.13.0 (Unreleased) + +ENHANCEMENTS: +* Adds the attribute for notification priority override to the `firehydrant_signal_rule` resource ([#166](https://github.com/firehydrant/terraform-provider-firehydrant/pull/166)) + ## 0.12.1 REVERTED: diff --git a/docs/resources/signal_rule.md b/docs/resources/signal_rule.md index 90f5986c..2272ba7e 100644 --- a/docs/resources/signal_rule.md +++ b/docs/resources/signal_rule.md @@ -43,6 +43,7 @@ The following arguments are supported: * `target_type` - (Required) The type of resource to send alerts to. Valid values are `EscalationPolicy`, `OnCallSchedule`, and `User`. * `target_id` - (Required) The ID of the resource to send alerts to. * `incident_type_id` - (Optional) The ID of the incident type associated with this rule. +* `notification_priority_override` - (Optional) The priority to assign to notifications generated by this rule. Valid values are `LOW`, `MEDIUM`, and `HIGH`. Defaults to `HIGH`. ## Attributes Reference diff --git a/firehydrant/signals_rules.go b/firehydrant/signals_rules.go index 21891b1c..aeeeb98b 100644 --- a/firehydrant/signals_rules.go +++ b/firehydrant/signals_rules.go @@ -9,6 +9,23 @@ import ( "github.com/pkg/errors" ) +type NotificationPriority string + +const ( + NotificationPriorityLow NotificationPriority = "LOW" + NotificationPriorityMedium NotificationPriority = "MEDIUM" + NotificationPriorityHigh NotificationPriority = "HIGH" +) + +// Validate checks if the NotificationPriority is valid +func (np NotificationPriority) Validate() error { + switch np { + case NotificationPriorityLow, NotificationPriorityMedium, NotificationPriorityHigh: + return nil + } + return fmt.Errorf("invalid notification priority: %s", np) +} + type SignalsRules interface { Get(ctx context.Context, teamID, id string) (*SignalsRuleResponse, error) Create(ctx context.Context, teamID string, createReq CreateSignalsRuleRequest) (*SignalsRuleResponse, error) @@ -33,30 +50,33 @@ type SignalRuleIncidentType struct { } type SignalsRuleResponse struct { - ID string `json:"id"` - Name string `json:"name"` - Expression string `json:"expression"` - Target SignalRuleTarget `json:"target"` - IncidentType SignalRuleIncidentType `json:"incident_type"` + ID string `json:"id"` + Name string `json:"name"` + Expression string `json:"expression"` + Target SignalRuleTarget `json:"target"` + IncidentType SignalRuleIncidentType `json:"incident_type"` + NotificationPriorityOverride NotificationPriority `json:"notification_priority_override,omitempty"` CreatedAt time.Time `json:"created_at"` UpdatedAt time.Time `json:"updated_at"` } type CreateSignalsRuleRequest struct { - Name string `json:"name"` - Expression string `json:"expression"` - TargetType string `json:"target_type"` - TargetID string `json:"target_id"` - IncidentTypeID string `json:"incident_type_id,omitempty"` + Name string `json:"name"` + Expression string `json:"expression"` + TargetType string `json:"target_type"` + TargetID string `json:"target_id"` + IncidentTypeID string `json:"incident_type_id,omitempty"` + NotificationPriorityOverride NotificationPriority `json:"notification_priority_override,omitempty"` } type UpdateSignalsRuleRequest struct { - Name string `json:"name"` - Expression string `json:"expression"` - TargetType string `json:"target_type"` - TargetID string `json:"target_id"` - IncidentTypeID string `json:"incident_type_id,omitempty"` + Name string `json:"name"` + Expression string `json:"expression"` + TargetType string `json:"target_type"` + TargetID string `json:"target_id"` + IncidentTypeID string `json:"incident_type_id,omitempty"` + NotificationPriorityOverride NotificationPriority `json:"notification_priority_override,omitempty"` } func (c *RESTSignalsRulesClient) restClient() *sling.Sling { diff --git a/provider/signal_rule_resource.go b/provider/signal_rule_resource.go index 76ce360d..9545d321 100644 --- a/provider/signal_rule_resource.go +++ b/provider/signal_rule_resource.go @@ -10,6 +10,7 @@ import ( "github.com/hashicorp/terraform-plugin-log/tflog" "github.com/hashicorp/terraform-plugin-sdk/v2/diag" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation" ) func resourceSignalRule() *schema.Resource { @@ -48,6 +49,15 @@ func resourceSignalRule() *schema.Resource { Type: schema.TypeString, Optional: true, }, + "notification_priority_override": { + Type: schema.TypeString, + Optional: true, + ValidateFunc: validation.StringInSlice([]string{ + string(firehydrant.NotificationPriorityLow), + string(firehydrant.NotificationPriorityMedium), + string(firehydrant.NotificationPriorityHigh), + }, false), + }, }, } } @@ -93,6 +103,8 @@ func readResourceFireHydrantSignalRule(ctx context.Context, d *schema.ResourceDa } } + d.Set("notification_priority_override", string(signalRule.NotificationPriorityOverride)) + return diag.Diagnostics{} } @@ -102,11 +114,12 @@ func createResourceFireHydrantSignalRule(ctx context.Context, d *schema.Resource // Construct the create request createRequest := firehydrant.CreateSignalsRuleRequest{ - Name: d.Get("name").(string), - Expression: d.Get("expression").(string), - TargetType: d.Get("target_type").(string), - TargetID: d.Get("target_id").(string), - IncidentTypeID: d.Get("incident_type_id").(string), + Name: d.Get("name").(string), + Expression: d.Get("expression").(string), + TargetType: d.Get("target_type").(string), + TargetID: d.Get("target_id").(string), + IncidentTypeID: d.Get("incident_type_id").(string), + NotificationPriorityOverride: firehydrant.NotificationPriority(d.Get("notification_priority_override").(string)), } // Create the signal rule @@ -131,11 +144,12 @@ func updateResourceFireHydrantSignalRule(ctx context.Context, d *schema.Resource // Construct the update request updateRequest := firehydrant.UpdateSignalsRuleRequest{ - Name: d.Get("name").(string), - Expression: d.Get("expression").(string), - TargetType: d.Get("target_type").(string), - TargetID: d.Get("target_id").(string), - IncidentTypeID: d.Get("incident_type_id").(string), + Name: d.Get("name").(string), + Expression: d.Get("expression").(string), + TargetType: d.Get("target_type").(string), + TargetID: d.Get("target_id").(string), + IncidentTypeID: d.Get("incident_type_id").(string), + NotificationPriorityOverride: firehydrant.NotificationPriority(d.Get("notification_priority_override").(string)), } // Update the signal rule diff --git a/provider/signal_rule_resource_test.go b/provider/signal_rule_resource_test.go index 0e60c14e..5a581722 100644 --- a/provider/signal_rule_resource_test.go +++ b/provider/signal_rule_resource_test.go @@ -3,6 +3,7 @@ package provider import ( "fmt" "os" + "regexp" "testing" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" @@ -15,34 +16,62 @@ func TestAccFireHydrantSignalRule_basic(t *testing.T) { ProviderFactories: defaultProviderFactories(), Steps: []resource.TestStep{ { - Config: testAccFireHydrantSignalRuleConfigBasic(), + Config: testAccFireHydrantSignalRuleConfigBasic("MEDIUM"), Check: resource.ComposeTestCheckFunc( testAccCheckFireHydrantSignalRuleExists("firehydrant_signal_rule.test"), + resource.TestCheckResourceAttr("firehydrant_signal_rule.test", "notification_priority_override", "MEDIUM"), ), }, + { + Config: testAccFireHydrantSignalRuleConfigBasic("LOW"), + Check: resource.ComposeTestCheckFunc( + testAccCheckFireHydrantSignalRuleExists("firehydrant_signal_rule.test"), + resource.TestCheckResourceAttr("firehydrant_signal_rule.test", "notification_priority_override", "LOW"), + ), + }, + { + Config: testAccFireHydrantSignalRuleConfigBasic("HIGH"), + Check: resource.ComposeTestCheckFunc( + testAccCheckFireHydrantSignalRuleExists("firehydrant_signal_rule.test"), + resource.TestCheckResourceAttr("firehydrant_signal_rule.test", "notification_priority_override", "HIGH"), + ), + }, + }, + }) +} + +func TestAccFireHydrantSignalRule_invalidPriority(t *testing.T) { + resource.Test(t, resource.TestCase{ + PreCheck: func() { testFireHydrantIsSetup(t) }, + ProviderFactories: defaultProviderFactories(), + Steps: []resource.TestStep{ + { + Config: testAccFireHydrantSignalRuleConfigBasic("INVALID"), + ExpectError: regexp.MustCompile(`expected notification_priority_override to be one of \[LOW MEDIUM HIGH\], got INVALID`), + }, }, }) } -func testAccFireHydrantSignalRuleConfigBasic() string { +func testAccFireHydrantSignalRuleConfigBasic(priority string) string { return fmt.Sprintf(` data "firehydrant_user" "test_user" { - email = "%s" + email = "%s" } resource "firehydrant_team" "test" { - name = "test-team" + name = "test-team" } resource "firehydrant_signal_rule" "test" { - team_id = firehydrant_team.test.id - name = "test-signal-rule" - expression = "signal.summary == 'test-signal-summary'" - target_type = "User" - target_id = data.firehydrant_user.test_user.id - incident_type_id = "daa50f24-a2ba-4e66-a58d-16bca2353453" + team_id = firehydrant_team.test.id + name = "test-signal-rule" + expression = "signal.summary == 'test-signal-summary'" + target_type = "User" + target_id = data.firehydrant_user.test_user.id + notification_priority_override = "%s" } - `, os.Getenv("EXISTING_USER_EMAIL")) + `, os.Getenv("EXISTING_USER_EMAIL"), priority) } func TestAccFireHydrantSignalRule_IncidentTypeIDMissing(t *testing.T) {