- Notifications
You must be signed in to change notification settings - Fork1.1k
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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Merged
Uh oh!
There was an error while loading.Please reload this page.
Merged
Changes fromall commits
Commits
Show all changes
9 commits Select commitHold shift + click to select a range
6a59ee1 chore: add cli command to fetch group sync settings as json
Emyrk4e0146f linting
Emyrk87b9c45 work on cli test
Emyrk6225ddf chore add unit test for role sync
Emyrkc048019 linting
Emyrk5bdbd39 linting
Emyrkc4482e6 update golden files
Emyrkec78d00 make cobra sub commands
Emyrk3dc174f make gen
EmyrkFile filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
1 change: 1 addition & 0 deletionscli/organization.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
209 changes: 209 additions & 0 deletionscli/organizationsettings.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
| 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 deletioncli/root.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
13 changes: 7 additions & 6 deletionscli/testdata/coder_organizations_--help.golden
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
15 changes: 15 additions & 0 deletionscli/testdata/coder_organizations_settings_--help.golden
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
| 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 deletionscli/testdata/coder_organizations_settings_set_--help.golden
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
| 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 deletionscli/testdata/coder_organizations_settings_set_--help_--help.golden
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
| 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 deletionscli/testdata/coder_organizations_settings_show_--help.golden
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
| 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. |
17 changes: 17 additions & 0 deletionscli/testdata/coder_organizations_settings_show_--help_--help.golden
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
| 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. |
35 changes: 35 additions & 0 deletionsdocs/manifest.json
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Oops, something went wrong.
Uh oh!
There was an error while loading.Please reload this page.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.