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

Conversation

cytopia
Copy link
Contributor

@cytopia cytopia commented Jan 17, 2022

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

I experienced this behaviour while continuing the work on team slug: #802 So in order not to submit all changes at once, I will make this a separete PR (it does affect current provider as well)

Current state

When deleting a team on GitHub, the API will automatically delete all of its child team. In case we want to remove a team with the terraform provider which parent team has already been deleted, the resource will fail and report back with a 404 - team not found (or slightly similar).

Improvement

When deleting a team with this terraform provider and it fails, we try to fetch it and check to see if it returns a 404. If the team we tried to delete does not exist, we will simply remove it from TF state and continue grafefully.

Notes on code

As I am pretty new to golan, please give me any hints on styling, comments or best-practices which I might have done wrongly.

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?

@jcudit jcudit added this to the v4.20.0 milestone Jan 21, 2022
Copy link
Contributor

@jcudit jcudit left a comment

Choose a reason for hiding this comment

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

Looking close! This seems difficult to get a test case set up for so maybe we add additional documentation around this case for awareness.

Aiming to ship in an upcoming release.

Comment on lines +247 to +252
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.
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.

@cytopia
Copy link
Contributor Author

cytopia commented Jan 22, 2022

This seems difficult to get a test case set up for

I haven't checked yet how the internal tests work. Two integration tests I can think of might be written as so:

1. Positive check:

  1. Terraform Run 1: Provision team with child team
  2. GH API call: Remove parent team
  3. Terraform Run 2: Remove child team and ensure the WARN message exists in the output

2. Negative check:

  1. Terraform Run 1: Provision team with child team
  2. Terraform Run 2: Remove child team and ensure the WARN message does not exist
  3. Terraform Run 3: Remove team and ensure the WARN message does not exist

Copy link
Member

@kfcampbell kfcampbell left a comment

Choose a reason for hiding this comment

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

Let's go ahead and ship this, and we can respond to feedback as it comes up.

@kfcampbell kfcampbell modified the milestones: v4.20.0, v4.20.1 Mar 3, 2022
@kfcampbell kfcampbell merged commit 444448d into integrations:main Mar 3, 2022
kfcampbell pushed a commit to kfcampbell/terraform-provider-github that referenced this pull request Jul 26, 2022
kazaker pushed a commit to auto1-oss/terraform-provider-github that referenced this pull request Dec 28, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants