Skip to content

Commit 638266f

Browse files
authored
Add connectedOrgConfigs list command (#2892)
1 parent 80c76cc commit 638266f

File tree

8 files changed

+338
-2
lines changed

8 files changed

+338
-2
lines changed
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
.. _atlas-federatedAuthentication-federationSettings-connectedOrgConfigs-list:
2+
3+
=========================================================================
4+
atlas federatedAuthentication federationSettings connectedOrgConfigs list
5+
=========================================================================
6+
7+
.. default-domain:: mongodb
8+
9+
.. contents:: On this page
10+
:local:
11+
:backlinks: none
12+
:depth: 1
13+
:class: singlecol
14+
15+
Describe a Connected Org Config.
16+
17+
Syntax
18+
------
19+
20+
.. code-block::
21+
:caption: Command Syntax
22+
23+
atlas federatedAuthentication federationSettings connectedOrgConfigs list [options]
24+
25+
.. Code end marker, please don't delete this comment
26+
27+
Options
28+
-------
29+
30+
.. list-table::
31+
:header-rows: 1
32+
:widths: 20 10 10 60
33+
34+
* - Name
35+
- Type
36+
- Required
37+
- Description
38+
* - --federationSettingsId
39+
- string
40+
- true
41+
- Unique identifier of the federation settings.
42+
* - -h, --help
43+
-
44+
- false
45+
- help for list
46+
* - --limit
47+
- int
48+
- false
49+
- Number of items per results page, up to a maximum of 500. If you have more than 500 results, specify the --page option to change the results page. This value defaults to 100.
50+
* - --orgId
51+
- string
52+
- false
53+
- Organization ID to use. This option overrides the settings in the configuration file or environment variable.
54+
* - -o, --output
55+
- string
56+
- false
57+
- Output format. Valid values are json, json-path, go-template, or go-template-file. To see the full output, use the -o json option.
58+
* - --page
59+
- int
60+
- false
61+
- Page number that specifies a page of results. This value defaults to 1.
62+
63+
Inherited Options
64+
-----------------
65+
66+
.. list-table::
67+
:header-rows: 1
68+
:widths: 20 10 10 60
69+
70+
* - Name
71+
- Type
72+
- Required
73+
- Description
74+
* - -P, --profile
75+
- string
76+
- false
77+
- Name of the profile to use from your configuration file. To learn about profiles for the Atlas CLI, see https://dochub.mongodb.org/core/atlas-cli-save-connection-settings.
78+
79+
Examples
80+
--------
81+
82+
.. code-block::
83+
84+
# List all connected org config with federationSettingsId 5d1113b25a115342acc2d1aa
85+
atlas federatedAuthentication connectedOrgsConfig list --federationSettingsId 5d1113b25a115342acc2d1aa
86+

docs/command/atlas-federatedAuthentication-federationSettings-connectedOrgConfigs.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ Related Commands
5353
* :ref:`atlas-federatedAuthentication-federationSettings-connectedOrgConfigs-delete` - Delete a connected org config Organization.
5454
* :ref:`atlas-federatedAuthentication-federationSettings-connectedOrgConfigs-describe` - Describe a Connected Org Config.
5555
* :ref:`atlas-federatedAuthentication-federationSettings-connectedOrgConfigs-disconnect` - Connect an Identity Provider to an Organization.
56+
* :ref:`atlas-federatedAuthentication-federationSettings-connectedOrgConfigs-list` - Describe a Connected Org Config.
5657
* :ref:`atlas-federatedAuthentication-federationSettings-connectedOrgConfigs-update` - Update One Org Config Connected to One Federation Setting.
5758

5859

@@ -63,5 +64,6 @@ Related Commands
6364
delete </command/atlas-federatedAuthentication-federationSettings-connectedOrgConfigs-delete>
6465
describe </command/atlas-federatedAuthentication-federationSettings-connectedOrgConfigs-describe>
6566
disconnect </command/atlas-federatedAuthentication-federationSettings-connectedOrgConfigs-disconnect>
67+
list </command/atlas-federatedAuthentication-federationSettings-connectedOrgConfigs-list>
6668
update </command/atlas-federatedAuthentication-federationSettings-connectedOrgConfigs-update>
6769

internal/cli/federatedauthentication/federationsettings/connectedorgsconfigs/connected_orgs_configs.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ func Builder() *cobra.Command {
3030
ConnectBuilder(),
3131
DisconnectBuilder(),
3232
DescribeBuilder(),
33+
ListBuilder(),
3334
DeleteBuilder(),
3435
UpdateBuilder(),
3536
)
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
// Copyright 2024 MongoDB Inc
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package connectedorgsconfigs
16+
17+
import (
18+
"context"
19+
20+
"github.com/mongodb/mongodb-atlas-cli/atlascli/internal/cli"
21+
"github.com/mongodb/mongodb-atlas-cli/atlascli/internal/config"
22+
"github.com/mongodb/mongodb-atlas-cli/atlascli/internal/flag"
23+
"github.com/mongodb/mongodb-atlas-cli/atlascli/internal/store"
24+
"github.com/mongodb/mongodb-atlas-cli/atlascli/internal/usage"
25+
"github.com/spf13/cobra"
26+
atlasv2 "go.mongodb.org/atlas-sdk/v20231115013/admin"
27+
)
28+
29+
type ListOpts struct {
30+
cli.GlobalOpts
31+
cli.OutputOpts
32+
cli.InputOpts
33+
cli.ListOpts
34+
store store.ConnectedOrgConfigsLister
35+
36+
federationSettingsID string
37+
}
38+
39+
const listTemplate = `ORG ID IDENTITY PROVIDER ID DATA ACCESS IDENTITY PRODIVER IDs{{range valueOrEmptySlice .Results}}
40+
{{.OrgId}} {{if .IdentityProviderId }} {{ .IdentityProviderId }}{{else}}N/A{{end}} {{if and .DataAccessIdentityProviderIds (gt (len .DataAccessIdentityProviderIds) 0)}}{{range $index, $element := .DataAccessIdentityProviderIds}}{{if $index}}, {{end}}{{$element}}{{end}}{{else}}N/A{{end}}{{end}}`
41+
42+
func (opts *ListOpts) Run() error {
43+
params := &atlasv2.ListConnectedOrgConfigsApiParams{
44+
FederationSettingsId: opts.federationSettingsID,
45+
ItemsPerPage: &opts.ItemsPerPage,
46+
PageNum: &opts.PageNum,
47+
}
48+
49+
r, err := opts.store.ListConnectedOrgConfigs(params)
50+
if err != nil {
51+
return err
52+
}
53+
54+
return opts.Print(r)
55+
}
56+
57+
func (opts *ListOpts) InitStore(ctx context.Context) func() error {
58+
return func() error {
59+
if opts.store != nil {
60+
return nil
61+
}
62+
63+
var err error
64+
opts.store, err = store.New(store.AuthenticatedPreset(config.Default()), store.WithContext(ctx))
65+
return err
66+
}
67+
}
68+
69+
// atlas federatedAuthentication connectedOrgsConfig list --federationSettingsId federationSettingsId [-o/--output output].
70+
func ListBuilder() *cobra.Command {
71+
opts := new(ListOpts)
72+
cmd := &cobra.Command{
73+
Use: "list",
74+
Short: "Describe a Connected Org Config.",
75+
Args: cobra.NoArgs,
76+
Example: ` # List all connected org config with federationSettingsId 5d1113b25a115342acc2d1aa
77+
atlas federatedAuthentication connectedOrgsConfig list --federationSettingsId 5d1113b25a115342acc2d1aa
78+
`,
79+
PreRunE: func(cmd *cobra.Command, _ []string) error {
80+
return opts.PreRunE(
81+
opts.ValidateOrgID,
82+
opts.InitStore(cmd.Context()),
83+
opts.InitOutput(cmd.OutOrStdout(), listTemplate),
84+
)
85+
},
86+
RunE: func(_ *cobra.Command, _ []string) error {
87+
return opts.Run()
88+
},
89+
}
90+
91+
cmd.Flags().StringVar(&opts.federationSettingsID, flag.FederationSettingsID, "", usage.FederationSettingsID)
92+
cmd.Flags().StringVar(&opts.OrgID, flag.OrgID, "", usage.OrgID)
93+
cmd.Flags().IntVar(&opts.PageNum, flag.Page, cli.DefaultPage, usage.Page)
94+
cmd.Flags().IntVar(&opts.ItemsPerPage, flag.Limit, cli.DefaultPageLimit, usage.Limit)
95+
96+
cmd.Flags().StringVarP(&opts.Output, flag.Output, flag.OutputShort, "", usage.FormatOut)
97+
_ = cmd.RegisterFlagCompletionFunc(flag.Output, opts.AutoCompleteOutputFlag())
98+
99+
_ = cmd.MarkFlagRequired(flag.FederationSettingsID)
100+
101+
return cmd
102+
}
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
// Copyright 2024 MongoDB Inc
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
//go:build unit
16+
17+
package connectedorgsconfigs
18+
19+
import (
20+
"testing"
21+
22+
"github.com/golang/mock/gomock"
23+
"github.com/mongodb/mongodb-atlas-cli/atlascli/internal/cli"
24+
"github.com/mongodb/mongodb-atlas-cli/atlascli/internal/flag"
25+
"github.com/mongodb/mongodb-atlas-cli/atlascli/internal/mocks"
26+
"github.com/mongodb/mongodb-atlas-cli/atlascli/internal/test"
27+
atlasv2 "go.mongodb.org/atlas-sdk/v20231115013/admin"
28+
)
29+
30+
func TestList_Run(t *testing.T) {
31+
ctrl := gomock.NewController(t)
32+
listStore := mocks.NewMockConnectedOrgConfigsLister(ctrl)
33+
34+
listOpts := &ListOpts{
35+
federationSettingsID: "federationSettingsID",
36+
store: listStore,
37+
OutputOpts: cli.OutputOpts{
38+
Template: listTemplate,
39+
},
40+
ListOpts: cli.ListOpts{},
41+
}
42+
43+
idpID := "id"
44+
expected := &atlasv2.PaginatedConnectedOrgConfigs{
45+
Results: &[]atlasv2.ConnectedOrgConfig{
46+
{
47+
OrgId: "id",
48+
IdentityProviderId: &idpID,
49+
DataAccessIdentityProviderIds: &[]string{"id", "id2"},
50+
},
51+
},
52+
}
53+
54+
listStore.
55+
EXPECT().
56+
ListConnectedOrgConfigs(gomock.Any()).
57+
Return(expected, nil).
58+
Times(1)
59+
60+
if err := listOpts.Run(); err != nil {
61+
t.Fatalf("Run() unexpected error: %v", err)
62+
}
63+
64+
test.VerifyOutputTemplate(t, listTemplate, expected)
65+
}
66+
67+
func TestListBuilder(t *testing.T) {
68+
test.CmdValidator(
69+
t,
70+
ListBuilder(),
71+
0,
72+
[]string{flag.Output, flag.FederationSettingsID, flag.OrgID, flag.Page, flag.Limit},
73+
)
74+
}

internal/mocks/mock_connected_orgs_store.go

Lines changed: 39 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

internal/store/connected_org_configs.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import (
1818
atlasv2 "go.mongodb.org/atlas-sdk/v20231115013/admin"
1919
)
2020

21-
//go:generate mockgen -destination=../mocks/mock_connected_orgs_store.go -package=mocks github.com/mongodb/mongodb-atlas-cli/atlascli/internal/store ConnectedOrgConfigsUpdater,ConnectedOrgConfigsDescriber,ConnectedOrgConfigsDeleter
21+
//go:generate mockgen -destination=../mocks/mock_connected_orgs_store.go -package=mocks github.com/mongodb/mongodb-atlas-cli/atlascli/internal/store ConnectedOrgConfigsUpdater,ConnectedOrgConfigsDescriber,ConnectedOrgConfigsDeleter,ConnectedOrgConfigsLister
2222

2323
type ConnectedOrgConfigsUpdater interface {
2424
UpdateConnectedOrgConfig(opts *atlasv2.UpdateConnectedOrgConfigApiParams) (*atlasv2.ConnectedOrgConfig, error)
@@ -28,6 +28,9 @@ type ConnectedOrgConfigsDescriber interface {
2828
GetConnectedOrgConfig(opts *atlasv2.GetConnectedOrgConfigApiParams) (*atlasv2.ConnectedOrgConfig, error)
2929
}
3030

31+
type ConnectedOrgConfigsLister interface {
32+
ListConnectedOrgConfigs(opts *atlasv2.ListConnectedOrgConfigsApiParams) (*atlasv2.PaginatedConnectedOrgConfigs, error)
33+
}
3134
type ConnectedOrgConfigsDeleter interface {
3235
DeleteConnectedOrgConfig(federationSettingsID string, orgID string) error
3336
}
@@ -44,6 +47,12 @@ func (s *Store) GetConnectedOrgConfig(opts *atlasv2.GetConnectedOrgConfigApiPara
4447
return result, err
4548
}
4649

50+
// ListConnectedOrgConfigs encapsulate the logic to manage different cloud providers.
51+
func (s *Store) ListConnectedOrgConfigs(opts *atlasv2.ListConnectedOrgConfigsApiParams) (*atlasv2.PaginatedConnectedOrgConfigs, error) {
52+
result, _, err := s.clientv2.FederatedAuthenticationApi.ListConnectedOrgConfigsWithParams(s.ctx, opts).Execute()
53+
return result, err
54+
}
55+
4756
// DeleteConnectedOrgConfig encapsulate the logic to manage different cloud providers.
4857
func (s *Store) DeleteConnectedOrgConfig(federationSettingsID string, orgID string) error {
4958
_, _, err := s.clientv2.FederatedAuthenticationApi.RemoveConnectedOrgConfig(s.ctx, federationSettingsID, orgID).Execute()

test/e2e/atlas/identity_providers_test.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -397,6 +397,30 @@ func TestIdentityProviders(t *testing.T) {
397397
req.NoError(json.Unmarshal(resp, &provider))
398398
})
399399

400+
t.Run("List connectedOrgsConfig", func(t *testing.T) {
401+
cmd := exec.Command(cliPath,
402+
federatedAuthenticationEntity,
403+
federationSettingsEntity,
404+
connectedOrgsConfigsEntity,
405+
"list",
406+
"--federationSettingsId",
407+
federationSettingsID,
408+
"-o=json",
409+
)
410+
411+
cmd.Env = os.Environ()
412+
resp, err := cmd.CombinedOutput()
413+
req.NoError(err, string(resp))
414+
415+
var config atlasv2.PaginatedConnectedOrgConfigs
416+
req.NoError(json.Unmarshal(resp, &config))
417+
418+
assert.NotEmpty(t, config.GetResults())
419+
assert.Len(t, config.GetResults(), 1)
420+
assert.NotContains(t, config.GetResults()[0].GetDataAccessIdentityProviderIds(), oidcIWorkforceIdpID)
421+
assert.NotContains(t, config.GetResults()[0].GetDataAccessIdentityProviderIds(), oidcWorkloadIdpID)
422+
})
423+
400424
t.Run("Describe OIDC IdP WORKFORCE", func(t *testing.T) {
401425
cmd := exec.Command(cliPath,
402426
federatedAuthenticationEntity,

0 commit comments

Comments
 (0)