Skip to content

Commit

Permalink
CLOUDP-57840: List db users, C/OM (#55)
Browse files Browse the repository at this point in the history
  • Loading branch information
gssbzn authored Mar 19, 2020
1 parent ec5aa64 commit 737e9e6
Show file tree
Hide file tree
Showing 4 changed files with 123 additions and 1 deletion.
1 change: 1 addition & 0 deletions internal/cli/ops_manager_dbusers.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ A user’s roles apply to all the clusters in the project.`,
}

cmd.AddCommand(OpsManagerDBUsersCreateBuilder())
cmd.AddCommand(OpsManagerDBUsersListBuilder())

return cmd
}
4 changes: 3 additions & 1 deletion internal/cli/ops_manager_dbusers_create.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ import (
"github.com/spf13/cobra"
)

const scransha1 = "SCRAM-SHA-1"

type opsManagerDBUsersCreateOpts struct {
*globalOpts
username string
Expand Down Expand Up @@ -112,7 +114,7 @@ func OpsManagerDBUsersCreateBuilder() *cobra.Command {
cmd.Flags().StringVar(&opts.password, flags.Password, "", usage.Password)
cmd.Flags().StringVar(&opts.authDB, flags.AuthDB, convert.AdminDB, usage.AuthDB)
cmd.Flags().StringSliceVar(&opts.roles, flags.Role, []string{}, usage.Roles)
cmd.Flags().StringSliceVar(&opts.mechanisms, flags.Mechanisms, []string{"SCRAM-SHA-1"}, usage.Mechanisms)
cmd.Flags().StringSliceVar(&opts.mechanisms, flags.Mechanisms, []string{scransha1}, usage.Mechanisms)

cmd.Flags().StringVar(&opts.projectID, flags.ProjectID, "", usage.ProjectID)

Expand Down
71 changes: 71 additions & 0 deletions internal/cli/ops_manager_dbusers_list.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
// 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/flags"
"github.com/mongodb/mongocli/internal/json"
"github.com/mongodb/mongocli/internal/store"
"github.com/mongodb/mongocli/internal/usage"
"github.com/spf13/cobra"
)

type opsManagerDBUsersListOpts struct {
*globalOpts
store store.AutomationGetter
}

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

var err error
opts.store, err = store.New()
return err
}

func (opts *opsManagerDBUsersListOpts) Run() error {
current, err := opts.store.GetAutomationConfig(opts.ProjectID())

if err != nil {
return err
}

return json.PrettyPrint(current.Auth.Users)
}

// mongocli om|cm dbuser(s) list [--projectId projectId]
func OpsManagerDBUsersListBuilder() *cobra.Command {
opts := &opsManagerDBUsersListOpts{
globalOpts: newGlobalOpts(),
}
cmd := &cobra.Command{
Use: "list",
Short: "List database users for a project.",
Aliases: []string{"ls"},
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().StringVar(&opts.projectID, flags.ProjectID, "", usage.ProjectID)

return cmd
}
48 changes: 48 additions & 0 deletions internal/cli/ops_manager_dbusers_list_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// 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 TestOpsManagerDBUserList_Run(t *testing.T) {
ctrl := gomock.NewController(t)
mockStore := mocks.NewMockAutomationPatcher(ctrl)

defer ctrl.Finish()

expected := fixtures.AutomationConfig()

createOpts := &opsManagerDBUsersListOpts{
globalOpts: newGlobalOpts(),
store: mockStore,
}

mockStore.
EXPECT().
GetAutomationConfig(createOpts.projectID).
Return(expected, nil).
Times(1)

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

0 comments on commit 737e9e6

Please sign in to comment.