Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitf31af9c

Browse files
committed
feat: add structured response for invalidate prebuilds API
Changes:- Add InvalidatePrebuildsResponse type with Count and Workspaces fields- Update API handler to return list of invalidated workspace IDs- Update SDK client to parse structured response- Remove error messages in favor of structured data- Simplify logging (success/failure counts)Response structure:{ "count": 5, "workspaces": ["uuid1", "uuid2", ...]}
1 parente2a0039 commitf31af9c

File tree

2 files changed

+23
-20
lines changed

2 files changed

+23
-20
lines changed

‎coderd/templates.go‎

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1206,15 +1206,15 @@ func (api *API) postInvalidateTemplatePrebuilds(rw http.ResponseWriter, r *http.
12061206
}
12071207

12081208
iflen(prebuildWorkspaces)==0 {
1209-
httpapi.Write(ctx,rw,http.StatusOK, codersdk.Response{
1210-
Message:"No prebuilt workspaces found for this template's active version.",
1209+
httpapi.Write(ctx,rw,http.StatusOK, codersdk.InvalidatePrebuildsResponse{
1210+
Count:0,
1211+
Workspaces: []uuid.UUID{},
12111212
})
12121213
return
12131214
}
12141215

12151216
// Delete each prebuilt workspace using the existing workspace delete logic
1216-
varinvalidatedCountint
1217-
varerrors []string
1217+
varinvalidatedWorkspaces []uuid.UUID
12181218

12191219
api.Logger.Info(ctx,"invalidating prebuilt workspaces",
12201220
slog.F("template_id",template.ID),
@@ -1240,7 +1240,6 @@ func (api *API) postInvalidateTemplatePrebuilds(rw http.ResponseWriter, r *http.
12401240
audit.WorkspaceBuildBaggageFromRequest(r),
12411241
)
12421242
iferr!=nil {
1243-
errors=append(errors,fmt.Sprintf("workspace %s: %v",workspace.Name,err))
12441243
api.Logger.Warn(ctx,"failed to invalidate prebuilt workspace",
12451244
slog.F("workspace_id",workspace.ID),
12461245
slog.F("workspace_name",workspace.Name),
@@ -1249,22 +1248,17 @@ func (api *API) postInvalidateTemplatePrebuilds(rw http.ResponseWriter, r *http.
12491248
continue
12501249
}
12511250

1252-
invalidatedCount++
1251+
invalidatedWorkspaces=append(invalidatedWorkspaces,workspace.ID)
12531252
}
12541253

12551254
api.Logger.Info(ctx,"completed prebuild invalidation",
12561255
slog.F("template_id",template.ID),
1257-
slog.F("invalidated",invalidatedCount),
1258-
slog.F("failed",len(errors)),
1256+
slog.F("invalidated",len(invalidatedWorkspaces)),
1257+
slog.F("failed",len(prebuildWorkspaces)-len(invalidatedWorkspaces)),
12591258
)
12601259

1261-
message:=fmt.Sprintf("Successfully invalidated %d prebuilt workspace(s).",invalidatedCount)
1262-
iflen(errors)>0 {
1263-
message=fmt.Sprintf("Invalidated %d prebuilt workspace(s), %d failed. Errors: %s",
1264-
invalidatedCount,len(errors),strings.Join(errors,"; "))
1265-
}
1266-
1267-
httpapi.Write(ctx,rw,http.StatusOK, codersdk.Response{
1268-
Message:message,
1260+
httpapi.Write(ctx,rw,http.StatusOK, codersdk.InvalidatePrebuildsResponse{
1261+
Count:len(invalidatedWorkspaces),
1262+
Workspaces:invalidatedWorkspaces,
12691263
})
12701264
}

‎codersdk/templates.go‎

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -508,22 +508,31 @@ func (c *Client) StarterTemplates(ctx context.Context) ([]TemplateExample, error
508508
returntemplateExamples,json.NewDecoder(res.Body).Decode(&templateExamples)
509509
}
510510

511+
// InvalidatePrebuildsResponse contains the result of invalidating prebuilt workspaces.
512+
typeInvalidatePrebuildsResponsestruct {
513+
// Count is the number of prebuilt workspaces that were invalidated.
514+
Countint`json:"count"`
515+
// Workspaces is the list of invalidated prebuilt workspace IDs.
516+
Workspaces []uuid.UUID`json:"workspaces"`
517+
}
518+
511519
// InvalidateTemplatePrebuilds invalidates all prebuilt workspaces for the
512520
// template's active version by deleting them. This is useful when prebuilds
513521
// become stale due to updated base images, repository changes, or other factors.
514-
func (c*Client)InvalidateTemplatePrebuilds(ctx context.Context,template uuid.UUID)error {
522+
func (c*Client)InvalidateTemplatePrebuilds(ctx context.Context,template uuid.UUID)(InvalidatePrebuildsResponse,error) {
515523
res,err:=c.Request(ctx,http.MethodPost,
516524
fmt.Sprintf("/api/v2/templates/%s/prebuilds/invalidate",template),
517525
nil,
518526
)
519527
iferr!=nil {
520-
returnerr
528+
returnInvalidatePrebuildsResponse{},err
521529
}
522530
deferres.Body.Close()
523531

524532
ifres.StatusCode!=http.StatusOK {
525-
returnReadBodyAsError(res)
533+
returnInvalidatePrebuildsResponse{},ReadBodyAsError(res)
526534
}
527535

528-
returnnil
536+
varresponseInvalidatePrebuildsResponse
537+
returnresponse,json.NewDecoder(res.Body).Decode(&response)
529538
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp