Skip to content

Commit

Permalink
CLOUDP-70019: mongocli iam team(s) delete (#397)
Browse files Browse the repository at this point in the history
  • Loading branch information
andreaangiolillo authored Sep 2, 2020
1 parent 069f5be commit 07bdc80
Show file tree
Hide file tree
Showing 6 changed files with 181 additions and 2 deletions.
69 changes: 69 additions & 0 deletions internal/cli/iam/teams/delete.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
// 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 teams

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.TeamDeleter
}

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.DeleteTeam, opts.ConfigOrgID())
}

// mongocli iam team(s) delete <ID> [--force] [--orgId orgId]
func DeleteBuilder() *cobra.Command {
opts := &DeleteOpts{
DeleteOpts: cli.NewDeleteOpts("Team '%s' deleted\n", "Team not deleted"),
}
cmd := &cobra.Command{
Use: "delete <TeamID>",
Aliases: []string{"rm"},
Short: deleteTeam,
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().BoolVar(&opts.Confirm, flag.Force, false, usage.Force)

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

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

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.NewMockTeamDeleter(ctrl)
defer ctrl.Finish()

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

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

err := opts.Run()
if err != nil {
t.Fatalf("Run() unexpected error: %v", err)
}
}
1 change: 1 addition & 0 deletions internal/cli/iam/teams/description.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,5 @@ const (
listTeams = "Get all teams in an organization."
describeTeam = "Get a team in an organization."
createTeam = "Create a team in an organization."
deleteTeam = "Delete a team from an organization."
)
1 change: 1 addition & 0 deletions internal/cli/iam/teams/teams.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ func Builder() *cobra.Command {
DescribeBuilder(),
CreateBuilder(),
users.Builder(),
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
//go:generate mockgen -destination=../mocks/mock_teams.go -package=mocks github.com/mongodb/mongocli/internal/store TeamLister,TeamDescriber,TeamCreator,TeamDeleter

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

type TeamDeleter interface {
DeleteTeam(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 @@ -92,3 +96,17 @@ func (s *Store) CreateTeam(orgID string, team *atlas.Team) (*atlas.Team, error)
return nil, fmt.Errorf("unsupported service: %s", s.service)
}
}

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

0 comments on commit 07bdc80

Please sign in to comment.