Skip to content

Commit 4e8f1bf

Browse files
author
Ciprian Tibulca
authored
CLOUDP-240596: fix search index create watcher for Atlas deployments (#2821)
1 parent 07319bb commit 4e8f1bf

File tree

4 files changed

+43
-11
lines changed

4 files changed

+43
-11
lines changed

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

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@ const (
4343
)
4444

4545
var ErrSearchIndexDuplicated = errors.New("search index is duplicated")
46-
var ErrWatchNotAvailable = errors.New("watch is not available for Atlas resources")
4746

4847
type CreateOpts struct {
4948
cli.WatchOpts
@@ -160,13 +159,25 @@ func (opts *CreateOpts) status(ctx context.Context) (string, error) {
160159
return status, nil
161160
}
162161

163-
func (opts *CreateOpts) watch(ctx context.Context) (any, bool, error) {
162+
func (opts *CreateOpts) watchLocal(ctx context.Context) (any, bool, error) {
164163
state, err := opts.status(ctx)
165164
if err != nil {
166165
return nil, false, err
167166
}
168167
if state == "READY" {
169-
return nil, true, nil
168+
opts.index.Status = &state
169+
return opts.index, true, nil
170+
}
171+
return nil, false, nil
172+
}
173+
174+
func (opts *CreateOpts) watchAtlas(_ context.Context) (any, bool, error) {
175+
index, err := opts.store.SearchIndex(opts.ConfigProjectID(), opts.DeploymentName, *opts.index.IndexID)
176+
if err != nil {
177+
return nil, false, err
178+
}
179+
if index.GetStatus() == "STEADY" {
180+
return index, true, nil
170181
}
171182
return nil, false, nil
172183
}
@@ -177,11 +188,19 @@ func (opts *CreateOpts) PostRun(ctx context.Context) error {
177188
return opts.Print(opts.index)
178189
}
179190

180-
if _, err := opts.Watch(func() (any, bool, error) {
181-
return opts.watch(ctx)
182-
}); err != nil {
191+
watch := opts.watchLocal
192+
if opts.IsAtlasDeploymentType() {
193+
watch = opts.watchAtlas
194+
}
195+
196+
watchResult, err := opts.Watch(func() (any, bool, error) {
197+
return watch(ctx)
198+
})
199+
200+
if err != nil {
183201
return err
184202
}
203+
opts.index = watchResult.(*admin.ClusterSearchIndex)
185204

186205
if err := opts.Print(opts.index); err != nil {
187206
return err
@@ -244,10 +263,6 @@ func CreateBuilder() *cobra.Command {
244263
w := cmd.OutOrStdout()
245264
opts.WatchOpts.OutWriter = w
246265

247-
if opts.DeploymentType == "atlas" && opts.EnableWatch {
248-
return ErrWatchNotAvailable
249-
}
250-
251266
if opts.Filename != "" && (opts.DBName != "" || opts.Collection != "") {
252267
return errors.New("the '-file' flag cannot be used in conjunction with the 'db' and 'collection' flags, please choose either 'file' or 'db' and '-collection', but not both")
253268
}

internal/mocks/mock_search.go

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

internal/store/search.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ type SearchIndexLister interface {
2626

2727
type SearchIndexCreator interface {
2828
CreateSearchIndexes(string, string, *atlasv2.ClusterSearchIndex) (*atlasv2.ClusterSearchIndex, error)
29+
SearchIndex(string, string, string) (*atlasv2.ClusterSearchIndex, error)
2930
}
3031

3132
type SearchIndexDescriber interface {

test/e2e/atlas/deployments_atlas_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ func TestDeploymentsAtlas(t *testing.T) {
144144
})
145145
require.NoError(t, watchCluster(g.projectID, clusterName))
146146

147-
t.Run("Create Index", func(t *testing.T) {
147+
t.Run("Create Search Index", func(t *testing.T) {
148148
cmd := exec.Command(cliPath,
149149
deploymentEntity,
150150
searchEntity,
@@ -159,6 +159,7 @@ func TestDeploymentsAtlas(t *testing.T) {
159159
databaseNameAtlas,
160160
"--collection",
161161
collectionNameAtlas,
162+
"--watch",
162163
)
163164
cmd.Env = os.Environ()
164165

0 commit comments

Comments
 (0)