Skip to content

Commit

Permalink
refactor: use consistent task status names
Browse files Browse the repository at this point in the history
  • Loading branch information
dkoshkin committed Jan 3, 2024
1 parent 5a13aa2 commit 3e63d4c
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 31 deletions.
2 changes: 1 addition & 1 deletion controllers/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -346,7 +346,7 @@ func GetImageUUID(ctx context.Context, client *nutanixClientV3.Client, imageName
// HasTaskInProgress returns true if the given task is in progress
func HasTaskInProgress(ctx context.Context, client *nutanixClientV3.Client, taskUUID string) (bool, error) {
log := ctrl.LoggerFrom(ctx)
taskStatus, err := nutanixClientHelper.GetTaskState(ctx, client, taskUUID)
taskStatus, err := nutanixClientHelper.GetTaskStatus(ctx, client, taskUUID)
if err != nil {
return false, err
}
Expand Down
2 changes: 1 addition & 1 deletion controllers/nutanixmachine_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -666,7 +666,7 @@ func (r *NutanixMachineReconciler) getOrCreateVM(rctx *nctx.MachineContext) (*nu
return nil, errorMsg
}
log.Info(fmt.Sprintf("Waiting for task %s to get completed for VM %s", lastTaskUUID, rctx.NutanixMachine.Name))
err = nutanixClient.WaitForTaskCompletion(ctx, nc, lastTaskUUID)
err = nutanixClient.WaitForTaskToSucceed(ctx, nc, lastTaskUUID)
if err != nil {
errorMsg := fmt.Errorf("error occurred while waiting for task %s to start: %v", lastTaskUUID, err)
rctx.SetFailureStatus(capierrors.CreateMachineError, errorMsg)
Expand Down
22 changes: 11 additions & 11 deletions pkg/client/state.go → pkg/client/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,24 +29,24 @@ import (

const (
pollingInterval = time.Second * 2
stateSucceeded = "SUCCEEDED"
statusSucceeded = "SUCCEEDED"
)

// WaitForTaskCompletion will poll indefinitely every 2 seconds for the task with uuid to have status of "SUCCEEDED".
// Returns an error from GetTaskState or a timeout error if the context is cancelled.
func WaitForTaskCompletion(ctx context.Context, conn *nutanixClientV3.Client, uuid string) error {
// WaitForTaskToSucceed will poll indefinitely every 2 seconds for the task with uuid to have status of "SUCCEEDED".
// Returns an error from GetTaskStatus or a timeout error if the context is cancelled.
func WaitForTaskToSucceed(ctx context.Context, conn *nutanixClientV3.Client, uuid string) error {
return wait.PollImmediateInfiniteWithContext(ctx, pollingInterval, func(ctx context.Context) (done bool, err error) {
state, getErr := GetTaskState(ctx, conn, uuid)
return state == stateSucceeded, getErr
status, getErr := GetTaskStatus(ctx, conn, uuid)
return status == statusSucceeded, getErr
})
}

func GetTaskState(ctx context.Context, client *nutanixClientV3.Client, taskUUID string) (string, error) {
func GetTaskStatus(ctx context.Context, client *nutanixClientV3.Client, uuid string) (string, error) {
log := ctrl.LoggerFrom(ctx)
log.V(1).Info(fmt.Sprintf("Getting task with UUID %s", taskUUID))
v, err := client.V3.GetTask(ctx, taskUUID)
log.V(1).Info(fmt.Sprintf("Getting task with UUID %s", uuid))
v, err := client.V3.GetTask(ctx, uuid)
if err != nil {
log.Error(err, fmt.Sprintf("error occurred while waiting for task with UUID %s", taskUUID))
log.Error(err, fmt.Sprintf("error occurred while waiting for task with UUID %s", uuid))
return "", err
}

Expand All @@ -55,6 +55,6 @@ func GetTaskState(ctx context.Context, client *nutanixClientV3.Client, taskUUID
fmt.Errorf("error_detail: %s, progress_message: %s", utils.StringValue(v.ErrorDetail), utils.StringValue(v.ProgressMessage))
}
taskStatus := *v.Status
log.V(1).Info(fmt.Sprintf("Status for task with UUID %s: %s", taskUUID, taskStatus))
log.V(1).Info(fmt.Sprintf("Status for task with UUID %s: %s", uuid, taskStatus))
return taskStatus, nil
}
36 changes: 18 additions & 18 deletions pkg/client/state_test.go → pkg/client/status_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
nutanixTestClient "github.com/nutanix-cloud-native/cluster-api-provider-nutanix/test/helpers/prism-go-client/v3"
)

func Test_GetTaskState(t *testing.T) {
func Test_GetTaskStatus(t *testing.T) {
client, err := nutanixTestClient.NewTestClient()
assert.NoError(t, err)
// use cleanup over defer as the connection gets closed before the tests run with t.Parallel()
Expand All @@ -23,21 +23,21 @@ func Test_GetTaskState(t *testing.T) {

t.Parallel()
tests := []struct {
name string
taskUUID string
handler func(w http.ResponseWriter, r *http.Request)
ctx context.Context
expectedState string
expectedErr error
name string
taskUUID string
handler func(w http.ResponseWriter, r *http.Request)
ctx context.Context
expectedStatus string
expectedErr error
}{
{
name: "succeeded",
taskUUID: "succeeded",
handler: func(w http.ResponseWriter, r *http.Request) {
fmt.Fprint(w, `{"status": "SUCCEEDED"}`)
},
ctx: context.Background(),
expectedState: "SUCCEEDED",
ctx: context.Background(),
expectedStatus: "SUCCEEDED",
},
{
name: "unauthorized",
Expand All @@ -54,19 +54,19 @@ func Test_GetTaskState(t *testing.T) {
handler: func(w http.ResponseWriter, r *http.Request) {
fmt.Fprint(w, `{"status": "INVALID_UUID", "error_detail": "invalid UUID", "progress_message": "invalid UUID"}`)
},
ctx: context.Background(),
expectedState: "INVALID_UUID",
expectedErr: fmt.Errorf("error_detail: invalid UUID, progress_message: invalid UUID"),
ctx: context.Background(),
expectedStatus: "INVALID_UUID",
expectedErr: fmt.Errorf("error_detail: invalid UUID, progress_message: invalid UUID"),
},
{
name: "failed",
taskUUID: "failed",
handler: func(w http.ResponseWriter, r *http.Request) {
fmt.Fprint(w, `{"status": "FAILED", "error_detail": "task failed", "progress_message": "will never succeed"}`)
},
ctx: context.Background(),
expectedState: "FAILED",
expectedErr: fmt.Errorf("error_detail: task failed, progress_message: will never succeed"),
ctx: context.Background(),
expectedStatus: "FAILED",
expectedErr: fmt.Errorf("error_detail: task failed, progress_message: will never succeed"),
},
}
for _, tt := range tests {
Expand All @@ -75,9 +75,9 @@ func Test_GetTaskState(t *testing.T) {
t.Parallel()
client.AddHandler(nutanixTestClient.GetTaskURLPath(tt.taskUUID), tt.handler)

state, err := GetTaskState(tt.ctx, client.Client, tt.taskUUID)
status, err := GetTaskStatus(tt.ctx, client.Client, tt.taskUUID)
assert.Equal(t, tt.expectedErr, err)
assert.Equal(t, tt.expectedState, state)
assert.Equal(t, tt.expectedStatus, status)
})
}
}
Expand Down Expand Up @@ -136,7 +136,7 @@ func Test_WaitForTaskCompletion(t *testing.T) {
t.Parallel()
client.AddHandler(nutanixTestClient.GetTaskURLPath(tt.taskUUID), tt.handler)

err := WaitForTaskCompletion(tt.ctx, client.Client, tt.taskUUID)
err := WaitForTaskToSucceed(tt.ctx, client.Client, tt.taskUUID)
if tt.expectedErr != nil {
assert.ErrorContains(t, err, tt.expectedErr.Error())
} else {
Expand Down

0 comments on commit 3e63d4c

Please sign in to comment.