Skip to content

Commit 1088ea4

Browse files
authored
CLOUDP-283287 Move local search index code to collection level (#3375)
1 parent e9a3b05 commit 1088ea4

File tree

12 files changed

+278
-332
lines changed

12 files changed

+278
-332
lines changed

internal/cli/deployments/search/indexes/create.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -88,12 +88,12 @@ func (opts *CreateOpts) RunLocal(ctx context.Context) error {
8888

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

91-
db := opts.mongodbClient.Database(opts.index.Database)
92-
if idx, _ := db.SearchIndexByName(ctx, opts.index.Name, opts.index.CollectionName); idx != nil {
91+
coll := opts.mongodbClient.Database(opts.index.Database).Collection(opts.index.CollectionName)
92+
if idx, _ := coll.SearchIndexByName(ctx, opts.index.Name); idx != nil {
9393
return ErrSearchIndexDuplicated
9494
}
9595

96-
opts.index, err = db.CreateSearchIndex(ctx, opts.index.CollectionName, opts.index)
96+
opts.index, err = coll.CreateSearchIndex(ctx, opts.index)
9797
return err
9898
}
9999

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

129129
func (opts *CreateOpts) initMongoDBClient(ctx context.Context) func() error {
130130
return func() error {
131-
opts.mongodbClient = mongodbclient.NewClientWithContext(ctx)
131+
opts.mongodbClient = mongodbclient.NewClient(ctx)
132132
return nil
133133
}
134134
}

internal/cli/deployments/search/indexes/create_test.go

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ func TestCreate_RunLocal(t *testing.T) {
5050
ctrl := gomock.NewController(t)
5151
mockMongodbClient := mocks.NewMockMongoDBClient(ctrl)
5252
mockDB := mocks.NewMockDatabase(ctrl)
53+
mockColl := mocks.NewMockCollection(ctrl)
5354
ctx := context.Background()
5455

5556
testDeployments := fixture.NewMockLocalDeploymentOpts(ctrl, expectedLocalDeployment)
@@ -110,6 +111,11 @@ func TestCreate_RunLocal(t *testing.T) {
110111
Database(expectedDB).
111112
Return(mockDB).
112113
Times(1)
114+
mockDB.
115+
EXPECT().
116+
Collection(expectedCollection).
117+
Return(mockColl).
118+
Times(1)
113119

114120
index := &atlasv2.ClusterSearchIndex{
115121
Analyzer: &opts.Analyzer,
@@ -138,15 +144,15 @@ func TestCreate_RunLocal(t *testing.T) {
138144
Type: pointer.Get(search.DefaultType),
139145
}
140146

141-
mockDB.
147+
mockColl.
142148
EXPECT().
143-
SearchIndexByName(ctx, index.Name, index.CollectionName).
149+
SearchIndexByName(ctx, index.Name).
144150
Return(nil, mongodbclient.ErrSearchIndexNotFound).
145151
Times(1)
146152

147-
mockDB.
153+
mockColl.
148154
EXPECT().
149-
CreateSearchIndex(ctx, expectedCollection, gomock.Any()).
155+
CreateSearchIndex(ctx, gomock.Any()).
150156
Return(indexWithID, nil).
151157
Times(1)
152158

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

171178
testDeployments := fixture.NewMockLocalDeploymentOpts(ctrl, expectedLocalDeployment)
@@ -226,6 +233,11 @@ func TestCreate_Duplicated(t *testing.T) {
226233
Database(expectedDB).
227234
Return(mockDB).
228235
Times(1)
236+
mockDB.
237+
EXPECT().
238+
Collection(expectedCollection).
239+
Return(mockColl).
240+
Times(1)
229241

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

255-
mockDB.
267+
mockColl.
256268
EXPECT().
257-
SearchIndexByName(ctx, index.Name, index.CollectionName).
269+
SearchIndexByName(ctx, index.Name).
258270
Return(indexWithID, nil).
259271
Times(1)
260272
if err := opts.Run(ctx); err == nil || !errors.Is(err, ErrSearchIndexDuplicated) {

internal/cli/deployments/search/indexes/delete.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ func (opts *DeleteOpts) initStore(ctx context.Context) func() error {
9494

9595
func (opts *DeleteOpts) initMongoDBClient(ctx context.Context) func() error {
9696
return func() error {
97-
opts.mongodbClient = mongodbclient.NewClientWithContext(ctx)
97+
opts.mongodbClient = mongodbclient.NewClient(ctx)
9898
return nil
9999
}
100100
}

internal/cli/deployments/search/indexes/describe.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ func (opts *DescribeOpts) RunLocal(ctx context.Context) error {
9595

9696
func (opts *DescribeOpts) initMongoDBClient(ctx context.Context) func() error {
9797
return func() error {
98-
opts.mongodbClient = mongodbclient.NewClientWithContext(ctx)
98+
opts.mongodbClient = mongodbclient.NewClient(ctx)
9999
return nil
100100
}
101101
}

internal/cli/deployments/search/indexes/list.go

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -77,17 +77,19 @@ func (opts *ListOpts) RunLocal(ctx context.Context) error {
7777
}
7878
defer opts.mongodbClient.Disconnect()
7979

80-
r, err := opts.mongodbClient.Database(opts.DBName).SearchIndexes(ctx, opts.Collection)
80+
r, err := opts.mongodbClient.Database(opts.DBName).Collection(opts.Collection).SearchIndexes(ctx)
8181
if err != nil {
8282
return err
8383
}
8484

8585
return opts.Print(r)
8686
}
8787

88-
func (opts *ListOpts) initMongoDBClient() error {
89-
opts.mongodbClient = mongodbclient.NewClient()
90-
return nil
88+
func (opts *ListOpts) initMongoDBClient(ctx context.Context) func() error {
89+
return func() error {
90+
opts.mongodbClient = mongodbclient.NewClient(ctx)
91+
return nil
92+
}
9193
}
9294

9395
func (opts *ListOpts) initStore(ctx context.Context) func() error {
@@ -133,7 +135,7 @@ func ListBuilder() *cobra.Command {
133135
opts.InitOutput(w, listTemplate),
134136
opts.InitStore(cmd.Context(), cmd.OutOrStdout()),
135137
opts.initStore(cmd.Context()),
136-
opts.initMongoDBClient,
138+
opts.initMongoDBClient(cmd.Context()),
137139
)
138140
},
139141
RunE: func(cmd *cobra.Command, _ []string) error {

internal/cli/deployments/search/indexes/list_test.go

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ func TestList_RunLocal(t *testing.T) {
3939
ctrl := gomock.NewController(t)
4040
mockMongodbClient := mocks.NewMockMongoDBClient(ctrl)
4141
mockDB := mocks.NewMockDatabase(ctrl)
42+
mockColl := mocks.NewMockCollection(ctrl)
4243
ctx := context.Background()
4344

4445
const (
@@ -110,6 +111,12 @@ func TestList_RunLocal(t *testing.T) {
110111
Return(mockDB).
111112
Times(1)
112113

114+
mockDB.
115+
EXPECT().
116+
Collection(expectedCollection).
117+
Return(mockColl).
118+
Times(1)
119+
113120
expected := []*atlasv2.ClusterSearchIndex{
114121
{
115122
Name: expectedName,
@@ -121,9 +128,9 @@ func TestList_RunLocal(t *testing.T) {
121128
},
122129
}
123130

124-
mockDB.
131+
mockColl.
125132
EXPECT().
126-
SearchIndexes(ctx, expectedCollection).
133+
SearchIndexes(ctx).
127134
Return(expected, nil).
128135
Times(1)
129136

internal/cli/deployments/setup.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ type SetupOpts struct {
118118

119119
func (opts *SetupOpts) initMongoDBClient(ctx context.Context) func() error {
120120
return func() error {
121-
opts.mongodbClient = mongodbclient.NewClientWithContext(ctx)
121+
opts.mongodbClient = mongodbclient.NewClient(ctx)
122122
return nil
123123
}
124124
}

internal/mocks/mock_mongodb_client.go

Lines changed: 34 additions & 78 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

internal/mongodbclient/client.go

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ import (
2828

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

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

3333
type MongoDBClient interface {
3434
Connect(connectionString string, waitSeconds int64) error
@@ -43,13 +43,7 @@ type mongodbClient struct {
4343
ctx context.Context
4444
}
4545

46-
func NewClient() MongoDBClient {
47-
return &mongodbClient{
48-
ctx: context.Background(),
49-
}
50-
}
51-
52-
func NewClientWithContext(ctx context.Context) MongoDBClient {
46+
func NewClient(ctx context.Context) MongoDBClient {
5347
return &mongodbClient{
5448
ctx: ctx,
5549
}

0 commit comments

Comments
 (0)