Skip to content

Add a command to list processes #101

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 1 commit into from
Apr 15, 2020
Merged
Show file tree
Hide file tree
Changes from all 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
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions internal/cli/atlas.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ func AtlasBuilder() *cobra.Command {
cmd.AddCommand(AtlasBackupsBuilder())
cmd.AddCommand(AtlasEventsBuilder())
cmd.AddCommand(AtlasMeasurementsBuilder())
// cmd.AddCommand(AtlasLogsBuilder())
Copy link
Collaborator

Choose a reason for hiding this comment

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

[np] Can be removed now? Not blocking

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

That's what I was working on and could not use as I needed the new format, I'll merge away as the next PR will remove the comments

cmd.AddCommand(AtlasProcessesBuilder())

return cmd
}
32 changes: 32 additions & 0 deletions internal/cli/atlas_processes.go
Original file line number Diff line number Diff line change
@@ -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
}
76 changes: 76 additions & 0 deletions internal/cli/atlas_processes_list.go
Original file line number Diff line number Diff line change
@@ -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
}
47 changes: 47 additions & 0 deletions internal/cli/atlas_processes_list_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
}
3 changes: 3 additions & 0 deletions internal/description/description.go
Original file line number Diff line number Diff line change
Expand Up @@ -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."
Expand All @@ -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."
Copy link
Contributor

Choose a reason for hiding this comment

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

Should the language here always refer to a project?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

This format was done as a favour from the docs team, I'll ask them to give this list of descriptions another go

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.
Expand Down
23 changes: 23 additions & 0 deletions internal/fixtures/processes.go
Original file line number Diff line number Diff line change
@@ -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{}
}
49 changes: 49 additions & 0 deletions internal/mocks/mock_processes.go

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

38 changes: 38 additions & 0 deletions internal/store/processes.go
Original file line number Diff line number Diff line change
@@ -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)
}
}