Skip to content

Commit 84fdfd2

Browse files
Emyrkjaaydenh
andauthored
chore: remove UpsertCustomRole in favor of Insert + Update (coder#14217)
* chore: remove UpsertCustomRole in favor of Insert + Update --------- Co-authored-by: Jaayden Halko <jaayden.halko@gmail.com>
1 parent 712a1b5 commit 84fdfd2

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
return err
154154
}
155155

156+
createNewRole := true
156157
var customRole codersdk.Role
157158
if jsonInput {
158159
// JSON Upload mode
@@ -174,17 +175,30 @@ func (r *RootCmd) editOrganizationRole(orgContext *OrganizationContext) *serpent
174175
}
175176
return xerrors.Errorf("json input does not appear to be a valid role")
176177
}
178+
179+
existingRoles, err := client.ListOrganizationRoles(ctx, org.ID)
180+
if err != nil {
181+
return xerrors.Errorf("listing existing roles: %w", err)
182+
}
183+
for _, existingRole := range existingRoles {
184+
if strings.EqualFold(customRole.Name, existingRole.Name) {
185+
// Editing an existing role
186+
createNewRole = false
187+
break
188+
}
189+
}
177190
} else {
178191
if len(inv.Args) == 0 {
179192
return xerrors.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
if err != nil {
184197
return xerrors.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+
switch createNewRole {
221+
case true:
222+
updated, err = client.CreateOrganizationRole(ctx, customRole)
223+
default:
224+
updated, err = client.UpdateOrganizationRole(ctx, customRole)
225+
}
207226
if err != nil {
208227
return xerrors.Errorf("patch role: %w", err)
209228
}
@@ -223,11 +242,12 @@ func (r *RootCmd) editOrganizationRole(orgContext *OrganizationContext) *serpent
223242
return cmd
224243
}
225244

226-
func interactiveOrgRoleEdit(inv *serpent.Invocation, orgID uuid.UUID, client *codersdk.Client) (*codersdk.Role, error) {
245+
func interactiveOrgRoleEdit(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
if err != nil {
230-
return nil, xerrors.Errorf("listing roles: %w", err)
250+
return nil, 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
if err != nil {
249-
return nil, xerrors.Errorf("abort: %w", err)
269+
return nil, 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
if len(originalRole.SitePermissions) > 0 {
260-
return nil, xerrors.Errorf("unable to edit role in interactive mode, it contains site wide permissions")
281+
return nil, newRole, xerrors.Errorf("unable to edit role in interactive mode, it contains site wide permissions")
261282
}
262283

263284
if len(originalRole.UserPermissions) > 0 {
264-
return nil, xerrors.Errorf("unable to edit role in interactive mode, it contains user permissions")
285+
return nil, 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
if err != nil {
286-
return role, xerrors.Errorf("selecting resource: %w", err)
307+
return role, newRole, xerrors.Errorf("selecting resource: %w", err)
287308
}
288309
switch selected {
289310
case done:
290311
break customRoleLoop
291312
case abort:
292-
return role, xerrors.Errorf("edit role %q aborted", role.Name)
313+
return role, 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
if err != nil {
303-
return role, xerrors.Errorf("selecting actions for resource %q: %w", resource, err)
324+
return role, 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-
return role, nil
333+
return role, newRole, nil
313334
}
314335

315336
func applyOrgResourceActions(role *codersdk.Role, resource string, actions []string) {

coderd/apidoc/docs.go

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

0 commit comments

Comments
 (0)