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 all 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
3 changes: 0 additions & 3 deletions e2e/atlas/helper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,6 @@ const (
configEntity = "settings"
dbusersEntity = "dbusers"
certsEntity = "certs"
iamEntity = "iam"
projectsEntity = "projects"
usersEntity = "users"
privateEndpointsEntity = "privateendpoints"
onlineArchiveEntity = "onlineArchives"
)
Expand Down
54 changes: 52 additions & 2 deletions e2e/atlas/users_test.go → e2e/iam/atlas_users_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@
// 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 e2e atlas,generic
// +build e2e iam

package atlas_test
package iam_test

import (
"encoding/json"
Expand All @@ -30,6 +30,8 @@ func TestUsers(t *testing.T) {
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
var username string
var userID string

t.Run("List", func(t *testing.T) {
cmd := exec.Command(cliPath,
Expand All @@ -49,8 +51,56 @@ func TestUsers(t *testing.T) {
if err := json.Unmarshal(resp, &users); err != nil {
t.Fatalf("unexpected error: %v", err)
}

if len(users) == 0 {
t.Fatalf("expected len(users) > 0, got %v", len(users))
}

username = users[0].Username
userID = users[0].ID
})

t.Run("Describe by username", func(t *testing.T) {
cmd := exec.Command(cliPath,
iamEntity,
usersEntity,
"describe",
"--username",
username,
"-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)
}
Comment on lines +80 to +82
Copy link
Collaborator

Choose a reason for hiding this comment

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

[nit] can you please check that the username matches the one you asked for?

})

t.Run("Describe by id", func(t *testing.T) {
cmd := exec.Command(cliPath,
iamEntity,
usersEntity,
"describe",
"--id",
userID,
"-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)
}
Comment on lines +102 to +104
Copy link
Collaborator

Choose a reason for hiding this comment

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

[nit] similar to above, right now we only check it doesn't error but we should also check the data, at least the id matches

})
}
2 changes: 2 additions & 0 deletions e2e/iam/helper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ const (
projectEntity = "projects"
apiKeysEntity = "apikeys"
apiKeyWhitelistEntity = "whitelist"
usersEntity = "users"
projectsEntity = "projects"
)

func createOrgAPIKey() (string, error) {
Expand Down
122 changes: 122 additions & 0 deletions internal/cli/iam/users/describe.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
// 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 EMAIL
{{.ID}} {{.FirstName}} {{.LastName}} {{.Username}} {{.EmailAddress}}
`

type DescribeOpts struct {
cli.OutputOpts
store store.UserDescriber
username string
id string
}

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'")
}

if opts.id != "" && opts.username != "" {
return errors.New("cannot supply both 'id' and 'username'")
}

return nil
}

type cmdOpt func() error

// PreRunE is a function to call before running the command,
// It calls any additional function pass as a callback
func PreRunE(cbs ...cmdOpt) error {
for _, f := range cbs {
if err := f(); err != nil {
return err
}
}
return nil
}

// mongocli iam user(s) describe --id id --username USERNAME
func DescribeBuilder() *cobra.Command {
opts := &DescribeOpts{}
cmd := &cobra.Command{
Use: "describe",
Aliases: []string{"get"},
Example: `
Describe a user by ID
$ mongocli iam users describe --id <id>

Describe a user by username
$ mongocli iam users describe --username <username>
`,
Short: describeIAMUser,
Args: cobra.NoArgs,
PreRunE: func(cmd *cobra.Command, args []string) error {
return PreRunE(
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
}
72 changes: 72 additions & 0 deletions internal/cli/iam/users/describe_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
// 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.NewMockUserDescriber(ctrl)
defer ctrl.Finish()

var expected mongodbatlas.AtlasUser

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

mockStore.
EXPECT().
UserByID(descOpts.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.NewMockUserDescriber(ctrl)
defer ctrl.Finish()

var expected mongodbatlas.AtlasUser

descOpts := &DescribeOpts{
store: mockStore,
username: "test",
}

mockStore.
EXPECT().
UserByName(descOpts.username).
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 username or id."
)
6 changes: 5 additions & 1 deletion internal/cli/iam/users/users.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ func Builder() *cobra.Command {
Aliases: cli.GenerateAliases(use),
}

cmd.AddCommand(InviteBuilder())
cmd.AddCommand(
InviteBuilder(),
DescribeBuilder(),
)

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.

Loading