From 3d2ac0fd06217bd0ac4cddb5342233bccd2eb57a Mon Sep 17 00:00:00 2001 From: Charles Verdad Date: Tue, 16 Jun 2020 15:41:20 +1000 Subject: [PATCH] remove debug lines and add delete rule test --- sentry/resource_sentry_project_rule.go | 55 +-------------------- sentry/resource_sentry_project_rule_test.go | 49 +++++++----------- sentryclient/rules.go | 40 ++------------- 3 files changed, 22 insertions(+), 122 deletions(-) diff --git a/sentry/resource_sentry_project_rule.go b/sentry/resource_sentry_project_rule.go index 6b928faf..e4406564 100644 --- a/sentry/resource_sentry_project_rule.go +++ b/sentry/resource_sentry_project_rule.go @@ -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" @@ -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) @@ -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) @@ -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) } diff --git a/sentry/resource_sentry_project_rule_test.go b/sentry/resource_sentry_project_rule_test.go index 5e5b9983..7781bb05 100644 --- a/sentry/resource_sentry_project_rule_test.go +++ b/sentry/resource_sentry_project_rule_test.go @@ -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" @@ -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, @@ -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", @@ -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", @@ -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, }, }, @@ -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 { @@ -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 @@ -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) @@ -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 } diff --git a/sentryclient/rules.go b/sentryclient/rules.go index 775cdd31..55cd8de1 100644 --- a/sentryclient/rules.go +++ b/sentryclient/rules.go @@ -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. @@ -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"` @@ -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. @@ -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) } @@ -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) }