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 polling for the get instance #249

Merged
merged 1 commit into from
Dec 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 13 additions & 2 deletions builder/powervs/step_create_instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,21 @@ func (s *StepCreateInstance) Run(_ context.Context, state multistep.StateBag) mu
return multistep.ActionHalt
}

in, err := instanceClient.Get(insIDs[0])
if err != nil {
var in *models.PVMInstance

//nolint:staticcheck // SA1015 this disable staticcheck for the next line
if err := pollUntil(time.Tick(30*time.Second), time.After(5*time.Minute), func() (bool, error) {
in, err = instanceClient.Get(insIDs[0])
if err != nil || in == nil {
ui.Message("No response or error encountered while retrieving the instance. Retrying...")
return false, nil
}
return true, nil
}); err != nil {
ui.Error(fmt.Sprintf("failed to get instance: %v", err))
return multistep.ActionHalt
}

ui.Message(fmt.Sprintf("Instance Created, Name: %s, ID: %s", *in.ServerName, *in.PvmInstanceID))

state.Put("instance", in)
Expand Down
24 changes: 24 additions & 0 deletions builder/powervs/util.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package powervs

import (
"fmt"
"time"
)

// pollUntil validates if a certain condition is met at defined poll intervals.
// If a timeout is reached, an associated error is returned to the caller.
// condition contains the use-case specific code that returns true when a certain condition is achieved.
func pollUntil(pollInterval, timeOut <-chan time.Time, condition func() (bool, error)) error {
for {
select {
case <-timeOut:
return fmt.Errorf("timed out while waiting for job to complete")
case <-pollInterval:
if done, err := condition(); err != nil {
return err
} else if done {
return nil
}
}
}
}
Loading