Skip to content

Commit

Permalink
Remove team from state if deletion failed and it does not exist
Browse files Browse the repository at this point in the history
  • Loading branch information
cytopia committed Jan 17, 2022
1 parent 98dddd2 commit 872296c
Showing 1 changed file with 26 additions and 0 deletions.
26 changes: 26 additions & 0 deletions github/resource_github_team.go
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,32 @@ func resourceGithubTeamDelete(d *schema.ResourceData, meta interface{}) error {

log.Printf("[DEBUG] Deleting team: %s", 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
}
}
// In case of a different error, return it as well
log.Printf("[ERROR] Failed to delete team: %s", d.Id())
return err
}
}
return err
}

Expand Down

0 comments on commit 872296c

Please sign in to comment.