@Summary Update workspace metadata by ID @ID update-workspace-metadata-by-id @Security CoderSessionToken @Accept json @Tags Workspaces @Param workspace path string true "Workspace ID" format(uuid) @Param request body codersdk.UpdateWorkspaceRequest true "Metadata update request" @Success 204 @Router
(rw http.ResponseWriter, r *http.Request)
| 1115 | // @Success 204 |
| 1116 | // @Router /api/v2/workspaces/{workspace} [patch] |
| 1117 | func (api *API) patchWorkspace(rw http.ResponseWriter, r *http.Request) { |
| 1118 | var ( |
| 1119 | ctx = r.Context() |
| 1120 | workspace = httpmw.WorkspaceParam(r) |
| 1121 | auditor = api.Auditor.Load() |
| 1122 | aReq, commitAudit = audit.InitRequest[database.WorkspaceTable](rw, &audit.RequestParams{ |
| 1123 | Audit: *auditor, |
| 1124 | Log: api.Logger, |
| 1125 | Request: r, |
| 1126 | Action: database.AuditActionWrite, |
| 1127 | OrganizationID: workspace.OrganizationID, |
| 1128 | }) |
| 1129 | ) |
| 1130 | defer commitAudit() |
| 1131 | aReq.Old = workspace.WorkspaceTable() |
| 1132 | |
| 1133 | var req codersdk.UpdateWorkspaceRequest |
| 1134 | if !httpapi.Read(ctx, rw, r, &req) { |
| 1135 | return |
| 1136 | } |
| 1137 | |
| 1138 | if req.Name == "" || req.Name == workspace.Name { |
| 1139 | aReq.New = workspace.WorkspaceTable() |
| 1140 | // Nothing changed, optionally this could be an error. |
| 1141 | rw.WriteHeader(http.StatusNoContent) |
| 1142 | return |
| 1143 | } |
| 1144 | |
| 1145 | // The reason we double check here is in case more fields can be |
| 1146 | // patched in the future, it's enough if one changes. |
| 1147 | name := workspace.Name |
| 1148 | if req.Name != "" || req.Name != workspace.Name { |
| 1149 | if !api.Options.AllowWorkspaceRenames { |
| 1150 | httpapi.Write(ctx, rw, http.StatusBadRequest, codersdk.Response{ |
| 1151 | Message: "Workspace renames are not allowed.", |
| 1152 | }) |
| 1153 | return |
| 1154 | } |
| 1155 | name = req.Name |
| 1156 | } |
| 1157 | |
| 1158 | newWorkspace, err := api.Database.UpdateWorkspace(ctx, database.UpdateWorkspaceParams{ |
| 1159 | ID: workspace.ID, |
| 1160 | Name: name, |
| 1161 | }) |
| 1162 | if err != nil { |
| 1163 | // The query protects against updating deleted workspaces and |
| 1164 | // the existence of the workspace is checked in the request, |
| 1165 | // if we get ErrNoRows it means the workspace was deleted. |
| 1166 | // |
| 1167 | // We could do this check earlier but we'd need to start a |
| 1168 | // transaction. |
| 1169 | if errors.Is(err, sql.ErrNoRows) { |
| 1170 | httpapi.Write(ctx, rw, http.StatusMethodNotAllowed, codersdk.Response{ |
| 1171 | Message: fmt.Sprintf("Workspace %q is deleted and cannot be updated.", workspace.Name), |
| 1172 | }) |
| 1173 | return |
| 1174 | } |
no test coverage detected