-
Notifications
You must be signed in to change notification settings - Fork 89
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
DXCDT-458: Handling 404 errors in data sources #698
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
1a2a71b
Merge branch 'v1' of https://github.com/auth0/terraform-provider-auth…
willvedd 066f4c0
Adding recordings
willvedd f90c71a
Modifying error message
willvedd c2d0901
Fixing test
willvedd 651ef54
Merge branch 'v1' into DXCDT-458-handle-404-data-sources
willvedd 7d916c1
Merge branch 'v1' of https://github.com/auth0/terraform-provider-auth…
willvedd 21441ea
Merge branch 'DXCDT-458-handle-404-data-sources' of https://github.co…
willvedd File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package auth0 | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
|
||
"github.com/hashicorp/terraform-plugin-sdk/v2/diag" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" | ||
) | ||
|
||
// CheckFor404Error accepts and executes a resource's read function within the context | ||
// of a data source and will appropriately error when a resource is not found. | ||
func CheckFor404Error(ctx context.Context, readFunc func(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics, d *schema.ResourceData, m interface{}) diag.Diagnostics { | ||
diags := readFunc(ctx, d, m) | ||
if diags != nil { | ||
return diags | ||
} | ||
if d.Id() == "" { | ||
return diag.FromErr(errors.New("no resource found with that identifier (404)")) | ||
} | ||
return nil | ||
} | ||
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,7 +2,6 @@ package organization | |
|
||
import ( | ||
"context" | ||
"net/http" | ||
|
||
"github.com/auth0/go-auth0/management" | ||
"github.com/hashicorp/go-multierror" | ||
|
@@ -79,10 +78,6 @@ func readOrganizationForDataSource(ctx context.Context, data *schema.ResourceDat | |
if organizationID != "" { | ||
foundOrganization, err = api.Organization.Read(ctx, organizationID) | ||
if err != nil { | ||
if mErr, ok := err.(management.Error); ok && mErr.Status() == http.StatusNotFound { | ||
data.SetId("") | ||
return nil | ||
} | ||
Comment on lines
-82
to
-85
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Deleted because we want to handle 404s in the data source. |
||
return diag.FromErr(err) | ||
} | ||
} else { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,10 +2,8 @@ package resourceserver | |
|
||
import ( | ||
"context" | ||
"net/http" | ||
"net/url" | ||
|
||
"github.com/auth0/go-auth0/management" | ||
"github.com/hashicorp/go-multierror" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/diag" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" | ||
|
@@ -49,10 +47,6 @@ func readResourceServerForDataSource(ctx context.Context, data *schema.ResourceD | |
api := meta.(*config.Config).GetAPI() | ||
resourceServer, err := api.ResourceServer.Read(ctx, resourceServerID) | ||
if err != nil { | ||
if mErr, ok := err.(management.Error); ok && mErr.Status() == http.StatusNotFound { | ||
data.SetId("") | ||
return nil | ||
} | ||
Comment on lines
-52
to
-55
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Deleted because we want to handle 404s in the data source. This fixes #604 . |
||
return diag.FromErr(err) | ||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
39 changes: 39 additions & 0 deletions
39
test/data/recordings/TestAccDataSourceClientNonexistentID.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
--- | ||
version: 2 | ||
interactions: | ||
- id: 0 | ||
request: | ||
proto: HTTP/1.1 | ||
proto_major: 1 | ||
proto_minor: 1 | ||
content_length: 5 | ||
transfer_encoding: [] | ||
trailer: {} | ||
host: terraform-provider-auth0-dev.eu.auth0.com | ||
remote_addr: "" | ||
request_uri: "" | ||
body: | | ||
null | ||
form: {} | ||
headers: | ||
Content-Type: | ||
- application/json | ||
User-Agent: | ||
- Go-Auth0/1.0.0-beta.0 | ||
url: https://terraform-provider-auth0-dev.eu.auth0.com/api/v2/clients/this-client-does-not-exist | ||
method: GET | ||
response: | ||
proto: HTTP/2.0 | ||
proto_major: 2 | ||
proto_minor: 0 | ||
transfer_encoding: [] | ||
trailer: {} | ||
content_length: -1 | ||
uncompressed: true | ||
body: '{"signing_keys":[{"cert":"[REDACTED]"}]}' | ||
headers: | ||
Content-Type: | ||
- application/json; charset=utf-8 | ||
status: 404 Not Found | ||
code: 404 | ||
duration: 138.304ms |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Aside from abstracting-out each read function and handling the errors within the respective instances (resource vs data source), detecting an empty ID is the simplest solution, albeit not the most elegant.
This is mostly just a proposal of what we could do but at this point I don't think doing a wider refactor would be that much more effort and establishes a better code pattern.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is already a solid n+1, we can refactor further after everything's in v1 and we have time. 👍🏻