Skip to content

Commit

Permalink
CLOUDP-172196: migrate ListCloudProviderRegions (#1950)
Browse files Browse the repository at this point in the history
  • Loading branch information
matt-condon authored May 30, 2023
1 parent 37abdb4 commit b231c6a
Show file tree
Hide file tree
Showing 11 changed files with 83 additions and 73 deletions.
5 changes: 3 additions & 2 deletions internal/cli/atlas/clusters/availableregions/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"github.com/mongodb/mongodb-atlas-cli/internal/cli/require"
"github.com/mongodb/mongodb-atlas-cli/internal/config"
"github.com/mongodb/mongodb-atlas-cli/internal/flag"
"github.com/mongodb/mongodb-atlas-cli/internal/pointer"
"github.com/mongodb/mongodb-atlas-cli/internal/store"
"github.com/mongodb/mongodb-atlas-cli/internal/usage"
"github.com/spf13/cobra"
Expand Down Expand Up @@ -49,9 +50,9 @@ var listTemplate = `PROVIDER INSTANCE SIZE REGIONS{{range .Results}}{{ $provider

func (opts *ListOpts) Run() error {
// Set provider if existent
var provider []*string
var provider *[]string
if opts.provider != "" {
provider = []*string{&opts.provider}
provider = pointer.Get([]string{opts.provider})
}

r, err := opts.store.CloudProviderRegions(opts.ConfigProjectID(), opts.tier, provider)
Expand Down
11 changes: 6 additions & 5 deletions internal/cli/atlas/clusters/availableregions/list_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,17 @@ import (
"github.com/golang/mock/gomock"
"github.com/mongodb/mongodb-atlas-cli/internal/flag"
"github.com/mongodb/mongodb-atlas-cli/internal/mocks"
"github.com/mongodb/mongodb-atlas-cli/internal/pointer"
"github.com/mongodb/mongodb-atlas-cli/internal/test"
"go.mongodb.org/atlas/mongodbatlas"
atlasv2 "go.mongodb.org/atlas-sdk/admin"
)

func TestList_Run_NoFlags(t *testing.T) {
ctrl := gomock.NewController(t)
mockStore := mocks.NewMockCloudProviderRegionsLister(ctrl)

var expected *mongodbatlas.CloudProviders
var empty []*string
var expected *atlasv2.PaginatedApiAtlasProviderRegions
var empty *[]string

listOpts := &ListOpts{
store: mockStore,
Expand All @@ -52,7 +53,7 @@ func TestList_Run(t *testing.T) {
ctrl := gomock.NewController(t)
mockStore := mocks.NewMockCloudProviderRegionsLister(ctrl)

var expected *mongodbatlas.CloudProviders
var expected *atlasv2.PaginatedApiAtlasProviderRegions

listOpts := &ListOpts{
store: mockStore,
Expand All @@ -62,7 +63,7 @@ func TestList_Run(t *testing.T) {

mockStore.
EXPECT().
CloudProviderRegions(listOpts.ProjectID, listOpts.tier, []*string{&listOpts.provider}).
CloudProviderRegions(listOpts.ProjectID, listOpts.tier, pointer.Get([]string{listOpts.provider})).
Return(expected, nil).
Times(1)

Expand Down
15 changes: 8 additions & 7 deletions internal/cli/atlas/clusters/region_tier_autocomplete.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"github.com/mongodb/mongodb-atlas-cli/internal/cli"
"github.com/mongodb/mongodb-atlas-cli/internal/config"
"github.com/mongodb/mongodb-atlas-cli/internal/flag"
"github.com/mongodb/mongodb-atlas-cli/internal/pointer"
"github.com/mongodb/mongodb-atlas-cli/internal/store"
"github.com/mongodb/mongodb-atlas-cli/internal/validate"
"github.com/spf13/cobra"
Expand All @@ -31,7 +32,7 @@ type autoFunc func(cmd *cobra.Command, args []string, toComplete string) ([]stri

type autoCompleteOpts struct {
cli.GlobalOpts
providers []*string
providers *[]string
tier string
store store.CloudProviderRegionsLister
}
Expand Down Expand Up @@ -69,8 +70,8 @@ func (opts *autoCompleteOpts) tierSuggestions(toComplete string) ([]string, erro
availableTiers := map[string]bool{}
for _, p := range result.Results {
for _, i := range p.InstanceSizes {
if _, ok := availableTiers[i.Name]; !ok && strings.HasPrefix(i.Name, strings.ToUpper(toComplete)) {
availableTiers[i.Name] = true
if _, ok := availableTiers[*i.Name]; !ok && strings.HasPrefix(*i.Name, strings.ToUpper(toComplete)) {
availableTiers[*i.Name] = true
}
}
}
Expand Down Expand Up @@ -117,8 +118,8 @@ func (opts *autoCompleteOpts) regionSuggestions(toComplete string) ([]string, er
for _, p := range result.Results {
for _, i := range p.InstanceSizes {
for _, r := range i.AvailableRegions {
if _, ok := availableRegions[r.Name]; !ok && strings.HasPrefix(r.Name, strings.ToUpper(toComplete)) {
availableRegions[r.Name] = true
if _, ok := availableRegions[*r.Name]; !ok && strings.HasPrefix(*r.Name, strings.ToUpper(toComplete)) {
availableRegions[*r.Name] = true
}
}
}
Expand Down Expand Up @@ -151,9 +152,9 @@ func (opts *autoCompleteOpts) parseFlags(cmd *cobra.Command) {
if project := cmd.Flag(flag.ProjectID).Value.String(); project != "" {
opts.ProjectID = project
}
opts.providers = make([]*string, 0, 1)
providers := make([]string, 0, 1)
if provider := cmd.Flag(flag.Provider).Value.String(); provider != "" {
opts.providers = append(opts.providers, &provider)
opts.providers = pointer.Get(append(providers, provider))
}

if tier := cmd.Flag(flag.Tier).Value.String(); tier != "" {
Expand Down
29 changes: 15 additions & 14 deletions internal/cli/atlas/clusters/region_tier_autocomplete_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,9 @@ import (

"github.com/golang/mock/gomock"
"github.com/mongodb/mongodb-atlas-cli/internal/mocks"
"github.com/mongodb/mongodb-atlas-cli/internal/pointer"
"github.com/stretchr/testify/require"
atlas "go.mongodb.org/atlas/mongodbatlas"
atlasv2 "go.mongodb.org/atlas-sdk/admin"
)

func Test_autoCompleteOpts_tierSuggestions(t *testing.T) {
Expand All @@ -33,13 +34,13 @@ func Test_autoCompleteOpts_tierSuggestions(t *testing.T) {

store: mockStore,
}
expected := &atlas.CloudProviders{
Results: []*atlas.CloudProvider{
expected := &atlasv2.PaginatedApiAtlasProviderRegions{
Results: []atlasv2.ProviderRegions{
{
Provider: "AWS",
InstanceSizes: []*atlas.InstanceSize{
Provider: pointer.Get("AWS"),
InstanceSizes: []atlasv2.ProviderInstanceSize{
{
Name: "M0",
Name: pointer.Get("M0"),
AvailableRegions: nil,
},
},
Expand All @@ -65,17 +66,17 @@ func Test_autoCompleteOpts_regionSuggestions(t *testing.T) {

store: mockStore,
}
expected := &atlas.CloudProviders{
Results: []*atlas.CloudProvider{
expected := &atlasv2.PaginatedApiAtlasProviderRegions{
Results: []atlasv2.ProviderRegions{
{
Provider: "AWS",
InstanceSizes: []*atlas.InstanceSize{
Provider: pointer.Get("AWS"),
InstanceSizes: []atlasv2.ProviderInstanceSize{
{
Name: "M0",
AvailableRegions: []*atlas.AvailableRegion{
Name: pointer.Get("M0"),
AvailableRegions: []atlasv2.AvailableRegion{
{
Name: "EU_EAST",
Default: false,
Name: pointer.Get("EU_EAST"),
Default: pointer.Get(false),
},
},
},
Expand Down
7 changes: 4 additions & 3 deletions internal/cli/atlas/quickstart/cluster_setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"github.com/AlecAivazis/survey/v2"
"github.com/mongodb/mongodb-atlas-cli/internal/cli"
"github.com/mongodb/mongodb-atlas-cli/internal/flag"
"github.com/mongodb/mongodb-atlas-cli/internal/pointer"
"github.com/mongodb/mongodb-atlas-cli/internal/search"
"github.com/mongodb/mongodb-atlas-cli/internal/telemetry"
"github.com/mongodb/mongodb-atlas-cli/internal/usage"
Expand Down Expand Up @@ -182,7 +183,7 @@ func (opts *Opts) defaultRegions() ([]string, error) {
cloudProviders, err := opts.store.CloudProviderRegions(
opts.ConfigProjectID(),
opts.Tier,
[]*string{&opts.Provider},
pointer.Get([]string{opts.Provider}),
)

if err != nil {
Expand All @@ -201,14 +202,14 @@ func (opts *Opts) defaultRegions() ([]string, error) {
if popularRegionIndex != -1 {
// the most popular region must be the first in the list
popularRegion := availableRegions[popularRegionIndex]
defaultRegions = append(defaultRegions, popularRegion.Name)
defaultRegions = append(defaultRegions, *popularRegion.Name)

// remove popular region from availableRegions
availableRegions = append(availableRegions[:popularRegionIndex], availableRegions[popularRegionIndex+1:]...)
}

for _, v := range availableRegions {
defaultRegions = append(defaultRegions, v.Name)
defaultRegions = append(defaultRegions, *v.Name)
}

return defaultRegions, nil
Expand Down
6 changes: 3 additions & 3 deletions internal/mocks/mock_cloud_provider_regions.go

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

4 changes: 2 additions & 2 deletions internal/mocks/mock_clusters.go

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

5 changes: 3 additions & 2 deletions internal/search/search.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package search

import (
"go.mongodb.org/atlas-sdk/admin"
atlas "go.mongodb.org/atlas/mongodbatlas"
"go.mongodb.org/ops-manager/opsmngr"
"go.mongodb.org/ops-manager/search"
Expand Down Expand Up @@ -54,9 +55,9 @@ func AtlasClusterExists(clusters []atlas.Cluster, name string) bool {
}

// DefaultRegion returns the index of the default region.
func DefaultRegion(regions []*atlas.AvailableRegion) int {
func DefaultRegion(regions []admin.AvailableRegion) int {
for i, v := range regions {
if v.Default {
if v.Default != nil && *v.Default {
return i
}
}
Expand Down
46 changes: 24 additions & 22 deletions internal/search/search_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ package search
import (
"testing"

"github.com/mongodb/mongodb-atlas-cli/internal/pointer"
"github.com/mongodb/mongodb-atlas-cli/internal/test/fixture"
atlasv2 "go.mongodb.org/atlas-sdk/admin"
atlas "go.mongodb.org/atlas/mongodbatlas"
)

Expand Down Expand Up @@ -126,61 +128,61 @@ func TestAtlasClusterExists(t *testing.T) {

func TestDefaultRegion(t *testing.T) {
tests := []struct {
input []*atlas.AvailableRegion
input []atlasv2.AvailableRegion
want int
}{
{
input: []*atlas.AvailableRegion{},
input: []atlasv2.AvailableRegion{},
want: -1,
},
{
input: []*atlas.AvailableRegion{
input: []atlasv2.AvailableRegion{
{
Name: "test",
Default: false,
Name: pointer.Get("test"),
Default: pointer.Get(false),
},
},
want: -1,
},
{
input: []*atlas.AvailableRegion{
input: []atlasv2.AvailableRegion{
{
Name: "test",
Default: true,
Name: pointer.Get("test"),
Default: pointer.Get(true),
},
},
want: 0,
},
{
input: []*atlas.AvailableRegion{
input: []atlasv2.AvailableRegion{
{
Name: "test",
Default: false,
Name: pointer.Get("test"),
Default: pointer.Get(false),
},
{
Name: "test2",
Default: true,
Name: pointer.Get("test2"),
Default: pointer.Get(true),
},
{
Name: "test1",
Default: false,
Name: pointer.Get("test1"),
Default: pointer.Get(false),
},
},
want: 1,
},
{
input: []*atlas.AvailableRegion{
input: []atlasv2.AvailableRegion{
{
Name: "test",
Default: false,
Name: pointer.Get("test"),
Default: pointer.Get(false),
},
{
Name: "test2",
Default: false,
Name: pointer.Get("test2"),
Default: pointer.Get(false),
},
{
Name: "test1",
Default: false,
Name: pointer.Get("test1"),
Default: pointer.Get(false),
},
},
want: -1,
Expand Down
14 changes: 8 additions & 6 deletions internal/store/cloud_provider_regions.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,24 +18,26 @@ import (
"fmt"

"github.com/mongodb/mongodb-atlas-cli/internal/config"
atlas "go.mongodb.org/atlas/mongodbatlas"
atlasv2 "go.mongodb.org/atlas-sdk/admin"
)

//go:generate mockgen -destination=../mocks/mock_cloud_provider_regions.go -package=mocks github.com/mongodb/mongodb-atlas-cli/internal/store CloudProviderRegionsLister

type CloudProviderRegionsLister interface {
CloudProviderRegions(string, string, []*string) (*atlas.CloudProviders, error)
CloudProviderRegions(string, string, *[]string) (*atlasv2.PaginatedApiAtlasProviderRegions, error)
}

// CloudProviderRegions encapsulates the logic to manage different cloud providers.
func (s *Store) CloudProviderRegions(projectID, tier string, providerName []*string) (*atlas.CloudProviders, error) {
options := &atlas.CloudProviderRegionsOptions{
func (s *Store) CloudProviderRegions(projectID, tier string, providerName *[]string) (*atlasv2.PaginatedApiAtlasProviderRegions, error) {
params := &atlasv2.ListCloudProviderRegionsApiParams{
GroupId: projectID,
Providers: providerName,
Tier: tier,
Tier: &tier,
}

switch s.service {
case config.CloudService, config.CloudGovService:
result, _, err := s.client.(*atlas.Client).Clusters.ListCloudProviderRegions(s.ctx, projectID, options)
result, _, err := s.clientv2.ClustersApi.ListCloudProviderRegionsWithParams(s.ctx, params).Execute()
return result, err
default:
return nil, fmt.Errorf("%w: %s", errUnsupportedService, s.service)
Expand Down
Loading

0 comments on commit b231c6a

Please sign in to comment.