Skip to content

Commit b256b20

Browse files
authored
feat: add endpoint for partial updates to org sync field and assign_default (#16337)
1 parent 6c90aef commit b256b20

File tree

9 files changed

+337
-4
lines changed

9 files changed

+337
-4
lines changed

coderd/apidoc/docs.go

Lines changed: 50 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

coderd/apidoc/swagger.json

Lines changed: 44 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

codersdk/idpsync.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,25 @@ func (c *Client) PatchOrganizationIDPSyncSettings(ctx context.Context, req Organ
144144
return resp, json.NewDecoder(res.Body).Decode(&resp)
145145
}
146146

147+
type PatchOrganizationIDPSyncConfigRequest struct {
148+
Field string `json:"field"`
149+
AssignDefault bool `json:"assign_default"`
150+
}
151+
152+
func (c *Client) PatchOrganizationIDPSyncConfig(ctx context.Context, req PatchOrganizationIDPSyncConfigRequest) (OrganizationSyncSettings, error) {
153+
res, err := c.Request(ctx, http.MethodPatch, "/api/v2/settings/idpsync/organization/config", req)
154+
if err != nil {
155+
return OrganizationSyncSettings{}, xerrors.Errorf("make request: %w", err)
156+
}
157+
defer res.Body.Close()
158+
159+
if res.StatusCode != http.StatusOK {
160+
return OrganizationSyncSettings{}, ReadBodyAsError(res)
161+
}
162+
var resp OrganizationSyncSettings
163+
return resp, json.NewDecoder(res.Body).Decode(&resp)
164+
}
165+
147166
// If the same mapping is present in both Add and Remove, Remove will take presidence.
148167
type PatchOrganizationIDPSyncMappingRequest struct {
149168
Add []IDPSyncMapping[uuid.UUID]

docs/reference/api/enterprise.md

Lines changed: 56 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docs/reference/api/schemas.md

Lines changed: 16 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

enterprise/coderd/coderd.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -295,6 +295,7 @@ func New(ctx context.Context, options *Options) (_ *API, err error) {
295295
r.Route("/organization", func(r chi.Router) {
296296
r.Get("/", api.organizationIDPSyncSettings)
297297
r.Patch("/", api.patchOrganizationIDPSyncSettings)
298+
r.Patch("/config", api.patchOrganizationIDPSyncConfig)
298299
r.Patch("/mapping", api.patchOrganizationIDPSyncMapping)
299300
})
300301

enterprise/coderd/idpsync.go

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -319,6 +319,75 @@ func (api *API) patchOrganizationIDPSyncSettings(rw http.ResponseWriter, r *http
319319
})
320320
}
321321

322+
// @Summary Update organization IdP Sync config
323+
// @ID update-organization-idp-sync-config
324+
// @Security CoderSessionToken
325+
// @Produce json
326+
// @Accept json
327+
// @Tags Enterprise
328+
// @Success 200 {object} codersdk.OrganizationSyncSettings
329+
// @Param request body codersdk.PatchOrganizationIDPSyncConfigRequest true "New config values"
330+
// @Router /settings/idpsync/organization/config [patch]
331+
func (api *API) patchOrganizationIDPSyncConfig(rw http.ResponseWriter, r *http.Request) {
332+
ctx := r.Context()
333+
auditor := *api.AGPL.Auditor.Load()
334+
aReq, commitAudit := audit.InitRequest[idpsync.OrganizationSyncSettings](rw, &audit.RequestParams{
335+
Audit: auditor,
336+
Log: api.Logger,
337+
Request: r,
338+
Action: database.AuditActionWrite,
339+
})
340+
defer commitAudit()
341+
342+
if !api.Authorize(r, policy.ActionUpdate, rbac.ResourceIdpsyncSettings) {
343+
httpapi.Forbidden(rw)
344+
return
345+
}
346+
347+
var req codersdk.PatchOrganizationIDPSyncConfigRequest
348+
if !httpapi.Read(ctx, rw, r, &req) {
349+
return
350+
}
351+
352+
var settings *idpsync.OrganizationSyncSettings
353+
//nolint:gocritic // Requires system context to update runtime config
354+
sysCtx := dbauthz.AsSystemRestricted(ctx)
355+
err := database.ReadModifyUpdate(api.Database, func(tx database.Store) error {
356+
existing, err := api.IDPSync.OrganizationSyncSettings(sysCtx, tx)
357+
if err != nil {
358+
return err
359+
}
360+
aReq.Old = *existing
361+
362+
err = api.IDPSync.UpdateOrganizationSyncSettings(sysCtx, tx, idpsync.OrganizationSyncSettings{
363+
Field: req.Field,
364+
AssignDefault: req.AssignDefault,
365+
Mapping: existing.Mapping,
366+
})
367+
if err != nil {
368+
return err
369+
}
370+
371+
settings, err = api.IDPSync.OrganizationSyncSettings(sysCtx, tx)
372+
if err != nil {
373+
return err
374+
}
375+
376+
return nil
377+
})
378+
if err != nil {
379+
httpapi.InternalServerError(rw, err)
380+
return
381+
}
382+
383+
aReq.New = *settings
384+
httpapi.Write(ctx, rw, http.StatusOK, codersdk.OrganizationSyncSettings{
385+
Field: settings.Field,
386+
Mapping: settings.Mapping,
387+
AssignDefault: settings.AssignDefault,
388+
})
389+
}
390+
322391
// @Summary Update organization IdP Sync mapping
323392
// @ID update-organization-idp-sync-mapping
324393
// @Security CoderSessionToken

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy