Skip to content

Commit

Permalink
fix remove default test and key
Browse files Browse the repository at this point in the history
  • Loading branch information
zander-canva committed Aug 26, 2022
1 parent 0ba5630 commit 7a5c996
Show file tree
Hide file tree
Showing 2 changed files with 114 additions and 3 deletions.
49 changes: 48 additions & 1 deletion sentry/resource_sentry_project.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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
}
68 changes: 66 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,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,
Expand Down Expand Up @@ -221,16 +231,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)
`, projectName, removeDefaultKey, removeDefaultRule)
}

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 +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
}
}

0 comments on commit 7a5c996

Please sign in to comment.