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-70022: mongocli iam team(s) user(s) delete #401

Merged
merged 7 commits into from
Sep 2, 2020
Merged
Show file tree
Hide file tree
Changes from 6 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
73 changes: 73 additions & 0 deletions internal/cli/iam/teams/users/delete.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
// 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 (
"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"
)

type DeleteOpts struct {
cli.GlobalOpts
*cli.DeleteOpts
store store.TeamUserRemover
teamID string
}

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

func (opts *DeleteOpts) Run() error {
return opts.Delete(opts.store.RemoveUserFromTeam, opts.ConfigOrgID(), opts.teamID)
}

// mongocli iam team(s) users(s) delete <ID> [--force] --orgId orgId
func DeleteBuilder() *cobra.Command {
opts := &DeleteOpts{
DeleteOpts: cli.NewDeleteOpts("User '%s' deleted from the team\n", "User not deleted"),
}
cmd := &cobra.Command{
Use: "delete <UserID>",
Aliases: []string{"rm"},
Short: deleteUser,
Args: cobra.ExactArgs(1),
PreRunE: func(cmd *cobra.Command, args []string) error {
opts.Entry = args[0]
return opts.PreRunEOrg(
opts.init,
opts.Prompt,
)
},
RunE: func(cmd *cobra.Command, args []string) error {
return opts.Run()
},
}

cmd.Flags().StringVar(&opts.teamID, flag.TeamID, "", usage.TeamID)

cmd.Flags().BoolVar(&opts.Confirm, flag.Force, false, usage.Force)
cmd.Flags().StringVar(&opts.OrgID, flag.OrgID, "", usage.OrgID)

_ = cmd.MarkFlagRequired(flag.TeamID)

return cmd
}
54 changes: 54 additions & 0 deletions internal/cli/iam/teams/users/delete_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
// 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/cli"
"github.com/mongodb/mongocli/internal/mocks"
)

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

opts := &DeleteOpts{
store: mockStore,
DeleteOpts: &cli.DeleteOpts{
Entry: "5a0a1e7e0f2912c554080adc",
Confirm: true,
},
teamID: "213123",
GlobalOpts: cli.GlobalOpts{
OrgID: "2a0a1e7e0f2912c554080adc",
},
}

mockStore.
EXPECT().
RemoveUserFromTeam(opts.OrgID, opts.teamID, opts.Entry).
Return(nil).
Times(1)

err := opts.Run()
if err != nil {
t.Fatalf("Run() unexpected error: %v", err)
}
}
7 changes: 4 additions & 3 deletions internal/cli/iam/teams/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 users."
listUsers = "List users of a team."
short = "Users operations."
long = "Create, list and manage your users."
listUsers = "List users of a team."
deleteUser = "Delete user from a team."
)
1 change: 1 addition & 0 deletions internal/cli/iam/teams/users/users.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ func Builder() *cobra.Command {
cmd.AddCommand(
ListBuilder(),
AddBuilder(),
DeleteBuilder(),
)

return cmd
Expand Down
39 changes: 38 additions & 1 deletion internal/mocks/mock_teams.go

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

20 changes: 19 additions & 1 deletion internal/store/teams.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import (
"go.mongodb.org/ops-manager/opsmngr"
)

//go:generate mockgen -destination=../mocks/mock_teams.go -package=mocks github.com/mongodb/mongocli/internal/store TeamLister,TeamDescriber,TeamCreator,TeamDeleter,TeamAdder
//go:generate mockgen -destination=../mocks/mock_teams.go -package=mocks github.com/mongodb/mongocli/internal/store TeamLister,TeamDescriber,TeamCreator,TeamDeleter,TeamAdder,TeamUserRemover

type TeamLister interface {
Teams(string, *atlas.ListOptions) ([]atlas.Team, error)
Expand All @@ -46,6 +46,10 @@ type TeamAdder interface {
AddUsersToTeam(string, string, []string) (interface{}, error)
}

type TeamUserRemover interface {
RemoveUserFromTeam(string, string, string) error
}

// TeamByID encapsulates the logic to manage different cloud providers
func (s *Store) TeamByID(orgID, teamID string) (*atlas.Team, error) {
switch s.service {
Expand Down Expand Up @@ -129,3 +133,17 @@ func (s *Store) AddUsersToTeam(orgID, teamID string, users []string) (interface{
return nil, fmt.Errorf("unsupported service: %s", s.service)
}
}

// RemoveUserFromTeam encapsulates the logic to manage different cloud providers
func (s *Store) RemoveUserFromTeam(orgID, teamID, userID string) error {
switch s.service {
case config.CloudService:
_, err := s.client.(*atlas.Client).Teams.RemoveUserToTeam(context.Background(), orgID, teamID, userID)
return err
case config.OpsManagerService:
_, err := s.client.(*opsmngr.Client).Teams.RemoveUserToTeam(context.Background(), orgID, teamID, userID)
return err
default:
return fmt.Errorf("unsupported service: %s", s.service)
}
}