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

Fix not found error checks #327

Merged
merged 8 commits into from
Aug 10, 2023

Conversation

MusicDin
Copy link
Member

@MusicDin MusicDin commented Aug 9, 2023

Currently, when the provider retrieves LXD storage pool (or tries to delete it), it checks whether an error equals to No such object. In such case it detects that the storage pool does not exist and an error is not reported.

However, LXD returns different error in such cases (https://github.com/canonical/lxd/blob/7f46f71133411a66be20fce1a2b92de3923db705/lxd/db/storage_pools.go#L666) - Storage pool not found.

@MusicDin
Copy link
Member Author

MusicDin commented Aug 9, 2023

Since the error message has been changed only one year ago (canonical/lxd@ce7b3fb), maybe we should keep both options for now (for those who are still running older versions of LXD):

if err != nil && (err.Error() == "No such object" || err.Error() == "Storage pool not found") {
        // ...
}

@simondeziel @adamcstephens Any thoughts on that?

@MusicDin MusicDin changed the title Fix check if storage pool is not found Fix error check if storage pool is not found Aug 9, 2023
@simondeziel
Copy link
Member

Thanks for thinking about backward compat! The commit you found was backported to 5.0 LTS (canonical/lxd@d442d20) but not to 4.0 LTS, so yeah, I think it's worth checking for both error strings.

Excuse my ignorance but isn't there a HTTP return code accompanying the error? I'd expect the old and new errors to both come with a 404. If that's available, that'd potentially be less brittle than trying to look at the error string itself.

@MusicDin
Copy link
Member Author

MusicDin commented Aug 9, 2023

You are right, we could check if the error is of type api.StatusError and extract the status code from it.

@MusicDin MusicDin changed the title Fix error check if storage pool is not found Fix not found error checks Aug 10, 2023
@MusicDin
Copy link
Member Author

I have replaced the string comparison error checks with the status code error checks.

This approach works both for LXD version 4 and 5. However, LXD versions 3 and below do not implement StatusError, which is why I have created an utility function in errors.go (maybe not an appropriate place for that) that checks if the error message equals No such object or not found before trying to convert it into StatusError.

In addition, I have replaced most of the not found errors which follow the same approach.

Copy link
Collaborator

@adamcstephens adamcstephens left a comment

Choose a reason for hiding this comment

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

Thanks!

@adamcstephens adamcstephens merged commit 5d7ec86 into terraform-lxd:master Aug 10, 2023
2 checks passed
@MusicDin MusicDin deleted the fix/storage-pool-exists branch February 28, 2024 15:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants