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

Modify github_team_repository to accept slug as a valid team_id as well #693

Merged
merged 7 commits into from
Feb 18, 2021
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
40 changes: 24 additions & 16 deletions github/resource_github_team_repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@ func resourceGithubTeamRepository() *schema.Resource {

Schema: map[string]*schema.Schema{
"team_id": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validateTeamIDFunc,
Type: schema.TypeString,
Required: true,
ForceNew: true,
Description: "ID or slug of team",
},
"repository": {
Type: schema.TypeString,
Expand Down Expand Up @@ -55,18 +55,20 @@ func resourceGithubTeamRepositoryCreate(d *schema.ResourceData, meta interface{}
client := meta.(*Owner).v3client
orgId := meta.(*Owner).id

teamIdString := d.Get("team_id").(string)
teamId, err := strconv.ParseInt(teamIdString, 10, 64)
// The given team id could be an id or a slug
givenTeamId := d.Get("team_id").(string)
teamId, err := getTeamID(givenTeamId, meta)
if err != nil {
return unconvertibleIdErr(teamIdString, err)
return err
}

orgName := meta.(*Owner).name
repoName := d.Get("repository").(string)
permission := d.Get("permission").(string)
ctx := context.Background()

log.Printf("[DEBUG] Creating team repository association: %s:%s (%s/%s)",
teamIdString, permission, orgName, repoName)
givenTeamId, permission, orgName, repoName)
_, err = client.Teams.AddTeamRepoByID(ctx,
orgId,
teamId,
Expand All @@ -81,7 +83,7 @@ func resourceGithubTeamRepositoryCreate(d *schema.ResourceData, meta interface{}
return err
}

d.SetId(buildTwoPartID(teamIdString, repoName))
d.SetId(buildTwoPartID(strconv.FormatInt(teamId, 10), repoName))

return resourceGithubTeamRepositoryRead(d, meta)
}
Expand All @@ -99,7 +101,6 @@ func resourceGithubTeamRepositoryRead(d *schema.ResourceData, meta interface{})
if err != nil {
return err
}

teamId, err := strconv.ParseInt(teamIdString, 10, 64)
if err != nil {
return unconvertibleIdErr(teamIdString, err)
Expand Down Expand Up @@ -128,7 +129,11 @@ func resourceGithubTeamRepositoryRead(d *schema.ResourceData, meta interface{})
}

d.Set("etag", resp.Header.Get("ETag"))
d.Set("team_id", teamIdString)
if d.Get("team_id") == "" {
// If team_id is empty, that means we are importing the resource.
// Set the team_id to be the id of the team.
d.Set("team_id", teamIdString)
}
Comment on lines +132 to +136
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems like the riskiest bit. What was your experience leading up to this diff?

Copy link
Contributor Author

@k24dizzle k24dizzle Feb 11, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In my testing, I didn't have this if statement at first.

I tried setting the team_id to be a team slug, and was able to plan and apply the following config to create a new team repo connection:

resource "github_team_repository" "test" {
    team_id = "sampleteam"
    repository = "testteamrepo"
    permission = "push"
}

When I tried another plan after that I expected that terraform would say that there would be no changes necessary. However, I got the following output:

Terraform will perform the following actions:

  # github_team_repository.test must be replaced
-/+ resource "github_team_repository" "test" {
      ~ etag       = "W/\"6469d39bbad910892556bd4bb130c23ab7ffb655ec6a312d592f2e76736d67c9\"" -> (known after apply)
      ~ id         = "4466024:testteamrepo" -> (known after apply)
        permission = "push"
        repository = "testteamrepo"
      ~ team_id    = "4466024" -> "123" # forces replacement
    }

Plan: 1 to add, 0 to change, 1 to destroy.

When terraform refreshed the state, using this resourceGithubTeamRepositoryRead function, it would replace the team_id from a slug to the actual team id, making it think that it would need to replace this resource.

So I added this if statement to avoid this situation. We want to make sure that the team_id field remains whatever the user set initially. The only situation I could think of where we would want to actually set this team_id field here, would be when we were importing a team_repo resource, in which we would want to populate this field for the user.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Appreciate the detail here, thanks.

d.Set("repository", repo.GetName())

permName, permErr := getRepoPermission(repo.GetPermissions())
Expand All @@ -150,13 +155,15 @@ func resourceGithubTeamRepositoryUpdate(d *schema.ResourceData, meta interface{}
client := meta.(*Owner).v3client
orgId := meta.(*Owner).id

teamIdString := d.Get("team_id").(string)
teamIdString, repoName, err := parseTwoPartID(d.Id(), "team_id", "repository")
if err != nil {
return err
}
teamId, err := strconv.ParseInt(teamIdString, 10, 64)
if err != nil {
return unconvertibleIdErr(teamIdString, err)
}
orgName := meta.(*Owner).name
repoName := d.Get("repository").(string)
permission := d.Get("permission").(string)
ctx := context.WithValue(context.Background(), ctxId, d.Id())

Expand Down Expand Up @@ -190,14 +197,15 @@ func resourceGithubTeamRepositoryDelete(d *schema.ResourceData, meta interface{}
client := meta.(*Owner).v3client
orgId := meta.(*Owner).id

teamIdString := d.Get("team_id").(string)

teamIdString, repoName, err := parseTwoPartID(d.Id(), "team_id", "repository")
if err != nil {
return err
}
teamId, err := strconv.ParseInt(teamIdString, 10, 64)
if err != nil {
return unconvertibleIdErr(teamIdString, err)
}
orgName := meta.(*Owner).name
repoName := d.Get("repository").(string)
ctx := context.WithValue(context.Background(), ctxId, d.Id())

log.Printf("[DEBUG] Deleting team repository association: %s (%s/%s)",
Expand Down
Loading