Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix project team not updating #172

Merged
merged 2 commits into from
Jun 26, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 35 additions & 10 deletions sentry/resource_sentry_project.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package sentry

import (
"context"
"net/http"

"github.com/hashicorp/go-multierror"
"github.com/hashicorp/terraform-plugin-log/tflog"
Expand Down Expand Up @@ -184,7 +185,7 @@ func resourceSentryProjectRead(ctx context.Context, d *schema.ResourceData, meta
func resourceSentryProjectUpdate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
client := meta.(*sentry.Client)

slug := d.Id()
project := d.Id()
org := d.Get("organization").(string)
params := &sentry.UpdateProjectParams{
Name: d.Get("name").(string),
Expand All @@ -208,21 +209,45 @@ func resourceSentryProjectUpdate(ctx context.Context, d *schema.ResourceData, me
params.ResolveAge = sentry.Int(v.(int))
}

tflog.Debug(ctx, "Updating Sentry project", map[string]interface{}{
"projectSlug": slug,
"org": org,
tflog.Debug(ctx, "Updating project", map[string]interface{}{
"org": org,
"project": project,
})
proj, _, err := client.Projects.Update(ctx, org, slug, params)
proj, _, err := client.Projects.Update(ctx, org, project, params)
if err != nil {
return diag.FromErr(err)
}
tflog.Debug(ctx, "Updated Sentry project", map[string]interface{}{
"projectSlug": proj.Slug,
"projectID": proj.ID,
"org": org,
})

d.SetId(proj.Slug)

if d.HasChange("team") {
o, n := d.GetChange("team")

tflog.Debug(ctx, "Adding team to project", map[string]interface{}{
"org": org,
"project": project,
"team": n,
})
_, _, err = client.Projects.AddTeam(ctx, org, project, n.(string))
if err != nil {
return diag.FromErr(err)
}

if o := o.(string); o != "" {
tflog.Debug(ctx, "Removing team from project", map[string]interface{}{
"org": org,
"project": project,
"team": o,
})
resp, err := client.Projects.RemoveTeam(ctx, org, project, o)
if err != nil {
if resp.Response.StatusCode != http.StatusNotFound {
return diag.FromErr(err)
}
}
}
}

return resourceSentryProjectRead(ctx, d, meta)
}

Expand Down
64 changes: 63 additions & 1 deletion sentry/resource_sentry_project_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,45 @@ func TestAccSentryProject_basic(t *testing.T) {
})
}

func TestAccSentryProject_changeTeam(t *testing.T) {
teamName1 := acctest.RandomWithPrefix("tf-team")
teamName2 := acctest.RandomWithPrefix("tf-team")
projectName := acctest.RandomWithPrefix("tf-project")
rn := "sentry_project.test"

check := func(teamName, projectName string) resource.TestCheckFunc {
var projectID string

return resource.ComposeTestCheckFunc(
testAccCheckSentryProjectExists(rn, &projectID),
resource.TestCheckResourceAttr(rn, "organization", testOrganization),
resource.TestCheckResourceAttr(rn, "team", teamName),
resource.TestCheckResourceAttr(rn, "name", projectName),
resource.TestCheckResourceAttrSet(rn, "slug"),
resource.TestCheckResourceAttr(rn, "platform", "go"),
resource.TestCheckResourceAttrSet(rn, "internal_id"),
resource.TestCheckResourceAttrPtr(rn, "internal_id", &projectID),
resource.TestCheckResourceAttrPair(rn, "project_id", rn, "internal_id"),
)
}

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
ProviderFactories: testAccProviderFactories,
CheckDestroy: testAccCheckSentryProjectDestroy,
Steps: []resource.TestStep{
{
Config: testAccSentryProjectConfig_changeTeam(teamName1, teamName2, projectName, "test_1"),
Check: check(teamName1, projectName),
},
{
Config: testAccSentryProjectConfig_changeTeam(teamName1, teamName2, projectName, "test_2"),
Check: check(teamName2, projectName),
},
},
})
}

func testAccCheckSentryProjectDestroy(s *terraform.State) error {
client := testAccProvider.Meta().(*sentry.Client)

Expand Down Expand Up @@ -121,9 +160,32 @@ func testAccSentryProjectConfig(teamName, projectName string) string {
return testAccSentryTeamConfig(teamName) + fmt.Sprintf(`
resource "sentry_project" "test" {
organization = sentry_team.test.organization
team = sentry_team.test.id
team = sentry_team.test.slug
name = "%[1]s"
platform = "go"
}
`, projectName)
}

func testAccSentryProjectConfig_changeTeam(teamName1, teamName2, projectName, teamResourceName string) string {
return testAccSentryOrganizationDataSourceConfig + fmt.Sprintf(`
resource "sentry_team" "test_1" {
organization = data.sentry_organization.test.id
name = "%[1]s"
slug = "%[1]s"
}

resource "sentry_team" "test_2" {
organization = data.sentry_organization.test.id
name = "%[2]s"
slug = "%[2]s"
}

resource "sentry_project" "test" {
organization = sentry_team.%[4]s.organization
team = sentry_team.%[4]s.slug
name = "%[3]s"
platform = "go"
}
`, teamName1, teamName2, projectName, teamResourceName)
}
1 change: 1 addition & 0 deletions sentry/resource_sentry_team_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ func TestAccSentryTeam_basic(t *testing.T) {
check := func(teamName string) resource.TestCheckFunc {
return resource.ComposeTestCheckFunc(
testAccCheckSentryTeamExists(rn, &teamID),
resource.TestCheckResourceAttr(rn, "id", teamName),
resource.TestCheckResourceAttrPair(rn, "organization", "data.sentry_organization.test", "id"),
resource.TestCheckResourceAttr(rn, "name", teamName),
resource.TestCheckResourceAttr(rn, "slug", teamName),
Expand Down