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

Commit84fdfd2

Browse files
Emyrkjaaydenh
andauthored
chore: remove UpsertCustomRole in favor of Insert + Update (#14217)
* chore: remove UpsertCustomRole in favor of Insert + Update---------Co-authored-by: Jaayden Halko <jaayden.halko@gmail.com>
1 parent712a1b5 commit84fdfd2

File tree

39 files changed

+1053
-420
lines changed

39 files changed

+1053
-420
lines changed

‎cli/organizationroles.go

Lines changed: 32 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,7 @@ func (r *RootCmd) editOrganizationRole(orgContext *OrganizationContext) *serpent
153153
returnerr
154154
}
155155

156+
createNewRole:=true
156157
varcustomRole codersdk.Role
157158
ifjsonInput {
158159
// JSON Upload mode
@@ -174,17 +175,30 @@ func (r *RootCmd) editOrganizationRole(orgContext *OrganizationContext) *serpent
174175
}
175176
returnxerrors.Errorf("json input does not appear to be a valid role")
176177
}
178+
179+
existingRoles,err:=client.ListOrganizationRoles(ctx,org.ID)
180+
iferr!=nil {
181+
returnxerrors.Errorf("listing existing roles: %w",err)
182+
}
183+
for_,existingRole:=rangeexistingRoles {
184+
ifstrings.EqualFold(customRole.Name,existingRole.Name) {
185+
// Editing an existing role
186+
createNewRole=false
187+
break
188+
}
189+
}
177190
}else {
178191
iflen(inv.Args)==0 {
179192
returnxerrors.Errorf("missing role name argument, usage:\"coder organizations roles edit <role_name>\"")
180193
}
181194

182-
interactiveRole,err:=interactiveOrgRoleEdit(inv,org.ID,client)
195+
interactiveRole,newRole,err:=interactiveOrgRoleEdit(inv,org.ID,client)
183196
iferr!=nil {
184197
returnxerrors.Errorf("editing role: %w",err)
185198
}
186199

187200
customRole=*interactiveRole
201+
createNewRole=newRole
188202

189203
preview:=fmt.Sprintf("permissions: %d site, %d org, %d user",
190204
len(customRole.SitePermissions),len(customRole.OrganizationPermissions),len(customRole.UserPermissions))
@@ -203,7 +217,12 @@ func (r *RootCmd) editOrganizationRole(orgContext *OrganizationContext) *serpent
203217
// Do not actually post
204218
updated=customRole
205219
}else {
206-
updated,err=client.PatchOrganizationRole(ctx,customRole)
220+
switchcreateNewRole {
221+
casetrue:
222+
updated,err=client.CreateOrganizationRole(ctx,customRole)
223+
default:
224+
updated,err=client.UpdateOrganizationRole(ctx,customRole)
225+
}
207226
iferr!=nil {
208227
returnxerrors.Errorf("patch role: %w",err)
209228
}
@@ -223,11 +242,12 @@ func (r *RootCmd) editOrganizationRole(orgContext *OrganizationContext) *serpent
223242
returncmd
224243
}
225244

226-
funcinteractiveOrgRoleEdit(inv*serpent.Invocation,orgID uuid.UUID,client*codersdk.Client) (*codersdk.Role,error) {
245+
funcinteractiveOrgRoleEdit(inv*serpent.Invocation,orgID uuid.UUID,client*codersdk.Client) (*codersdk.Role,bool,error) {
246+
newRole:=false
227247
ctx:=inv.Context()
228248
roles,err:=client.ListOrganizationRoles(ctx,orgID)
229249
iferr!=nil {
230-
returnnil,xerrors.Errorf("listing roles: %w",err)
250+
returnnil,newRole,xerrors.Errorf("listing roles: %w",err)
231251
}
232252

233253
// Make sure the role actually exists first
@@ -246,22 +266,23 @@ func interactiveOrgRoleEdit(inv *serpent.Invocation, orgID uuid.UUID, client *co
246266
IsConfirm:true,
247267
})
248268
iferr!=nil {
249-
returnnil,xerrors.Errorf("abort: %w",err)
269+
returnnil,newRole,xerrors.Errorf("abort: %w",err)
250270
}
251271

252272
originalRole.Role= codersdk.Role{
253273
Name:inv.Args[0],
254274
OrganizationID:orgID.String(),
255275
}
276+
newRole=true
256277
}
257278

258279
// Some checks since interactive mode is limited in what it currently sees
259280
iflen(originalRole.SitePermissions)>0 {
260-
returnnil,xerrors.Errorf("unable to edit role in interactive mode, it contains site wide permissions")
281+
returnnil,newRole,xerrors.Errorf("unable to edit role in interactive mode, it contains site wide permissions")
261282
}
262283

263284
iflen(originalRole.UserPermissions)>0 {
264-
returnnil,xerrors.Errorf("unable to edit role in interactive mode, it contains user permissions")
285+
returnnil,newRole,xerrors.Errorf("unable to edit role in interactive mode, it contains user permissions")
265286
}
266287

267288
role:=&originalRole.Role
@@ -283,13 +304,13 @@ customRoleLoop:
283304
Options:append(permissionPreviews(role,allowedResources),done,abort),
284305
})
285306
iferr!=nil {
286-
returnrole,xerrors.Errorf("selecting resource: %w",err)
307+
returnrole,newRole,xerrors.Errorf("selecting resource: %w",err)
287308
}
288309
switchselected {
289310
casedone:
290311
break customRoleLoop
291312
caseabort:
292-
returnrole,xerrors.Errorf("edit role %q aborted",role.Name)
313+
returnrole,newRole,xerrors.Errorf("edit role %q aborted",role.Name)
293314
default:
294315
strs:=strings.Split(selected,"::")
295316
resource:=strings.TrimSpace(strs[0])
@@ -300,7 +321,7 @@ customRoleLoop:
300321
Defaults:defaultActions(role,resource),
301322
})
302323
iferr!=nil {
303-
returnrole,xerrors.Errorf("selecting actions for resource %q: %w",resource,err)
324+
returnrole,newRole,xerrors.Errorf("selecting actions for resource %q: %w",resource,err)
304325
}
305326
applyOrgResourceActions(role,resource,actions)
306327
// back to resources!
@@ -309,7 +330,7 @@ customRoleLoop:
309330
// This println is required because the prompt ends us on the same line as some text.
310331
_,_=fmt.Println()
311332

312-
returnrole,nil
333+
returnrole,newRole,nil
313334
}
314335

315336
funcapplyOrgResourceActions(role*codersdk.Role,resourcestring,actions []string) {

‎coderd/apidoc/docs.go

Lines changed: 80 additions & 32 deletions
Some generated files are not rendered by default. Learn more aboutcustomizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp