Skip to content

Commit

Permalink
Merge pull request #236 from rollbar/pawel/add_notification
Browse files Browse the repository at this point in the history
added notification to terraform
  • Loading branch information
pawelsz-rb committed Sep 1, 2021
2 parents f5f6c7b + 8893f32 commit db1f586
Show file tree
Hide file tree
Showing 27 changed files with 768 additions and 100 deletions.
7 changes: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ Terraform provider for Rollbar
The Rollbar provider allows Terraform to control resources on
[Rollbar.com](https://rollbar.com), the Continuous Code Improvement Platform.

The provider allows you to manage your Rollbar projects, tokens, users, and
teams with ease. It must be configured with the proper credentials before it can
The provider allows you to manage your Rollbar projects, tokens, users,
teams and notifications with ease. It must be configured with the proper credentials before it can
be used.


Expand Down Expand Up @@ -39,6 +39,7 @@ terraform {
# Configure the Rollbar provider
provider "rollbar" {
api_key = "YOUR_API_KEY"
project_api_key = "YOUR_PROJECT_API_KEY" # needed for notifications
}
# Create a team
Expand Down Expand Up @@ -182,4 +183,4 @@ and
[latest-release]: https://github.com/rollbar/terraform-provider-rollbar/releases/latest
[requiring-providers]: https://www.terraform.io/docs/configuration/provider-requirements.html#requiring-providers
[semantic-release]: https://github.com/semantic-release/semantic-release
[pub-to-registry]: https://github.com/rollbar/terraform-provider-rollbar/issues/153
[pub-to-registry]: https://github.com/rollbar/terraform-provider-rollbar/issues/153
4 changes: 3 additions & 1 deletion client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,9 @@ func NewClient(baseURL, token string) *RollbarAPIClient {

// Authentication
if token != "" {
r = r.SetHeader("X-Rollbar-Access-Token", token)
r = r.SetHeaders(map[string]string{
"X-Rollbar-Access-Token": token,
"X-Rollbar-Terraform": "true"})
} else {
log.Warn().Msg("Rollbar API token not set")
}
Expand Down
32 changes: 32 additions & 0 deletions client/fixtures/notification/create.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
{
"result": [
{
"action": "send_email",
"trigger": "new_item",
"config": {
"teams": [
"Owners"
]
},
"id": 5127954,
"filters": [
{
"operation": "eq",
"type": "environment",
"value": "development"
},
{
"operation": "eq",
"type": "framework",
"value": "14"
},
{
"operation": "gte",
"type": "level",
"value": "error"
}
]
}
],
"err": 0
}
6 changes: 6 additions & 0 deletions client/fixtures/notification/delete.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"result": {
"id": 5127954
},
"err": 0
}
21 changes: 21 additions & 0 deletions client/fixtures/notification/read.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"result": {
"action": "send_email",
"trigger": "new_item",
"config": {},
"id": 5127954,
"filters": [
{
"operation": "neq",
"type": "environment",
"value": "production"
},
{
"operation": "eq",
"type": "level",
"value": "error"
}
]
},
"err": 0
}
4 changes: 4 additions & 0 deletions client/fixtures/notification/read_deleted.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"message": "Not found",
"err": 1
}
30 changes: 30 additions & 0 deletions client/fixtures/notification/update.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
{
"result": {
"action": "send_email",
"trigger": "new_item",
"config": {
"teams": [
"Owners"
]
},
"id": 5127954,
"filters": [
{
"operation": "eq",
"type": "environment",
"value": "development"
},
{
"operation": "eq",
"type": "framework",
"value": "14"
},
{
"operation": "gte",
"type": "level",
"value": "error"
}
]
},
"err": 0
}
173 changes: 173 additions & 0 deletions client/notification.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,173 @@
/*
* Copyright (c) 2021 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
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/

package client

import (
"github.com/rs/zerolog/log"
"strconv"
"strings"
)

type Notification struct {
ID int `model:"id" mapstructure:"id"`
Action string `model:"action" mapstructure:"action"`
Trigger string `model:"trigger" mapstructure:"trigger"`
}

// CreateNotification creates a new Rollbar notification.
func (c *RollbarAPIClient) CreateNotification(channel string, filters, trigger, config interface{}) (*Notification, error) {
u := c.BaseURL + pathNotificationCreate
u = strings.ReplaceAll(u, "{channel}", channel)
l := log.With().
Str("channel", channel).
Logger()
l.Debug().Msg("Creating new notification")

resp, err := c.Resty.R().
SetBody([]map[string]interface{}{{"filters": filters, "trigger": trigger, "config": config}}).
SetResult(notificationsResponse{}).
SetError(ErrorResult{}).
Post(u)
if err != nil {
l.Err(err).Msg("Error creating notification")
return nil, err
}
err = errorFromResponse(resp)
if err != nil {
l.Err(err).Send()
return nil, err
}
l.Debug().Msg("Notification successfully created")
nr := resp.Result().(*notificationsResponse)
return &nr.Result[0], nil

}

// UpdateNotification updates a Rollbar notification.
func (c *RollbarAPIClient) UpdateNotification(notificationID int, channel string, filters, trigger, config interface{}) (*Notification, error) {
u := c.BaseURL + pathNotificationReadOrDeleteOrUpdate
l := log.With().
Str("channel", channel).
Logger()
l.Debug().Msg("Updating notification")

resp, err := c.Resty.R().
SetBody(map[string]interface{}{"filters": filters, "trigger": trigger, "config": config}).
SetResult(notificationResponse{}).
SetError(ErrorResult{}).
SetPathParams(map[string]string{
"notificationID": strconv.Itoa(notificationID),
"channel": channel,
}).
Put(u)
if err != nil {
l.Err(err).Msg("Error updating notification")
return nil, err
}
err = errorFromResponse(resp)
if err != nil {
l.Err(err).Send()
return nil, err
}
l.Debug().Msg("Notification successfully updated")
nr := resp.Result().(*notificationResponse)
return &nr.Result, nil

}

// ReadNotification reads a Rollbar notification from the API. If no matching notification is found,
// returns error ErrNotFound.
func (c *RollbarAPIClient) ReadNotification(notificationID int, channel string) error {
u := c.BaseURL + pathNotificationReadOrDeleteOrUpdate

l := log.With().
Int("notificationID", notificationID).
Logger()
l.Debug().Msg("Reading notification from API")

resp, err := c.Resty.R().
SetResult(notificationResponse{}).
SetError(ErrorResult{}).
SetPathParams(map[string]string{
"notificationID": strconv.Itoa(notificationID),
"channel": channel,
}).
Get(u)

if err != nil {
l.Err(err).Msg(resp.Status())
return err
}
err = errorFromResponse(resp)
if err != nil {
l.Err(err).Send()
return err
}
nr := resp.Result().(*notificationResponse)
if nr.Err != 0 {
l.Warn().Msg("Notification not found")
return ErrNotFound
}
l.Debug().Msg("Notification successfully read")
return nil

}

// DeleteNotification deletes a Rollbar notification. If no matching notification is found,
// returns error ErrNotFound.
func (c *RollbarAPIClient) DeleteNotification(notificationID int, channel string) error {
u := c.BaseURL + pathNotificationReadOrDeleteOrUpdate
l := log.With().
Int("notificationID", notificationID).
Logger()
l.Debug().Msg("Deleting notification")

resp, err := c.Resty.R().
SetError(ErrorResult{}).
SetPathParams(map[string]string{
"notificationID": strconv.Itoa(notificationID),
"channel": channel,
}).
Delete(u)
if err != nil {
l.Err(err).Msg("Error deleting notification")
return err
}
err = errorFromResponse(resp)
if err != nil {
l.Err(err).Send()
return err
}
l.Debug().Msg("Notifications successfully deleted")
return nil
}

type notificationResponse struct {
Err int `json:"err"`
Result Notification `json:"result"`
}

type notificationsResponse struct {
Err int `json:"err"`
Result []Notification `json:"result"`
}

0 comments on commit db1f586

Please sign in to comment.