mirror of
https://github.com/coder/coder.git
synced 2026-10-16 14:48:56 +08:00
`default_org_member_roles` was only settable through Terraform or a raw
`PATCH /api/v2/organizations/{organization}`. This adds a CLI
equivalent.
```console
coder organizations edit --org acme --default-org-member-roles organization-workspace-access,organization-template-admin
coder organizations edit --org acme --default-org-member-roles "" # grant no roles
```
- The flag replaces the organization's current list rather than adding
to it. It accepts a comma-separated list, may be repeated, and
de-duplicates. An empty value removes every role.
- Removing a role prompts for confirmation, listing the roles being
removed, unless `-y` is passed. Adding roles applies without a prompt.
- The resulting set is printed, so no follow-up command is needed to
confirm what was applied.
- The organization is chosen with `-O/--org` or `$CODER_ORGANIZATION`,
and is implicit when the caller belongs to exactly one organization.
The org update endpoint is behind `FeatureMultipleOrganizations`, so the
command is enterprise/premium only. Non-built-in roles are rejected by
the server, and `dbauthz` already prevents a caller from setting a
default they could not assign to a member individually.
<details>
<summary>Implementation notes</summary>
## Shape
`codersdk.UpdateOrganizationRequest.DefaultOrgMemberRoles *[]string` and
the enterprise `patchOrganization` handler already existed; only the CLI
was missing, and nothing in `cli/` called `UpdateOrganization` at all.
No server or SDK changes are needed.
`organizations edit` follows the partial-update pattern of `coder groups
edit`: check `ParsedFlags().Changed(...)`, populate only the fields the
user set, and leave the rest nil so the handler preserves them. Passing
no flags is an error.
Two earlier shapes were tried and dropped:
- **A `default-org-member-roles` entry in the `organizations settings`
registry.** Every sibling entry unmarshals an SDK type that maps 1:1 to
a dedicated endpoint; this field has no such endpoint. Reusing
`UpdateOrganizationRequest` for the piped JSON meant a stray `name`
field could rename the org, and the documented `show > file`, edit, `set
< file` workflow failed on the default organization, whose name is
reserved.
- **A `--default-org-member-roles` flag on `organizations create`.**
`create` cannot set display name, description, or icon either, so a
roles-only flag there is asymmetric. It would also require
`CreateOrganizationRequest` to grow the field plus server-side
validation.
## Known sharp edge
`serpent.StringArray` resets its slice when any flag value is empty, so
`--default-org-member-roles a --default-org-member-roles ""` clears the
list rather than keeping `a`. This is pinned by a test and called out in
a code comment. The confirmation prompt catches it interactively; a
script passing `-y` would not. A custom flag value that ignores empty
occurrences was prototyped and reverted as more machinery than the
behavior warranted.
## Validation
Manually verified against a local dev deployment with a premium license:
setting, adding, clearing, prompt accepted and declined, duplicate and
whitespace handling, invalid role rejection, and a plain member being
denied. Confirmed with `/authcheck` that a member loses workspace-create
access when the roles are cleared and regains it when restored, and that
the change is recorded in the audit log.
## Follow-ups worth considering
- `--name`, `--display-name`, `--description`, and `--icon` on
`organizations edit`; all already exist on `UpdateOrganizationRequest`.
Rename deserves its own discussion.
- Setting the defaults to `organization-admin` makes every member an org
admin, including the ability to edit the org further, and those users
still render as plain members in member listings. Reachable today via
the API, UI, and Terraform; a warning on privileged roles would help.
- `dbauthz.InsertOrganization` has no `canAssignRoles` check, unlike
`UpdateOrganization`. Harmless while the only caller passes the
deployment default constant.
- `docs/admin/users/organizations.md` still points users at the
dashboard or a raw `PATCH`.
</details>
---
Created by Coder Agents on behalf of @jscottmiller.
164 lines
4.6 KiB
Go
164 lines
4.6 KiB
Go
package cli
|
|
|
|
import (
|
|
"fmt"
|
|
"strings"
|
|
|
|
"golang.org/x/xerrors"
|
|
|
|
"github.com/coder/coder/v2/cli/cliui"
|
|
"github.com/coder/coder/v2/codersdk"
|
|
"github.com/coder/serpent"
|
|
)
|
|
|
|
func (r *RootCmd) organizations() *serpent.Command {
|
|
orgContext := NewOrganizationContext()
|
|
|
|
cmd := &serpent.Command{
|
|
Use: "organizations [subcommand]",
|
|
Short: "Organization related commands",
|
|
Aliases: []string{"organization", "org", "orgs"},
|
|
Handler: func(inv *serpent.Invocation) error {
|
|
return inv.Command.HelpHandler(inv)
|
|
},
|
|
Children: []*serpent.Command{
|
|
r.showOrganization(orgContext),
|
|
r.listOrganizations(),
|
|
r.createOrganization(),
|
|
r.editOrganization(orgContext),
|
|
r.deleteOrganization(orgContext),
|
|
r.organizationMembers(orgContext),
|
|
r.organizationRoles(orgContext),
|
|
r.organizationSettings(orgContext),
|
|
},
|
|
}
|
|
|
|
orgContext.AttachOptions(cmd)
|
|
return cmd
|
|
}
|
|
|
|
func (r *RootCmd) showOrganization(orgContext *OrganizationContext) *serpent.Command {
|
|
var (
|
|
stringFormat func(orgs []codersdk.Organization) (string, error)
|
|
formatter = cliui.NewOutputFormatter(
|
|
cliui.ChangeFormatterData(cliui.TextFormat(), func(data any) (any, error) {
|
|
typed, ok := data.([]codersdk.Organization)
|
|
if !ok {
|
|
// This should never happen
|
|
return "", xerrors.Errorf("expected []Organization, got %T", data)
|
|
}
|
|
return stringFormat(typed)
|
|
}),
|
|
cliui.TableFormat([]codersdk.Organization{}, []string{"id", "name", "default"}),
|
|
cliui.JSONFormat(),
|
|
)
|
|
onlyID = false
|
|
)
|
|
cmd := &serpent.Command{
|
|
Use: "show [\"selected\"|\"me\"|uuid|org_name]",
|
|
Short: "Show the organization. " +
|
|
"Using \"selected\" will show the selected organization from the \"--org\" flag. " +
|
|
"Using \"me\" will show all organizations you are a member of.",
|
|
Long: FormatExamples(
|
|
Example{
|
|
Description: "coder org show selected",
|
|
Command: "Shows the organizations selected with '--org=<org_name>'. " +
|
|
"This organization is the organization used by the cli.",
|
|
},
|
|
Example{
|
|
Description: "coder org show me",
|
|
Command: "List of all organizations you are a member of.",
|
|
},
|
|
Example{
|
|
Description: "coder org show developers",
|
|
Command: "Show organization with name 'developers'",
|
|
},
|
|
Example{
|
|
Description: "coder org show 90ee1875-3db5-43b3-828e-af3687522e43",
|
|
Command: "Show organization with the given ID.",
|
|
},
|
|
),
|
|
Middleware: serpent.Chain(
|
|
serpent.RequireRangeArgs(0, 1),
|
|
),
|
|
Options: serpent.OptionSet{
|
|
{
|
|
Name: "only-id",
|
|
Description: "Only print the organization ID.",
|
|
Required: false,
|
|
Flag: "only-id",
|
|
Value: serpent.BoolOf(&onlyID),
|
|
},
|
|
},
|
|
Handler: func(inv *serpent.Invocation) error {
|
|
client, err := r.InitClient(inv)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
orgArg := "selected"
|
|
if len(inv.Args) >= 1 {
|
|
orgArg = inv.Args[0]
|
|
}
|
|
|
|
var orgs []codersdk.Organization
|
|
switch strings.ToLower(orgArg) {
|
|
case "selected":
|
|
stringFormat = func(orgs []codersdk.Organization) (string, error) {
|
|
if len(orgs) != 1 {
|
|
return "", xerrors.Errorf("expected 1 organization, got %d", len(orgs))
|
|
}
|
|
return fmt.Sprintf("Current CLI Organization: %s (%s)\n", orgs[0].Name, orgs[0].ID.String()), nil
|
|
}
|
|
org, err := orgContext.Selected(inv, client)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
orgs = []codersdk.Organization{org}
|
|
case "me":
|
|
stringFormat = func(orgs []codersdk.Organization) (string, error) {
|
|
var str strings.Builder
|
|
_, _ = fmt.Fprint(&str, "Organizations you are a member of:\n")
|
|
for _, org := range orgs {
|
|
_, _ = fmt.Fprintf(&str, "\t%s (%s)\n", org.Name, org.ID.String())
|
|
}
|
|
return str.String(), nil
|
|
}
|
|
orgs, err = client.OrganizationsByUser(inv.Context(), codersdk.Me)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
default:
|
|
stringFormat = func(orgs []codersdk.Organization) (string, error) {
|
|
if len(orgs) != 1 {
|
|
return "", xerrors.Errorf("expected 1 organization, got %d", len(orgs))
|
|
}
|
|
return fmt.Sprintf("Organization: %s (%s)\n", orgs[0].Name, orgs[0].ID.String()), nil
|
|
}
|
|
// This works for a uuid or a name
|
|
org, err := client.OrganizationByName(inv.Context(), orgArg)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
orgs = []codersdk.Organization{org}
|
|
}
|
|
|
|
if onlyID {
|
|
for _, org := range orgs {
|
|
_, _ = fmt.Fprintf(inv.Stdout, "%s\n", org.ID)
|
|
}
|
|
} else {
|
|
out, err := formatter.Format(inv.Context(), orgs)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
_, _ = fmt.Fprint(inv.Stdout, out)
|
|
}
|
|
return nil
|
|
},
|
|
}
|
|
formatter.AttachOptions(&cmd.Options)
|
|
|
|
return cmd
|
|
}
|