Skip to content

Commit

Permalink
Add notification overrides to rules (#166)
Browse files Browse the repository at this point in the history
  • Loading branch information
bobbytables authored Oct 17, 2024
1 parent cca0384 commit fc848dd
Show file tree
Hide file tree
Showing 5 changed files with 105 additions and 36 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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:
Expand Down
1 change: 1 addition & 0 deletions docs/resources/signal_rule.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
50 changes: 35 additions & 15 deletions firehydrant/signals_rules.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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 {
Expand Down
34 changes: 24 additions & 10 deletions provider/signal_rule_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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),
},
},
}
}
Expand Down Expand Up @@ -93,6 +103,8 @@ func readResourceFireHydrantSignalRule(ctx context.Context, d *schema.ResourceDa
}
}

d.Set("notification_priority_override", string(signalRule.NotificationPriorityOverride))

return diag.Diagnostics{}
}

Expand All @@ -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
Expand All @@ -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
Expand Down
51 changes: 40 additions & 11 deletions provider/signal_rule_resource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package provider
import (
"fmt"
"os"
"regexp"
"testing"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
Expand All @@ -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) {
Expand Down

0 comments on commit fc848dd

Please sign in to comment.