MCPcopy Index your code
hub / github.com/coder/coder / patchUserSecret

Method patchUserSecret

coderd/usersecrets.go:168–273  ·  view source on GitHub ↗

@Summary Update a user secret @ID update-a-user-secret @Security CoderSessionToken @Accept json @Produce json @Tags Secrets @Param user path string true "User ID, username, or me" @Param name path string true "Secret name" @Param request body codersdk.UpdateUserSecretRequest true "Update secret requ

(rw http.ResponseWriter, r *http.Request)

Source from the content-addressed store, hash-verified

166// @Success 200 {object} codersdk.UserSecret
167// @Router /api/v2/users/{user}/secrets/{name} [patch]
168func (api *API) patchUserSecret(rw http.ResponseWriter, r *http.Request) {
169 var (
170 ctx = r.Context()
171 user = httpmw.UserParam(r)
172 name = chi.URLParam(r, userSecretNameField)
173 auditor = api.Auditor.Load()
174 aReq, commitAudit = audit.InitRequest[database.UserSecret](rw, &audit.RequestParams{
175 Audit: *auditor,
176 Log: api.Logger,
177 Request: r,
178 Action: database.AuditActionWrite,
179 })
180 )
181 defer commitAudit()
182
183 var req codersdk.UpdateUserSecretRequest
184 if !httpapi.Read(ctx, rw, r, &req) {
185 return
186 }
187
188 if req.Value == nil && req.Description == nil && req.EnvName == nil && req.FilePath == nil {
189 httpapi.Write(ctx, rw, http.StatusBadRequest, codersdk.Response{
190 Message: "At least one field must be provided.",
191 })
192 return
193 }
194 if validations := updateUserSecretValidationErrors(req); len(validations) > 0 {
195 writeUserSecretValidationErrors(ctx, rw, http.StatusBadRequest, validations)
196 return
197 }
198
199 params := database.UpdateUserSecretByUserIDAndNameParams{
200 UserID: user.ID,
201 Name: name,
202 UpdateValue: req.Value != nil,
203 Value: "",
204 ValueKeyID: sql.NullString{},
205 UpdateDescription: req.Description != nil,
206 Description: "",
207 UpdateEnvName: req.EnvName != nil,
208 EnvName: "",
209 UpdateFilePath: req.FilePath != nil,
210 FilePath: "",
211 }
212 if req.Value != nil {
213 params.Value = *req.Value
214 }
215 if req.Description != nil {
216 params.Description = *req.Description
217 }
218 if req.EnvName != nil {
219 params.EnvName = *req.EnvName
220 }
221 if req.FilePath != nil {
222 params.FilePath = *req.FilePath
223 }
224
225 // Pre-read the secret inside a transaction so the audit diff has both an

Callers

nothing calls this directly

Calls 15

UserParamFunction · 0.92
InitRequestFunction · 0.92
ReadFunction · 0.92
WriteFunction · 0.92
ResourceNotFoundFunction · 0.92
UserSecretFromFullFunction · 0.92
userSecretLimitResponseFunction · 0.85
ContextMethod · 0.65
InTxMethod · 0.65

Tested by

no test coverage detected