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

Add drift handling #226

Merged
merged 16 commits into from
Mar 9, 2022
Merged

Add drift handling #226

merged 16 commits into from
Mar 9, 2022

Conversation

samuel-br
Copy link
Contributor

@samuel-br samuel-br commented Feb 17, 2022

Issue & Steps to Reproduce / Feature Request

fixes #225

Solution

Add check if deleted in read function to determine if drift happened , and invoke destroy that remove the resource from the state, instead of throwing error

@samuel-br samuel-br changed the title add drift handling Add drift handling #225 Feb 17, 2022
@eranelbaz eranelbaz requested a review from a team February 17, 2022 11:16
Copy link
Contributor

@yaronya yaronya left a comment

Choose a reason for hiding this comment

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

Nice work @samuel-br
Left some comments/questions

Please also see my comments about the missing test case. It looks like you're missing this tests in all relevant resources

env0/resource_cloud_credentials_project_assignment.go Outdated Show resolved Hide resolved
env0/resource_environment.go Outdated Show resolved Hide resolved
env0/resource_team_project_assignment.go Outdated Show resolved Hide resolved
env0/resource_team_project_assignment.go Show resolved Hide resolved
@yaronya yaronya changed the title Add drift handling #225 Add drift handling Feb 17, 2022
yaronya
yaronya previously approved these changes Feb 20, 2022
@eranelbaz eranelbaz removed their request for review February 21, 2022 09:52
@yaronya yaronya dismissed their stale review February 21, 2022 09:52

Raz has some additional comments

@razbensimon
Copy link
Contributor

#219 (comment)
according to that comment, I am wondering why we don't have any call to d.IsNewResource ?
general behavior IIUC that drift flow - is when d.IsNewResource is true, we should fail/error when exists.
if d.IsNewResource is false AND we got some relevant Http Code (eg. NotFound) we should understand it as drift and call SetId("").

I'm not 100% sure about what I said, and just raise a flag for you, because that's what I understood from the TF Read examples over the web (eg AWS).
also I saw that maybe it can help.

@samuel-br just validate that please, and QA your changes by running the provider after drift changes (in env0).
don't hesitate to ask for help if needed.

@samuel-br
Copy link
Contributor Author

I think you right but as you said i want to validate it, the explenation i think, its because the d.isnewresource is true when we plan a resource its pushing it to TF state, but its not yet created on the "real world" and then every read will get exeption, but what i dont understand is how we dont use this for now and there is still no error when apply a new resource.

@samuel-br samuel-br requested a review from yaronya March 6, 2022 19:26
Copy link
Contributor

@razbensimon razbensimon left a comment

Choose a reason for hiding this comment

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

@samuel-br I am not sure i understood your comment.
also please write a propper description of that PR.

@yaronya @eranelbaz please revisit my comments here.
I am not sure about it...

Comment on lines 73 to 77
if !found {
if !d.IsNewResource() {
d.SetId("")
}
}
Copy link
Contributor

Choose a reason for hiding this comment

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

if !d.IsNewResource() && !found {
		log.Printf("[WARN] Project (%s) not found, removing from state", d.Id())
		d.SetId("")
		return nil
	}

Looks better to me. I took it from S3 bucket provider as example / reference.
Please do the same for others if can.

I am still not sure about this one.
IIUC we should still throw on d.IsNewResource()==true && someErrorOrBadCodeExists.
I don't see any error returned on that case. @yaronya maybe you can revisit my thoughts here?

Copy link
Member

Choose a reason for hiding this comment

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

i don't think we need to throw something, but we do need to return nil here

Copy link
Contributor

Choose a reason for hiding this comment

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

Yes, @eranelbaz is correct - we don't need to throw anything. This is what we wanted to avoid in first place.
We should be removing the resource from state.

@samuel-br haven't we previously discussed it and said that we don't really need to check whether IsNewResource because TF doesn't read the resource before create (only before update/delete) so this would always return false?

Copy link
Contributor

Choose a reason for hiding this comment

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

@yaronya i think read is invoked on create too..

Copy link
Member

Choose a reason for hiding this comment

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

read invokes on terraform refresh IIRC

Copy link
Member

Choose a reason for hiding this comment

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

@razbensimon did you mean to fix the if or for the log?

Copy link
Contributor

Choose a reason for hiding this comment

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

the if is good now, just suggested adding logs to the user as I saw in the S3 bucket example.
for your decision @samuel-br

Copy link
Contributor Author

Choose a reason for hiding this comment

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

"drift detected: TerraForm will remove {this resource} from state" is that good wording?

Copy link
Contributor

Choose a reason for hiding this comment

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

"[WARN] Drift Detected: Terraform will remove {this resource} from state"

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@@ -286,6 +286,7 @@ type Environment struct {
LifespanEndAt string `json:"lifespanEndAt"`
LatestDeploymentLogId string `json:"latestDeploymentLogId"`
LatestDeploymentLog DeploymentLog `json:"latestDeploymentLog"`
IsArchived bool `json:"isArchived"`
Copy link
Contributor

Choose a reason for hiding this comment

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

don't we need to use it on the logic somewhere?
just wondering

Copy link
Contributor

Choose a reason for hiding this comment

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

The PR handled also environment resource at start but since it isn't so trivial to re-create an environment on some drift and there's a deeper product decision to make before implementing it, we've decided to ditch it for now and create a separate issue for environment and project resources.
This looks like a leftover tho.

@samuel-br did you create that seperate issue btw?

Copy link
Member

@eranelbaz eranelbaz Mar 7, 2022

Choose a reason for hiding this comment

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

Copy link
Contributor

Choose a reason for hiding this comment

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

I'm not so sure. I expect to see an issue that includes environment and project resources in its description, but couldn't find it.

If I get that correctly:

Copy link
Contributor Author

Choose a reason for hiding this comment

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

nope, Ill do it later today, but I want to summary this before on channel, cause for now there is three or for types of resources in this contexts of problem

Copy link
Member

@eranelbaz eranelbaz Mar 9, 2022

Choose a reason for hiding this comment

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

@samuel-br maybe just change their title?
Handle drift - general
Handle drift - specific resources
etc

and link them

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 confused with all of those issues.
I just asked to make the PR with a good description (copy-paste the issue if needed to the PR).

anyways, if that line of code is related to this PR keep it.
if not - remove it

@samuel-br
cc @eranelbaz

Copy link
Contributor

Choose a reason for hiding this comment

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

I've changed the title for #250 already FYI.
And yes, prefer not having multiple issues with the same name.

Copy link
Contributor

Choose a reason for hiding this comment

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

@samuel-br can you please share on the main issue (i.e #205) what's needed to be done basically and why it differs from the other issues (parsing the error codes from http client layer and etc.)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@yaronya @eranelbaz I changed the issues names (@yaronya thanks on #250 ) and add #205 (comment) on the main issue, please take a look on that comment to ensure its clear enough

@github-actions github-actions bot added the fix label Mar 8, 2022
@github-actions github-actions bot added ready to merge PR approved - can be merged once the PR owner is ready and removed pending final review labels Mar 9, 2022
@samuel-br samuel-br merged commit f735547 into main Mar 9, 2022
@samuel-br samuel-br deleted the fix-handle_drift_withIsDeleted-#225 branch March 9, 2022 11:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
api-client fix provider ready to merge PR approved - can be merged once the PR owner is ready
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Handle drift - specific resources
4 participants