-
Notifications
You must be signed in to change notification settings - Fork 30
feat (intake): refactor wait handler to use helper struct #7163
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
Open
GokceGK
wants to merge
10
commits into
main
Choose a base branch
from
feat/STACKITSDK-375-intake-refactor-wait-handler
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
42eacb3
feat (intake): refactor wait handler to use helper struct
GokceGK eb9d9da
Update services/intake/VERSION
GokceGK 0d778e1
Merge branch 'main' into feat/STACKITSDK-375-intake-refactor-wait-han…
GokceGK 6902f4c
feat(intake): update versions in the changelogs
GokceGK 4082397
Merge branch 'main' into feat/STACKITSDK-375-intake-refactor-wait-han…
GokceGK d8b940b
Merge branch 'main' into feat/STACKITSDK-375-intake-refactor-wait-han…
GokceGK c958575
Merge branch 'main' into feat/STACKITSDK-375-intake-refactor-wait-han…
GokceGK 1f87304
Merge branch 'main' into feat/STACKITSDK-375-intake-refactor-wait-han…
GokceGK ba4e042
Merge branch 'main' into feat/STACKITSDK-375-intake-refactor-wait-han…
GokceGK 2b0c501
feat(intake): seperate wait handlers for create and update operations
GokceGK 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 hidden or 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 hidden or 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 hidden or 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 |
|---|---|---|
| @@ -1 +1 @@ | ||
| v0.9.0 | ||
| v0.10.0 |
This file contains hidden or 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 |
|---|---|---|
|
|
@@ -3,11 +3,9 @@ package wait | |
| import ( | ||
| "context" | ||
| "errors" | ||
| "fmt" | ||
| "net/http" | ||
| "time" | ||
|
|
||
| "github.com/stackitcloud/stackit-sdk-go/core/oapierror" | ||
| "github.com/stackitcloud/stackit-sdk-go/core/wait" | ||
| intake "github.com/stackitcloud/stackit-sdk-go/services/intake/v1betaapi" | ||
| ) | ||
|
|
@@ -27,133 +25,147 @@ const ( | |
| INTAKEUSERRESPONSESTATE_DELETING = "deleting" | ||
| ) | ||
|
|
||
| func CreateOrUpdateIntakeRunnerWaitHandler(ctx context.Context, a intake.DefaultAPI, projectId, region, intakeRunnerId string) *wait.AsyncActionHandler[intake.IntakeRunnerResponse] { | ||
| handler := wait.New(func() (waitFinished bool, response *intake.IntakeRunnerResponse, err error) { | ||
| runner, err := a.GetIntakeRunner(ctx, projectId, region, intakeRunnerId).Execute() | ||
| if err != nil { | ||
| return false, nil, err | ||
| } | ||
|
|
||
| if runner == nil { | ||
| return false, nil, fmt.Errorf("API returned a nil response for Intake Runner %s", intakeRunnerId) | ||
| } | ||
|
|
||
| if runner.Id == intakeRunnerId && runner.State == INTAKERUNNERRESPONSESTATE_ACTIVE { | ||
| return true, runner, nil | ||
| } | ||
|
|
||
| // Deprecated: Will be removed after 2026-11-13. Use the CreateIntakeRunnerWaitHandler or UpdateIntakeRunnerWaitHandler instead | ||
| func CreateOrUpdateIntakeRunnerWaitHandler(ctx context.Context, client intake.DefaultAPI, projectId, region, intakeRunnerId string) *wait.AsyncActionHandler[intake.IntakeRunnerResponse] { | ||
| // TODO: mark function as private after deprecation period | ||
| waitConfig := wait.WaiterHelper[intake.IntakeRunnerResponse, string]{ | ||
| FetchInstance: client.GetIntakeRunner(ctx, projectId, region, intakeRunnerId).Execute, | ||
| GetState: func(response *intake.IntakeRunnerResponse) (string, error) { | ||
| if response == nil { | ||
| return "", errors.New("empty response") | ||
| } | ||
| return response.State, nil | ||
| }, | ||
| ActiveState: []string{INTAKERUNNERRESPONSESTATE_ACTIVE}, | ||
| ErrorState: []string{}, | ||
| // The API does not have a dedicated failure state for this resource, | ||
| // so we rely on the timeout for cases where it never becomes active. | ||
| return false, nil, nil | ||
| }) | ||
| } | ||
|
|
||
| handler := wait.New(waitConfig.Wait()) | ||
| handler.SetTimeout(15 * time.Minute) | ||
| return handler | ||
| } | ||
|
|
||
| func DeleteIntakeRunnerWaitHandler(ctx context.Context, a intake.DefaultAPI, projectId, region, intakeRunnerId string) *wait.AsyncActionHandler[intake.IntakeRunnerResponse] { | ||
| handler := wait.New(func() (waitFinished bool, response *intake.IntakeRunnerResponse, err error) { | ||
| _, err = a.GetIntakeRunner(ctx, projectId, region, intakeRunnerId).Execute() | ||
| if err == nil { | ||
| // Resource still exists | ||
| return false, nil, nil | ||
| } | ||
|
|
||
| var oapiError *oapierror.GenericOpenAPIError | ||
| if errors.As(err, &oapiError) { | ||
| if oapiError.StatusCode == http.StatusNotFound { | ||
| // Success: Resource is gone | ||
| return true, nil, nil | ||
| func CreateIntakeRunnerWaitHandler(ctx context.Context, client intake.DefaultAPI, projectId, region, intakeRunnerId string) *wait.AsyncActionHandler[intake.IntakeRunnerResponse] { | ||
| return CreateOrUpdateIntakeRunnerWaitHandler(ctx, client, projectId, region, intakeRunnerId) | ||
| } | ||
|
|
||
| func UpdateIntakeRunnerWaitHandler(ctx context.Context, client intake.DefaultAPI, projectId, region, intakeRunnerId string) *wait.AsyncActionHandler[intake.IntakeRunnerResponse] { | ||
| return CreateOrUpdateIntakeRunnerWaitHandler(ctx, client, projectId, region, intakeRunnerId) | ||
| } | ||
|
|
||
| func DeleteIntakeRunnerWaitHandler(ctx context.Context, client intake.DefaultAPI, projectId, region, intakeRunnerId string) *wait.AsyncActionHandler[intake.IntakeRunnerResponse] { | ||
| waitConfig := wait.WaiterHelper[intake.IntakeRunnerResponse, string]{ | ||
| FetchInstance: client.GetIntakeRunner(ctx, projectId, region, intakeRunnerId).Execute, | ||
| GetState: func(response *intake.IntakeRunnerResponse) (string, error) { | ||
| if response == nil { | ||
| return "", errors.New("empty response") | ||
| } | ||
| } | ||
| // An unexpected error occurred | ||
| return false, nil, err | ||
| }) | ||
| return response.State, nil | ||
| }, | ||
| ActiveState: []string{}, | ||
| ErrorState: []string{}, | ||
| DeleteHttpErrorStatusCodes: []int{http.StatusNotFound}, | ||
| } | ||
|
|
||
| handler := wait.New(waitConfig.Wait()) | ||
| handler.SetTimeout(15 * time.Minute) | ||
| return handler | ||
| } | ||
|
|
||
| func CreateOrUpdateIntakeWaitHandler(ctx context.Context, a intake.DefaultAPI, projectId, region, intakeId string) *wait.AsyncActionHandler[intake.IntakeResponse] { | ||
| handler := wait.New(func() (waitFinished bool, response *intake.IntakeResponse, err error) { | ||
| ik, err := a.GetIntake(ctx, projectId, region, intakeId).Execute() | ||
| if err != nil { | ||
| return false, nil, err | ||
| } | ||
|
|
||
| if ik == nil { | ||
| return false, nil, fmt.Errorf("API returned a nil response for Intake %s", intakeId) | ||
| } | ||
|
|
||
| if ik.Id == intakeId && ik.State == INTAKERESPONSESTATE_ACTIVE { | ||
| return true, ik, nil | ||
| } | ||
|
|
||
| if ik.Id == intakeId && ik.State == INTAKERESPONSESTATE_FAILED { | ||
| return true, ik, fmt.Errorf("create/update failed for Intake %s", intakeId) | ||
| } | ||
| // Deprecated: Will be removed after 2026-11-13. Use the CreateIntakeWaitHandler or UpdateIntakeWaitHandler instead | ||
| func CreateOrUpdateIntakeWaitHandler(ctx context.Context, client intake.DefaultAPI, projectId, region, intakeId string) *wait.AsyncActionHandler[intake.IntakeResponse] { | ||
|
Member
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. same here :) |
||
| // TODO: mark function as private after deprecation period | ||
| waitConfig := wait.WaiterHelper[intake.IntakeResponse, string]{ | ||
| FetchInstance: client.GetIntake(ctx, projectId, region, intakeId).Execute, | ||
| GetState: func(response *intake.IntakeResponse) (string, error) { | ||
| if response == nil { | ||
| return "", errors.New("empty response") | ||
| } | ||
| return response.State, nil | ||
| }, | ||
| ActiveState: []string{INTAKERUNNERRESPONSESTATE_ACTIVE}, | ||
| ErrorState: []string{INTAKERESPONSESTATE_FAILED}, | ||
| } | ||
|
|
||
| return false, nil, nil | ||
| }) | ||
| handler := wait.New(waitConfig.Wait()) | ||
| handler.SetTimeout(10 * time.Minute) | ||
| return handler | ||
| } | ||
|
|
||
| func DeleteIntakeWaitHandler(ctx context.Context, a intake.DefaultAPI, projectId, region, intakeId string) *wait.AsyncActionHandler[intake.IntakeResponse] { | ||
| handler := wait.New(func() (waitFinished bool, response *intake.IntakeResponse, err error) { | ||
| _, err = a.GetIntake(ctx, projectId, region, intakeId).Execute() | ||
| if err == nil { | ||
| return false, nil, nil | ||
| } | ||
|
|
||
| var oapiError *oapierror.GenericOpenAPIError | ||
| if errors.As(err, &oapiError) { | ||
| if oapiError.StatusCode == http.StatusNotFound { | ||
| return true, nil, nil | ||
| func CreateIntakeWaitHandler(ctx context.Context, client intake.DefaultAPI, projectId, region, intakeId string) *wait.AsyncActionHandler[intake.IntakeResponse] { | ||
| return CreateOrUpdateIntakeWaitHandler(ctx, client, projectId, region, intakeId) | ||
| } | ||
|
|
||
| func UpdateIntakeWaitHandler(ctx context.Context, client intake.DefaultAPI, projectId, region, intakeId string) *wait.AsyncActionHandler[intake.IntakeResponse] { | ||
| return CreateOrUpdateIntakeWaitHandler(ctx, client, projectId, region, intakeId) | ||
| } | ||
|
|
||
| func DeleteIntakeWaitHandler(ctx context.Context, client intake.DefaultAPI, projectId, region, intakeId string) *wait.AsyncActionHandler[intake.IntakeResponse] { | ||
| waitConfig := wait.WaiterHelper[intake.IntakeResponse, string]{ | ||
| FetchInstance: client.GetIntake(ctx, projectId, region, intakeId).Execute, | ||
| GetState: func(response *intake.IntakeResponse) (string, error) { | ||
| if response == nil { | ||
| return "", errors.New("empty response") | ||
| } | ||
| } | ||
| return false, nil, err | ||
| }) | ||
| return response.State, nil | ||
| }, | ||
| ActiveState: []string{}, | ||
| ErrorState: []string{}, | ||
| DeleteHttpErrorStatusCodes: []int{http.StatusNotFound}, | ||
| } | ||
|
|
||
| handler := wait.New(waitConfig.Wait()) | ||
| handler.SetTimeout(10 * time.Minute) | ||
| return handler | ||
| } | ||
|
|
||
| func CreateOrUpdateIntakeUserWaitHandler(ctx context.Context, a intake.DefaultAPI, projectId, region, intakeId, intakeUserId string) *wait.AsyncActionHandler[intake.IntakeUserResponse] { | ||
| handler := wait.New(func() (waitFinished bool, response *intake.IntakeUserResponse, err error) { | ||
| user, err := a.GetIntakeUser(ctx, projectId, region, intakeId, intakeUserId).Execute() | ||
| if err != nil { | ||
| return false, nil, err | ||
| } | ||
|
|
||
| if user == nil { | ||
| return false, nil, fmt.Errorf("API returned a nil response for Intake User %s", intakeUserId) | ||
| } | ||
|
|
||
| if user.Id == intakeUserId && user.State == INTAKEUSERRESPONSESTATE_ACTIVE { | ||
| return true, user, nil | ||
| } | ||
|
|
||
| // The API does not have a dedicated failure state for this resource, we rely on the timeout for cases where | ||
| // it never becomes active. | ||
| return false, nil, nil | ||
| }) | ||
| // Deprecated: Will be removed after 2026-11-13. Use the CreateIntakeUserWaitHandler or UpdateIntakeUserWaitHandler instead | ||
| func CreateOrUpdateIntakeUserWaitHandler(ctx context.Context, client intake.DefaultAPI, projectId, region, intakeId, intakeUserId string) *wait.AsyncActionHandler[intake.IntakeUserResponse] { | ||
|
Member
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. You can directly apply the thing I mentioned in the daily here // Deprecated: Will be removed after YYYY-MM-DD. Use the CreateIntakeUserWaitHandler or UpdateIntakeUserWaitHandler instead
func CreateOrUpdateIntakeUserWaitHandler(...) ... {
// TODO: mark function as private after deprecation period
// keep the existing logic
}
func CreateIntakeUserWaitHandler(...) ... {
return CreateOrUpdateIntakeUserWaitHandler(...)
}
func UpdateIntakeUserWaitHandler(...) ... {
return CreateOrUpdateIntakeUserWaitHandler(...)
}Deprecation period: now + 6 months |
||
| // TODO: mark function as private after deprecation period | ||
| waitConfig := wait.WaiterHelper[intake.IntakeUserResponse, string]{ | ||
| FetchInstance: client.GetIntakeUser(ctx, projectId, region, intakeId, intakeUserId).Execute, | ||
| GetState: func(response *intake.IntakeUserResponse) (string, error) { | ||
| if response == nil { | ||
| return "", errors.New("empty response") | ||
| } | ||
| return response.State, nil | ||
| }, | ||
| ActiveState: []string{INTAKEUSERRESPONSESTATE_ACTIVE}, | ||
| ErrorState: []string{}, | ||
| // The API does not have a dedicated failure state for this resource, | ||
| // so we rely on the timeout for cases where it never becomes active. | ||
| } | ||
|
|
||
| handler := wait.New(waitConfig.Wait()) | ||
| handler.SetTimeout(5 * time.Minute) | ||
| return handler | ||
| } | ||
|
|
||
| func DeleteIntakeUserWaitHandler(ctx context.Context, a intake.DefaultAPI, projectId, region, intakeId, intakeUserId string) *wait.AsyncActionHandler[intake.IntakeUserResponse] { | ||
| handler := wait.New(func() (waitFinished bool, response *intake.IntakeUserResponse, err error) { | ||
| _, err = a.GetIntakeUser(ctx, projectId, region, intakeId, intakeUserId).Execute() | ||
| if err == nil { | ||
| return false, nil, nil | ||
| } | ||
|
|
||
| var oapiError *oapierror.GenericOpenAPIError | ||
| if errors.As(err, &oapiError) { | ||
| if oapiError.StatusCode == http.StatusNotFound { | ||
| return true, nil, nil | ||
| func CreateIntakeUserWaitHandler(ctx context.Context, client intake.DefaultAPI, projectId, region, intakeId, intakeUserId string) *wait.AsyncActionHandler[intake.IntakeUserResponse] { | ||
| return CreateOrUpdateIntakeUserWaitHandler(ctx, client, projectId, region, intakeId, intakeUserId) | ||
| } | ||
|
|
||
| func UpdateIntakeUserWaitHandler(ctx context.Context, client intake.DefaultAPI, projectId, region, intakeId, intakeUserId string) *wait.AsyncActionHandler[intake.IntakeUserResponse] { | ||
| return CreateOrUpdateIntakeUserWaitHandler(ctx, client, projectId, region, intakeId, intakeUserId) | ||
| } | ||
|
|
||
| func DeleteIntakeUserWaitHandler(ctx context.Context, client intake.DefaultAPI, projectId, region, intakeId, intakeUserId string) *wait.AsyncActionHandler[intake.IntakeUserResponse] { | ||
| waitConfig := wait.WaiterHelper[intake.IntakeUserResponse, string]{ | ||
| FetchInstance: client.GetIntakeUser(ctx, projectId, region, intakeId, intakeUserId).Execute, | ||
| GetState: func(response *intake.IntakeUserResponse) (string, error) { | ||
| if response == nil { | ||
| return "", errors.New("empty response") | ||
| } | ||
| } | ||
| return false, nil, err | ||
| }) | ||
| return response.State, nil | ||
| }, | ||
| ActiveState: []string{}, | ||
| ErrorState: []string{}, | ||
| DeleteHttpErrorStatusCodes: []int{http.StatusNotFound}, | ||
| } | ||
|
|
||
| handler := wait.New(waitConfig.Wait()) | ||
| handler.SetTimeout(5 * time.Minute) | ||
| return handler | ||
| } | ||
Oops, something went wrong.
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.
and here :)