Skip to content

Commit 87b9c45

Browse files
committed
work on cli test
1 parent 4e0146f commit 87b9c45

File tree

3 files changed

+158
-0
lines changed

3 files changed

+158
-0
lines changed

cli/organizationsettings.go

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,11 @@ import (
44
"bytes"
55
"encoding/json"
66
"fmt"
7+
"io"
78
"strings"
89

10+
"golang.org/x/xerrors"
11+
912
"github.com/coder/coder/v2/codersdk"
1013
"github.com/coder/serpent"
1114
)
@@ -21,6 +24,75 @@ func (r *RootCmd) organizationSettings(orgContext *OrganizationContext) *serpent
2124
Hidden: true,
2225
Children: []*serpent.Command{
2326
r.printOrganizationSetting(orgContext),
27+
r.updateOrganizationSetting(orgContext),
28+
},
29+
}
30+
return cmd
31+
}
32+
33+
func (r *RootCmd) updateOrganizationSetting(orgContext *OrganizationContext) *serpent.Command {
34+
client := new(codersdk.Client)
35+
cmd := &serpent.Command{
36+
Use: "set <groupsync | rolesync>",
37+
Short: "Update specified organization setting.",
38+
Long: FormatExamples(
39+
Example{
40+
Description: "Update group sync settings.",
41+
Command: "coder organization settings set groupsync < input.json",
42+
},
43+
),
44+
Options: []serpent.Option{},
45+
Middleware: serpent.Chain(
46+
serpent.RequireNArgs(1),
47+
r.InitClient(client),
48+
),
49+
Handler: func(inv *serpent.Invocation) error {
50+
ctx := inv.Context()
51+
org, err := orgContext.Selected(inv, client)
52+
if err != nil {
53+
return err
54+
}
55+
56+
// Read in the json
57+
inputData, err := io.ReadAll(inv.Stdin)
58+
if err != nil {
59+
return xerrors.Errorf("reading stdin: %w", err)
60+
}
61+
62+
var setting any
63+
switch strings.ToLower(inv.Args[0]) {
64+
case "groupsync", "group-sync":
65+
var req codersdk.GroupSyncSettings
66+
err = json.Unmarshal(inputData, &req)
67+
if err != nil {
68+
return xerrors.Errorf("unmarshalling group sync settings: %w", err)
69+
}
70+
setting, err = client.PatchGroupIDPSyncSettings(ctx, org.ID.String(), req)
71+
case "rolesync", "role-sync":
72+
// TODO: Implement role sync settings.
73+
return fmt.Errorf("role sync settings are not implemented")
74+
default:
75+
_, _ = fmt.Fprintln(inv.Stderr, "Valid organization settings are: 'groupsync', 'rolesync'")
76+
return fmt.Errorf("unknown organization setting %s", inv.Args[0])
77+
}
78+
79+
if err != nil {
80+
return fmt.Errorf("failed to get organization setting %s: %w", inv.Args[0], err)
81+
}
82+
83+
settingJSON, err := json.Marshal(setting)
84+
if err != nil {
85+
return fmt.Errorf("failed to marshal organization setting %s: %w", inv.Args[0], err)
86+
}
87+
88+
var dst bytes.Buffer
89+
err = json.Indent(&dst, settingJSON, "", "\t")
90+
if err != nil {
91+
return fmt.Errorf("failed to indent organization setting as json %s: %w", inv.Args[0], err)
92+
}
93+
94+
_, err = fmt.Fprintln(inv.Stdout, dst.String())
95+
return err
2496
},
2597
}
2698
return cmd
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
package cli_test
2+
3+
import (
4+
"bytes"
5+
"encoding/json"
6+
"regexp"
7+
"testing"
8+
9+
"github.com/google/uuid"
10+
"github.com/stretchr/testify/require"
11+
12+
"github.com/coder/coder/v2/cli/clitest"
13+
"github.com/coder/coder/v2/coderd/coderdtest"
14+
"github.com/coder/coder/v2/codersdk"
15+
"github.com/coder/coder/v2/enterprise/coderd/coderdenttest"
16+
"github.com/coder/coder/v2/enterprise/coderd/license"
17+
"github.com/coder/coder/v2/testutil"
18+
)
19+
20+
func TestUpdateGroupSync(t *testing.T) {
21+
t.Parallel()
22+
23+
t.Run("OK", func(t *testing.T) {
24+
dv := coderdtest.DeploymentValues(t)
25+
dv.Experiments = []string{string(codersdk.ExperimentMultiOrganization)}
26+
27+
owner, first := coderdenttest.New(t, &coderdenttest.Options{
28+
Options: &coderdtest.Options{
29+
DeploymentValues: dv,
30+
},
31+
LicenseOptions: &coderdenttest.LicenseOptions{
32+
Features: license.Features{
33+
codersdk.FeatureMultipleOrganizations: 1,
34+
},
35+
},
36+
})
37+
38+
ctx := testutil.Context(t, testutil.WaitLong)
39+
inv, root := clitest.New(t, "organization", "settings", "set", "groupsync")
40+
clitest.SetupConfig(t, owner, root)
41+
42+
expectedSettings := codersdk.GroupSyncSettings{
43+
Field: "groups",
44+
Mapping: map[string][]uuid.UUID{
45+
"test": {first.OrganizationID},
46+
},
47+
RegexFilter: regexp.MustCompile("^foo"),
48+
AutoCreateMissing: true,
49+
LegacyNameMapping: nil,
50+
}
51+
expectedData, err := json.Marshal(expectedSettings)
52+
require.NoError(t, err)
53+
54+
buf := new(bytes.Buffer)
55+
inv.Stdout = buf
56+
inv.Stdin = bytes.NewBuffer(expectedData)
57+
err = inv.WithContext(ctx).Run()
58+
require.NoError(t, err)
59+
require.JSONEq(t, string(expectedData), buf.String())
60+
61+
// Now read it back
62+
inv, root = clitest.New(t, "organization", "settings", "show", "groupsync")
63+
clitest.SetupConfig(t, owner, root)
64+
65+
buf = new(bytes.Buffer)
66+
inv.Stdout = buf
67+
err = inv.WithContext(ctx).Run()
68+
require.NoError(t, err)
69+
require.JSONEq(t, string(expectedData), buf.String())
70+
})
71+
}

enterprise/coderd/idpsync.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"github.com/coder/coder/v2/coderd/idpsync"
1010
"github.com/coder/coder/v2/coderd/rbac"
1111
"github.com/coder/coder/v2/coderd/rbac/policy"
12+
"github.com/coder/coder/v2/codersdk"
1213
)
1314

1415
// @Summary Get group IdP Sync settings by organization
@@ -61,6 +62,20 @@ func (api *API) patchGroupIDPSyncSettings(rw http.ResponseWriter, r *http.Reques
6162
return
6263
}
6364

65+
if len(req.LegacyNameMapping) > 0 {
66+
httpapi.Write(ctx, rw, http.StatusBadRequest, codersdk.Response{
67+
Message: "Unexpected field 'legacy_group_name_mapping'. Field not allowed, set to null or remove it.",
68+
Detail: "legacy_group_name_mapping is deprecated, use mapping instead",
69+
Validations: []codersdk.ValidationError{
70+
{
71+
Field: "legacy_group_name_mapping",
72+
Detail: "field is not allowed",
73+
},
74+
},
75+
})
76+
return
77+
}
78+
6479
//nolint:gocritic // Requires system context to update runtime config
6580
sysCtx := dbauthz.AsSystemRestricted(ctx)
6681
err := api.IDPSync.UpdateGroupSettings(sysCtx, org.ID, api.Database, req)

0 commit comments

Comments
 (0)