Skip to content

Commit

Permalink
remove debug lines and add delete rule test
Browse files Browse the repository at this point in the history
  • Loading branch information
charlesverdad committed Jun 16, 2020
1 parent 2b9fcbe commit 3d2ac0f
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 122 deletions.
55 changes: 2 additions & 53 deletions sentry/resource_sentry_project_rule.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
package sentry

import (
// "errors"
"encoding/json"
"log"

"github.com/canva/terraform-provider-sentry/sentryclient"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"github.com/mitchellh/mapstructure"
Expand Down Expand Up @@ -126,13 +122,6 @@ func resourceSentryRuleCreate(d *schema.ResourceData, meta interface{}) error {
return err
}

encoded_params, _ := json.Marshal(params)
log.Printf("create rule params: %s\n", string(encoded_params))

encoded_rule, _ := json.Marshal(rule)
log.Printf("rule create rule: %s\n", string(encoded_rule))


d.SetId(rule.ID)

return resourceSentryRuleRead(d, meta)
Expand All @@ -144,46 +133,13 @@ func resourceSentryRuleRead(d *schema.ResourceData, meta interface{}) error {
project := d.Get("project").(string)
id := d.Id()

// rules, resp, err := client.Rules.List(org, project)
// if found, err := checkClientGet(resp, err, d); !found {
// return err
// }


// rules, _, err := client.Rules.List(org, project)
// if err != nil {
// d.SetId("")
// return nil
// }

// log.Printf("Looking for rule with id %s", id)


// var rule *sentryclient.Rule
// for _, r := range rules {
// log.Printf("id: %s", r.ID)

// if r.ID == id {
// rule = &r
// break
// }
// }

// if rule == nil {
// return errors.New("Could not find rule with ID " + id)
// }

//try read
rule, _, err := client.Rules.Read(org, project, id)
rule, _, err := client.Rules.Get(org, project, id)

if err != nil {
d.SetId("")
return nil
}

encoded_rule, _ := json.Marshal(rule)
log.Printf("rule read: %s\n", string(encoded_rule))

d.SetId(rule.ID)
d.Set("name", rule.Name)
d.Set("actions", rule.Actions)
Expand Down Expand Up @@ -245,18 +201,11 @@ func resourceSentryRuleUpdate(d *schema.ResourceData, meta interface{}) error {
params.Environment = environment
}

rule, _, err := client.Rules.Update(org, project, id, params)
_, _, err := client.Rules.Update(org, project, id, params)
if err != nil {
return err
}

encoded_params, _ := json.Marshal(params)
log.Printf("update rule params: %s\n", string(encoded_params))

encoded_rule, _ := json.Marshal(rule)
log.Printf("rule update rule: %s\n", string(encoded_rule))


return resourceSentryRuleRead(d, meta)
}

Expand Down
49 changes: 17 additions & 32 deletions sentry/resource_sentry_project_rule_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"testing"

"github.com/canva/terraform-provider-sentry/sentryclient"
// "github.com/hashicorp/terraform-plugin-sdk/helper/acctest"
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/terraform"
"github.com/google/go-cmp/cmp"
Expand Down Expand Up @@ -35,21 +34,19 @@ func TestAccSentryRule_basic(t *testing.T) {
{
id = "sentry.rules.conditions.event_frequency.EventFrequencyCondition"
value = 101
name = "An issue is seen more than 100 times in 1 hour"
name = "An issue is seen more than 101 times in 1h"
interval = "1h"
},
{
id = "sentry.rules.conditions.event_frequency.EventUniqueUserFrequencyCondition"
interval = "1m"
name = "An issue is seen by more than 25 users in 1 minute"
name = "An issue is seen by more than 30 users in 1 minute"
value = 30
}
]
}
`, testOrganization, projectSlug)

fmt.Printf("%s", testAccSentryRuleUpdateConfig)

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
Expand All @@ -61,8 +58,6 @@ func TestAccSentryRule_basic(t *testing.T) {
testAccCheckSentryRuleExists("sentry_rule.test_rule", &rule, testOrganization, projectSlug),
testAccCheckSentryRuleAttributes(&rule, &testAccSentryRuleExpectedAttributes{
Name: "Important Issue",
// Organization: testOrganization,
// Project: projectSlug,
ActionMatch: "all",
Frequency: 1440,
Environment: "prod",
Expand Down Expand Up @@ -95,8 +90,6 @@ func TestAccSentryRule_basic(t *testing.T) {
testAccCheckSentryRuleExists("sentry_rule.test_rule", &rule, testOrganization, projectSlug),
testAccCheckSentryRuleAttributes(&rule, &testAccSentryRuleExpectedAttributes{
Name: "Important Issue",
// Organization: testOrganization,
// Project: projectSlug,
ActionMatch: "all",
Frequency: 1300,
Environment: "prod",
Expand All @@ -110,13 +103,13 @@ func TestAccSentryRule_basic(t *testing.T) {
{
ID: "sentry.rules.conditions.event_frequency.EventFrequencyCondition",
Value: 101,
Name: "An issue is seen more than 100 times in 1m",
Name: "An issue is seen more than 101 times in 1h",
Interval: "1h",
},
{
ID: "sentry.rules.conditions.event_frequency.EventUniqueUserFrequencyCondition",
Interval: "1m",
Name: "An issue is seen by more than 25 users in 1m",
Name: "An issue is seen by more than 30 users in 1m",
Value: 30,
},
},
Expand All @@ -131,17 +124,23 @@ func testAccCheckSentryRuleDestroy(s *terraform.State) error {
client := testAccProvider.Meta().(*sentryclient.Client)

for _, rs := range s.RootModule().Resources {
if rs.Type != "sentry_team" {
if rs.Type != "sentry_rule" {
continue
}

team, resp, err := client.Teams.Get(
rs.Primary.Attributes["organization"],
rs.Primary.ID,
)

rules, resp, err := client.Rules.List(rs.Primary.Attributes["organization"], projectSlug)
var rule *sentryclient.Rule
for _, r := range rules {
if r.ID == rs.Primary.ID {
rule = &r
break
}
}

if err == nil {
if team != nil {
return errors.New("Team still exists")
if rule != nil {
return errors.New("Rule still exists")
}
}
if resp.StatusCode != 404 {
Expand Down Expand Up @@ -169,12 +168,10 @@ func testAccCheckSentryRuleExists(n string, rule *sentryclient.Rule, org string,
if err != nil {
return err
}
fmt.Printf("EXPECTED: %+v\n", rs.Primary)

var SentryRule *sentryclient.Rule

for _, r := range SentryRules {
fmt.Printf("RULE: %+v\n", r)
if r.ID == rs.Primary.ID {
SentryRule = &r
break
Expand Down Expand Up @@ -207,12 +204,6 @@ func testAccCheckSentryRuleAttributes(rule *sentryclient.Rule, want *testAccSent
if rule.Name != want.Name {
return fmt.Errorf("got rule name %q; want %q", rule.Name, want.Name)
}
// if rule.Organization != want.Organization {
// return fmt.Errorf("got organization %q; want %q", rule.Organization, want.Organization)
// }
// if rule.Project != want.Project {
// return fmt.Errorf("got project %q; want %q", rule.Project, want.Project)
// }

if rule.ActionMatch != want.ActionMatch {
return fmt.Errorf("got action_match %s; want %s", rule.ActionMatch, want.ActionMatch)
Expand All @@ -233,12 +224,6 @@ func testAccCheckSentryRuleAttributes(rule *sentryclient.Rule, want *testAccSent
if !cmp.Equal(rule.Conditions, want.Conditions){
return fmt.Errorf("got conditions: %+v\n; want %+v\n", rule.Conditions, want.Conditions)
}
// return errors.New("got empty slug; want non-empty slug")
// }

// if want.Slug != "" && team.Slug != want.Slug {
// return fmt.Errorf("got slug %q; want %q", team.Slug, want.Slug)
// }

return nil
}
Expand Down
40 changes: 3 additions & 37 deletions sentryclient/rules.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,7 @@ package sentryclient
import (
"net/http"
"time"
"fmt"
"encoding/json"
"github.com/dghubble/sling"
"log"
// "github.com/mitchellh/mapstructure"

)

// Rule represents an alert rule configured for this project.
Expand All @@ -26,7 +21,7 @@ type Rule struct {

// RuleCondition represents the conditions for each rule.
// https://github.com/getsentry/sentry/blob/9.0.0/src/sentry/api/serializers/models/rule.py
type RuleCondition struct {
type RuleCondition struct {
ID string `json:"id"`
Name string `json:"name"`
Attribute string `json:"attribute,omitempty"`
Expand Down Expand Up @@ -66,25 +61,14 @@ func (s *RuleService) List(organizationSlug string, projectSlug string) ([]Rule,
rules := new([]Rule)
apiError := new(APIError)
resp, err := s.sling.New().Get("projects/"+organizationSlug+"/"+projectSlug+"/rules/").Receive(rules, apiError)
// for i, rule := range (*rules) {
// encoded_rule, _ := json.Marshal(rule)
// log.Printf("rule[%d]: %s\n", i, string(encoded_rule))
// }

// for i := 0; i < len(*rules); i++ {
// encoded_rule, _ := json.Marshal((*rules)[i])
// log.Printf("rule[%i]: %s\n", i, string(encoded_rule))
// }

// log.Printf("LIST: %+v\n", rules)
return *rules, resp, relevantError(err, *apiError)
}

func (s *RuleService) Read(organizationSlug string, projectSlug string, ruleId string) (Rule, *http.Response, error) {
func (s *RuleService) Get(organizationSlug string, projectSlug string, ruleId string) (*Rule, *http.Response, error) {
rule := new(Rule)
apiError := new(APIError)
resp, err := s.sling.New().Get("projects/"+organizationSlug+"/"+projectSlug+"/rules/"+ruleId+"/").Receive(rule, apiError)
return *rule, resp, relevantError(err, *apiError)
return rule, resp, relevantError(err, *apiError)
}

// CreateRuleParams are the parameters for RuleService.Create.
Expand Down Expand Up @@ -160,18 +144,6 @@ func (s *RuleService) Create(organizationSlug string, projectSlug string, params
rule := new(Rule)
apiError := new(APIError)
resp, err := s.sling.New().Post("projects/"+organizationSlug+"/"+projectSlug+"/rules/").BodyJSON(params).Receive(rule, apiError)
// log.Printf("EXPECTED: %+v\n", params)

encoded_params, _ := json.Marshal(params)
log.Printf("CREATE: %s\n", string(encoded_params))
fmt.Printf("CREATE: %s\n", string(encoded_params))

// var decoded_params CreateRuleParams
// mapstructure.WeakDecode(encoded_params, &decoded_params)
// log.Printf("EXPECTED: %+v\n", decoded_params)


fmt.Printf("HELLO WORLD")
return rule, resp, relevantError(err, *apiError)
}

Expand All @@ -180,12 +152,6 @@ func (s *RuleService) Update(organizationSlug string, projectSlug string, ruleID
rule := new(Rule)
apiError := new(APIError)
resp, err := s.sling.New().Put("projects/"+organizationSlug+"/"+projectSlug+"/rules/"+ruleID+"/").BodyJSON(params).Receive(rule, apiError)

encoded_params, _ := json.Marshal(params)
log.Printf("UPDATE: %s\n", string(encoded_params))
fmt.Printf("UPDATE: %s\n", string(encoded_params))


return rule, resp, relevantError(err, *apiError)
}

Expand Down

0 comments on commit 3d2ac0f

Please sign in to comment.