Skip to content

Commit

Permalink
CLOUDP-69181: mongocli atlas networking peering delete <peerId> (#365)
Browse files Browse the repository at this point in the history
  • Loading branch information
andreaangiolillo authored Aug 17, 2020
1 parent 38a05e2 commit e0a9d23
Show file tree
Hide file tree
Showing 6 changed files with 176 additions and 4 deletions.
69 changes: 69 additions & 0 deletions internal/cli/atlas/networking/peering/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 peering

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

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

func (opts *DeleteOpts) Run() error {
return opts.Delete(opts.store.DeletePeeringConnection, opts.ConfigProjectID())
}

// mongocli atlas networking peering delete <ID> --force [--projectId projectId]
func DeleteBuilder() *cobra.Command {
opts := &DeleteOpts{
DeleteOpts: cli.NewDeleteOpts("Peering connection '%s' deleted\n", "Peering connection not deleted"),
}
cmd := &cobra.Command{
Use: "delete <ID>",
Aliases: []string{"rm"},
Short: deletePeeringConnection,
Args: cobra.ExactArgs(1),
PreRunE: func(cmd *cobra.Command, args []string) error {
if err := opts.PreRunE(opts.initStore); err != nil {
return err
}
opts.Entry = args[0]
return 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.ProjectID, flag.ProjectID, "", usage.ProjectID)

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

import (
"testing"

"github.com/golang/mock/gomock"
"github.com/mongodb/mongocli/internal/cli"
"github.com/mongodb/mongocli/internal/mocks"
"github.com/stretchr/testify/assert"
)

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

deleteOpts := &DeleteOpts{
DeleteOpts: &cli.DeleteOpts{
Entry: "to_delete",
Confirm: true,
},
store: mockStore,
}

mockStore.
EXPECT().
DeletePeeringConnection(deleteOpts.ProjectID, deleteOpts.Entry).
Return(nil).
Times(1)

err := deleteOpts.Run()
assert.NoError(t, err)
}
5 changes: 3 additions & 2 deletions internal/cli/atlas/networking/peering/descriptions.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package peering

const (
peering = "Manage Network Peering"
listPeering = "Retrieve details for all network peering connections in one Atlas project"
peering = "Manage Network Peering"
listPeering = "Retrieve details for all network peering connections in one Atlas project"
deletePeeringConnection = "Delete a peering connection from an Atlas project"
)
1 change: 1 addition & 0 deletions internal/cli/atlas/networking/peering/peering.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ func Builder() *cobra.Command {
}

cmd.AddCommand(ListBuilder())
cmd.AddCommand(DeleteBuilder())

return cmd
}

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

17 changes: 16 additions & 1 deletion internal/store/peering_connections.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,16 @@ import (
atlas "go.mongodb.org/atlas/mongodbatlas"
)

//go:generate mockgen -destination=../mocks/mock_peeringConnections.go -package=mocks github.com/mongodb/mongocli/internal/store PeeringConnectionLister
//go:generate mockgen -destination=../mocks/mock_peering_connections.go -package=mocks github.com/mongodb/mongocli/internal/store PeeringConnectionLister,PeeringConnectionDeleter

type PeeringConnectionLister interface {
PeeringConnections(string, *atlas.ListOptions) ([]atlas.Peer, error)
}

type PeeringConnectionDeleter interface {
DeletePeeringConnection(string, string) error
}

// PeeringConnections encapsulates the logic to manage different cloud providers
func (s *Store) PeeringConnections(projectID string, opts *atlas.ListOptions) ([]atlas.Peer, error) {
switch s.service {
Expand All @@ -38,3 +42,14 @@ func (s *Store) PeeringConnections(projectID string, opts *atlas.ListOptions) ([
return nil, fmt.Errorf("unsupported service: %s", s.service)
}
}

// DeletePrivateEndpoint encapsulates the logic to manage different cloud providers
func (s *Store) DeletePeeringConnection(projectID, peerID string) error {
switch s.service {
case config.CloudService:
_, err := s.client.(*atlas.Client).Peers.Delete(context.Background(), projectID, peerID)
return err
default:
return fmt.Errorf("unsupported service: %s", s.service)
}
}

0 comments on commit e0a9d23

Please sign in to comment.