Skip to content

Commit

Permalink
Remove team from state if deletion failed and it does not exist (inte…
Browse files Browse the repository at this point in the history
  • Loading branch information
cytopia authored and kazaker committed Dec 28, 2022
1 parent 8f747af commit 1fd9311
Showing 1 changed file with 23 additions and 0 deletions.
23 changes: 23 additions & 0 deletions github/resource_github_team.go
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,29 @@ func resourceGithubTeamDelete(d *schema.ResourceData, meta interface{}) error {
ctx := context.WithValue(context.Background(), ctxId, d.Id())

_, err = client.Teams.DeleteTeamByID(ctx, orgId, id)
/*
When deleting a team and it failed, we need to check if it has already been deleted meanwhile.
This could be the case when deleting nested teams via Terraform by looping through a module
or resource and the parent team might have been deleted already. If the parent team had
been deleted already (via parallel runs), the child team is also already gone (deleted by
GitHub automatically).
So we're checking if it still exists and if not, simply remove it from TF state.
*/
if err != nil {
// Fetch the team in order to see if it exists or not (http 404)
_, _, err = client.Teams.GetTeamByID(ctx, orgId, id)
if err != nil {
if ghErr, ok := err.(*github.ErrorResponse); ok {
if ghErr.Response.StatusCode == http.StatusNotFound {
// If team we failed to delete does not exist, remove it from TF state.
log.Printf("[WARN] Removing team: %s from state because it no longer exists",
d.Id())
d.SetId("")
return nil
}
}
}
}
return err
}

Expand Down

0 comments on commit 1fd9311

Please sign in to comment.