Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
8 changes: 4 additions & 4 deletions internal/cli/deployments/search/indexes/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,12 +88,12 @@ func (opts *CreateOpts) RunLocal(ctx context.Context) error {

telemetry.AppendOption(telemetry.WithSearchIndexType(opts.index.GetType()))

db := opts.mongodbClient.Database(opts.index.Database)
if idx, _ := db.SearchIndexByName(ctx, opts.index.Name, opts.index.CollectionName); idx != nil {
coll := opts.mongodbClient.Database(opts.index.Database).Collection(opts.index.CollectionName)
if idx, _ := coll.SearchIndexByName(ctx, opts.index.Name); idx != nil {
return ErrSearchIndexDuplicated
}

opts.index, err = db.CreateSearchIndex(ctx, opts.index.CollectionName, opts.index)
opts.index, err = coll.CreateSearchIndex(ctx, opts.index)
return err
}

Expand Down Expand Up @@ -128,7 +128,7 @@ func (opts *CreateOpts) Run(ctx context.Context) error {

func (opts *CreateOpts) initMongoDBClient(ctx context.Context) func() error {
return func() error {
opts.mongodbClient = mongodbclient.NewClientWithContext(ctx)
opts.mongodbClient = mongodbclient.NewClient(ctx)
return nil
}
}
Expand Down
24 changes: 18 additions & 6 deletions internal/cli/deployments/search/indexes/create_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ func TestCreate_RunLocal(t *testing.T) {
ctrl := gomock.NewController(t)
mockMongodbClient := mocks.NewMockMongoDBClient(ctrl)
mockDB := mocks.NewMockDatabase(ctrl)
mockColl := mocks.NewMockCollection(ctrl)
ctx := context.Background()

testDeployments := fixture.NewMockLocalDeploymentOpts(ctrl, expectedLocalDeployment)
Expand Down Expand Up @@ -110,6 +111,11 @@ func TestCreate_RunLocal(t *testing.T) {
Database(expectedDB).
Return(mockDB).
Times(1)
mockDB.
EXPECT().
Collection(expectedCollection).
Return(mockColl).
Times(1)

index := &atlasv2.ClusterSearchIndex{
Analyzer: &opts.Analyzer,
Expand Down Expand Up @@ -138,15 +144,15 @@ func TestCreate_RunLocal(t *testing.T) {
Type: pointer.Get(search.DefaultType),
}

mockDB.
mockColl.
EXPECT().
SearchIndexByName(ctx, index.Name, index.CollectionName).
SearchIndexByName(ctx, index.Name).
Return(nil, mongodbclient.ErrSearchIndexNotFound).
Times(1)

mockDB.
mockColl.
EXPECT().
CreateSearchIndex(ctx, expectedCollection, gomock.Any()).
CreateSearchIndex(ctx, gomock.Any()).
Return(indexWithID, nil).
Times(1)

Expand All @@ -166,6 +172,7 @@ func TestCreate_Duplicated(t *testing.T) {
ctrl := gomock.NewController(t)
mockMongodbClient := mocks.NewMockMongoDBClient(ctrl)
mockDB := mocks.NewMockDatabase(ctrl)
mockColl := mocks.NewMockCollection(ctrl)
ctx := context.Background()

testDeployments := fixture.NewMockLocalDeploymentOpts(ctrl, expectedLocalDeployment)
Expand Down Expand Up @@ -226,6 +233,11 @@ func TestCreate_Duplicated(t *testing.T) {
Database(expectedDB).
Return(mockDB).
Times(1)
mockDB.
EXPECT().
Collection(expectedCollection).
Return(mockColl).
Times(1)

index := &atlasv2.ClusterSearchIndex{
Analyzer: &opts.Analyzer,
Expand All @@ -252,9 +264,9 @@ func TestCreate_Duplicated(t *testing.T) {
IndexID: &indexID,
}

mockDB.
mockColl.
EXPECT().
SearchIndexByName(ctx, index.Name, index.CollectionName).
SearchIndexByName(ctx, index.Name).
Return(indexWithID, nil).
Times(1)
if err := opts.Run(ctx); err == nil || !errors.Is(err, ErrSearchIndexDuplicated) {
Expand Down
2 changes: 1 addition & 1 deletion internal/cli/deployments/search/indexes/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ func (opts *DeleteOpts) initStore(ctx context.Context) func() error {

func (opts *DeleteOpts) initMongoDBClient(ctx context.Context) func() error {
return func() error {
opts.mongodbClient = mongodbclient.NewClientWithContext(ctx)
opts.mongodbClient = mongodbclient.NewClient(ctx)
return nil
}
}
Expand Down
2 changes: 1 addition & 1 deletion internal/cli/deployments/search/indexes/describe.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ func (opts *DescribeOpts) RunLocal(ctx context.Context) error {

func (opts *DescribeOpts) initMongoDBClient(ctx context.Context) func() error {
return func() error {
opts.mongodbClient = mongodbclient.NewClientWithContext(ctx)
opts.mongodbClient = mongodbclient.NewClient(ctx)
return nil
}
}
Expand Down
12 changes: 7 additions & 5 deletions internal/cli/deployments/search/indexes/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,17 +77,19 @@ func (opts *ListOpts) RunLocal(ctx context.Context) error {
}
defer opts.mongodbClient.Disconnect()

r, err := opts.mongodbClient.Database(opts.DBName).SearchIndexes(ctx, opts.Collection)
r, err := opts.mongodbClient.Database(opts.DBName).Collection(opts.Collection).SearchIndexes(ctx)
if err != nil {
return err
}

return opts.Print(r)
}

func (opts *ListOpts) initMongoDBClient() error {
opts.mongodbClient = mongodbclient.NewClient()
return nil
func (opts *ListOpts) initMongoDBClient(ctx context.Context) func() error {
return func() error {
opts.mongodbClient = mongodbclient.NewClient(ctx)
return nil
}
}

func (opts *ListOpts) initStore(ctx context.Context) func() error {
Expand Down Expand Up @@ -133,7 +135,7 @@ func ListBuilder() *cobra.Command {
opts.InitOutput(w, listTemplate),
opts.InitStore(cmd.Context(), cmd.OutOrStdout()),
opts.initStore(cmd.Context()),
opts.initMongoDBClient,
opts.initMongoDBClient(cmd.Context()),
)
},
RunE: func(cmd *cobra.Command, _ []string) error {
Expand Down
11 changes: 9 additions & 2 deletions internal/cli/deployments/search/indexes/list_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ func TestList_RunLocal(t *testing.T) {
ctrl := gomock.NewController(t)
mockMongodbClient := mocks.NewMockMongoDBClient(ctrl)
mockDB := mocks.NewMockDatabase(ctrl)
mockColl := mocks.NewMockCollection(ctrl)
ctx := context.Background()

const (
Expand Down Expand Up @@ -110,6 +111,12 @@ func TestList_RunLocal(t *testing.T) {
Return(mockDB).
Times(1)

mockDB.
EXPECT().
Collection(expectedCollection).
Return(mockColl).
Times(1)

expected := []*atlasv2.ClusterSearchIndex{
{
Name: expectedName,
Expand All @@ -121,9 +128,9 @@ func TestList_RunLocal(t *testing.T) {
},
}

mockDB.
mockColl.
EXPECT().
SearchIndexes(ctx, expectedCollection).
SearchIndexes(ctx).
Return(expected, nil).
Times(1)

Expand Down
2 changes: 1 addition & 1 deletion internal/cli/deployments/setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ type SetupOpts struct {

func (opts *SetupOpts) initMongoDBClient(ctx context.Context) func() error {
return func() error {
opts.mongodbClient = mongodbclient.NewClientWithContext(ctx)
opts.mongodbClient = mongodbclient.NewClient(ctx)
return nil
}
}
Expand Down
112 changes: 34 additions & 78 deletions internal/mocks/mock_mongodb_client.go

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

10 changes: 2 additions & 8 deletions internal/mongodbclient/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import (

var errConnectFailed = errors.New("failed to connect to mongodb server")

//go:generate mockgen -destination=../mocks/mock_mongodb_client.go -package=mocks github.com/mongodb/mongodb-atlas-cli/atlascli/internal/mongodbclient MongoDBClient,Database,SearchIndex
//go:generate mockgen -destination=../mocks/mock_mongodb_client.go -package=mocks github.com/mongodb/mongodb-atlas-cli/atlascli/internal/mongodbclient MongoDBClient,Database,Collection

type MongoDBClient interface {
Connect(connectionString string, waitSeconds int64) error
Expand All @@ -43,13 +43,7 @@ type mongodbClient struct {
ctx context.Context
}

func NewClient() MongoDBClient {
return &mongodbClient{
ctx: context.Background(),
}
}

func NewClientWithContext(ctx context.Context) MongoDBClient {
func NewClient(ctx context.Context) MongoDBClient {
return &mongodbClient{
ctx: ctx,
}
Expand Down
Loading