-
Notifications
You must be signed in to change notification settings - Fork 208
chore: Use timeouts attributes in autogen resources #3817
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
Conversation
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.
Pull Request Overview
This PR refactors autogenerated resource handlers to use timeout attributes from Terraform configurations instead of hard-coded timeout values. The changes move error checking into the shared handler functions and replace the TimeoutSeconds field with a Timeout field of type time.Duration.
Key changes:
- Shared handler functions now perform early error checking via
HasError()calls - Resources with wait configurations now extract timeout values from plan/state using the Terraform framework's timeout methods
- The
WaitReqstruct'sTimeoutSecondsinteger field is replaced with aTimeoutfield of typetime.Duration
Reviewed Changes
Copilot reviewed 20 out of 20 changed files in this pull request and generated 2 comments.
Show a summary per file
| File | Description |
|---|---|
| tools/codegen/gofilegen/resource/testdata/wait-configuration.golden.go | Test data updated to demonstrate timeout extraction from plan/state and removal of early error checks |
| tools/codegen/gofilegen/resource/testdata/update-with-put.golden.go | Test data showing removal of early error checks in handlers |
| tools/codegen/gofilegen/resource/testdata/static-request-body-delete.golden.go | Test data showing removal of early error checks in handlers |
| tools/codegen/gofilegen/resource/testdata/no-op-delete-operation.golden.go | Test data showing removal of early error checks in handlers |
| tools/codegen/gofilegen/resource/testdata/different-urls-with-path-params.golden.go | Test data showing removal of early error checks in handlers |
| tools/codegen/gofilegen/codetemplate/resource-file.go.tmpl | Template updated to generate timeout extraction code and use Timeout field instead of TimeoutSeconds |
| internal/serviceapi/streamprocessorapi/resource.go | Resource updated to extract timeouts and pass time.Duration values |
| internal/serviceapi/streaminstanceapi/resource.go | Resource updated to remove early error checks |
| internal/serviceapi/searchdeploymentapi/resource.go | Resource updated to extract timeouts and pass time.Duration values |
| internal/serviceapi/resourcepolicyapi/resource.go | Resource updated to remove early error checks |
| internal/serviceapi/pushbasedlogexportapi/resource.go | Resource updated to extract timeouts and pass time.Duration values |
| internal/serviceapi/projectsettingsapi/resource.go | Resource updated to remove early error checks |
| internal/serviceapi/projectapi/resource.go | Resource updated to remove early error checks |
| internal/serviceapi/orgserviceaccountapi/resource.go | Resource updated to remove early error checks |
| internal/serviceapi/maintenancewindowapi/resource.go | Resource updated to remove early error checks |
| internal/serviceapi/databaseuserapi/resource.go | Resource updated to remove early error checks |
| internal/serviceapi/customdbroleapi/resource.go | Resource updated to remove early error checks |
| internal/serviceapi/clusterapi/resource.go | Resource updated to extract timeouts and pass time.Duration values |
| internal/serviceapi/auditingapi/resource.go | Resource updated to remove early error checks |
| internal/common/autogen/handle_operations.go | Shared handler functions updated to check for errors early and accept time.Duration for timeout |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| func (r *rs) Create(ctx context.Context, req resource.CreateRequest, resp *resource.CreateResponse) { | ||
| var plan TFModel | ||
| resp.Diagnostics.Append(req.Plan.Get(ctx, &plan)...) | ||
| if resp.Diagnostics.HasError() { |
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.
Why are we removing this?
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.
as in the PR description, we are moving it to the beginning of the handle methods like HandleCreate, etc.
No need to check it in each single resource when it can be done in the common code
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.
I see, so the line plan.Timeouts.Create(ctx, 300*time.Second) will not have some weird error if this fails earlier?
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.
uhm, let me try that
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.
simulated that setting the plan fails, it works fine because timeouts just get the default one, and then HandleCreate checks HasErrors in the beginning and displays the error
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.
Just a few questions. I think it makes the code a bit less readable, but no strong opinion.
| if resp.Diagnostics.HasError() { | ||
| return | ||
| } |
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.
I appreciate the effort to reduce template size. In this case I think its clearer to leave this check here right after the Plan.Get() call instead of waiting until the create/update/read operation to check the diags.
I understand that following this approach would also mean adding a check for the timeout creation.
Given that checking errors after a function call is a typical go pattern, I think its ok to generate some extra code for it.
No strong feelings either way, we can always go back to generating checks if necessary.
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.
I think it's clearer just to set all the input params for handle operations, and the doing the "real" logic including error checks in handler functions.
Good thing about diagnostics is that it keeps a list of errors, warning, etc. so we don't lose information even if we accumulate some of them and we don't do HasErrors every time.
as in this comment, there is no need to do additional checks as timeouts won't fail (it will just get the default value), but if both @manupedrozo @EspenAlbert prefer keeping the check, I'll do that, reverting those changes.
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.
timeouts never fails (it only logs info sometimes in current implementation) but will also add hasErrors to follow the same 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.
Feel free to keep it; no strong opinion, and in general, the handler functions will have better test coverage.
Anything added to the resources before calling the handler function must ensure that they can handle default values.
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.
I think current approach has better tradeoffs (keeping resources just with input params and move away logic that is common) but also think it's important to get consensus, reverted here: e903635
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.
Thank you!
* master: chore: Invoke Delete operation if timeout in autogen (#3820) Add support for typed nested sets in autogen (#3819) chore: Use timeouts attributes in autogen resources (#3817) refactor: Adds invalid update error for delete_on_create_timeout for sdkv2 (#3810) Add support for typed lists in autogen (#3816) refactor: Uses delete_on_create_timeout with default=true support across TPF resources (#3809) Rename customtype package to customtypes (#3814) chore: Add support for typed nested lists in autogen (#3813) don't schedule matrix until CLOUDP-353513 (#3812) chore: Fix stream tests notifications (#3805) chore: Bump golang.org/x/oauth2 from 0.31.0 to 0.32.0 (#3803) depends on data sources (#3808) test: Avoids flaky test by simplifying the check, ensuring count > 0 instead of reading API response and trying exact match (#3806) chore: Generate timeouts attribute for autogen resources (#3802) # Conflicts: # .github/workflows/acceptance-tests-runner.yml
Description
Use timeouts attributes in autogen resources.
Link to any related issue(s): CLOUDP-352321
Type of change:
Required Checklist:
Further comments