Thanks to visit codestin.com
Credit goes to github.com

Skip to content

chore: add cli command to fetch group sync settings as json #14694

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
Sep 17, 2024
Merged
Show file tree
Hide file tree
Changes from 8 commits
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
1 change: 1 addition & 0 deletions cli/organization.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ func (r *RootCmd) organizations() *serpent.Command {
r.createOrganization(),
r.organizationMembers(orgContext),
r.organizationRoles(orgContext),
r.organizationSettings(orgContext),
},
}

Expand Down
209 changes: 209 additions & 0 deletions cli/organizationsettings.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,209 @@
package cli

import (
"bytes"
"context"
"encoding/json"
"fmt"
"io"

"github.com/google/uuid"
"golang.org/x/xerrors"

"github.com/coder/coder/v2/codersdk"
"github.com/coder/serpent"
)

func (r *RootCmd) organizationSettings(orgContext *OrganizationContext) *serpent.Command {
settings := []organizationSetting{
{
Name: "group-sync",
Aliases: []string{"groupsync"},
Short: "Group sync settings to sync groups from an IdP.",
Patch: func(ctx context.Context, cli *codersdk.Client, org uuid.UUID, input json.RawMessage) (any, error) {
var req codersdk.GroupSyncSettings
err := json.Unmarshal(input, &req)
if err != nil {
return nil, xerrors.Errorf("unmarshalling group sync settings: %w", err)
}
return cli.PatchGroupIDPSyncSettings(ctx, org.String(), req)
},
Fetch: func(ctx context.Context, cli *codersdk.Client, org uuid.UUID) (any, error) {
return cli.GroupIDPSyncSettings(ctx, org.String())
},
},
{
Name: "role-sync",
Aliases: []string{"rolesync"},
Short: "Role sync settings to sync organization roles from an IdP.",
Patch: func(ctx context.Context, cli *codersdk.Client, org uuid.UUID, input json.RawMessage) (any, error) {
var req codersdk.RoleSyncSettings
err := json.Unmarshal(input, &req)
if err != nil {
return nil, xerrors.Errorf("unmarshalling role sync settings: %w", err)
}
return cli.PatchRoleIDPSyncSettings(ctx, org.String(), req)
},
Fetch: func(ctx context.Context, cli *codersdk.Client, org uuid.UUID) (any, error) {
return cli.RoleIDPSyncSettings(ctx, org.String())
},
},
}
cmd := &serpent.Command{
Use: "settings",
Short: "Manage organization settings.",
Aliases: []string{"setting"},
Handler: func(inv *serpent.Invocation) error {
return inv.Command.HelpHandler(inv)
},
Children: []*serpent.Command{
r.printOrganizationSetting(orgContext, settings),
r.setOrganizationSettings(orgContext, settings),
},
}
return cmd
}

type organizationSetting struct {
Name string
Aliases []string
Short string
Patch func(ctx context.Context, cli *codersdk.Client, org uuid.UUID, input json.RawMessage) (any, error)
Fetch func(ctx context.Context, cli *codersdk.Client, org uuid.UUID) (any, error)
}

func (r *RootCmd) setOrganizationSettings(orgContext *OrganizationContext, settings []organizationSetting) *serpent.Command {
client := new(codersdk.Client)
cmd := &serpent.Command{
Use: "set",
Short: "Update specified organization setting.",
Long: FormatExamples(
Example{
Description: "Update group sync settings.",
Command: "coder organization settings set groupsync < input.json",
},
),
Options: []serpent.Option{},
Middleware: serpent.Chain(
serpent.RequireNArgs(0),
r.InitClient(client),
),
Handler: func(inv *serpent.Invocation) error {
return inv.Command.HelpHandler(inv)
},
}

for _, set := range settings {
set := set
patch := set.Patch
cmd.Children = append(cmd.Children, &serpent.Command{
Use: set.Name,
Aliases: set.Aliases,
Short: set.Short,
Options: []serpent.Option{},
Middleware: serpent.Chain(
serpent.RequireNArgs(0),
r.InitClient(client),
),
Handler: func(inv *serpent.Invocation) error {
ctx := inv.Context()
org, err := orgContext.Selected(inv, client)
if err != nil {
return err
}

// Read in the json
inputData, err := io.ReadAll(inv.Stdin)
if err != nil {
return xerrors.Errorf("reading stdin: %w", err)
}

output, err := patch(ctx, client, org.ID, inputData)
if err != nil {
return xerrors.Errorf("patching %q: %w", set.Name, err)
}

settingJSON, err := json.Marshal(output)
if err != nil {
return fmt.Errorf("failed to marshal organization setting %s: %w", inv.Args[0], err)
}

var dst bytes.Buffer
err = json.Indent(&dst, settingJSON, "", "\t")
if err != nil {
return fmt.Errorf("failed to indent organization setting as json %s: %w", inv.Args[0], err)
}

_, err = fmt.Fprintln(inv.Stdout, dst.String())
return err
},
})
}

return cmd
}

func (r *RootCmd) printOrganizationSetting(orgContext *OrganizationContext, settings []organizationSetting) *serpent.Command {
client := new(codersdk.Client)
cmd := &serpent.Command{
Use: "show",
Short: "Outputs specified organization setting.",
Long: FormatExamples(
Example{
Description: "Output group sync settings.",
Command: "coder organization settings show groupsync",
},
),
Options: []serpent.Option{},
Middleware: serpent.Chain(
serpent.RequireNArgs(0),
r.InitClient(client),
),
Handler: func(inv *serpent.Invocation) error {
return inv.Command.HelpHandler(inv)
},
}

for _, set := range settings {
set := set
fetch := set.Fetch
cmd.Children = append(cmd.Children, &serpent.Command{
Use: set.Name,
Aliases: set.Aliases,
Short: set.Short,
Options: []serpent.Option{},
Middleware: serpent.Chain(
serpent.RequireNArgs(0),
r.InitClient(client),
),
Handler: func(inv *serpent.Invocation) error {
ctx := inv.Context()
org, err := orgContext.Selected(inv, client)
if err != nil {
return err
}

output, err := fetch(ctx, client, org.ID)
if err != nil {
return xerrors.Errorf("patching %q: %w", set.Name, err)
}

settingJSON, err := json.Marshal(output)
if err != nil {
return fmt.Errorf("failed to marshal organization setting %s: %w", inv.Args[0], err)
}

var dst bytes.Buffer
err = json.Indent(&dst, settingJSON, "", "\t")
if err != nil {
return fmt.Errorf("failed to indent organization setting as json %s: %w", inv.Args[0], err)
}

_, err = fmt.Fprintln(inv.Stdout, dst.String())
return err
},
})
}

return cmd
}
6 changes: 5 additions & 1 deletion cli/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import (
"golang.org/x/mod/semver"
"golang.org/x/xerrors"

"github.com/coder/coder/v2/coderd/database/db2sdk"
"github.com/coder/pretty"

"github.com/coder/coder/v2/buildinfo"
Expand Down Expand Up @@ -657,7 +658,10 @@ func (o *OrganizationContext) Selected(inv *serpent.Invocation, client *codersdk
}

// No org selected, and we are more than 1? Return an error.
return codersdk.Organization{}, xerrors.Errorf("Must select an organization with --org=<org_name>.")
validOrgs := db2sdk.List(orgs, func(org codersdk.Organization) string {
return fmt.Sprintf("%q", org.Name)
})
return codersdk.Organization{}, xerrors.Errorf("Must select an organization with --org=<org_name>. Choose from: %s", strings.Join(validOrgs, ", "))
}

func splitNamedWorkspace(identifier string) (owner string, workspaceName string, err error) {
Expand Down
13 changes: 7 additions & 6 deletions cli/testdata/coder_organizations_--help.golden
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,13 @@ USAGE:
Aliases: organization, org, orgs

SUBCOMMANDS:
create Create a new organization.
members Manage organization members
roles Manage organization roles.
show 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.
create Create a new organization.
members Manage organization members
roles Manage organization roles.
settings Manage organization settings.
show 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.

OPTIONS:
-O, --org string, $CODER_ORGANIZATION
Expand Down
15 changes: 15 additions & 0 deletions cli/testdata/coder_organizations_settings_--help.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
coder v0.0.0-devel

USAGE:
coder organizations settings

Manage organization settings.

Aliases: setting

SUBCOMMANDS:
set Update specified organization setting.
show Outputs specified organization setting.

———
Run `coder --help` for a list of global options.
17 changes: 17 additions & 0 deletions cli/testdata/coder_organizations_settings_set_--help.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
coder v0.0.0-devel

USAGE:
coder organizations settings set

Update specified organization setting.

- Update group sync settings.:

$ coder organization settings set groupsync < input.json

SUBCOMMANDS:
group-sync Group sync settings to sync groups from an IdP.
role-sync Role sync settings to sync organization roles from an IdP.

———
Run `coder --help` for a list of global options.
17 changes: 17 additions & 0 deletions cli/testdata/coder_organizations_settings_set_--help_--help.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
coder v0.0.0-devel

USAGE:
coder organizations settings set

Update specified organization setting.

- Update group sync settings.:

$ coder organization settings set groupsync < input.json

SUBCOMMANDS:
group-sync Group sync settings to sync groups from an IdP.
role-sync Role sync settings to sync organization roles from an IdP.

———
Run `coder --help` for a list of global options.
17 changes: 17 additions & 0 deletions cli/testdata/coder_organizations_settings_show_--help.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
coder v0.0.0-devel

USAGE:
coder organizations settings show

Outputs specified organization setting.

- Output group sync settings.:

$ coder organization settings show groupsync

SUBCOMMANDS:
group-sync Group sync settings to sync groups from an IdP.
role-sync Role sync settings to sync organization roles from an IdP.

———
Run `coder --help` for a list of global options.
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
coder v0.0.0-devel

USAGE:
coder organizations settings show

Outputs specified organization setting.

- Output group sync settings.:

$ coder organization settings show groupsync

SUBCOMMANDS:
group-sync Group sync settings to sync groups from an IdP.
role-sync Role sync settings to sync organization roles from an IdP.

———
Run `coder --help` for a list of global options.
Loading
Loading