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

Remove team from state if deletion failed and it does not exist #1039

Merged
merged 1 commit into from
Mar 3, 2022
Merged
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
23 changes: 23 additions & 0 deletions github/resource_github_team.go
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,29 @@ 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.
Comment on lines +247 to +252
Copy link
Contributor

Choose a reason for hiding this comment

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

This is a really good comment to add to our docs on this resource as well.

*/
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",
Copy link
Contributor Author

Choose a reason for hiding this comment

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

The following warning does not show up with TF_LOG=WARN. It only shows up in TF_LOG=DEBUG. Not sure if this is a general issue with this provider or just with me doing something wrong in the code:

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@jcudit the only thing I'd need some help on is the logging level as the above is only showing up in TG_LOG=DEBUG. Example line below:

2022-01-22T10:47:11.813Z [DEBUG] provider.terraform-provider-github_v4.19.1-fl.4: 2022/01/22 10:47:11 [WARN] Removing team: 5592187 from state because it no longer exists

However, when looking at a valid WARN message, it looks like this:

2022-01-22T10:47:04.978Z [WARN]  ValidateProviderConfig from "provider[\"registry.terraform.io/flaconi/github\"]" changed the config value, but that value is unused

Any idea on what I'm doing wrong here?

Copy link
Contributor

Choose a reason for hiding this comment

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

I am stumped. This looks very similar to what I'm seeing in hashicorp/terraform, so 😕 why it is misbehaving for you.

Does TF_LOG=JSON do anything different? Perhaps TG_LOG was used in error?

d.Id())
d.SetId("")
return nil
}
}
}
}
return err
}

Expand Down