diff --git a/internal/cli/deployments/search/indexes/create.go b/internal/cli/deployments/search/indexes/create.go index 780e77d174..05c23a5133 100644 --- a/internal/cli/deployments/search/indexes/create.go +++ b/internal/cli/deployments/search/indexes/create.go @@ -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 @@ -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 } @@ -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 @@ -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") } diff --git a/internal/mocks/mock_search.go b/internal/mocks/mock_search.go index cf18e2bca0..05f3e9f1c8 100644 --- a/internal/mocks/mock_search.go +++ b/internal/mocks/mock_search.go @@ -87,6 +87,21 @@ func (mr *MockSearchIndexCreatorMockRecorder) CreateSearchIndexes(arg0, arg1, ar return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "CreateSearchIndexes", reflect.TypeOf((*MockSearchIndexCreator)(nil).CreateSearchIndexes), arg0, arg1, arg2) } +// SearchIndex mocks base method. +func (m *MockSearchIndexCreator) SearchIndex(arg0, arg1, arg2 string) (*admin.ClusterSearchIndex, error) { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "SearchIndex", arg0, arg1, arg2) + ret0, _ := ret[0].(*admin.ClusterSearchIndex) + ret1, _ := ret[1].(error) + return ret0, ret1 +} + +// SearchIndex indicates an expected call of SearchIndex. +func (mr *MockSearchIndexCreatorMockRecorder) SearchIndex(arg0, arg1, arg2 interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "SearchIndex", reflect.TypeOf((*MockSearchIndexCreator)(nil).SearchIndex), arg0, arg1, arg2) +} + // MockSearchIndexDescriber is a mock of SearchIndexDescriber interface. type MockSearchIndexDescriber struct { ctrl *gomock.Controller diff --git a/internal/store/search.go b/internal/store/search.go index 905d47687d..b27e54e5cb 100644 --- a/internal/store/search.go +++ b/internal/store/search.go @@ -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 { diff --git a/test/e2e/atlas/deployments_atlas_test.go b/test/e2e/atlas/deployments_atlas_test.go index ee27ab9936..1c70e10e90 100644 --- a/test/e2e/atlas/deployments_atlas_test.go +++ b/test/e2e/atlas/deployments_atlas_test.go @@ -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, @@ -159,6 +159,7 @@ func TestDeploymentsAtlas(t *testing.T) { databaseNameAtlas, "--collection", collectionNameAtlas, + "--watch", ) cmd.Env = os.Environ()