Skip to content

Commit

Permalink
Merge pull request #402 from rollbar/pawel/added_rule_enabled_field
Browse files Browse the repository at this point in the history
added "enabled" field for notification rule
  • Loading branch information
pawelsz-rb authored Jul 10, 2024
2 parents 5b51e25 + d4d5339 commit dda6fa2
Show file tree
Hide file tree
Showing 11 changed files with 120 additions and 40 deletions.
1 change: 1 addition & 0 deletions .github/workflows/acceptance.yml
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ jobs:

env:
ROLLBAR_API_KEY: ${{ secrets.ROLLBAR_API_KEY }}
ROLLBAR_PROJECT_API_KEY: ${{ secrets.ROLLBAR_PROJECT_API_KEY }}

steps:

Expand Down
18 changes: 8 additions & 10 deletions .github/workflows/analyze.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ name: Code Quality Analysis

on:
push:
branches: [ master ]
tags: [ v* ]
pull_request:
schedule:
- cron: '0 10 * * 3'
Expand All @@ -13,6 +11,11 @@ jobs:
name: CodeQL
runs-on: ubuntu-latest

permissions:
actions: read
contents: read
security-events: write

steps:
- name: Checkout repository
uses: actions/checkout@v2
Expand All @@ -21,11 +24,6 @@ jobs:
# a pull request then we can checkout the head.
fetch-depth: 2

# If this run was triggered by a pull request event, then checkout
# the head of the pull request instead of the merge commit.
- run: git checkout HEAD^2
if: ${{ github.event_name == 'pull_request' }}

# Restore cache
- uses: actions/cache@v2
with:
Expand All @@ -37,7 +35,7 @@ jobs:

# Initializes the CodeQL tools for scanning.
- name: Initialize CodeQL
uses: github/codeql-action/init@v1
uses: github/codeql-action/init@v2
with:
languages: ${{ matrix.language }}
# If you wish to specify custom queries, you can do so here or in a config file.
Expand All @@ -48,7 +46,7 @@ jobs:
# Autobuild attempts to build any compiled languages (C/C++, C#, or Java).
# If this step fails, then you should remove it and run the build manually (see below)
- name: Autobuild
uses: github/codeql-action/autobuild@v1
uses: github/codeql-action/autobuild@v2

# ℹ️ Command-line programs to run using the OS shell.
# 📚 https://git.io/JvXDl
Expand All @@ -62,7 +60,7 @@ jobs:
# make release

- name: Perform CodeQL Analysis
uses: github/codeql-action/analyze@v1
uses: github/codeql-action/analyze@v2

golangci:
name: golangci-lint
Expand Down
1 change: 1 addition & 0 deletions client/fixtures/notification/create.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"result": [
{
"action": "send_email",
"status": "enabled",
"trigger": "new_item",
"config": {
"teams": [
Expand Down
1 change: 1 addition & 0 deletions client/fixtures/notification/read.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"result": {
"action": "send_email",
"trigger": "new_item",
"status": "disabled",
"config": {},
"id": 5127954,
"filters": [
Expand Down
1 change: 1 addition & 0 deletions client/fixtures/notification/update.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"result": {
"action": "send_email",
"trigger": "new_item",
"status": "disabled",
"config": {
"teams": [
"Owners"
Expand Down
11 changes: 6 additions & 5 deletions client/notification.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2021 Rollbar, Inc.
* Copyright (c) 2024 Rollbar, Inc.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
Expand Down Expand Up @@ -31,6 +31,7 @@ import (

type Notification struct {
ID int `model:"id" mapstructure:"id"`
Status string `model:"status" mapstructure:"status"`
Action string `model:"action" mapstructure:"action"`
Trigger string `model:"trigger" mapstructure:"trigger"`
Channel string `model:"channel" mapstructure:"channel"`
Expand All @@ -39,7 +40,7 @@ type Notification struct {
}

// CreateNotification creates a new Rollbar notification.
func (c *RollbarAPIClient) CreateNotification(channel string, filters, trigger, config interface{}) (*Notification, error) {
func (c *RollbarAPIClient) CreateNotification(channel string, filters, trigger, config interface{}, status string) (*Notification, error) {
c.m.Lock()
defer c.m.Unlock()
u := c.BaseURL + pathNotificationCreate
Expand All @@ -50,7 +51,7 @@ func (c *RollbarAPIClient) CreateNotification(channel string, filters, trigger,
l.Debug().Msg("Creating new notification")

resp, err := c.Resty.R().
SetBody([]map[string]interface{}{{"filters": filters, "trigger": trigger, "config": config}}).
SetBody([]map[string]interface{}{{"filters": filters, "trigger": trigger, "config": config, "status": status}}).
SetResult(notificationsResponse{}).
SetError(ErrorResult{}).
Post(u)
Expand All @@ -70,7 +71,7 @@ func (c *RollbarAPIClient) CreateNotification(channel string, filters, trigger,
}

// UpdateNotification updates a Rollbar notification.
func (c *RollbarAPIClient) UpdateNotification(notificationID int, channel string, filters, trigger, config interface{}) (*Notification, error) {
func (c *RollbarAPIClient) UpdateNotification(notificationID int, channel string, filters, trigger, config interface{}, status string) (*Notification, error) {
c.m.Lock()
defer c.m.Unlock()
u := c.BaseURL + pathNotificationReadOrDeleteOrUpdate
Expand All @@ -80,7 +81,7 @@ func (c *RollbarAPIClient) UpdateNotification(notificationID int, channel string
l.Debug().Msg("Updating notification")

resp, err := c.Resty.R().
SetBody(map[string]interface{}{"filters": filters, "trigger": trigger, "config": config}).
SetBody(map[string]interface{}{"filters": filters, "trigger": trigger, "config": config, "status": status}).
SetResult(notificationResponse{}).
SetError(ErrorResult{}).
SetPathParams(map[string]string{
Expand Down
18 changes: 12 additions & 6 deletions client/notification_test.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2021 Rollbar, Inc.
* Copyright (c) 2024 Rollbar, Inc.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
Expand All @@ -24,10 +24,11 @@ package client

import (
"encoding/json"
"github.com/jarcoal/httpmock"
"net/http"
"strconv"
"strings"

"github.com/jarcoal/httpmock"
)

// TestCreateNotification tests creating a Rollbar notification.
Expand All @@ -38,6 +39,7 @@ func (s *Suite) TestCreateNotification() {
u = strings.ReplaceAll(u, "{channel}", channel)
action := "send_email"
trigger := "new_item"
status := "enabled"
filters := []map[string]interface{}{}
config := map[string]interface{}{}

Expand All @@ -52,14 +54,15 @@ func (s *Suite) TestCreateNotification() {
}

httpmock.RegisterResponder("POST", u, r)
notification, err := s.client.CreateNotification(channel, filters, trigger, config)
notification, err := s.client.CreateNotification(channel, filters, trigger, config, status)
s.Nil(err)
s.Equal(trigger, notification.Trigger)
s.Equal(action, notification.Action)
s.Equal(id, notification.ID)
s.Equal(status, notification.Status)

s.checkServerErrors("POST", u, func() error {
_, err = s.client.CreateNotification(channel, filters, trigger, config)
_, err = s.client.CreateNotification(channel, filters, trigger, config, status)
return err
})
}
Expand All @@ -74,6 +77,7 @@ func (s *Suite) TestUpdateNotification() {

action := "send_email"
trigger := "new_item"
status := "disabled"
filters := []map[string]interface{}{}
config := map[string]interface{}{}

Expand All @@ -88,14 +92,15 @@ func (s *Suite) TestUpdateNotification() {
}

httpmock.RegisterResponder("PUT", u, r)
notification, err := s.client.UpdateNotification(id, channel, filters, trigger, config)
notification, err := s.client.UpdateNotification(id, channel, filters, trigger, config, status)
s.Nil(err)
s.Equal(trigger, notification.Trigger)
s.Equal(action, notification.Action)
s.Equal(id, notification.ID)
s.Equal(status, notification.Status)

s.checkServerErrors("PUT", u, func() error {
_, err = s.client.UpdateNotification(id, channel, filters, trigger, config)
_, err = s.client.UpdateNotification(id, channel, filters, trigger, config, status)
return err
})
}
Expand All @@ -115,6 +120,7 @@ func (s *Suite) TestReadNotification() {
n, err := s.client.ReadNotification(id, channel)
s.Nil(err)
s.Equal("new_item", n.Trigger)
s.Equal("disabled", n.Status)

s.checkServerErrors("GET", u, func() error {
_, err := s.client.ReadNotification(id, channel)
Expand Down
46 changes: 35 additions & 11 deletions rollbar/resource_notification.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2022 Rollbar, Inc.
* Copyright (c) 2024 Rollbar, Inc.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
Expand Down Expand Up @@ -41,6 +41,11 @@ var configMap = map[string][]string{
"webhook": {"url", "format"},
}

const (
enabledStatus = "enabled"
disabledStatus = "disabled"
)

var emailDailySummaryConfigList = []string{"summary_time", "environments", "send_only_if_data", "min_item_level"}

func CustomNotificationImport(ctx context.Context, d *schema.ResourceData, m interface{}) ([]*schema.ResourceData, error) {
Expand Down Expand Up @@ -82,6 +87,12 @@ func resourceNotification() *schema.Resource {
Type: schema.TypeString,
Required: true,
},
"enabled": {
Description: "Enabled",
Type: schema.TypeBool,
Optional: true,
Default: true,
},
"filters": {
Description: "Filters",
Type: schema.TypeList,
Expand Down Expand Up @@ -227,16 +238,23 @@ func parseSet(setName string, d *schema.ResourceData) map[string]interface{} {
return map[string]interface{}{}
}

func parseRule(d *schema.ResourceData) (trigger string, filters interface{}) {
func parseRule(d *schema.ResourceData) (trigger string, filters interface{}, status string) {
rule := parseSet("rule", d)
for key, value := range rule {
if key == "trigger" {
switch key {
case "trigger":
trigger = value.(string)
} else {
case "enabled":
v := value.(bool)
status = disabledStatus
if v {
status = enabledStatus
}
default:
filters = value
}
}
return trigger, filters
return trigger, filters, status
}

func cleanConfig(channel, trigger string, config map[string]interface{}) map[string]interface{} {
Expand Down Expand Up @@ -265,7 +283,7 @@ func cleanConfig(channel, trigger string, config map[string]interface{}) map[str

func resourceNotificationCreate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {

trigger, filters := parseRule(d)
trigger, filters, status := parseRule(d)
channel := d.Get("channel").(string)
config := parseSet("config", d)
config = cleanConfig(channel, trigger, config)
Expand All @@ -276,7 +294,7 @@ func resourceNotificationCreate(ctx context.Context, d *schema.ResourceData, m i
c := m.(map[string]*client.RollbarAPIClient)[projectKeyToken]
c.SetHeaderResource(rollbarNotification)

n, err := c.CreateNotification(channel, filters, trigger, config)
n, err := c.CreateNotification(channel, filters, trigger, config, status)

if err != nil {
l.Err(err).Send()
Expand All @@ -294,7 +312,7 @@ func resourceNotificationCreate(ctx context.Context, d *schema.ResourceData, m i
func resourceNotificationUpdate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {

id := mustGetID(d)
trigger, filters := parseRule(d)
trigger, filters, status := parseRule(d)
channel := d.Get("channel").(string)
config := parseSet("config", d)
config = cleanConfig(channel, trigger, config)
Expand All @@ -304,7 +322,7 @@ func resourceNotificationUpdate(ctx context.Context, d *schema.ResourceData, m i

c := m.(map[string]*client.RollbarAPIClient)[projectKeyToken]
c.SetHeaderResource(rollbarNotification)
n, err := c.UpdateNotification(id, channel, filters, trigger, config)
n, err := c.UpdateNotification(id, channel, filters, trigger, config, status)

if err != nil {
l.Err(err).Send()
Expand Down Expand Up @@ -332,7 +350,7 @@ func flattenConfig(config map[string]interface{}) *schema.Set {
return set
}

func flattenRule(filters []interface{}, trigger string) *schema.Set {
func flattenRule(filters []interface{}, trigger, status string) *schema.Set {
var out = make([]interface{}, 0)
m := make(map[string]interface{})
for _, filter := range filters {
Expand All @@ -355,6 +373,12 @@ func flattenRule(filters []interface{}, trigger string) *schema.Set {
filterConv["value"] = strconv.FormatFloat(v, 'f', -1, 64)
}
}
if status == enabledStatus {
m["enabled"] = true
}
if status == disabledStatus {
m["enabled"] = false
}
m["filters"] = filters
out = append(out, m)
m["trigger"] = trigger
Expand Down Expand Up @@ -386,7 +410,7 @@ func resourceNotificationRead(ctx context.Context, d *schema.ResourceData, m int
}

mustSet(d, "config", flattenConfig(n.Config))
mustSet(d, "rule", flattenRule(n.Filters, n.Trigger))
mustSet(d, "rule", flattenRule(n.Filters, n.Trigger, n.Status))
l.Debug().Msg("Successfully read rollbar_notification resource")
return nil
}
Expand Down
2 changes: 0 additions & 2 deletions rollbar/test2/resource_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ import (

// TestIntegrationCreate tests creating an integration
func (s *AccSuite) TestIntegrationCreate() {
s.T().Skip("unauthorized")
integrationResourceName := "rollbar_integration.webhook_integration"
// language=hcl
config := `
Expand Down Expand Up @@ -58,7 +57,6 @@ func (s *AccSuite) TestIntegrationCreate() {

// TestIntegrationUpdate tests updating an integration
func (s *AccSuite) TestIntegrationUpdate() {
s.T().Skip("unauthorized")
integrationResourceName := "rollbar_integration.webhook_integration"
// language=hcl
config1 := `
Expand Down
Loading

0 comments on commit dda6fa2

Please sign in to comment.