From 3e63d4cf122636f0536f592d89663105c04e0760 Mon Sep 17 00:00:00 2001 From: Dimitri Koshkin Date: Wed, 3 Jan 2024 05:46:42 -0800 Subject: [PATCH] refactor: use consistent task status names --- controllers/helpers.go | 2 +- controllers/nutanixmachine_controller.go | 2 +- pkg/client/{state.go => status.go} | 22 ++++++------ pkg/client/{state_test.go => status_test.go} | 36 ++++++++++---------- 4 files changed, 31 insertions(+), 31 deletions(-) rename pkg/client/{state.go => status.go} (64%) rename pkg/client/{state_test.go => status_test.go} (80%) diff --git a/controllers/helpers.go b/controllers/helpers.go index 8ec4331f22..70e6e2fca9 100644 --- a/controllers/helpers.go +++ b/controllers/helpers.go @@ -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 } diff --git a/controllers/nutanixmachine_controller.go b/controllers/nutanixmachine_controller.go index 48ec100ceb..1b35b61204 100644 --- a/controllers/nutanixmachine_controller.go +++ b/controllers/nutanixmachine_controller.go @@ -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) diff --git a/pkg/client/state.go b/pkg/client/status.go similarity index 64% rename from pkg/client/state.go rename to pkg/client/status.go index 772a1a0e47..a56ca4069f 100644 --- a/pkg/client/state.go +++ b/pkg/client/status.go @@ -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 } @@ -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 } diff --git a/pkg/client/state_test.go b/pkg/client/status_test.go similarity index 80% rename from pkg/client/state_test.go rename to pkg/client/status_test.go index d01c2425b6..99cdcbbc01 100644 --- a/pkg/client/state_test.go +++ b/pkg/client/status_test.go @@ -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() @@ -23,12 +23,12 @@ 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", @@ -36,8 +36,8 @@ func Test_GetTaskState(t *testing.T) { 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", @@ -54,9 +54,9 @@ 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", @@ -64,9 +64,9 @@ func Test_GetTaskState(t *testing.T) { 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 { @@ -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) }) } } @@ -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 {