worker: adapt to new oapi-codegen

This commit is contained in:
Sanne Raymaekers 2025-03-21 11:56:43 +01:00 committed by Ondřej Budai
parent b2700903ae
commit 17416bf60b
4 changed files with 26 additions and 50 deletions

View file

@ -130,11 +130,9 @@ func APIError(code ServiceErrorCode, serviceError *serviceError, c echo.Context)
} }
return &Error{ return &Error{
ObjectReference: ObjectReference{ Href: fmt.Sprintf("%s/errors/%d", BasePath, se.code),
Href: fmt.Sprintf("%s/errors/%d", BasePath, se.code), Id: fmt.Sprintf("%d", se.code),
Id: fmt.Sprintf("%d", se.code), Kind: "Error",
Kind: "Error",
},
Code: fmt.Sprintf("%s%d", ErrorCodePrefix, se.code), Code: fmt.Sprintf("%s%d", ErrorCodePrefix, se.code),
OperationId: operationID, // set operation id from context OperationId: operationID, // set operation id from context
Reason: se.reason, Reason: se.reason,

View file

@ -196,11 +196,7 @@ func (c *Client) registerWorker() error {
return err return err
} }
workerID, err := uuid.Parse(wresp.WorkerId) c.workerID = wresp.WorkerId
if err != nil {
return err
}
c.workerID = workerID
return nil return nil
} }

View file

@ -955,11 +955,9 @@ func (h *apiHandlers) GetOpenapi(ctx echo.Context) error {
func (h *apiHandlers) GetStatus(ctx echo.Context) error { func (h *apiHandlers) GetStatus(ctx echo.Context) error {
return ctx.JSON(http.StatusOK, &api.StatusResponse{ return ctx.JSON(http.StatusOK, &api.StatusResponse{
ObjectReference: api.ObjectReference{ Href: fmt.Sprintf("%s/status", api.BasePath),
Href: fmt.Sprintf("%s/status", api.BasePath), Id: "status",
Id: "status", Kind: "Status",
Kind: "Status",
},
Status: "OK", Status: "OK",
}) })
} }
@ -1030,11 +1028,9 @@ func (h *apiHandlers) RequestJob(ctx echo.Context) error {
} }
response := api.RequestJobResponse{ response := api.RequestJobResponse{
ObjectReference: api.ObjectReference{ Href: fmt.Sprintf("%s/jobs", api.BasePath),
Href: fmt.Sprintf("%s/jobs", api.BasePath), Id: jobId.String(),
Id: jobId.String(), Kind: "RequestJob",
Kind: "RequestJob",
},
Location: fmt.Sprintf("%s/jobs/%v", api.BasePath, jobToken), Location: fmt.Sprintf("%s/jobs/%v", api.BasePath, jobToken),
ArtifactLocation: fmt.Sprintf("%s/jobs/%v/artifacts/", api.BasePath, jobToken), ArtifactLocation: fmt.Sprintf("%s/jobs/%v/artifacts/", api.BasePath, jobToken),
Type: jobType, Type: jobType,
@ -1062,11 +1058,9 @@ func (h *apiHandlers) GetJob(ctx echo.Context, tokenstr string) error {
if jobId == uuid.Nil { if jobId == uuid.Nil {
return ctx.JSON(http.StatusOK, api.GetJobResponse{ return ctx.JSON(http.StatusOK, api.GetJobResponse{
ObjectReference: api.ObjectReference{ Href: fmt.Sprintf("%s/jobs/%v", api.BasePath, token),
Href: fmt.Sprintf("%s/jobs/%v", api.BasePath, token), Id: token.String(),
Id: token.String(), Kind: "JobStatus",
Kind: "JobStatus",
},
Canceled: false, Canceled: false,
}) })
} }
@ -1079,11 +1073,9 @@ func (h *apiHandlers) GetJob(ctx echo.Context, tokenstr string) error {
} }
return ctx.JSON(http.StatusOK, api.GetJobResponse{ return ctx.JSON(http.StatusOK, api.GetJobResponse{
ObjectReference: api.ObjectReference{ Href: fmt.Sprintf("%s/jobs/%v", api.BasePath, token),
Href: fmt.Sprintf("%s/jobs/%v", api.BasePath, token), Id: token.String(),
Id: token.String(), Kind: "JobStatus",
Kind: "JobStatus",
},
Canceled: jobInfo.JobStatus.Canceled, Canceled: jobInfo.JobStatus.Canceled,
}) })
} }
@ -1169,21 +1161,15 @@ func (h *apiHandlers) PostWorkers(ctx echo.Context) error {
} }
return ctx.JSON(http.StatusCreated, api.PostWorkersResponse{ return ctx.JSON(http.StatusCreated, api.PostWorkersResponse{
ObjectReference: api.ObjectReference{ Href: fmt.Sprintf("%s/workers", api.BasePath),
Href: fmt.Sprintf("%s/workers", api.BasePath), Id: workerID.String(),
Id: workerID.String(), Kind: "WorkerID",
Kind: "WorkerID", WorkerId: workerID,
},
WorkerId: workerID.String(),
}) })
} }
func (h *apiHandlers) PostWorkerStatus(ctx echo.Context, workerIdstr string) error { func (h *apiHandlers) PostWorkerStatus(ctx echo.Context, workerID uuid.UUID) error {
workerID, err := uuid.Parse(workerIdstr) err := h.server.jobs.UpdateWorkerStatus(workerID)
if err != nil {
return api.HTTPErrorWithInternal(api.ErrorMalformedWorkerId, err)
}
err = h.server.jobs.UpdateWorkerStatus(workerID)
if err == jobqueue.ErrWorkerNotExist { if err == jobqueue.ErrWorkerNotExist {
return api.HTTPErrorWithInternal(api.ErrorWorkerIdNotFound, err) return api.HTTPErrorWithInternal(api.ErrorWorkerIdNotFound, err)

View file

@ -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") 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 var resp api.PostWorkersResponse
require.NoError(t, json.Unmarshal(reply, &resp)) 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) 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"}`, `{"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") "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") 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 var resp api.PostWorkersResponse
require.NoError(t, json.Unmarshal(reply, &resp)) require.NoError(t, json.Unmarshal(reply, &resp))
workerID, err := uuid.Parse(resp.WorkerId) test.TestRoute(t, server.Handler(), false, "POST", fmt.Sprintf("/api/worker/v1/workers/%s/status", resp.WorkerId), "{}", 200, "")
require.NoError(t, err)
test.TestRoute(t, server.Handler(), false, "POST", fmt.Sprintf("/api/worker/v1/workers/%s/status", workerID), "{}", 200, "")
distroStruct := newTestDistro(t) distroStruct := newTestDistro(t)
arch, err := distroStruct.GetArch(test_distro.TestArchName) arch, err := distroStruct.GetArch(test_distro.TestArchName)
@ -1539,7 +1535,7 @@ func TestRequestJobForWorker(t *testing.T) {
require.NoError(t, err) require.NoError(t, err)
// Can request a job with worker ID // 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.NoError(t, err)
require.Equal(t, jobId, j) require.Equal(t, jobId, j)
require.Equal(t, worker.JobTypeOSBuild, typ) require.Equal(t, worker.JobTypeOSBuild, typ)