-
Notifications
You must be signed in to change notification settings - Fork 72
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
CLOUDP-69463: mongocli atlas networking peering watch (#372)
- Loading branch information
Showing
7 changed files
with
186 additions
and
5 deletions.
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
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,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 | ||
} |
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,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) | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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