Skip to content

Commit

Permalink
CLOUDP-283508 "atlas deployments search index create --watch" does no…
Browse files Browse the repository at this point in the history
…t watch on correct index (#3377)
  • Loading branch information
fmenezes authored Nov 8, 2024
1 parent 5fb1e60 commit 64f8282
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 59 deletions.
27 changes: 6 additions & 21 deletions internal/cli/deployments/search/indexes/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,6 @@ import (
"github.com/spf13/afero"
"github.com/spf13/cobra"
"go.mongodb.org/atlas-sdk/v20240805005/admin"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo"
)

const (
Expand Down Expand Up @@ -83,7 +81,7 @@ func (opts *CreateOpts) RunLocal(ctx context.Context) error {
_ = opts.mongodbClient.Disconnect(ctx)
}()

opts.index, err = opts.DeprecatedNewSearchIndex()
opts.index, err = opts.NewSearchIndex()
if err != nil {
return err
}
Expand Down Expand Up @@ -135,34 +133,21 @@ func (opts *CreateOpts) initMongoDBClient() error {

func (opts *CreateOpts) status(ctx context.Context) (string, error) {
if err := opts.mongodbClient.Connect(ctx, opts.connectionString, connectWaitSeconds); err != nil {
return "", err
return notFoundState, err
}
defer func() {
_ = opts.mongodbClient.Disconnect(ctx)
}()

db := opts.mongodbClient.Database(opts.index.Database)
col := db.Collection(opts.index.CollectionName)
cursor, err := col.Aggregate(ctx, mongo.Pipeline{
{
{Key: "$listSearchIndexes", Value: bson.D{}},
},
})
coll := opts.mongodbClient.Database(opts.index.Database).Collection(opts.index.CollectionName)
index, err := coll.SearchIndexByName(ctx, opts.index.Name)
if err != nil {
return "", err
}
var results []bson.M
if err = cursor.All(ctx, &results); err != nil {
return "", err
}
if len(results) == 0 {
return notFoundState, nil
}
status, ok := results[0]["status"].(string)
if !ok {
if index.Status == nil {
return notFoundState, nil
}
return status, nil
return *index.Status, nil
}

func (opts *CreateOpts) watchLocal(ctx context.Context) (any, bool, error) {
Expand Down
38 changes: 0 additions & 38 deletions internal/cli/search/index_opts.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,44 +78,6 @@ func (opts *IndexOpts) validateOpts() error {
return nil
}

func (opts *IndexOpts) DeprecatedNewSearchIndex() (*atlasv2.ClusterSearchIndex, error) {
if len(opts.Filename) > 0 {
index := &atlasv2.ClusterSearchIndex{}
if err := file.Load(opts.Fs, opts.Filename, index); err != nil {
return nil, fmt.Errorf(failedToLoadIndexMessage, err.Error())
}

if index.Type == nil {
index.Type = pointer.Get(DefaultType)
}

return index, nil
}

f, err := opts.indexFields()
if err != nil {
return nil, err
}

if opts.SearchAnalyzer == "" {
opts.SearchAnalyzer = DefaultAnalyzer
}

i := &atlasv2.ClusterSearchIndex{
CollectionName: opts.Collection,
Database: opts.DBName,
Mappings: &atlasv2.ApiAtlasFTSMappings{
Dynamic: &opts.Dynamic,
Fields: f,
},
Name: opts.Name,
SearchAnalyzer: &opts.SearchAnalyzer,
// only search indexes can be created using flags
Type: pointer.Get(SearchIndexType),
}
return i, nil
}

func (opts *IndexOpts) NewSearchIndex() (*atlasv2.ClusterSearchIndex, error) {
if len(opts.Filename) > 0 {
index := &atlasv2.ClusterSearchIndex{}
Expand Down

0 comments on commit 64f8282

Please sign in to comment.