Skip to content

Commit f58bfb3

Browse files
authored
CLOUDP-60789: mongocli atlas measurements databases list (#99)
1 parent e82a35d commit f58bfb3

9 files changed

+280
-0
lines changed

Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ gen-mocks: ## Generate mocks
6767
mockgen -source=internal/store/events.go -destination=internal/mocks/mock_events.go -package=mocks
6868
mockgen -source=internal/store/process_measurements.go -destination=internal/mocks/mock_process_measurements.go -package=mocks
6969
mockgen -source=internal/store/process_disks.go -destination=internal/mocks/mock_process_disks.go -package=mocks
70+
mockgen -source=internal/store/process_databases.go -destination=internal/mocks/mock_process_databases.go -package=mocks
7071
mockgen -source=internal/store/host_measurements.go -destination=internal/mocks/mock_host_measurements.go -package=mocks
7172
mockgen -source=internal/store/indexes.go -destination=internal/mocks/mock_indexes.go -package=mocks
7273

internal/cli/atlas_measurements.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ func AtlasMeasurementsBuilder() *cobra.Command {
4949
}
5050
cmd.AddCommand(AtlasMeasurementsProcessBuilder())
5151
cmd.AddCommand(AtlasMeasurementsDisksBuilder())
52+
cmd.AddCommand(AtlasMeasurementsDatabasesBuilder())
5253

5354
return cmd
5455
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// Copyright 2020 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 cli
16+
17+
import (
18+
"github.com/mongodb/mongocli/internal/description"
19+
"github.com/spf13/cobra"
20+
)
21+
22+
func AtlasMeasurementsDatabasesBuilder() *cobra.Command {
23+
cmd := &cobra.Command{
24+
Use: "databases",
25+
Aliases: []string{"database"},
26+
Short: description.Databases,
27+
}
28+
29+
cmd.AddCommand(AtlasMeasurementsDatabasesListBuilder())
30+
31+
return cmd
32+
}
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
// Copyright 2020 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 cli
16+
17+
import (
18+
"github.com/mongodb/mongocli/internal/description"
19+
"github.com/mongodb/mongocli/internal/flags"
20+
"github.com/mongodb/mongocli/internal/json"
21+
"github.com/mongodb/mongocli/internal/store"
22+
"github.com/mongodb/mongocli/internal/usage"
23+
"github.com/spf13/cobra"
24+
)
25+
26+
type atlasMeasurementsDatabasesListsOpts struct {
27+
globalOpts
28+
listOpts
29+
host string
30+
port int
31+
store store.ProcessDatabaseLister
32+
}
33+
34+
func (opts *atlasMeasurementsDatabasesListsOpts) init() error {
35+
if opts.ProjectID() == "" {
36+
return errMissingProjectID
37+
}
38+
39+
var err error
40+
opts.store, err = store.New()
41+
return err
42+
}
43+
44+
func (opts *atlasMeasurementsDatabasesListsOpts) Run() error {
45+
listOpts := opts.newListOptions()
46+
result, err := opts.store.ProcessDatabases(opts.ProjectID(), opts.host, opts.port, listOpts)
47+
48+
if err != nil {
49+
return err
50+
}
51+
52+
return json.PrettyPrint(result)
53+
}
54+
55+
// mongocli atlas measurements process(es) disks lists host:port
56+
func AtlasMeasurementsDatabasesListBuilder() *cobra.Command {
57+
opts := &atlasMeasurementsDatabasesListsOpts{}
58+
cmd := &cobra.Command{
59+
Use: "list",
60+
Short: description.ListDatabases,
61+
Aliases: []string{"ls"},
62+
Args: cobra.ExactArgs(1),
63+
PreRunE: func(cmd *cobra.Command, args []string) error {
64+
return opts.init()
65+
},
66+
RunE: func(cmd *cobra.Command, args []string) error {
67+
var err error
68+
if opts.host, opts.port, err = GetHostNameAndPort(args[0]); err != nil {
69+
return err
70+
}
71+
72+
return opts.Run()
73+
},
74+
}
75+
76+
cmd.Flags().IntVar(&opts.pageNum, flags.Page, 0, usage.Page)
77+
cmd.Flags().IntVar(&opts.itemsPerPage, flags.Limit, 0, usage.Limit)
78+
cmd.Flags().StringVar(&opts.projectID, flags.ProjectID, "", usage.ProjectID)
79+
80+
return cmd
81+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
// Copyright 2020 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+
package cli
15+
16+
import (
17+
"testing"
18+
19+
"github.com/golang/mock/gomock"
20+
"github.com/mongodb/mongocli/internal/fixtures"
21+
"github.com/mongodb/mongocli/internal/mocks"
22+
)
23+
24+
func TestAtlasMeasurementsDatabasesListsOpts_Run(t *testing.T) {
25+
ctrl := gomock.NewController(t)
26+
mockStore := mocks.NewMockProcessDatabaseLister(ctrl)
27+
28+
defer ctrl.Finish()
29+
30+
expected := fixtures.ProcessDatabases()
31+
32+
listOpts := &atlasMeasurementsDatabasesListsOpts{
33+
host: "hard-00-00.mongodb.net",
34+
port: 27017,
35+
store: mockStore,
36+
}
37+
38+
hostName, port, err := GetHostNameAndPort("hard-00-00.mongodb.net:27017")
39+
if err != nil {
40+
t.Fatalf("Run() unexpected error: %v", err)
41+
}
42+
43+
opts := listOpts.newListOptions()
44+
mockStore.
45+
EXPECT().ProcessDatabases(listOpts.projectID, hostName, port, opts).
46+
Return(expected, nil).
47+
Times(1)
48+
49+
err = listOpts.Run()
50+
if err != nil {
51+
t.Fatalf("Run() unexpected error: %v", err)
52+
}
53+
}

internal/description/description.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,8 @@ A user’s roles apply to all the clusters in the project.`
5656
ProcessMeasurements = "Get measurements for a given host."
5757
Disks = "List available disks or disks measurements for a given host."
5858
ListDisks = "List available disks for a given host."
59+
Databases = "List available databases or databases measurements for a given host."
60+
ListDatabases = "List available databases for a given host."
5961
Whitelist = "Manage the IP whitelist for a project."
6062
CreateWhitelist = "Create an IP whitelist for a project."
6163
DeleteWhitelist = "Delete a database user for a project."

internal/fixtures/measurements.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,29 @@ func ProcessDisks() *atlas.ProcessDisksResponse {
3939
}
4040
}
4141

42+
func ProcessDatabases() *atlas.ProcessDatabasesResponse {
43+
return &atlas.ProcessDatabasesResponse{
44+
Links: []*atlas.Link{
45+
{
46+
Rel: "self",
47+
Href: "https://cloud.mongodb.com/api/atlas/v1.0/groups/12345678/processes/shard-00-00.mongodb.net:27017/databases",
48+
},
49+
},
50+
Results: []*atlas.ProcessDatabase{
51+
{
52+
Links: []*atlas.Link{
53+
{
54+
Rel: "self",
55+
Href: "https://cloud.mongodb.com/api/atlas/v1.0/groups/12345678/processes/shard-00-00.mongodb.net:27017/databases/test",
56+
},
57+
},
58+
DatabaseName: "test",
59+
},
60+
},
61+
TotalCount: 1,
62+
}
63+
}
64+
4265
func ProcessMeasurements() *atlas.ProcessMeasurements {
4366
return &atlas.ProcessMeasurements{
4467
End: "2017-08-22T20:31:14Z",

internal/mocks/mock_process_databases.go

Lines changed: 49 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

internal/store/process_databases.go

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
// Copyright 2020 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 store
16+
17+
import (
18+
"context"
19+
"fmt"
20+
21+
atlas "github.com/mongodb/go-client-mongodb-atlas/mongodbatlas"
22+
"github.com/mongodb/mongocli/internal/config"
23+
)
24+
25+
type ProcessDatabaseLister interface {
26+
ProcessDatabases(string, string, int, *atlas.ListOptions) (*atlas.ProcessDatabasesResponse, error)
27+
}
28+
29+
// ProcessDatabases encapsulate the logic to manage different cloud providers
30+
func (s *Store) ProcessDatabases(groupID, host string, port int, opts *atlas.ListOptions) (*atlas.ProcessDatabasesResponse, error) {
31+
switch s.service {
32+
case config.CloudService:
33+
result, _, err := s.client.(*atlas.Client).ProcessDatabases.List(context.Background(), groupID, host, port, opts)
34+
return result, err
35+
default:
36+
return nil, fmt.Errorf("unsupported service: %s", s.service)
37+
}
38+
}

0 commit comments

Comments
 (0)