Skip to content

Commit 99dd13d

Browse files
authored
chore: add cli command to update organization sync settings (coder#15459)
1 parent 75b2990 commit 99dd13d

11 files changed

+162
-24
lines changed

cli/organizationsettings.go

+40-8
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,23 @@ func (r *RootCmd) organizationSettings(orgContext *OrganizationContext) *serpent
4848
return cli.RoleIDPSyncSettings(ctx, org.String())
4949
},
5050
},
51+
{
52+
Name: "organization-sync",
53+
Aliases: []string{"organizationsync", "org-sync", "orgsync"},
54+
Short: "Organization sync settings to sync organization memberships from an IdP.",
55+
DisableOrgContext: true,
56+
Patch: func(ctx context.Context, cli *codersdk.Client, _ uuid.UUID, input json.RawMessage) (any, error) {
57+
var req codersdk.OrganizationSyncSettings
58+
err := json.Unmarshal(input, &req)
59+
if err != nil {
60+
return nil, xerrors.Errorf("unmarshalling organization sync settings: %w", err)
61+
}
62+
return cli.PatchOrganizationIDPSyncSettings(ctx, req)
63+
},
64+
Fetch: func(ctx context.Context, cli *codersdk.Client, _ uuid.UUID) (any, error) {
65+
return cli.OrganizationIDPSyncSettings(ctx)
66+
},
67+
},
5168
}
5269
cmd := &serpent.Command{
5370
Use: "settings",
@@ -68,8 +85,13 @@ type organizationSetting struct {
6885
Name string
6986
Aliases []string
7087
Short string
71-
Patch func(ctx context.Context, cli *codersdk.Client, org uuid.UUID, input json.RawMessage) (any, error)
72-
Fetch func(ctx context.Context, cli *codersdk.Client, org uuid.UUID) (any, error)
88+
// DisableOrgContext is kinda a kludge. It tells the command constructor
89+
// to not require an organization context. This is used for the organization
90+
// sync settings which are not tied to a specific organization.
91+
// It feels excessive to build a more elaborate solution for this one-off.
92+
DisableOrgContext bool
93+
Patch func(ctx context.Context, cli *codersdk.Client, org uuid.UUID, input json.RawMessage) (any, error)
94+
Fetch func(ctx context.Context, cli *codersdk.Client, org uuid.UUID) (any, error)
7395
}
7496

7597
func (r *RootCmd) setOrganizationSettings(orgContext *OrganizationContext, settings []organizationSetting) *serpent.Command {
@@ -107,9 +129,14 @@ func (r *RootCmd) setOrganizationSettings(orgContext *OrganizationContext, setti
107129
),
108130
Handler: func(inv *serpent.Invocation) error {
109131
ctx := inv.Context()
110-
org, err := orgContext.Selected(inv, client)
111-
if err != nil {
112-
return err
132+
var org codersdk.Organization
133+
var err error
134+
135+
if !set.DisableOrgContext {
136+
org, err = orgContext.Selected(inv, client)
137+
if err != nil {
138+
return err
139+
}
113140
}
114141

115142
// Read in the json
@@ -178,9 +205,14 @@ func (r *RootCmd) printOrganizationSetting(orgContext *OrganizationContext, sett
178205
),
179206
Handler: func(inv *serpent.Invocation) error {
180207
ctx := inv.Context()
181-
org, err := orgContext.Selected(inv, client)
182-
if err != nil {
183-
return err
208+
var org codersdk.Organization
209+
var err error
210+
211+
if !set.DisableOrgContext {
212+
org, err = orgContext.Selected(inv, client)
213+
if err != nil {
214+
return err
215+
}
184216
}
185217

186218
output, err := fetch(ctx, client, org.ID)

cli/testdata/coder_organizations_settings_set_--help.golden

+5-2
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,11 @@ USAGE:
1010
$ coder organization settings set groupsync < input.json
1111

1212
SUBCOMMANDS:
13-
group-sync Group sync settings to sync groups from an IdP.
14-
role-sync Role sync settings to sync organization roles from an IdP.
13+
group-sync Group sync settings to sync groups from an IdP.
14+
organization-sync Organization sync settings to sync organization
15+
memberships from an IdP.
16+
role-sync Role sync settings to sync organization roles from an
17+
IdP.
1518

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

cli/testdata/coder_organizations_settings_set_--help_--help.golden

+5-2
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,11 @@ USAGE:
1010
$ coder organization settings set groupsync < input.json
1111

1212
SUBCOMMANDS:
13-
group-sync Group sync settings to sync groups from an IdP.
14-
role-sync Role sync settings to sync organization roles from an IdP.
13+
group-sync Group sync settings to sync groups from an IdP.
14+
organization-sync Organization sync settings to sync organization
15+
memberships from an IdP.
16+
role-sync Role sync settings to sync organization roles from an
17+
IdP.
1518

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

cli/testdata/coder_organizations_settings_show_--help.golden

+5-2
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,11 @@ USAGE:
1010
$ coder organization settings show groupsync
1111

1212
SUBCOMMANDS:
13-
group-sync Group sync settings to sync groups from an IdP.
14-
role-sync Role sync settings to sync organization roles from an IdP.
13+
group-sync Group sync settings to sync groups from an IdP.
14+
organization-sync Organization sync settings to sync organization
15+
memberships from an IdP.
16+
role-sync Role sync settings to sync organization roles from an
17+
IdP.
1518

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

cli/testdata/coder_organizations_settings_show_--help_--help.golden

+5-2
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,11 @@ USAGE:
1010
$ coder organization settings show groupsync
1111

1212
SUBCOMMANDS:
13-
group-sync Group sync settings to sync groups from an IdP.
14-
role-sync Role sync settings to sync organization roles from an IdP.
13+
group-sync Group sync settings to sync groups from an IdP.
14+
organization-sync Organization sync settings to sync organization
15+
memberships from an IdP.
16+
role-sync Role sync settings to sync organization roles from an
17+
IdP.
1518

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

docs/manifest.json

+10
Original file line numberDiff line numberDiff line change
@@ -1056,6 +1056,11 @@
10561056
"description": "Group sync settings to sync groups from an IdP.",
10571057
"path": "reference/cli/organizations_settings_set_group-sync.md"
10581058
},
1059+
{
1060+
"title": "organizations settings set organization-sync",
1061+
"description": "Organization sync settings to sync organization memberships from an IdP.",
1062+
"path": "reference/cli/organizations_settings_set_organization-sync.md"
1063+
},
10591064
{
10601065
"title": "organizations settings set role-sync",
10611066
"description": "Role sync settings to sync organization roles from an IdP.",
@@ -1071,6 +1076,11 @@
10711076
"description": "Group sync settings to sync groups from an IdP.",
10721077
"path": "reference/cli/organizations_settings_show_group-sync.md"
10731078
},
1079+
{
1080+
"title": "organizations settings show organization-sync",
1081+
"description": "Organization sync settings to sync organization memberships from an IdP.",
1082+
"path": "reference/cli/organizations_settings_show_organization-sync.md"
1083+
},
10741084
{
10751085
"title": "organizations settings show role-sync",
10761086
"description": "Role sync settings to sync organization roles from an IdP.",

docs/reference/cli/organizations_settings_set.md

+5-4
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docs/reference/cli/organizations_settings_set_organization-sync.md

+17
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docs/reference/cli/organizations_settings_show.md

+5-4
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docs/reference/cli/organizations_settings_show_organization-sync.md

+17
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

enterprise/cli/organizationsettings_test.go

+48
Original file line numberDiff line numberDiff line change
@@ -115,3 +115,51 @@ func TestUpdateRoleSync(t *testing.T) {
115115
require.JSONEq(t, string(expectedData), buf.String())
116116
})
117117
}
118+
119+
func TestUpdateOrganizationSync(t *testing.T) {
120+
t.Parallel()
121+
122+
t.Run("OK", func(t *testing.T) {
123+
t.Parallel()
124+
125+
owner, _ := coderdenttest.New(t, &coderdenttest.Options{
126+
LicenseOptions: &coderdenttest.LicenseOptions{
127+
Features: license.Features{
128+
codersdk.FeatureMultipleOrganizations: 1,
129+
},
130+
},
131+
})
132+
133+
ctx := testutil.Context(t, testutil.WaitLong)
134+
inv, root := clitest.New(t, "organization", "settings", "set", "organization-sync")
135+
//nolint:gocritic // Using the owner, testing the cli not perms
136+
clitest.SetupConfig(t, owner, root)
137+
138+
expectedSettings := codersdk.OrganizationSyncSettings{
139+
Field: "organizations",
140+
Mapping: map[string][]uuid.UUID{
141+
"test": {uuid.New()},
142+
},
143+
}
144+
expectedData, err := json.Marshal(expectedSettings)
145+
require.NoError(t, err)
146+
147+
buf := new(bytes.Buffer)
148+
inv.Stdout = buf
149+
inv.Stdin = bytes.NewBuffer(expectedData)
150+
err = inv.WithContext(ctx).Run()
151+
require.NoError(t, err)
152+
require.JSONEq(t, string(expectedData), buf.String())
153+
154+
// Now read it back
155+
inv, root = clitest.New(t, "organization", "settings", "show", "organization-sync")
156+
//nolint:gocritic // Using the owner, testing the cli not perms
157+
clitest.SetupConfig(t, owner, root)
158+
159+
buf = new(bytes.Buffer)
160+
inv.Stdout = buf
161+
err = inv.WithContext(ctx).Run()
162+
require.NoError(t, err)
163+
require.JSONEq(t, string(expectedData), buf.String())
164+
})
165+
}

0 commit comments

Comments
 (0)