- Notifications
You must be signed in to change notification settings - Fork927
chore: remove UpsertCustomRole in favor of Insert + Update#14217
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Uh oh!
There was an error while loading.Please reload this page.
Changes fromall commits
b1776c1
f4d504e
8b71ecf
e6282d0
52e7ea5
fb546df
82ce462
b49b713
e53fabd
954f0d2
7e18088
2d81996
File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -153,6 +153,7 @@ func (r *RootCmd) editOrganizationRole(orgContext *OrganizationContext) *serpent | ||
return err | ||
} | ||
createNewRole := true | ||
var customRole codersdk.Role | ||
if jsonInput { | ||
// JSON Upload mode | ||
@@ -174,17 +175,30 @@ func (r *RootCmd) editOrganizationRole(orgContext *OrganizationContext) *serpent | ||
} | ||
return xerrors.Errorf("json input does not appear to be a valid role") | ||
} | ||
existingRoles, err := client.ListOrganizationRoles(ctx, org.ID) | ||
if err != nil { | ||
return xerrors.Errorf("listing existing roles: %w", err) | ||
} | ||
for _, existingRole := range existingRoles { | ||
if strings.EqualFold(customRole.Name, existingRole.Name) { | ||
// Editing an existing role | ||
createNewRole = false | ||
break | ||
} | ||
} | ||
} else { | ||
if len(inv.Args) == 0 { | ||
return xerrors.Errorf("missing role name argument, usage: \"coder organizations roles edit <role_name>\"") | ||
} | ||
interactiveRole,newRole,err := interactiveOrgRoleEdit(inv, org.ID, client) | ||
if err != nil { | ||
return xerrors.Errorf("editing role: %w", err) | ||
} | ||
customRole = *interactiveRole | ||
createNewRole = newRole | ||
preview := fmt.Sprintf("permissions: %d site, %d org, %d user", | ||
len(customRole.SitePermissions), len(customRole.OrganizationPermissions), len(customRole.UserPermissions)) | ||
@@ -203,7 +217,12 @@ func (r *RootCmd) editOrganizationRole(orgContext *OrganizationContext) *serpent | ||
// Do not actually post | ||
updated = customRole | ||
} else { | ||
switch createNewRole { | ||
case true: | ||
updated, err = client.CreateOrganizationRole(ctx, customRole) | ||
default: | ||
updated, err = client.UpdateOrganizationRole(ctx, customRole) | ||
} | ||
Comment on lines +220 to +225 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. This feels like a smell to me, my gut would say that we should just have a separate There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. You are right@f0ssel. This PR is already quite big, and we are not launching this yet. Can I create an issue to split these out into 2 commands and do it in another PR? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. Totally 👍 | ||
if err != nil { | ||
return xerrors.Errorf("patch role: %w", err) | ||
} | ||
@@ -223,11 +242,12 @@ func (r *RootCmd) editOrganizationRole(orgContext *OrganizationContext) *serpent | ||
return cmd | ||
} | ||
func interactiveOrgRoleEdit(inv *serpent.Invocation, orgID uuid.UUID, client *codersdk.Client) (*codersdk.Role, bool, error) { | ||
newRole := false | ||
ctx := inv.Context() | ||
roles, err := client.ListOrganizationRoles(ctx, orgID) | ||
if err != nil { | ||
return nil,newRole,xerrors.Errorf("listing roles: %w", err) | ||
} | ||
// Make sure the role actually exists first | ||
@@ -246,22 +266,23 @@ func interactiveOrgRoleEdit(inv *serpent.Invocation, orgID uuid.UUID, client *co | ||
IsConfirm: true, | ||
}) | ||
if err != nil { | ||
return nil,newRole,xerrors.Errorf("abort: %w", err) | ||
} | ||
originalRole.Role = codersdk.Role{ | ||
Name: inv.Args[0], | ||
OrganizationID: orgID.String(), | ||
} | ||
newRole = true | ||
} | ||
// Some checks since interactive mode is limited in what it currently sees | ||
if len(originalRole.SitePermissions) > 0 { | ||
return nil,newRole,xerrors.Errorf("unable to edit role in interactive mode, it contains site wide permissions") | ||
} | ||
if len(originalRole.UserPermissions) > 0 { | ||
return nil,newRole,xerrors.Errorf("unable to edit role in interactive mode, it contains user permissions") | ||
} | ||
role := &originalRole.Role | ||
@@ -283,13 +304,13 @@ customRoleLoop: | ||
Options: append(permissionPreviews(role, allowedResources), done, abort), | ||
}) | ||
if err != nil { | ||
return role,newRole,xerrors.Errorf("selecting resource: %w", err) | ||
} | ||
switch selected { | ||
case done: | ||
break customRoleLoop | ||
case abort: | ||
return role,newRole,xerrors.Errorf("edit role %q aborted", role.Name) | ||
default: | ||
strs := strings.Split(selected, "::") | ||
resource := strings.TrimSpace(strs[0]) | ||
@@ -300,7 +321,7 @@ customRoleLoop: | ||
Defaults: defaultActions(role, resource), | ||
}) | ||
if err != nil { | ||
return role,newRole,xerrors.Errorf("selecting actions for resource %q: %w", resource, err) | ||
} | ||
applyOrgResourceActions(role, resource, actions) | ||
// back to resources! | ||
@@ -309,7 +330,7 @@ customRoleLoop: | ||
// This println is required because the prompt ends us on the same line as some text. | ||
_, _ = fmt.Println() | ||
return role,newRole,nil | ||
} | ||
func applyOrgResourceActions(role *codersdk.Role, resource string, actions []string) { | ||
Some generated files are not rendered by default. Learn more abouthow customized files appear on GitHub.
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.