Skip to content

Commit

Permalink
[ODC-905] Fix remove default rule and key Merge pull request #38 from…
Browse files Browse the repository at this point in the history
… Canva/zander-fixRemoveDefaultRule
  • Loading branch information
zander-canva authored Aug 29, 2022
2 parents 0ba5630 + b3eb54c commit 5c020cd
Show file tree
Hide file tree
Showing 2 changed files with 181 additions and 3 deletions.
51 changes: 50 additions & 1 deletion sentry/resource_sentry_project.go
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand All @@ -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)
}

Expand Down Expand Up @@ -253,9 +255,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)
Expand Down Expand Up @@ -284,6 +287,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,
Expand Down Expand Up @@ -377,3 +394,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
}
133 changes: 131 additions & 2 deletions sentry/resource_sentry_project_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
)
}

Expand All @@ -47,6 +49,17 @@ func TestAccSentryProject_basic(t *testing.T) {
Config: testAccSentryProjectConfig(teamName, projectName+"-renamed"),
Check: check(projectName + "-renamed"),
},
{
Config: testAccSentryProjectConfigComplex(teamName, projectName, true, false),
Check: resource.ComposeAggregateTestCheckFunc(
testAccSentryCheckDefaultKey(rn, true),
testAccSentryCheckDefaultRule(rn, false),
),
},
{
Config: testAccSentryProjectConfigComplex(teamName, projectName, false, true),
Check: testAccSentryCheckDefaultRule(rn, true),
},
{
ResourceName: rn,
ImportState: true,
Expand All @@ -60,6 +73,48 @@ 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: resource.ComposeAggregateTestCheckFunc(
testAccSentryCheckDefaultKey(rn, true),
testAccSentryCheckDefaultRule(rn, false),
),
},
},
})
}

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: resource.ComposeAggregateTestCheckFunc(
testAccSentryCheckDefaultKey(rn, false),
testAccSentryCheckDefaultRule(rn, true),
),
},
},
})
}

func TestAccSentryProject_changeTeam(t *testing.T) {
teamName1 := acctest.RandomWithPrefix("tf-team")
teamName2 := acctest.RandomWithPrefix("tf-team")
Expand Down Expand Up @@ -221,16 +276,22 @@ 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
team = sentry_team.test.slug
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, removeDefaultKey, removeDefaultRule)
}
`, projectName)

func testAccSentryProjectConfig(teamName, projectName string) string {
return testAccSentryProjectConfigComplex(teamName, projectName, false, false)
}

func testAccSentryProjectConfig_changeTeam(teamName1, teamName2, projectName, teamResourceName string) string {
Expand Down Expand Up @@ -300,3 +361,71 @@ resource "sentry_team" "%[1]s" {
}
`, teamName)
}

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()
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 found
}
}
return notFound
}
}

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()
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 == "Send a notification for new issues" {
return found
}
}
return notFound
}
}

0 comments on commit 5c020cd

Please sign in to comment.