Skip to content

Commit

Permalink
Add polling for the get instance
Browse files Browse the repository at this point in the history
  • Loading branch information
mkumatag committed Dec 16, 2024
1 parent 2d0d6fe commit 5f8e151
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 2 deletions.
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
}
}
}
}

0 comments on commit 5f8e151

Please sign in to comment.