diff --git a/Makefile b/Makefile index 97f0943974..e10ccd147d 100644 --- a/Makefile +++ b/Makefile @@ -71,6 +71,7 @@ gen-mocks: ## Generate mocks mockgen -source=internal/store/process_databases.go -destination=internal/mocks/mock_process_databases.go -package=mocks mockgen -source=internal/store/host_measurements.go -destination=internal/mocks/mock_host_measurements.go -package=mocks mockgen -source=internal/store/indexes.go -destination=internal/mocks/mock_indexes.go -package=mocks + mockgen -source=internal/store/processes.go -destination=internal/mocks/mock_processes.go -package=mocks .PHONY: build build: ## Generate a binary in ./bin diff --git a/internal/cli/atlas.go b/internal/cli/atlas.go index 0cfc30f41d..a8ccb50928 100644 --- a/internal/cli/atlas.go +++ b/internal/cli/atlas.go @@ -31,6 +31,8 @@ func AtlasBuilder() *cobra.Command { cmd.AddCommand(AtlasBackupsBuilder()) cmd.AddCommand(AtlasEventsBuilder()) cmd.AddCommand(AtlasMeasurementsBuilder()) + // cmd.AddCommand(AtlasLogsBuilder()) + cmd.AddCommand(AtlasProcessesBuilder()) return cmd } diff --git a/internal/cli/atlas_processes.go b/internal/cli/atlas_processes.go new file mode 100644 index 0000000000..41baf5e44d --- /dev/null +++ b/internal/cli/atlas_processes.go @@ -0,0 +1,32 @@ +// 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 cli + +import ( + "github.com/mongodb/mongocli/internal/description" + "github.com/spf13/cobra" +) + +func AtlasProcessesBuilder() *cobra.Command { + cmd := &cobra.Command{ + Use: "processes", + Aliases: []string{"process"}, + Short: description.Clusters, + Hidden: true, + } + cmd.AddCommand(AtlasProcessListBuilder()) + + return cmd +} diff --git a/internal/cli/atlas_processes_list.go b/internal/cli/atlas_processes_list.go new file mode 100644 index 0000000000..ede99a7208 --- /dev/null +++ b/internal/cli/atlas_processes_list.go @@ -0,0 +1,76 @@ +// 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 cli + +import ( + "github.com/mongodb/mongocli/internal/description" + "github.com/mongodb/mongocli/internal/flags" + "github.com/mongodb/mongocli/internal/json" + "github.com/mongodb/mongocli/internal/store" + "github.com/mongodb/mongocli/internal/usage" + "github.com/spf13/cobra" +) + +type atlasProcessesListOpts struct { + globalOpts + listOpts + store store.ProcessLister +} + +func (opts *atlasProcessesListOpts) init() error { + if opts.ProjectID() == "" { + return errMissingProjectID + } + + var err error + opts.store, err = store.New() + return err +} + +func (opts *atlasProcessesListOpts) Run() error { + listOpts := opts.newListOptions() + result, err := opts.store.Processes(opts.ProjectID(), listOpts) + + if err != nil { + return err + } + + return json.PrettyPrint(result) +} + +// mongocli atlas process(es) list --projectId projectId [--page N] [--limit N] +func AtlasProcessListBuilder() *cobra.Command { + opts := &atlasProcessesListOpts{} + cmd := &cobra.Command{ + Use: "list", + Short: description.ListProcesses, + Aliases: []string{"ls"}, + Hidden: true, + Args: cobra.NoArgs, + PreRunE: func(cmd *cobra.Command, args []string) error { + return opts.init() + }, + RunE: func(cmd *cobra.Command, args []string) error { + return opts.Run() + }, + } + + cmd.Flags().IntVar(&opts.pageNum, flags.Page, 0, usage.Page) + cmd.Flags().IntVar(&opts.itemsPerPage, flags.Limit, 0, usage.Limit) + + cmd.Flags().StringVar(&opts.projectID, flags.ProjectID, "", usage.ProjectID) + + return cmd +} diff --git a/internal/cli/atlas_processes_list_test.go b/internal/cli/atlas_processes_list_test.go new file mode 100644 index 0000000000..3afdbaab85 --- /dev/null +++ b/internal/cli/atlas_processes_list_test.go @@ -0,0 +1,47 @@ +// 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 cli + +import ( + "testing" + + "github.com/golang/mock/gomock" + "github.com/mongodb/mongocli/internal/fixtures" + "github.com/mongodb/mongocli/internal/mocks" +) + +func TestAtlasProcessesList_Run(t *testing.T) { + ctrl := gomock.NewController(t) + mockStore := mocks.NewMockProcessLister(ctrl) + + defer ctrl.Finish() + + expected := fixtures.Processes() + + listOpts := &atlasProcessesListOpts{ + store: mockStore, + } + + mockStore. + EXPECT(). + Processes(listOpts.projectID, listOpts.newListOptions()). + Return(expected, nil). + Times(1) + + err := listOpts.Run() + if err != nil { + t.Fatalf("Run() unexpected error: %v", err) + } +} diff --git a/internal/description/description.go b/internal/description/description.go index 39e5005609..0a5763764e 100644 --- a/internal/description/description.go +++ b/internal/description/description.go @@ -35,6 +35,7 @@ const ( StartRestore = "Start a restore job." Snapshots = "Manage continuous snapshots for your project." ListSnapshots = "List continuous snapshots for a project." + Logs = "Download host logs." Clusters = "Manage clusters for your project." ClustersLong = "The clusters command provides access to your cluster configurations. You can create, edit, and delete clusters." CreateCluster = "Create a MongoDB cluster." @@ -43,6 +44,8 @@ const ( DescribeCluster = "Describe a cluster." ListClusters = "List clusters for a project." UpdateCluster = "Update a MongoDB cluster in Atlas." + Processes = "Manage processes for your project." + ListProcesses = "List processes for a project." DBUsers = "Manage database users for your project." DBUsersLong = ` The dbusers command retrieves, creates and modifies the MongoDB database users in your cluster. diff --git a/internal/fixtures/processes.go b/internal/fixtures/processes.go new file mode 100644 index 0000000000..183afc60a0 --- /dev/null +++ b/internal/fixtures/processes.go @@ -0,0 +1,23 @@ +// 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" +) + +func Processes() []*atlas.Process { + return []*atlas.Process{} +} diff --git a/internal/mocks/mock_processes.go b/internal/mocks/mock_processes.go new file mode 100644 index 0000000000..b86f68b06c --- /dev/null +++ b/internal/mocks/mock_processes.go @@ -0,0 +1,49 @@ +// Code generated by MockGen. DO NOT EDIT. +// Source: internal/store/processes.go + +// Package mocks is a generated GoMock package. +package mocks + +import ( + gomock "github.com/golang/mock/gomock" + mongodbatlas "github.com/mongodb/go-client-mongodb-atlas/mongodbatlas" + reflect "reflect" +) + +// MockProcessLister is a mock of ProcessLister interface +type MockProcessLister struct { + ctrl *gomock.Controller + recorder *MockProcessListerMockRecorder +} + +// MockProcessListerMockRecorder is the mock recorder for MockProcessLister +type MockProcessListerMockRecorder struct { + mock *MockProcessLister +} + +// NewMockProcessLister creates a new mock instance +func NewMockProcessLister(ctrl *gomock.Controller) *MockProcessLister { + mock := &MockProcessLister{ctrl: ctrl} + mock.recorder = &MockProcessListerMockRecorder{mock} + return mock +} + +// EXPECT returns an object that allows the caller to indicate expected use +func (m *MockProcessLister) EXPECT() *MockProcessListerMockRecorder { + return m.recorder +} + +// Processes mocks base method +func (m *MockProcessLister) Processes(arg0 string, arg1 *mongodbatlas.ListOptions) ([]*mongodbatlas.Process, error) { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "Processes", arg0, arg1) + ret0, _ := ret[0].([]*mongodbatlas.Process) + ret1, _ := ret[1].(error) + return ret0, ret1 +} + +// Processes indicates an expected call of Processes +func (mr *MockProcessListerMockRecorder) Processes(arg0, arg1 interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Processes", reflect.TypeOf((*MockProcessLister)(nil).Processes), arg0, arg1) +} diff --git a/internal/store/processes.go b/internal/store/processes.go new file mode 100644 index 0000000000..34c6deb464 --- /dev/null +++ b/internal/store/processes.go @@ -0,0 +1,38 @@ +// 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 store + +import ( + "context" + "fmt" + + atlas "github.com/mongodb/go-client-mongodb-atlas/mongodbatlas" + "github.com/mongodb/mongocli/internal/config" +) + +type ProcessLister interface { + Processes(string, *atlas.ListOptions) ([]*atlas.Process, error) +} + +// Processes encapsulate the logic to manage different cloud providers +func (s *Store) Processes(groupID string, opts *atlas.ListOptions) ([]*atlas.Process, error) { + switch s.service { + case config.CloudService: + result, _, err := s.client.(*atlas.Client).Processes.List(context.Background(), groupID, opts) + return result, err + default: + return nil, fmt.Errorf("unsupported service: %s", s.service) + } +}