Skip to content

Commit

Permalink
CLOUDP-69463: mongocli atlas networking peering watch (#372)
Browse files Browse the repository at this point in the history
  • Loading branch information
gssbzn authored Aug 18, 2020
1 parent 53f9f10 commit d8e4149
Show file tree
Hide file tree
Showing 7 changed files with 186 additions and 5 deletions.
5 changes: 2 additions & 3 deletions internal/cli/atlas/networking/peering/create/aws_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
package create

import (
"reflect"
"testing"

"github.com/golang/mock/gomock"
Expand Down Expand Up @@ -97,8 +96,8 @@ func TestNormalizeAtlasRegion(t *testing.T) {

for _, tc := range tests {
got := normalizeAtlasRegion(tc.input)
if !reflect.DeepEqual(tc.want, got) {
t.Fatalf("expected: %v, got: %v", tc.want, got)
if tc.want != got {
t.Errorf("expected: %s, got: %s", tc.want, got)
}
}
}
1 change: 1 addition & 0 deletions internal/cli/atlas/networking/peering/descriptions.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,5 @@ const (
peering = "Manage Network Peering connections"
listPeering = "Retrieve details for all network peering connections in one Atlas project"
deletePeeringConnection = "Delete a peering connection from an Atlas project"
watchPeering = "Watch for a peering connection to be available"
)
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 @@ -28,6 +28,7 @@ func Builder() *cobra.Command {

cmd.AddCommand(ListBuilder())
cmd.AddCommand(create.Builder())
cmd.AddCommand(WatchBuilder())
cmd.AddCommand(DeleteBuilder())

return cmd
Expand Down
78 changes: 78 additions & 0 deletions internal/cli/atlas/networking/peering/watch.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
// 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 (
"fmt"
"time"

"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 WatchOpts struct {
cli.GlobalOpts
id string
store store.PeeringConnectionDescriber
}

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

const defaultWait = 4 * time.Second

func (opts *WatchOpts) Run() error {
for {
result, err := opts.store.PeeringConnection(opts.ConfigProjectID(), opts.id)
if err != nil {
return err
}
if result.Status == "WAITING_FOR_USER" || result.Status == "FAILED" || result.Status == "AVAILABLE" {
fmt.Printf("\nNetwork peering changes completed.\n")
break
}
fmt.Print(".")
time.Sleep(defaultWait)
}

return nil
}

// mongocli atlas networking peering watch <ID> [--projectId projectId]
func WatchBuilder() *cobra.Command {
opts := &WatchOpts{}
cmd := &cobra.Command{
Use: "watch <ID>",
Short: watchPeering,
Args: cobra.ExactArgs(1),
PreRunE: func(cmd *cobra.Command, args []string) error {
return opts.PreRunE(opts.initStore)
},
RunE: func(cmd *cobra.Command, args []string) error {
opts.id = args[0]
return opts.Run()
},
}

cmd.Flags().StringVar(&opts.ProjectID, flag.ProjectID, "", usage.ProjectID)
return cmd
}
49 changes: 49 additions & 0 deletions internal/cli/atlas/networking/peering/watch_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/mocks"
"go.mongodb.org/atlas/mongodbatlas"
)

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

describeOpts := &WatchOpts{
id: "test",
store: mockStore,
}

expected := &mongodbatlas.Peer{Status: "WAITING_FOR_USER"}

mockStore.
EXPECT().
PeeringConnection(describeOpts.ProjectID, describeOpts.id).
Return(expected, nil).
Times(1)

err := describeOpts.Run()
if err != nil {
t.Fatalf("Run() unexpected error: %v", err)
}
}
40 changes: 39 additions & 1 deletion internal/mocks/mock_peering_connections.go

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_peering_connections.go -package=mocks github.com/mongodb/mongocli/internal/store PeeringConnectionLister,PeeringConnectionDeleter,AzurePeeringConnectionCreator,AWSPeeringConnectionCreator,GCPPeeringConnectionCreator,PeeringConnectionCreator
//go:generate mockgen -destination=../mocks/mock_peering_connections.go -package=mocks github.com/mongodb/mongocli/internal/store PeeringConnectionLister,PeeringConnectionDescriber,PeeringConnectionDeleter,AzurePeeringConnectionCreator,AWSPeeringConnectionCreator,GCPPeeringConnectionCreator,PeeringConnectionCreator

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

type PeeringConnectionDescriber interface {
PeeringConnection(string, string) (*atlas.Peer, error)
}

type PeeringConnectionCreator interface {
CreateContainer(string, *atlas.Container) (*atlas.Container, error)
CreatePeeringConnection(string, *atlas.Peer) (*atlas.Peer, error)
Expand Down Expand Up @@ -63,6 +67,17 @@ func (s *Store) PeeringConnections(projectID string, opts *atlas.ContainersListO
}
}

// PeeringConnections encapsulates the logic to manage different cloud providers
func (s *Store) PeeringConnection(projectID, peerID string) (*atlas.Peer, error) {
switch s.service {
case config.CloudService:
result, _, err := s.client.(*atlas.Client).Peers.Get(context.Background(), projectID, peerID)
return result, err
default:
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 {
Expand Down

0 comments on commit d8e4149

Please sign in to comment.