From 17416bf60b6738c5efea4c3df741b028c2303de1 Mon Sep 17 00:00:00 2001 From: Sanne Raymaekers Date: Fri, 21 Mar 2025 11:56:43 +0100 Subject: [PATCH] worker: adapt to new oapi-codegen --- internal/worker/api/errors.go | 8 ++---- internal/worker/client.go | 6 +--- internal/worker/server.go | 50 ++++++++++++---------------------- internal/worker/server_test.go | 12 +++----- 4 files changed, 26 insertions(+), 50 deletions(-) diff --git a/internal/worker/api/errors.go b/internal/worker/api/errors.go index 157dea08c..400bc2824 100644 --- a/internal/worker/api/errors.go +++ b/internal/worker/api/errors.go @@ -130,11 +130,9 @@ func APIError(code ServiceErrorCode, serviceError *serviceError, c echo.Context) } return &Error{ - ObjectReference: ObjectReference{ - Href: fmt.Sprintf("%s/errors/%d", BasePath, se.code), - Id: fmt.Sprintf("%d", se.code), - Kind: "Error", - }, + Href: fmt.Sprintf("%s/errors/%d", BasePath, se.code), + Id: fmt.Sprintf("%d", se.code), + Kind: "Error", Code: fmt.Sprintf("%s%d", ErrorCodePrefix, se.code), OperationId: operationID, // set operation id from context Reason: se.reason, diff --git a/internal/worker/client.go b/internal/worker/client.go index 77fb17ef8..cb0bead4b 100644 --- a/internal/worker/client.go +++ b/internal/worker/client.go @@ -196,11 +196,7 @@ func (c *Client) registerWorker() error { return err } - workerID, err := uuid.Parse(wresp.WorkerId) - if err != nil { - return err - } - c.workerID = workerID + c.workerID = wresp.WorkerId return nil } diff --git a/internal/worker/server.go b/internal/worker/server.go index 8990fdebd..134c470f6 100644 --- a/internal/worker/server.go +++ b/internal/worker/server.go @@ -955,11 +955,9 @@ func (h *apiHandlers) GetOpenapi(ctx echo.Context) error { func (h *apiHandlers) GetStatus(ctx echo.Context) error { return ctx.JSON(http.StatusOK, &api.StatusResponse{ - ObjectReference: api.ObjectReference{ - Href: fmt.Sprintf("%s/status", api.BasePath), - Id: "status", - Kind: "Status", - }, + Href: fmt.Sprintf("%s/status", api.BasePath), + Id: "status", + Kind: "Status", Status: "OK", }) } @@ -1030,11 +1028,9 @@ func (h *apiHandlers) RequestJob(ctx echo.Context) error { } response := api.RequestJobResponse{ - ObjectReference: api.ObjectReference{ - Href: fmt.Sprintf("%s/jobs", api.BasePath), - Id: jobId.String(), - Kind: "RequestJob", - }, + Href: fmt.Sprintf("%s/jobs", api.BasePath), + Id: jobId.String(), + Kind: "RequestJob", Location: fmt.Sprintf("%s/jobs/%v", api.BasePath, jobToken), ArtifactLocation: fmt.Sprintf("%s/jobs/%v/artifacts/", api.BasePath, jobToken), Type: jobType, @@ -1062,11 +1058,9 @@ func (h *apiHandlers) GetJob(ctx echo.Context, tokenstr string) error { if jobId == uuid.Nil { return ctx.JSON(http.StatusOK, api.GetJobResponse{ - ObjectReference: api.ObjectReference{ - Href: fmt.Sprintf("%s/jobs/%v", api.BasePath, token), - Id: token.String(), - Kind: "JobStatus", - }, + Href: fmt.Sprintf("%s/jobs/%v", api.BasePath, token), + Id: token.String(), + Kind: "JobStatus", Canceled: false, }) } @@ -1079,11 +1073,9 @@ func (h *apiHandlers) GetJob(ctx echo.Context, tokenstr string) error { } return ctx.JSON(http.StatusOK, api.GetJobResponse{ - ObjectReference: api.ObjectReference{ - Href: fmt.Sprintf("%s/jobs/%v", api.BasePath, token), - Id: token.String(), - Kind: "JobStatus", - }, + Href: fmt.Sprintf("%s/jobs/%v", api.BasePath, token), + Id: token.String(), + Kind: "JobStatus", Canceled: jobInfo.JobStatus.Canceled, }) } @@ -1169,21 +1161,15 @@ func (h *apiHandlers) PostWorkers(ctx echo.Context) error { } return ctx.JSON(http.StatusCreated, api.PostWorkersResponse{ - ObjectReference: api.ObjectReference{ - Href: fmt.Sprintf("%s/workers", api.BasePath), - Id: workerID.String(), - Kind: "WorkerID", - }, - WorkerId: workerID.String(), + Href: fmt.Sprintf("%s/workers", api.BasePath), + Id: workerID.String(), + Kind: "WorkerID", + WorkerId: workerID, }) } -func (h *apiHandlers) PostWorkerStatus(ctx echo.Context, workerIdstr string) error { - workerID, err := uuid.Parse(workerIdstr) - if err != nil { - return api.HTTPErrorWithInternal(api.ErrorMalformedWorkerId, err) - } - err = h.server.jobs.UpdateWorkerStatus(workerID) +func (h *apiHandlers) PostWorkerStatus(ctx echo.Context, workerID uuid.UUID) error { + err := h.server.jobs.UpdateWorkerStatus(workerID) if err == jobqueue.ErrWorkerNotExist { return api.HTTPErrorWithInternal(api.ErrorWorkerIdNotFound, err) diff --git a/internal/worker/server_test.go b/internal/worker/server_test.go index 41039a44e..374742067 100644 --- a/internal/worker/server_test.go +++ b/internal/worker/server_test.go @@ -1499,12 +1499,10 @@ func TestWorkerWatch(t *testing.T) { reply := test.TestRouteWithReply(t, server.Handler(), false, "POST", "/api/worker/v1/workers", fmt.Sprintf(`{"arch":"%s"}`, arch.Current().String()), 201, `{"href":"/api/worker/v1/workers","kind":"WorkerID","id": "15"}`, "id", "worker_id") var resp api.PostWorkersResponse require.NoError(t, json.Unmarshal(reply, &resp)) - workerID, err := uuid.Parse(resp.WorkerId) - require.NoError(t, err) - test.TestRoute(t, server.Handler(), false, "POST", fmt.Sprintf("/api/worker/v1/workers/%s/status", workerID), "{}", 200, "") + test.TestRoute(t, server.Handler(), false, "POST", fmt.Sprintf("/api/worker/v1/workers/%s/status", resp.WorkerId), "{}", 200, "") time.Sleep(time.Millisecond * 400) - test.TestRoute(t, server.Handler(), false, "POST", fmt.Sprintf("/api/worker/v1/workers/%s/status", workerID), "", 400, + test.TestRoute(t, server.Handler(), false, "POST", fmt.Sprintf("/api/worker/v1/workers/%s/status", resp.WorkerId), "", 400, `{"href":"/api/worker/v1/errors/18","code":"IMAGE-BUILDER-WORKER-18","id":"18","kind":"Error","message":"Given worker id doesn't exist","reason":"Given worker id doesn't exist"}`, "operation_id") } @@ -1514,9 +1512,7 @@ func TestRequestJobForWorker(t *testing.T) { reply := test.TestRouteWithReply(t, server.Handler(), false, "POST", "/api/worker/v1/workers", fmt.Sprintf(`{"arch":"%s"}`, arch.Current().String()), 201, `{"href":"/api/worker/v1/workers","kind":"WorkerID","id": "15"}`, "id", "worker_id") var resp api.PostWorkersResponse require.NoError(t, json.Unmarshal(reply, &resp)) - workerID, err := uuid.Parse(resp.WorkerId) - require.NoError(t, err) - test.TestRoute(t, server.Handler(), false, "POST", fmt.Sprintf("/api/worker/v1/workers/%s/status", workerID), "{}", 200, "") + test.TestRoute(t, server.Handler(), false, "POST", fmt.Sprintf("/api/worker/v1/workers/%s/status", resp.WorkerId), "{}", 200, "") distroStruct := newTestDistro(t) arch, err := distroStruct.GetArch(test_distro.TestArchName) @@ -1539,7 +1535,7 @@ func TestRequestJobForWorker(t *testing.T) { require.NoError(t, err) // Can request a job with worker ID - j, _, typ, args, dynamicArgs, err := server.RequestJob(context.Background(), arch.Name(), []string{worker.JobTypeOSBuild}, []string{""}, workerID) + j, _, typ, args, dynamicArgs, err := server.RequestJob(context.Background(), arch.Name(), []string{worker.JobTypeOSBuild}, []string{""}, resp.WorkerId) require.NoError(t, err) require.Equal(t, jobId, j) require.Equal(t, worker.JobTypeOSBuild, typ)