-
Notifications
You must be signed in to change notification settings - Fork 70
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
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 3ca5ef7
fix client usage
robcarlan-mlab d5bc60f
add IAM user describe
robcarlan-mlab aeb026e
Merge branch 'master' into CLOUDP-70013-iam-users-list
robcarlan-mlab f64a3ff
Merge branch 'master' into CLOUDP-700100-iam-users-describe
robcarlan-mlab 92d1258
remove local development line
robcarlan-mlab c5a5c56
Merge branch 'CLOUDP-70013-iam-users-list' of github.com:mongodb/mong…
robcarlan-mlab b380370
Merge branch 'CLOUDP-70013-iam-users-list' into CLOUDP-700100-iam-use…
robcarlan-mlab f993e7e
update OM client version
robcarlan-mlab abfc5ba
undo isatty upgrade
robcarlan-mlab 72ecba1
Merge branch 'master' into CLOUDP-70013-iam-users-list
robcarlan-mlab 7190c46
add e2e
robcarlan-mlab b6c37fd
Merge branch 'CLOUDP-70013-iam-users-list' of github.com:mongodb/mong…
robcarlan-mlab 3694cb4
add e2e
robcarlan-mlab 2e4e2b4
add e2e
robcarlan-mlab a58320a
Merge branch 'master' into CLOUDP-70013-iam-users-list
robcarlan-mlab ba6eee3
preemptively fix e2e
robcarlan-mlab 07a16ee
Merge branch 'CLOUDP-70013-iam-users-list' of github.com:mongodb/mong…
robcarlan-mlab abc1a80
Merge branch 'CLOUDP-70013-iam-users-list' into CLOUDP-700100-iam-use…
robcarlan-mlab e774b2b
e2e test fix
robcarlan-mlab 0eb249f
Merge branch 'master' into CLOUDP-700100-iam-users-describe
robcarlan-mlab 79369d6
Merge remote-tracking branch 'origin/master' into CLOUDP-700100-iam-u…
andreaangiolillo e683edb
Refactoring
andreaangiolillo 26fb4fc
Refactoring
andreaangiolillo 28f6c5b
Reverted some changes
andreaangiolillo ae34f97
Addressed PR comments
andreaangiolillo a3f9dd1
Update description.go
andreaangiolillo 6b7ff63
Update helper_test.go
andreaangiolillo 5260c88
Merge remote-tracking branch 'origin/master' into CLOUDP-700100-iam-u…
andreaangiolillo b046cd5
Update atlas_users_test.go
andreaangiolillo 6a70373
fixed e2e tests
andreaangiolillo 1f4f6c2
Addressed PR comments
andreaangiolillo b68929f
Update describe.go
andreaangiolillo File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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" | ||
|
@@ -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, | ||
|
@@ -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) | ||
} | ||
}) | ||
|
||
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
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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?