Skip to content

chore: add edit organization role to cli #13365

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 our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 9 commits into from
Jun 3, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Add custom role cli command
  • Loading branch information
Emyrk committed May 31, 2024
commit da0107c3cb2bdd19de050ccfa6f0d15eb94eac58
16 changes: 8 additions & 8 deletions cli/organizationroles.go
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,7 @@ customRoleLoop:
for {
selected, err := cliui.Select(inv, cliui.SelectOptions{
Message: "Select which resources to edit permissions",
Options: append(permissionPreviews(role, orgID, allowedResources), done, abort),
Options: append(permissionPreviews(role, allowedResources), done, abort),
})
if err != nil {
return role, xerrors.Errorf("selecting resource: %w", err)
Expand All @@ -297,12 +297,12 @@ customRoleLoop:
actions, err := cliui.MultiSelect(inv, cliui.MultiSelectOptions{
Message: fmt.Sprintf("Select actions to allow across the whole deployment for resources=%q", resource),
Options: slice.ToStrings(codersdk.RBACResourceActions[codersdk.RBACResource(resource)]),
Defaults: defaultActions(role, orgID, resource),
Defaults: defaultActions(role, resource),
})
if err != nil {
return role, xerrors.Errorf("selecting actions for resource %q: %w", resource, err)
}
applyOrgResourceActions(role, orgID, resource, actions)
applyOrgResourceActions(role, resource, actions)
// back to resources!
}
}
Expand All @@ -312,7 +312,7 @@ customRoleLoop:
return role, nil
}

func applyOrgResourceActions(role *codersdk.Role, orgID uuid.UUID, resource string, actions []string) {
func applyOrgResourceActions(role *codersdk.Role, resource string, actions []string) {
if role.OrganizationPermissions == nil {
role.OrganizationPermissions = make([]codersdk.Permission, 0)
}
Expand All @@ -338,7 +338,7 @@ func applyOrgResourceActions(role *codersdk.Role, orgID uuid.UUID, resource stri
role.OrganizationPermissions = keep
}

func defaultActions(role *codersdk.Role, orgID uuid.UUID, resource string) []string {
func defaultActions(role *codersdk.Role, resource string) []string {
if role.OrganizationPermissions == nil {
role.OrganizationPermissions = []codersdk.Permission{}
}
Expand All @@ -352,15 +352,15 @@ func defaultActions(role *codersdk.Role, orgID uuid.UUID, resource string) []str
return defaults
}

func permissionPreviews(role *codersdk.Role, orgID uuid.UUID, resources []codersdk.RBACResource) []string {
func permissionPreviews(role *codersdk.Role, resources []codersdk.RBACResource) []string {
previews := make([]string, 0, len(resources))
for _, resource := range resources {
previews = append(previews, permissionPreview(role, orgID, resource))
previews = append(previews, permissionPreview(role, resource))
}
return previews
}

func permissionPreview(role *codersdk.Role, orgID uuid.UUID, resource codersdk.RBACResource) string {
func permissionPreview(role *codersdk.Role, resource codersdk.RBACResource) string {
if role.OrganizationPermissions == nil {
role.OrganizationPermissions = []codersdk.Permission{}
}
Expand Down
16 changes: 7 additions & 9 deletions enterprise/cli/organization_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,17 +39,15 @@ func TestEditOrganizationRoles(t *testing.T) {
// Use json input, as interactive mode would be challenging to control
inv.Stdin = bytes.NewBufferString(fmt.Sprintf(`{
"name": "new-role",
"organization_id": "%[1]s",
"organization_id": "%s",
"display_name": "",
"site_permissions": [],
"organization_permissions": {
"%[1]s": [
{
"resource_type": "workspace",
"action": "read"
}
]
},
"organization_permissions": [
{
"resource_type": "workspace",
"action": "read"
}
],
"user_permissions": [],
"assignable": false,
"built_in": false
Expand Down