From 7a5c9960d973701bf6d45ea6b52231fb729ff112 Mon Sep 17 00:00:00 2001 From: Zander Zhuang Date: Fri, 26 Aug 2022 13:03:26 +1000 Subject: [PATCH 1/3] fix remove default test and key --- sentry/resource_sentry_project.go | 49 ++++++++++++++++++- sentry/resource_sentry_project_test.go | 68 +++++++++++++++++++++++++- 2 files changed, 114 insertions(+), 3 deletions(-) diff --git a/sentry/resource_sentry_project.go b/sentry/resource_sentry_project.go index 462bdf6fe..90f007491 100644 --- a/sentry/resource_sentry_project.go +++ b/sentry/resource_sentry_project.go @@ -253,9 +253,10 @@ func resourceSentryProjectUpdate(ctx context.Context, d *schema.ResourceData, me project := d.Id() org := d.Get("organization").(string) + slug := d.Get("slug").(string) params := &sentry.UpdateProjectParams{ Name: d.Get("name").(string), - Slug: d.Get("slug").(string), + Slug: slug, } platform := d.Get("platform").(string) @@ -284,6 +285,20 @@ func resourceSentryProjectUpdate(ctx context.Context, d *schema.ResourceData, me params.AllowedDomains = allowedDomains } + if _, ok := d.GetOk("remove_default_key"); ok { + err := removeDefaultKey(ctx, client, org, slug) + if err != nil { + return diag.FromErr(err) + } + } + + if _, ok := d.GetOk("remove_default_rule"); ok { + err := removeDefaultRule(ctx, client, org, slug) + if err != nil { + return diag.FromErr(err) + } + } + tflog.Debug(ctx, "Updating project", map[string]interface{}{ "org": org, "project": project, @@ -377,3 +392,35 @@ func resourceSentryProjectDelete(ctx context.Context, d *schema.ResourceData, me return diag.FromErr(err) } + +func removeDefaultKey(ctx context.Context, client *sentry.Client, org, projSlug string) error { + keys, _, err := client.ProjectKeys.List(ctx, org, projSlug, nil) + if err != nil { + return err + } + + for _, key := range keys { + if key.Name == "Default" { + _, err = client.ProjectKeys.Delete(ctx, org, projSlug, key.ID) + return err + } + } + + return nil +} + +func removeDefaultRule(ctx context.Context, client *sentry.Client, org, projSlug string) error { + rules, _, err := client.IssueAlerts.List(ctx, org, projSlug, nil) + if err != nil { + return err + } + + for _, rule := range rules { + if *rule.Name == "Send a notification for new issues" { + _, err = client.IssueAlerts.Delete(ctx, org, projSlug, *rule.ID) + return err + } + } + + return nil +} diff --git a/sentry/resource_sentry_project_test.go b/sentry/resource_sentry_project_test.go index 40841c931..348ac5c75 100644 --- a/sentry/resource_sentry_project_test.go +++ b/sentry/resource_sentry_project_test.go @@ -31,6 +31,8 @@ func TestAccSentryProject_basic(t *testing.T) { resource.TestCheckResourceAttrSet(rn, "internal_id"), resource.TestCheckResourceAttrPtr(rn, "internal_id", &projectID), resource.TestCheckResourceAttrPair(rn, "project_id", rn, "internal_id"), + resource.TestCheckResourceAttrSet(rn, "remove_default_key"), + resource.TestCheckResourceAttrSet(rn, "remove_default_rule"), ) } @@ -47,6 +49,14 @@ func TestAccSentryProject_basic(t *testing.T) { Config: testAccSentryProjectConfig(teamName, projectName+"-renamed"), Check: check(projectName + "-renamed"), }, + { + Config: testAccSentryProjectConfigComplex(teamName, projectName, true, false), + Check: testAccSentryKeyRemoved(rn), + }, + { + Config: testAccSentryProjectConfigComplex(teamName, projectName, false, true), + Check: testAccSentryRuleRemoved(rn), + }, { ResourceName: rn, ImportState: true, @@ -221,7 +231,7 @@ func testAccSentryProjectImportStateIdFunc(n string) resource.ImportStateIdFunc } } -func testAccSentryProjectConfig(teamName, projectName string) string { +func testAccSentryProjectConfigComplex(teamName, projectName string, removeDefaultKey bool, removeDefaultRule bool) string { return testAccSentryTeamConfig(teamName) + fmt.Sprintf(` resource "sentry_project" "test" { organization = sentry_team.test.organization @@ -229,8 +239,14 @@ resource "sentry_project" "test" { name = "%[1]s" platform = "go" allowed_domains = ["www.test2.com", "www.test.com", "www.yourapp.com"] + remove_default_key = %[2]v + remove_default_rule = %[3]v } - `, projectName) + `, projectName, removeDefaultKey, removeDefaultRule) +} + +func testAccSentryProjectConfig(teamName, projectName string) string { + return testAccSentryProjectConfigComplex(teamName, projectName, false, false) } func testAccSentryProjectConfig_changeTeam(teamName1, teamName2, projectName, teamResourceName string) string { @@ -300,3 +316,51 @@ resource "sentry_team" "%[1]s" { } `, teamName) } + +func testAccSentryKeyRemoved(n string) resource.TestCheckFunc { + return func(s *terraform.State) error { + rs := s.RootModule().Resources[n] + client := testAccProvider.Meta().(*sentry.Client) + ctx := context.Background() + keys, _, err := client.ProjectKeys.List( + ctx, + rs.Primary.Attributes["organization"], + rs.Primary.Attributes["slug"], + nil, + ) + if err != nil { + return err + } + + for _, key := range keys { + if key.Name == "Default" { + return fmt.Errorf("default key not removed") + } + } + return nil + } +} + +func testAccSentryRuleRemoved(n string) resource.TestCheckFunc { + return func(s *terraform.State) error { + rs := s.RootModule().Resources[n] + client := testAccProvider.Meta().(*sentry.Client) + ctx := context.Background() + rules, _, err := client.IssueAlerts.List( + ctx, + rs.Primary.Attributes["organization"], + rs.Primary.Attributes["slug"], + nil, + ) + if err != nil { + return err + } + + for _, rule := range rules { + if *rule.Name == "Default" { + return fmt.Errorf("default rule not removed") + } + } + return nil + } +} From 338c636e6687a45c65582a6e093058d82eff1507 Mon Sep 17 00:00:00 2001 From: Zander Zhuang Date: Fri, 26 Aug 2022 16:39:33 +1000 Subject: [PATCH 2/3] added remove default on create rules, add slug to schema on create --- sentry/resource_sentry_project.go | 2 ++ sentry/resource_sentry_project_test.go | 36 ++++++++++++++++++++++++++ 2 files changed, 38 insertions(+) diff --git a/sentry/resource_sentry_project.go b/sentry/resource_sentry_project.go index 90f007491..c1bfa223e 100644 --- a/sentry/resource_sentry_project.go +++ b/sentry/resource_sentry_project.go @@ -160,6 +160,7 @@ func resourceSentryProjectCreate(ctx context.Context, d *schema.ResourceData, me teams = d.Get("teams").(*schema.Set).List() team = teams[0].(string) } + params := &sentry.CreateProjectParams{ Name: d.Get("name").(string), Slug: d.Get("slug").(string), @@ -182,6 +183,7 @@ func resourceSentryProjectCreate(ctx context.Context, d *schema.ResourceData, me }) d.SetId(proj.Slug) + d.Set("slug", proj.Slug) return resourceSentryProjectUpdate(ctx, d, meta) } diff --git a/sentry/resource_sentry_project_test.go b/sentry/resource_sentry_project_test.go index 348ac5c75..4228a3520 100644 --- a/sentry/resource_sentry_project_test.go +++ b/sentry/resource_sentry_project_test.go @@ -70,6 +70,42 @@ func TestAccSentryProject_basic(t *testing.T) { }) } +func TestAccSentryProject_removeDefaultKeyOnCreate(t *testing.T) { + teamName := acctest.RandomWithPrefix("tf-team") + projectName := acctest.RandomWithPrefix("tf-project") + rn := "sentry_project.test" + + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + ProviderFactories: testAccProviderFactories, + CheckDestroy: testAccCheckSentryProjectDestroy, + Steps: []resource.TestStep{ + { + Config: testAccSentryProjectConfigComplex(teamName, projectName, true, false), + Check: testAccSentryKeyRemoved(rn), + }, + }, + }) +} + +func TestAccSentryProject_removeDefaultRuleOnCreate(t *testing.T) { + teamName := acctest.RandomWithPrefix("tf-team") + projectName := acctest.RandomWithPrefix("tf-project") + rn := "sentry_project.test" + + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + ProviderFactories: testAccProviderFactories, + CheckDestroy: testAccCheckSentryProjectDestroy, + Steps: []resource.TestStep{ + { + Config: testAccSentryProjectConfigComplex(teamName, projectName, false, true), + Check: testAccSentryRuleRemoved(rn), + }, + }, + }) +} + func TestAccSentryProject_changeTeam(t *testing.T) { teamName1 := acctest.RandomWithPrefix("tf-team") teamName2 := acctest.RandomWithPrefix("tf-team") From b3eb54c8f43c5aaeea3f4e773e1d2c084548297d Mon Sep 17 00:00:00 2001 From: Zander Zhuang Date: Mon, 29 Aug 2022 12:26:10 +1000 Subject: [PATCH 3/3] add tests for checking key/rule not removed if set to false, fix test for rule --- sentry/resource_sentry_project_test.go | 51 ++++++++++++++++++++------ 1 file changed, 40 insertions(+), 11 deletions(-) diff --git a/sentry/resource_sentry_project_test.go b/sentry/resource_sentry_project_test.go index 4228a3520..708699693 100644 --- a/sentry/resource_sentry_project_test.go +++ b/sentry/resource_sentry_project_test.go @@ -51,11 +51,14 @@ func TestAccSentryProject_basic(t *testing.T) { }, { Config: testAccSentryProjectConfigComplex(teamName, projectName, true, false), - Check: testAccSentryKeyRemoved(rn), + Check: resource.ComposeAggregateTestCheckFunc( + testAccSentryCheckDefaultKey(rn, true), + testAccSentryCheckDefaultRule(rn, false), + ), }, { Config: testAccSentryProjectConfigComplex(teamName, projectName, false, true), - Check: testAccSentryRuleRemoved(rn), + Check: testAccSentryCheckDefaultRule(rn, true), }, { ResourceName: rn, @@ -82,7 +85,10 @@ func TestAccSentryProject_removeDefaultKeyOnCreate(t *testing.T) { Steps: []resource.TestStep{ { Config: testAccSentryProjectConfigComplex(teamName, projectName, true, false), - Check: testAccSentryKeyRemoved(rn), + Check: resource.ComposeAggregateTestCheckFunc( + testAccSentryCheckDefaultKey(rn, true), + testAccSentryCheckDefaultRule(rn, false), + ), }, }, }) @@ -100,7 +106,10 @@ func TestAccSentryProject_removeDefaultRuleOnCreate(t *testing.T) { Steps: []resource.TestStep{ { Config: testAccSentryProjectConfigComplex(teamName, projectName, false, true), - Check: testAccSentryRuleRemoved(rn), + Check: resource.ComposeAggregateTestCheckFunc( + testAccSentryCheckDefaultKey(rn, false), + testAccSentryCheckDefaultRule(rn, true), + ), }, }, }) @@ -353,8 +362,18 @@ resource "sentry_team" "%[1]s" { `, teamName) } -func testAccSentryKeyRemoved(n string) resource.TestCheckFunc { +func testAccSentryCheckDefaultKey(n string, remove bool) resource.TestCheckFunc { return func(s *terraform.State) error { + var found, notFound error + + if remove { + found = fmt.Errorf("default key not removed") + notFound = nil + } else { + found = nil + notFound = fmt.Errorf("default key removed") + } + rs := s.RootModule().Resources[n] client := testAccProvider.Meta().(*sentry.Client) ctx := context.Background() @@ -370,15 +389,25 @@ func testAccSentryKeyRemoved(n string) resource.TestCheckFunc { for _, key := range keys { if key.Name == "Default" { - return fmt.Errorf("default key not removed") + return found } } - return nil + return notFound } } -func testAccSentryRuleRemoved(n string) resource.TestCheckFunc { +func testAccSentryCheckDefaultRule(n string, removed bool) resource.TestCheckFunc { return func(s *terraform.State) error { + var found, notFound error + + if removed { + found = fmt.Errorf("default rule not removed") + notFound = nil + } else { + found = nil + notFound = fmt.Errorf("default rule removed") + } + rs := s.RootModule().Resources[n] client := testAccProvider.Meta().(*sentry.Client) ctx := context.Background() @@ -393,10 +422,10 @@ func testAccSentryRuleRemoved(n string) resource.TestCheckFunc { } for _, rule := range rules { - if *rule.Name == "Default" { - return fmt.Errorf("default rule not removed") + if *rule.Name == "Send a notification for new issues" { + return found } } - return nil + return notFound } }