Skip to content

Commit

Permalink
Merge pull request #324 from rollbar/pawel/adding_more_tests
Browse files Browse the repository at this point in the history
  • Loading branch information
pawelsz-rb authored Oct 26, 2022
2 parents 3417b7b + 807feab commit f714479
Show file tree
Hide file tree
Showing 6 changed files with 496 additions and 11 deletions.
36 changes: 35 additions & 1 deletion client/notification.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,10 @@
package client

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

"github.com/rs/zerolog/log"
)

type Notification struct {
Expand Down Expand Up @@ -165,6 +166,39 @@ func (c *RollbarAPIClient) DeleteNotification(notificationID int, channel string
return nil
}

func (c *RollbarAPIClient) ListNotifications(channel string) ([]Notification, error) {
u := c.BaseURL + pathNotificationCreate

l := log.With().
Logger()
l.Debug().Msg("Reading notifications from API")

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

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

type notificationResponse struct {
Err int `json:"err"`
Result Notification `json:"result"`
Expand Down
37 changes: 36 additions & 1 deletion client/service_link.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,9 @@
package client

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

"github.com/rs/zerolog/log"
)

type ServiceLink struct {
Expand Down Expand Up @@ -158,7 +159,41 @@ func (c *RollbarAPIClient) DeleteServiceLink(id int) error {
return nil
}

func (c *RollbarAPIClient) ListSerivceLinks() ([]ServiceLink, error) {
u := c.BaseURL + pathServiceLinkCreate

l := log.With().
Logger()
l.Debug().Msg("Reading service links from API")

resp, err := c.Resty.R().
SetResult(serviceLinksResponse{}).
SetError(ErrorResult{}).
Get(u)

if err != nil {
l.Err(err).Msg(resp.Status())
return nil, err
}
err = errorFromResponse(resp)
if err != nil {
l.Err(err).Send()
return nil, err
}
sl := resp.Result().(*serviceLinksResponse)
if sl.Err != 0 {
l.Warn().Msg("Service link not found")
return nil, ErrNotFound
}
l.Debug().Msg("Service link successfully read")
return sl.Result, nil
}

type serviceLinkResponse struct {
Err int `json:"err"`
Result ServiceLink `json:"result"`
}
type serviceLinksResponse struct {
Err int `json:"err"`
Result []ServiceLink `json:"result"`
}
101 changes: 101 additions & 0 deletions rollbar/resource_integration_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
/*
* Copyright (c) 2022 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 rollbar

import (
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
)

// TestIntegrationCreate tests creating an integration
func (s *AccSuite) TestIntegrationCreate() {
integrationResourceName := "rollbar_integration.webhook_integration"
// language=hcl
config := `
resource "rollbar_integration" "webhook_integration" {
webhook {
enabled = true
url = "https://www.rollbar.com"
}
}
`
resource.ParallelTest(s.T(), resource.TestCase{
PreCheck: func() { s.preCheck() },
Providers: s.providers,
CheckDestroy: nil,
Steps: []resource.TestStep{
{
Config: config,
Check: resource.ComposeTestCheckFunc(
s.checkResourceStateSanity(integrationResourceName),
resource.TestCheckResourceAttr(integrationResourceName, "webhook.0.url", "https://www.rollbar.com"),
resource.TestCheckResourceAttr(integrationResourceName, "webhook.0.enabled", "true"),
),
},
},
})
}

// TestIntegrationUpdate tests updating an integration
func (s *AccSuite) TestIntegrationUpdate() {
integrationResourceName := "rollbar_integration.webhook_integration"
// language=hcl
config1 := `
resource "rollbar_integration" "webhook_integration" {
webhook {
enabled = true
url = "https://www.rollbar.com"
}
}
`
config2 := `
resource "rollbar_integration" "webhook_integration" {
webhook {
enabled = false
url = "https://www.rollbar.com"
}
}
`
resource.ParallelTest(s.T(), resource.TestCase{
PreCheck: func() { s.preCheck() },
Providers: s.providers,
CheckDestroy: nil,
Steps: []resource.TestStep{
{
Config: config1,
Check: resource.ComposeTestCheckFunc(
s.checkResourceStateSanity(integrationResourceName),
resource.TestCheckResourceAttr(integrationResourceName, "webhook.0.url", "https://www.rollbar.com"),
resource.TestCheckResourceAttr(integrationResourceName, "webhook.0.enabled", "true"),
),
},
{
Config: config2,
Check: resource.ComposeTestCheckFunc(
s.checkResourceStateSanity(integrationResourceName),
resource.TestCheckResourceAttr(integrationResourceName, "webhook.0.url", "https://www.rollbar.com"),
resource.TestCheckResourceAttr(integrationResourceName, "webhook.0.enabled", "false"),
),
},
},
})
}
16 changes: 7 additions & 9 deletions rollbar/resource_notification.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ import (

var configMap = map[string][]string{"email": {"users", "teams"},
"slack": {"message_template", "channel", "show_message_buttons"},
"pagerduty": {"service_key"}}
"pagerduty": {"service_key"},
"webhook": {"url", "format"}}

func CustomNotificationImport(ctx context.Context, d *schema.ResourceData, m interface{}) ([]*schema.ResourceData, error) {
splitID := strings.Split(d.Id(), ComplexImportSeparator)
Expand Down Expand Up @@ -281,14 +282,11 @@ func resourceNotificationUpdate(ctx context.Context, d *schema.ResourceData, m i

func flattenConfig(config map[string]interface{}) *schema.Set {
var out = make([]interface{}, 0)
m := make(map[string]interface{})
for key, value := range config {
m[key] = value
out = append(out, m)
}
out = append(out, config)
specResource := resourceNotification().Schema["config"].Elem.(*schema.Resource)
f := schema.HashResource(specResource)
return schema.NewSet(f, out)
set := schema.NewSet(f, out)
return set
}

func flattenRule(filters []interface{}, trigger string) *schema.Set {
Expand Down Expand Up @@ -317,10 +315,10 @@ func flattenRule(filters []interface{}, trigger string) *schema.Set {
m["filters"] = filters
out = append(out, m)
m["trigger"] = trigger
out = append(out, m)
specResource := resourceNotification().Schema["rule"].Elem.(*schema.Resource)
f := schema.HashResource(specResource)
return schema.NewSet(f, out)
set := schema.NewSet(f, out)
return set
}

func resourceNotificationRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
Expand Down
Loading

0 comments on commit f714479

Please sign in to comment.