Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CLOUDP-234527: Remove MongoCLI 3rd followup #2716

Merged
merged 8 commits into from
Mar 6, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .github/CODEOWNERS
Validating CODEOWNERS rules …
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,6 @@

# Docs maintained by Docs Cloud Team
/docs/ @mongodb/docs-cloud-team

/internal/kubernetes/operator/ @mongodb/atlas_kubernetes_team
/internal/cli/atlas/kubernetes/ @mongodb/atlas_kubernetes_team
2 changes: 1 addition & 1 deletion internal/cli/atlas/clusters/list_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func TestList_Run(t *testing.T) {
ctrl := gomock.NewController(t)
mockStore := mocks.NewMockClusterLister(ctrl)

expected := admin.PaginatedAdvancedClusterDescription{
expected := &admin.PaginatedAdvancedClusterDescription{
Results: &[]admin.AdvancedClusterDescription{
{
Name: pointer.Get("test"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import (
"github.com/mongodb/mongodb-atlas-cli/internal/cli"
"github.com/mongodb/mongodb-atlas-cli/internal/log"
"github.com/shirou/gopsutil/v3/host"
"go.mongodb.org/atlas-sdk/v20231115007/admin"
"go.mongodb.org/atlas/mongodbatlas"
)

Expand Down Expand Up @@ -108,11 +107,10 @@ func (opts *DeploymentOpts) AtlasDeployments(projectID string) ([]Deployment, er
ItemsPerPage: MaxItemsPerPage,
}

projectClusters, err := opts.AtlasClusterListStore.ProjectClusters(projectID, listOpts)
atlasClusters, err := opts.AtlasClusterListStore.ProjectClusters(projectID, listOpts)
if err != nil {
return nil, err
}
atlasClusters := projectClusters.(*admin.PaginatedAdvancedClusterDescription)
gssbzn marked this conversation as resolved.
Show resolved Hide resolved

deployments := make([]Deployment, len(atlasClusters.GetResults()))
for i, c := range atlasClusters.GetResults() {
Expand Down
9 changes: 5 additions & 4 deletions internal/cli/atlas/projects/describe_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,20 +21,21 @@ import (

"github.com/golang/mock/gomock"
mocks "github.com/mongodb/mongodb-atlas-cli/internal/mocks/atlas"
"go.mongodb.org/ops-manager/opsmngr"
"github.com/mongodb/mongodb-atlas-cli/internal/pointer"
"go.mongodb.org/atlas-sdk/v20231115007/admin"
)

func TestDescribe_Run(t *testing.T) {
ctrl := gomock.NewController(t)
mockStore := mocks.NewMockProjectDescriber(ctrl)
expected := opsmngr.Project{
expected := &admin.Group{
Name: "test",
ID: "5a0a1e7e0f2912c554080adc",
Id: pointer.Get("5a0a1e7e0f2912c554080adc"),
}
mockStore.
EXPECT().
Project(gomock.Eq("5a0a1e7e0f2912c554080adc")).
Return(&expected, nil).
Return(expected, nil).
Times(1)

opts := &DescribeOpts{
Expand Down
15 changes: 6 additions & 9 deletions internal/cli/auth/login.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,12 @@ var (
type LoginOpts struct {
cli.DefaultSetterOpts
cli.RefresherOpts
AccessToken string
RefreshToken string
IsGov bool
isCloudManager bool
NoBrowser bool
SkipConfig bool
config LoginConfig
AccessToken string
RefreshToken string
IsGov bool
NoBrowser bool
SkipConfig bool
config LoginConfig
}

// SyncWithOAuthAccessProfile returns a function that is synchronizing the oauth settings
Expand All @@ -67,8 +66,6 @@ func (opts *LoginOpts) SyncWithOAuthAccessProfile(c LoginConfig) func() error {
switch {
case opts.IsGov:
opts.Service = config.CloudGovService
case opts.isCloudManager:
opts.Service = config.CloudManagerService
default:
opts.Service = config.CloudService
}
Expand Down
10 changes: 6 additions & 4 deletions internal/cli/auth/login_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,11 @@ import (
"github.com/AlecAivazis/survey/v2"
"github.com/golang/mock/gomock"
"github.com/mongodb/mongodb-atlas-cli/internal/mocks"
"github.com/mongodb/mongodb-atlas-cli/internal/pointer"
"github.com/mongodb/mongodb-atlas-cli/internal/test"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.mongodb.org/atlas-sdk/v20231115007/admin"
"go.mongodb.org/atlas/auth"
atlas "go.mongodb.org/atlas/mongodbatlas"
)
Expand Down Expand Up @@ -134,10 +136,10 @@ func Test_loginOpts_Run(t *testing.T) {
mockConfig.EXPECT().Set("ops_manager_url", gomock.Any()).Times(0)
mockConfig.EXPECT().AccessTokenSubject().Return("[email protected]", nil).Times(1)
mockConfig.EXPECT().Save().Return(nil).Times(2)
expectedOrgs := &atlas.Organizations{
TotalCount: 1,
Results: []*atlas.Organization{
{ID: "o1", Name: "Org1"},
expectedOrgs := &admin.PaginatedOrganization{
TotalCount: pointer.Get(1),
Results: &[]admin.AtlasOrganization{
{Id: pointer.Get("o1"), Name: "Org1"},
},
}
mockStore.EXPECT().Organizations(gomock.Any()).Return(expectedOrgs, nil).Times(1)
Expand Down
10 changes: 6 additions & 4 deletions internal/cli/auth/register_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,11 @@ import (

"github.com/golang/mock/gomock"
"github.com/mongodb/mongodb-atlas-cli/internal/mocks"
"github.com/mongodb/mongodb-atlas-cli/internal/pointer"
"github.com/mongodb/mongodb-atlas-cli/internal/test"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.mongodb.org/atlas-sdk/v20231115007/admin"
"go.mongodb.org/atlas/auth"
atlas "go.mongodb.org/atlas/mongodbatlas"
)
Expand Down Expand Up @@ -97,10 +99,10 @@ func Test_registerOpts_Run(t *testing.T) {
mockConfig.EXPECT().Set("ops_manager_url", gomock.Any()).Times(0)
mockConfig.EXPECT().AccessTokenSubject().Return("[email protected]", nil).Times(1)
mockConfig.EXPECT().Save().Return(nil).Times(2)
expectedOrgs := &atlas.Organizations{
TotalCount: 1,
Results: []*atlas.Organization{
{ID: "o1", Name: "Org1"},
expectedOrgs := &admin.PaginatedOrganization{
TotalCount: pointer.Get(1),
Results: &[]admin.AtlasOrganization{
{Id: pointer.Get("o1"), Name: "Org1"},
},
}
mockStore.EXPECT().Organizations(gomock.Any()).Return(expectedOrgs, nil).Times(1)
Expand Down
113 changes: 23 additions & 90 deletions internal/cli/default_setter_opts.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,17 +30,16 @@ import (
"github.com/mongodb/mongodb-atlas-cli/internal/validate"
atlasv2 "go.mongodb.org/atlas-sdk/v20231115007/admin"
atlas "go.mongodb.org/atlas/mongodbatlas"
"go.mongodb.org/ops-manager/opsmngr"
)

//go:generate mockgen -destination=../mocks/mock_default_opts.go -package=mocks github.com/mongodb/mongodb-atlas-cli/internal/cli ProjectOrgsLister

type ProjectOrgsLister interface {
Project(id string) (interface{}, error)
Projects(*atlas.ListOptions) (interface{}, error)
Organization(id string) (interface{}, error)
Organizations(*atlas.OrganizationsListOptions) (interface{}, error)
GetOrgProjects(string, *atlas.ProjectsListOptions) (interface{}, error)
Project(id string) (*atlas.Project, error)
Projects(*atlas.ListOptions) (*atlas.Projects, error)
Organization(id string) (*atlasv2.AtlasOrganization, error)
Organizations(*atlas.OrganizationsListOptions) (*atlasv2.PaginatedOrganization, error)
GetOrgProjects(string, *atlas.ProjectsListOptions) (*atlas.Projects, error)
}

type DefaultSetterOpts struct {
Expand All @@ -66,14 +65,6 @@ func (opts *DefaultSetterOpts) InitStore(ctx context.Context) error {
return err
}

func (opts *DefaultSetterOpts) IsCloud() bool {
return opts.Service == config.CloudService || opts.Service == config.CloudGovService
}

func (opts *DefaultSetterOpts) IsOpsManager() bool {
return opts.Service == config.OpsManagerService
}

const resultsLimit = 500

var (
Expand All @@ -92,7 +83,7 @@ func (opts *DefaultSetterOpts) projects() (ids, names []string, err error) {
spin.Start()
defer spin.Stop()

var projects interface{}
var projects *atlas.Projects
if opts.OrgID == "" {
projects, err = opts.Store.Projects(&atlas.ListOptions{ItemsPerPage: resultsLimit})
} else {
Expand All @@ -108,30 +99,19 @@ func (opts *DefaultSetterOpts) projects() (ids, names []string, err error) {
}
return nil, nil, err
}
switch r := projects.(type) {
case *atlas.Projects:
if r.TotalCount == 0 {
return nil, nil, errNoResults
}
if r.TotalCount > resultsLimit {
return nil, nil, errTooManyResults
}
ids, names = atlasProjects(r.Results)
case *opsmngr.Projects:
if r.TotalCount == 0 {
return nil, nil, errNoResults
}
if r.TotalCount > resultsLimit {
return nil, nil, errTooManyResults
}
ids, names = omProjects(r.Results)
if projects.TotalCount == 0 {
return nil, nil, errNoResults
}
if projects.TotalCount > resultsLimit {
return nil, nil, errTooManyResults
}
ids, names = atlasProjects(projects.Results)

return ids, names, nil
}

// Orgs fetches organizations, filtering by name.
func (opts *DefaultSetterOpts) orgs(filter string) (results interface{}, err error) {
func (opts *DefaultSetterOpts) orgs(filter string) (results *[]atlasv2.AtlasOrganization, err error) {
spin := newSpinner()
spin.Start()
defer spin.Stop()
Expand All @@ -149,27 +129,14 @@ func (opts *DefaultSetterOpts) orgs(filter string) (results interface{}, err err
if orgs == nil {
return nil, errNoResults
}
switch r := orgs.(type) {
case *atlasv2.PaginatedOrganization:
if r.GetTotalCount() == 0 {
return nil, errNoResults
}
if r.GetTotalCount() > resultsLimit {
return nil, errTooManyResults
}
results = *r.Results
case *atlas.Organizations:
if r.TotalCount == 0 {
return nil, errNoResults
}
if r.TotalCount > resultsLimit {
return nil, errTooManyResults
}
results = r.Results
default:

if orgs.GetTotalCount() == 0 {
return nil, errNoResults
}
return results, nil
if orgs.GetTotalCount() > resultsLimit {
return nil, errTooManyResults
}
return orgs.Results, nil
}

// ProjectExists checks if the project exists and the current user has access to it.
Expand Down Expand Up @@ -289,13 +256,7 @@ func (opts *DefaultSetterOpts) askOrgWithFilter(filter string) error {
return opts.manualOrgID()
}

switch o := orgs.(type) {
case []atlasv2.AtlasOrganization:
return opts.selectOrg(o)
case []*atlas.Organization:
return opts.selectOnPremOrg(o)
}
return nil
return opts.selectOrg(orgs)
}

func (opts *DefaultSetterOpts) manualOrgID() error {
Expand All @@ -315,9 +276,9 @@ func (opts *DefaultSetterOpts) manualOrgID() error {
return nil
}

func (opts *DefaultSetterOpts) selectOrg(orgs []atlasv2.AtlasOrganization) error {
if len(orgs) == 1 {
opts.OrgID = *orgs[0].Id
func (opts *DefaultSetterOpts) selectOrg(orgs *[]atlasv2.AtlasOrganization) error {
if orgs != nil && len(*orgs) == 1 {
opts.OrgID = *(*orgs)[0].Id
return nil
}

Expand All @@ -332,23 +293,6 @@ func (opts *DefaultSetterOpts) selectOrg(orgs []atlasv2.AtlasOrganization) error
return nil
}

func (opts *DefaultSetterOpts) selectOnPremOrg(orgs []*atlas.Organization) error {
if len(orgs) == 1 {
opts.OrgID = orgs[0].ID
return nil
}

opts.runOnMultipleOrgsOrProjects()

p := prompt.NewOnPremOrgSelect(orgs)
if err := telemetry.TrackAskOne(p, &opts.OrgID); err != nil {
return err
}
opts.AskedOrgsOrProjects = true

return nil
}

func (opts *DefaultSetterOpts) SetUpProject() {
if opts.ProjectID != "" {
config.SetProjectID(opts.ProjectID)
Expand Down Expand Up @@ -376,17 +320,6 @@ func atlasProjects(projects []*atlas.Project) (ids, names []string) {
return ids, names
}

// omProjects transform []*opsmngr.Project to a []string of ids and another for names.
func omProjects(projects []*opsmngr.Project) (ids, names []string) {
names = make([]string, len(projects))
ids = make([]string, len(projects))
for i, p := range projects {
ids[i] = p.ID
names[i] = p.Name
}
return ids, names
}

func (*DefaultSetterOpts) DefaultQuestions() []*survey.Question {
defaultOutput := config.Output()
if defaultOutput == "" {
Expand Down
16 changes: 9 additions & 7 deletions internal/cli/default_setter_opts_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,10 @@ 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/assert"
"github.com/stretchr/testify/require"
"go.mongodb.org/atlas-sdk/v20231115007/admin"
atlas "go.mongodb.org/atlas/mongodbatlas"
)

Expand Down Expand Up @@ -115,20 +117,20 @@ func TestDefaultOpts_Orgs(t *testing.T) {
Store: mockStore,
}
t.Run("empty", func(t *testing.T) {
expectedOrgs := &atlas.Organizations{}
expectedOrgs := &admin.PaginatedOrganization{}
mockStore.EXPECT().Organizations(gomock.Any()).Return(expectedOrgs, nil).Times(1)
_, err := opts.orgs("")
require.Error(t, err)
})
t.Run("with one org", func(t *testing.T) {
expectedOrgs := &atlas.Organizations{
Results: []*atlas.Organization{
expectedOrgs := &admin.PaginatedOrganization{
Results: &[]admin.AtlasOrganization{
{
ID: "1",
Id: pointer.Get("1"),
Name: "Org 1",
},
},
TotalCount: 1,
TotalCount: pointer.Get(1),
}
mockStore.EXPECT().Organizations(gomock.Any()).Return(expectedOrgs, nil).Times(1)
gotOrgs, err := opts.orgs("")
Expand All @@ -137,8 +139,8 @@ func TestDefaultOpts_Orgs(t *testing.T) {
})

t.Run("with no org", func(t *testing.T) {
expectedOrgs := &atlas.Organizations{
Results: []*atlas.Organization{},
expectedOrgs := &admin.PaginatedOrganization{
Results: &[]admin.AtlasOrganization{},
}
mockStore.EXPECT().Organizations(gomock.Any()).Return(expectedOrgs, nil).Times(1)
_, err := opts.orgs("")
Expand Down
Loading