Skip to content
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

CLOUDP-70010: iam users describe #384

Merged
merged 33 commits into from
Sep 1, 2020
Merged
Show file tree
Hide file tree
Changes from 25 commits
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
39071d4
add users list
robcarlan-mlab Aug 26, 2020
3ca5ef7
fix client usage
robcarlan-mlab Aug 26, 2020
d5bc60f
add IAM user describe
robcarlan-mlab Aug 26, 2020
aeb026e
Merge branch 'master' into CLOUDP-70013-iam-users-list
robcarlan-mlab Aug 26, 2020
f64a3ff
Merge branch 'master' into CLOUDP-700100-iam-users-describe
robcarlan-mlab Aug 26, 2020
92d1258
remove local development line
robcarlan-mlab Aug 26, 2020
c5a5c56
Merge branch 'CLOUDP-70013-iam-users-list' of github.com:mongodb/mong…
robcarlan-mlab Aug 26, 2020
b380370
Merge branch 'CLOUDP-70013-iam-users-list' into CLOUDP-700100-iam-use…
robcarlan-mlab Aug 26, 2020
f993e7e
update OM client version
robcarlan-mlab Aug 28, 2020
abfc5ba
undo isatty upgrade
robcarlan-mlab Aug 28, 2020
72ecba1
Merge branch 'master' into CLOUDP-70013-iam-users-list
robcarlan-mlab Aug 28, 2020
7190c46
add e2e
robcarlan-mlab Aug 28, 2020
b6c37fd
Merge branch 'CLOUDP-70013-iam-users-list' of github.com:mongodb/mong…
robcarlan-mlab Aug 28, 2020
3694cb4
add e2e
robcarlan-mlab Aug 28, 2020
2e4e2b4
add e2e
robcarlan-mlab Aug 28, 2020
a58320a
Merge branch 'master' into CLOUDP-70013-iam-users-list
robcarlan-mlab Aug 28, 2020
ba6eee3
preemptively fix e2e
robcarlan-mlab Aug 28, 2020
07a16ee
Merge branch 'CLOUDP-70013-iam-users-list' of github.com:mongodb/mong…
robcarlan-mlab Aug 28, 2020
abc1a80
Merge branch 'CLOUDP-70013-iam-users-list' into CLOUDP-700100-iam-use…
robcarlan-mlab Aug 28, 2020
e774b2b
e2e test fix
robcarlan-mlab Aug 31, 2020
0eb249f
Merge branch 'master' into CLOUDP-700100-iam-users-describe
robcarlan-mlab Aug 31, 2020
79369d6
Merge remote-tracking branch 'origin/master' into CLOUDP-700100-iam-u…
andreaangiolillo Sep 1, 2020
e683edb
Refactoring
andreaangiolillo Sep 1, 2020
26fb4fc
Refactoring
andreaangiolillo Sep 1, 2020
28f6c5b
Reverted some changes
andreaangiolillo Sep 1, 2020
ae34f97
Addressed PR comments
andreaangiolillo Sep 1, 2020
a3f9dd1
Update description.go
andreaangiolillo Sep 1, 2020
6b7ff63
Update helper_test.go
andreaangiolillo Sep 1, 2020
5260c88
Merge remote-tracking branch 'origin/master' into CLOUDP-700100-iam-u…
andreaangiolillo Sep 1, 2020
b046cd5
Update atlas_users_test.go
andreaangiolillo Sep 1, 2020
6a70373
fixed e2e tests
andreaangiolillo Sep 1, 2020
1f4f6c2
Addressed PR comments
andreaangiolillo Sep 1, 2020
b68929f
Update describe.go
andreaangiolillo Sep 1, 2020
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
21 changes: 21 additions & 0 deletions e2e/atlas/users_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,25 @@ func TestUsers(t *testing.T) {
t.Fatalf("expected len(users) > 0, got %v", len(users))
}
})

t.Run("Describe", func(t *testing.T) {
cmd := exec.Command(cliPath,
iamEntity,
usersEntity,
"describe",
"nonExistentUser",
andreaangiolillo marked this conversation as resolved.
Show resolved Hide resolved
"-o=json")
cmd.Env = os.Environ()
resp, err := cmd.CombinedOutput()

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

var user mongodbatlas.AtlasUser

if err := json.Unmarshal(resp, &user); err != nil {
t.Fatalf("unexpected error: %v", err)
}
})
}
102 changes: 102 additions & 0 deletions internal/cli/iam/users/describe.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
// 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 users

import (
"errors"

"github.com/mongodb/mongocli/internal/cli"
"github.com/mongodb/mongocli/internal/config"
"github.com/mongodb/mongocli/internal/flag"
"github.com/mongodb/mongocli/internal/store"
"github.com/mongodb/mongocli/internal/usage"
"github.com/spf13/cobra"
)

const describeTemplate = `ID FIRST NAME LAST NAME USERNAME
{{.ID}} {{.FirstName}} {{.LastName}} {{.Username}}
andreaangiolillo marked this conversation as resolved.
Show resolved Hide resolved
`

type DescribeOpts struct {
cli.GlobalOpts
andreaangiolillo marked this conversation as resolved.
Show resolved Hide resolved
cli.OutputOpts
store store.UsersDescriber
username string
ID string
andreaangiolillo marked this conversation as resolved.
Show resolved Hide resolved
}

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

func (opts *DescribeOpts) Run() error {
var r interface{}
var err error

if opts.username != "" {
r, err = opts.store.UserByName(opts.username)
}

if opts.ID != "" {
r, err = opts.store.UserByID(opts.ID)
}

if err != nil {
return err
}

return opts.Print(r)
}

func (opts *DescribeOpts) validate() error {
if opts.ID == "" && opts.username == "" {
return errors.New("must supply one of 'ID' or 'username'")
andreaangiolillo marked this conversation as resolved.
Show resolved Hide resolved
}

if opts.ID != "" && opts.username != "" {
return errors.New("cannot supply both 'ID' and 'username'")
andreaangiolillo marked this conversation as resolved.
Show resolved Hide resolved
}

return nil
}

// mongocli iam project(s) user(s) describe --id ID --username USERNAME
func DescribeBuilder() *cobra.Command {
opts := &DescribeOpts{}
cmd := &cobra.Command{
Use: "describe --id ID | describe --name username",
andreaangiolillo marked this conversation as resolved.
Show resolved Hide resolved
Aliases: []string{"get"},
Short: describeIAMUser,
PreRunE: func(cmd *cobra.Command, args []string) error {
return opts.PreRunE(
andreaangiolillo marked this conversation as resolved.
Show resolved Hide resolved
opts.init,
opts.InitOutput(cmd.OutOrStdout(), describeTemplate),
opts.validate,
)
},
RunE: func(cmd *cobra.Command, args []string) error {
return opts.Run()
},
}

cmd.Flags().StringVar(&opts.username, flag.Username, "", usage.Username)
cmd.Flags().StringVar(&opts.ID, flag.ID, "", usage.UserID)

cmd.Flags().StringVarP(&opts.Output, flag.Output, flag.OutputShort, "", usage.FormatOut)

return cmd
}
77 changes: 77 additions & 0 deletions internal/cli/iam/users/describe_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
// 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.

// +build unit

package users

import (
"testing"

"github.com/golang/mock/gomock"
"github.com/mongodb/mongocli/internal/mocks"
"go.mongodb.org/atlas/mongodbatlas"
)

func TestDescribe_Run_ByID(t *testing.T) {
ctrl := gomock.NewController(t)
mockStore := mocks.NewMockUsersDescriber(ctrl)
defer ctrl.Finish()

var expected mongodbatlas.AtlasUser

var id = "some_id"

descOpts := &DescribeOpts{
store: mockStore,
ID: id,
}

mockStore.
EXPECT().
UserByID(id).
Return(expected, nil).
Times(1)

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

func TestDescribe_Run_ByName(t *testing.T) {
ctrl := gomock.NewController(t)
mockStore := mocks.NewMockUsersDescriber(ctrl)
defer ctrl.Finish()

var expected mongodbatlas.AtlasUser

var name = "some_name"

descOpts := &DescribeOpts{
store: mockStore,
username: name,
}

mockStore.
EXPECT().
UserByName(name).
Return(expected, nil).
Times(1)

err := descOpts.Run()
if err != nil {
t.Fatalf("Run() unexpected error: %v", err)
}
}
7 changes: 4 additions & 3 deletions internal/cli/iam/users/description.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@
package users

const (
short = "Users operations."
long = "Create, list and manage your Atlas/Cloud Manager/Ops Manager users."
inviteUser = "Invite a user."
short = "Users operations."
long = "Create, list and manage your Atlas/Cloud Manager/Ops Manager users."
inviteUser = "Invite a user."
describeIAMUser = "Get a user by name or ID."
andreaangiolillo marked this conversation as resolved.
Show resolved Hide resolved
)
1 change: 1 addition & 0 deletions internal/cli/iam/users/users.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,6 @@ func Builder() *cobra.Command {
}

cmd.AddCommand(InviteBuilder())
cmd.AddCommand(DescribeBuilder())
andreaangiolillo marked this conversation as resolved.
Show resolved Hide resolved
return cmd
}
1 change: 1 addition & 0 deletions internal/flag/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ const (
DiskSizeGB = "diskSizeGB" // DiskSizeGB flag
MDBVersion = "mdbVersion" // MDBVersion flag
Backup = "backup" // Backup flag
ID = "id" // ID flag
Username = "username" // Username flag
UsernameShort = "u" // UsernameShort flag
Password = "password" // Password flag
Expand Down
55 changes: 54 additions & 1 deletion internal/mocks/mock_users.go

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

35 changes: 34 additions & 1 deletion internal/store/users.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,17 @@ import (
"go.mongodb.org/ops-manager/opsmngr"
)

//go:generate mockgen -destination=../mocks/mock_users.go -package=mocks github.com/mongodb/mongocli/internal/store UserCreator
//go:generate mockgen -destination=../mocks/mock_users.go -package=mocks github.com/mongodb/mongocli/internal/store UserCreator,UsersDescriber

type UserCreator interface {
CreateUser(*UserRequest) (interface{}, error)
}

type UsersDescriber interface {
andreaangiolillo marked this conversation as resolved.
Show resolved Hide resolved
UserByID(string) (interface{}, error)
UserByName(string) (interface{}, error)
}

type UserRequest struct {
*opsmngr.User
AtlasRoles []atlas.AtlasRole
Expand Down Expand Up @@ -59,3 +64,31 @@ func (s *Store) CreateUser(user *UserRequest) (interface{}, error) {
return nil, fmt.Errorf("unsupported service: %s", s.service)
}
}

// UserByID gets an IAM user by ID
func (s *Store) UserByID(userID string) (interface{}, error) {
switch s.service {
case config.CloudService:
result, _, err := s.client.(*atlas.Client).AtlasUsers.Get(context.Background(), userID)
return result, err
case config.OpsManagerService, config.CloudManagerService:
result, _, err := s.client.(*opsmngr.Client).Users.Get(context.Background(), userID)
return result, err
default:
return nil, fmt.Errorf("unsupported service: %s", s.service)
}
}

// UserByName gets an IAM user by name
func (s *Store) UserByName(username string) (interface{}, error) {
switch s.service {
case config.CloudService:
result, _, err := s.client.(*atlas.Client).AtlasUsers.GetByName(context.Background(), username)
return result, err
case config.OpsManagerService, config.CloudManagerService:
result, _, err := s.client.(*opsmngr.Client).Users.GetByName(context.Background(), username)
return result, err
default:
return nil, fmt.Errorf("unsupported service: %s", s.service)
}
}
1 change: 1 addition & 0 deletions internal/usage/usage.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ const (
Limit = "Number of items per page."
Username = "Username of the user."
DBUsername = "Username for authenticating to MongoDB."
UserID = "The ID of the user."
Password = "User’s password." //nolint:gosec // This is just a message not a password
Country = "The ISO 3166-1 alpha 2 country code of the user’s country of residence."
Mobile = "The user’s mobile or cell phone number."
Expand Down