Skip to content

CLOUDP-56480: Clusters list command should return all clusters #45

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 12 commits into from
Mar 13, 2020
Merged
Show file tree
Hide file tree
Changes from 9 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
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ require (
github.com/mattn/go-isatty v0.0.12 // indirect
github.com/mitchellh/go-homedir v1.1.0
github.com/mongodb/go-client-mongodb-atlas v0.1.4-0.20200312093748-8894c1fe3bb6
github.com/mongodb/go-client-mongodb-ops-manager v0.0.2-0.20200312102454-e5173cc1a3b0
github.com/mongodb/go-client-mongodb-ops-manager v0.0.2-0.20200312104623-fa9799b0cf65
github.com/pelletier/go-toml v1.6.0 // indirect
github.com/spf13/afero v1.2.2
github.com/spf13/cast v1.3.1 // indirect
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,8 @@ github.com/mitchellh/mapstructure v1.1.2 h1:fmNYVwqnSfB9mZU6OS2O6GsXM+wcskZDuKQz
github.com/mitchellh/mapstructure v1.1.2/go.mod h1:FVVH3fgwuzCH5S8UJGiWEs2h04kUh9fWfEaFds41c1Y=
github.com/mongodb/go-client-mongodb-atlas v0.1.4-0.20200312093748-8894c1fe3bb6 h1:ukGCT8EiyLDm+dOJOgRqySvDjZJly5tUPUDZmQt+f0Y=
github.com/mongodb/go-client-mongodb-atlas v0.1.4-0.20200312093748-8894c1fe3bb6/go.mod h1:LS8O0YLkA+sbtOb3fZLF10yY3tJM+1xATXMJ3oU35LU=
github.com/mongodb/go-client-mongodb-ops-manager v0.0.2-0.20200312102454-e5173cc1a3b0 h1:yOYbV6B8nH8jBoSKjITRE7TkU0xeAst7mAIt+JPFUlE=
github.com/mongodb/go-client-mongodb-ops-manager v0.0.2-0.20200312102454-e5173cc1a3b0/go.mod h1:G9iLP4YeC2TRpxjZRddP6wKjz2JbpAtX+MlKriO7TOU=
github.com/mongodb/go-client-mongodb-ops-manager v0.0.2-0.20200312104623-fa9799b0cf65 h1:zPiImkSeOsd+XF6ewOLYO/AnXM+P5AD6Mo8q0EDafIE=
github.com/mongodb/go-client-mongodb-ops-manager v0.0.2-0.20200312104623-fa9799b0cf65/go.mod h1:G9iLP4YeC2TRpxjZRddP6wKjz2JbpAtX+MlKriO7TOU=
github.com/mwielbut/pointy v1.1.0 h1:U5/YEfoIkaGCHv0St3CgjduqXID4FNRoyZgLM1kY9vg=
github.com/mwielbut/pointy v1.1.0/go.mod h1:MvvO+uMFj9T5DMda33HlvogsFBX7pWWKAkFIn4teYwY=
github.com/mwitkow/go-conntrack v0.0.0-20161129095857-cc309e4a2223/go.mod h1:qRWi+5nqEBWmkhHvq77mSJWrCKwh8bxhgT7d/eI7P4U=
Expand Down
33 changes: 20 additions & 13 deletions internal/cli/cloud_manager_clusters_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
package cli

import (
om "github.com/mongodb/go-client-mongodb-ops-manager/opsmngr"
"github.com/mongodb/mongocli/internal/config"
"github.com/mongodb/mongocli/internal/convert"
"github.com/mongodb/mongocli/internal/flags"
"github.com/mongodb/mongocli/internal/json"
Expand All @@ -23,36 +25,41 @@ import (
"github.com/spf13/cobra"
)

type cmClustersListOpts struct {
type cloudManagerClustersListOpts struct {
*globalOpts
store store.AutomationGetter
store store.CloudManagerClustersLister
}

func (opts *cmClustersListOpts) init() error {
if opts.ProjectID() == "" {
return errMissingProjectID
}

func (opts *cloudManagerClustersListOpts) init() error {
var err error
opts.store, err = store.New()
return err
}

func (opts *cmClustersListOpts) Run() error {
result, err := opts.store.GetAutomationConfig(opts.ProjectID())
func (opts *cloudManagerClustersListOpts) Run() error {

var result interface{}
var err error
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good use of a programatic error 👍


if opts.projectID == "" && config.Service() == config.OpsManagerService {
result, err = opts.store.ListAllClustersProjects()

} else {
var clusterConfigs *om.AutomationConfig
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[np] this could be a function but feel free to ignore unless you want to do other changes

clusterConfigs, err = opts.store.GetAutomationConfig(opts.ProjectID())
result = convert.FromAutomationConfig(clusterConfigs)
}

if err != nil {
return err
}

clusterConfigs := convert.FromAutomationConfig(result)

return json.PrettyPrint(clusterConfigs)
return json.PrettyPrint(result)
}

// mongocli cloud-manager cluster(s) list --projectId projectId
func CloudManagerClustersListBuilder() *cobra.Command {
opts := &cmClustersListOpts{
opts := &cloudManagerClustersListOpts{
globalOpts: newGlobalOpts(),
}
cmd := &cobra.Command{
Expand Down
57 changes: 42 additions & 15 deletions internal/cli/cloud_manager_clusters_list_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,32 +17,59 @@ package cli
import (
"testing"

"github.com/mongodb/mongocli/internal/config"

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

small thing, extra empty line, let's remove it and fmtimport this file

"github.com/golang/mock/gomock"
"github.com/mongodb/mongocli/internal/fixtures"
"github.com/mongodb/mongocli/internal/mocks"
)

func TestCloudManagerClustersList_Run(t *testing.T) {
ctrl := gomock.NewController(t)
mockStore := mocks.NewMockAutomationGetter(ctrl)
mockStore := mocks.NewMockCloudManagerClustersLister(ctrl)

defer ctrl.Finish()

expected := fixtures.AutomationConfig()
t.Run("ProjectID is given", func(t *testing.T) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

brilliant 💯

expected := fixtures.AutomationConfig()

listOpts := &cloudManagerClustersListOpts{
globalOpts: newGlobalOpts(),
store: mockStore,
}

listOpts.projectID = "1"
mockStore.
EXPECT().
GetAutomationConfig(listOpts.projectID).
Return(expected, nil).
Times(1)

err := listOpts.Run()
if err != nil {
t.Fatalf("Run() unexpected error: %v", err)
}

})

t.Run("No ProjectID is given", func(t *testing.T) {
expected := fixtures.AllClusters()
config.SetService(config.OpsManagerService)
mockStore.
EXPECT().
ListAllClustersProjects().
Return(expected, nil).
Times(1)

listOpts := &cmClustersListOpts{
globalOpts: newGlobalOpts(),
store: mockStore,
}
listOpts := &cloudManagerClustersListOpts{
globalOpts: newGlobalOpts(),
store: mockStore,
}

mockStore.
EXPECT().
GetAutomationConfig(listOpts.projectID).
Return(expected, nil).
Times(1)
err := listOpts.Run()
if err != nil {
t.Fatalf("Run() unexpected error: %v", err)
}
})

err := listOpts.Run()
if err != nil {
t.Fatalf("Run() unexpected error: %v", err)
}
}
112 changes: 112 additions & 0 deletions internal/fixtures/all_clusters.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
// Copyright 2020 MongoDB Inc
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package fixtures

import (
atlas "github.com/mongodb/go-client-mongodb-atlas/mongodbatlas"
om "github.com/mongodb/go-client-mongodb-ops-manager/opsmngr"
)

func AllClusters() *om.AllClustersProjects {
return &om.AllClustersProjects{
Links: []*atlas.Link{},
Results: []*om.AllClustersProject{
{
GroupName: "AtlasGroup1",
OrgName: "TestAtlasOrg1",
PlanType: "Atlas",
GroupID: "5e5fbc29e76c9a4be2ed3d39",
OrgID: "5e5fbc29e76c9a4be2ed3d36",
Clusters: []om.AllClustersCluster{
{
ClusterID: "5e5fbc29e76c9a4be2ed3d4d",
Name: "AtlasCluster1",
Type: "sharded cluster",
Availability: "unavailable",
Versions: []string{"3.4.2"},
BackupEnabled: true,
AuthEnabled: true,
SSLEnabled: true,
AlertCount: 0,
DataSizeBytes: 1000000,
NodeCount: 7,
},
{
ClusterID: "5e5fbc29e76c9a4be2ed3d4f",
Name: "AtlasReplSet1",
Type: "replica set",
Availability: "dead",
Versions: []string{"3.4.1"},
BackupEnabled: false,
AuthEnabled: true,
SSLEnabled: true,
AlertCount: 0,
DataSizeBytes: 1300000,
NodeCount: 2,
},
},
},
{
GroupName: "CloudGroup1",
OrgName: "TestCloudOrg1",
PlanType: "Cloud Manager",
GroupID: "5e5fbc29e76c9a4be2ed3d38",
OrgID: "5e5fbc29e76c9a4be2ed3d34",
Tags: []string{"some tag 1", "some tag 2"},
Clusters: []om.AllClustersCluster{
{
ClusterID: "5e5fbc29e76c9a4be2ed3d42",
Name: "cluster1",
Type: "sharded cluster",
Availability: "warning",
Versions: []string{"3.4.1", "2.4.3"},
BackupEnabled: true,
AuthEnabled: false,
SSLEnabled: false,
AlertCount: 0,
DataSizeBytes: 1000000,
NodeCount: 6,
},
{
ClusterID: "5e5fbc29e76c9a4be2ed3d3c",
Name: "replica_set",
Type: "replica set",
Availability: "available",
Versions: []string{"3.4.1"},
BackupEnabled: true,
AuthEnabled: true,
SSLEnabled: true,
AlertCount: 0,
DataSizeBytes: 500000,
NodeCount: 2,
},
{
ClusterID: "da303f3fec69b2100bacf10dd9e6d5e0",
Name: "standalone:27017",
Type: "standalone",
Availability: "unavailable",
Versions: []string{"2.4.3"},
BackupEnabled: false,
AuthEnabled: false,
SSLEnabled: true,
AlertCount: 0,
DataSizeBytes: 2000000,
NodeCount: 1,
},
},
},
},
TotalCount: 2,
}
}
49 changes: 49 additions & 0 deletions internal/mocks/mock_all_clusters_projects.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading