Skip to content

Commit

Permalink
CLOUDP-240596: fix search index create watcher for Atlas deployments (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
tibulca authored Apr 2, 2024
1 parent 07319bb commit 4e8f1bf
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 11 deletions.
35 changes: 25 additions & 10 deletions internal/cli/deployments/search/indexes/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ const (
)

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

type CreateOpts struct {
cli.WatchOpts
Expand Down Expand Up @@ -160,13 +159,25 @@ func (opts *CreateOpts) status(ctx context.Context) (string, error) {
return status, nil
}

func (opts *CreateOpts) watch(ctx context.Context) (any, bool, error) {
func (opts *CreateOpts) watchLocal(ctx context.Context) (any, bool, error) {
state, err := opts.status(ctx)
if err != nil {
return nil, false, err
}
if state == "READY" {
return nil, true, nil
opts.index.Status = &state
return opts.index, true, nil
}
return nil, false, nil
}

func (opts *CreateOpts) watchAtlas(_ context.Context) (any, bool, error) {
index, err := opts.store.SearchIndex(opts.ConfigProjectID(), opts.DeploymentName, *opts.index.IndexID)
if err != nil {
return nil, false, err
}
if index.GetStatus() == "STEADY" {
return index, true, nil
}
return nil, false, nil
}
Expand All @@ -177,11 +188,19 @@ func (opts *CreateOpts) PostRun(ctx context.Context) error {
return opts.Print(opts.index)
}

if _, err := opts.Watch(func() (any, bool, error) {
return opts.watch(ctx)
}); err != nil {
watch := opts.watchLocal
if opts.IsAtlasDeploymentType() {
watch = opts.watchAtlas
}

watchResult, err := opts.Watch(func() (any, bool, error) {
return watch(ctx)
})

if err != nil {
return err
}
opts.index = watchResult.(*admin.ClusterSearchIndex)

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

if opts.DeploymentType == "atlas" && opts.EnableWatch {
return ErrWatchNotAvailable
}

if opts.Filename != "" && (opts.DBName != "" || opts.Collection != "") {
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")
}
Expand Down
15 changes: 15 additions & 0 deletions internal/mocks/mock_search.go

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

1 change: 1 addition & 0 deletions internal/store/search.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ type SearchIndexLister interface {

type SearchIndexCreator interface {
CreateSearchIndexes(string, string, *atlasv2.ClusterSearchIndex) (*atlasv2.ClusterSearchIndex, error)
SearchIndex(string, string, string) (*atlasv2.ClusterSearchIndex, error)
}

type SearchIndexDescriber interface {
Expand Down
3 changes: 2 additions & 1 deletion test/e2e/atlas/deployments_atlas_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ func TestDeploymentsAtlas(t *testing.T) {
})
require.NoError(t, watchCluster(g.projectID, clusterName))

t.Run("Create Index", func(t *testing.T) {
t.Run("Create Search Index", func(t *testing.T) {
cmd := exec.Command(cliPath,
deploymentEntity,
searchEntity,
Expand All @@ -159,6 +159,7 @@ func TestDeploymentsAtlas(t *testing.T) {
databaseNameAtlas,
"--collection",
collectionNameAtlas,
"--watch",
)
cmd.Env = os.Environ()

Expand Down

0 comments on commit 4e8f1bf

Please sign in to comment.