Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 31 additions & 2 deletions build/ci/evergreen.yml
Original file line number Diff line number Diff line change
Expand Up @@ -425,6 +425,12 @@ functions:
args:
- -f
- expansions.yaml
"docker_login":
- command: shell.exec
params:
shell: bash
script: |
echo "${dockerhub_password}" | docker login -u ${dockerhub_username} --password-stdin
tasks:
- name: compile
tags: ["code_health"]
Expand Down Expand Up @@ -1448,6 +1454,7 @@ tasks:
- func: "install gotestsum"
- func: "install mongodb database tools"
- func: "increase inotify limits"
- func: "docker_login"
- func: "e2e test"
timeout_secs: 11400 # 3 hours 10 minutes
vars:
Expand All @@ -1467,6 +1474,7 @@ tasks:
- func: "install gotestsum"
- func: "install mongodb database tools"
- func: "increase inotify limits"
- func: "docker_login"
- func: "e2e test"
timeout_secs: 11400 # 3 hours 10 minutes
vars:
Expand All @@ -1479,13 +1487,34 @@ tasks:
E2E_TAGS: atlas,deployments,local,nocli
E2E_TIMEOUT: 3h
- name: atlas_deployments_local_auth_e2e
tags: ["e2e","deployments","local","auth"]
tags: ["e2e","deployments","local","auth","new"]
must_have_test_results: true
exec_timeout_secs: 11400 # 3 hours 10 minutes
commands:
- func: "install gotestsum"
- func: "install mongodb database tools"
- func: "increase inotify limits"
- func: "docker_login"
- func: "e2e test"
timeout_secs: 11400 # 3 hours 10 minutes
vars:
MCLI_ORG_ID: ${atlas_org_id}
MCLI_PROJECT_ID: ${atlas_project_id}
MCLI_PRIVATE_API_KEY: ${atlas_private_api_key}
MCLI_PUBLIC_API_KEY: ${atlas_public_api_key}
MCLI_OPS_MANAGER_URL: ${mcli_ops_manager_url}
MCLI_SERVICE: cloud
E2E_TAGS: atlas,deployments,local,auth,new
E2E_TIMEOUT: 3h
- name: atlas_deployments_local_auth_deprecated_e2e
tags: ["e2e","deployments","local","auth","deprecated"]
must_have_test_results: true
exec_timeout_secs: 11400 # 3 hours 10 minutes
commands:
- func: "install gotestsum"
- func: "install mongodb database tools"
- func: "increase inotify limits"
- func: "docker_login"
- func: "e2e test"
timeout_secs: 11400 # 3 hours 10 minutes
vars:
Expand All @@ -1495,7 +1524,7 @@ tasks:
MCLI_PUBLIC_API_KEY: ${atlas_public_api_key}
MCLI_OPS_MANAGER_URL: ${mcli_ops_manager_url}
MCLI_SERVICE: cloud
E2E_TAGS: atlas,deployments,local,auth
E2E_TAGS: atlas,deployments,local,auth,deprecated
E2E_TIMEOUT: 3h
- name: build_win11_image
tags: ["packer", "windows", "win11"]
Expand Down
146 changes: 112 additions & 34 deletions internal/cli/deployments/search/indexes/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
"github.com/mongodb/mongodb-atlas-cli/atlascli/internal/cli/search"
"github.com/mongodb/mongodb-atlas-cli/atlascli/internal/config"
"github.com/mongodb/mongodb-atlas-cli/atlascli/internal/flag"
"github.com/mongodb/mongodb-atlas-cli/atlascli/internal/log"
"github.com/mongodb/mongodb-atlas-cli/atlascli/internal/mongodbclient"
"github.com/mongodb/mongodb-atlas-cli/atlascli/internal/store"
"github.com/mongodb/mongodb-atlas-cli/atlascli/internal/telemetry"
Expand All @@ -43,7 +44,18 @@ const (
notFoundState = "NOT_FOUND"
)

var ErrSearchIndexDuplicated = errors.New("search index is duplicated")
var (
ErrSearchIndexDuplicated = errors.New("search index is duplicated")
errInvalidIndex = errors.New("invalid index")
)

type IndexID struct {
ID string
Name string
Collection string
Database string
Index any
}

type CreateOpts struct {
cli.WatchOpts
Expand All @@ -53,8 +65,8 @@ type CreateOpts struct {
search.IndexOpts
mongodbClient mongodbclient.MongoDBClient
connectionString string
index *admin.ClusterSearchIndex
store store.SearchIndexCreator
store store.SearchIndexCreatorDescriber
indexID IndexID
}

func (opts *CreateOpts) initStore(ctx context.Context) func() error {
Expand Down Expand Up @@ -84,34 +96,61 @@ func (opts *CreateOpts) RunLocal(ctx context.Context) error {
_ = opts.mongodbClient.Disconnect(ctx)
}()

opts.index, err = opts.NewSearchIndex()
idx, err := opts.CreateSearchIndex()
if err != nil {
return err
}

telemetry.AppendOption(telemetry.WithSearchIndexType(opts.index.GetType()))
var definition any
var idxType *string

coll := opts.mongodbClient.Database(opts.index.Database).Collection(opts.index.CollectionName)
if idx, _ := coll.SearchIndexByName(ctx, opts.index.Name); idx != nil {
return ErrSearchIndexDuplicated
switch index := idx.(type) {
case *admin.SearchIndexCreateRequest:
idxType = index.Type

opts.indexID.Database = index.Database
opts.indexID.Collection = index.CollectionName
opts.indexID.Name = index.Name

definition = index.Definition
case *admin.ClusterSearchIndex:
_, _ = log.Warningln("you're using an old search index definition")
idxType = index.Type

opts.indexID.Database = index.Database
opts.indexID.Collection = index.CollectionName
opts.indexID.Name = index.Name

definition, err = buildIndexDefinition(index)
if err != nil {
return err
}
default:
return errInvalidIndex
}

if opts.index.Type == nil {
if idxType == nil {
defaultType := search.DefaultType
opts.index.Type = &defaultType
idxType = &defaultType
}

definition, err := buildIndexDefinition(opts.index)
if err != nil {
return err
telemetry.AppendOption(telemetry.WithSearchIndexType(*idxType))

coll := opts.mongodbClient.Database(opts.indexID.Database).Collection(opts.indexID.Collection)
if idx, _ := coll.SearchIndexByName(ctx, opts.indexID.Name); idx != nil {
return ErrSearchIndexDuplicated
}

result, err := coll.CreateSearchIndex(ctx, opts.index.Name, *opts.index.Type, definition)
result, err := coll.CreateSearchIndex(ctx, opts.indexID.Name, *idxType, definition)
if err != nil {
return err
}

opts.index.IndexID = result.IndexID
opts.indexID.Index = result

if result.IndexID != nil {
opts.indexID.ID = *result.IndexID
}

return nil
}
Expand Down Expand Up @@ -152,15 +191,56 @@ func (opts *CreateOpts) RunAtlas() error {
return err
}

index, err := opts.NewSearchIndex()
idx, err := opts.CreateSearchIndex()
if err != nil {
return err
}

telemetry.AppendOption(telemetry.WithSearchIndexType(opts.index.GetType()))
switch index := idx.(type) {
case *admin.SearchIndexCreateRequest:
telemetry.AppendOption(telemetry.WithSearchIndexType(index.GetType()))
r, err := opts.store.CreateSearchIndexes(opts.ConfigProjectID(), opts.DeploymentName, index)
if err != nil {
return err
}

opts.index, err = opts.store.CreateSearchIndexes(opts.ConfigProjectID(), opts.DeploymentName, index)
return err
if r.Database != nil {
opts.indexID.Database = *r.Database
}
if r.CollectionName != nil {
opts.indexID.Collection = *r.CollectionName
}
if r.Name != nil {
opts.indexID.Name = *r.Name
}
if r.IndexID != nil {
opts.indexID.ID = *r.IndexID
}

opts.indexID.Index = r

return nil
case *admin.ClusterSearchIndex:
_, _ = log.Warningln("you're using an old search index definition")
telemetry.AppendOption(telemetry.WithSearchIndexType(index.GetType()))
r, err := opts.store.CreateSearchIndexesDeprecated(opts.ConfigProjectID(), opts.DeploymentName, index)
if err != nil {
return err
}

opts.indexID.Database = r.Database
opts.indexID.Collection = r.CollectionName
opts.indexID.Name = r.Name
if r.IndexID != nil {
opts.indexID.ID = *r.IndexID
}

opts.indexID.Index = r

return nil
default:
return errInvalidIndex
}
}

func (opts *CreateOpts) Run(ctx context.Context) error {
Expand All @@ -181,39 +261,39 @@ func (opts *CreateOpts) initMongoDBClient() error {
return nil
}

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

coll := opts.mongodbClient.Database(opts.index.Database).Collection(opts.index.CollectionName)
index, err := coll.SearchIndexByName(ctx, opts.index.Name)
coll := opts.mongodbClient.Database(opts.indexID.Database).Collection(opts.indexID.Collection)
index, err := coll.SearchIndexByName(ctx, opts.indexID.Name)
if err != nil {
return notFoundState, nil
return index, notFoundState, nil
}
if index.Status == nil {
return notFoundState, nil
return index, notFoundState, nil
}
return *index.Status, nil
return index, *index.Status, nil
}

func (opts *CreateOpts) watchLocal(ctx context.Context) (any, bool, error) {
state, err := opts.status(ctx)
index, state, err := opts.status(ctx)
if err != nil {
return nil, false, err
}
if state == "READY" {
opts.index.Status = &state
return opts.index, true, nil
index.Status = &state
return 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)
index, err := opts.store.SearchIndexDeprecated(opts.ConfigProjectID(), opts.DeploymentName, opts.indexID.ID)
if err != nil {
return nil, false, err
}
Expand All @@ -226,7 +306,7 @@ func (opts *CreateOpts) watchAtlas(_ context.Context) (any, bool, error) {
func (opts *CreateOpts) PostRun(ctx context.Context) error {
opts.AppendDeploymentType()
if !opts.EnableWatch {
return opts.Print(opts.index)
return opts.Print(opts.indexID.Index)
}

watch := opts.watchLocal
Expand All @@ -241,9 +321,7 @@ func (opts *CreateOpts) PostRun(ctx context.Context) error {
if err != nil {
return err
}
opts.index = watchResult.(*admin.ClusterSearchIndex)

if err := opts.Print(opts.index); err != nil {
if err := opts.Print(watchResult); err != nil {
return err
}
return opts.PostRunMessages()
Expand Down
12 changes: 3 additions & 9 deletions internal/cli/deployments/search/indexes/create_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@ func TestCreate_Duplicated(t *testing.T) {

func TestCreate_RunAtlas(t *testing.T) {
ctrl := gomock.NewController(t)
mockIndexStore := mocks.NewMockSearchIndexCreator(ctrl)
mockIndexStore := mocks.NewMockSearchIndexCreatorDescriber(ctrl)
ctx := context.Background()
buf := new(bytes.Buffer)

Expand All @@ -278,18 +278,12 @@ func TestCreate_RunAtlas(t *testing.T) {
store: mockIndexStore,
}

index, err := opts.NewSearchIndex()
index, err := opts.CreateSearchIndex()
require.NoError(t, err)

indexWithID := &atlasv2.ClusterSearchIndex{
CollectionName: opts.Collection,
Database: opts.DBName,
Analyzer: &opts.Analyzer,
Mappings: &atlasv2.ApiAtlasFTSMappings{
Dynamic: &opts.Dynamic,
Fields: nil,
},
SearchAnalyzer: &opts.SearchAnalyzer,
Name: opts.Name,
IndexID: &indexID,
}
Expand All @@ -298,7 +292,7 @@ func TestCreate_RunAtlas(t *testing.T) {

mockIndexStore.
EXPECT().
CreateSearchIndexes(opts.ProjectID, opts.DeploymentName, index).
CreateSearchIndexesDeprecated(opts.ProjectID, opts.DeploymentName, index).
Times(1).
Return(indexWithID, nil)

Expand Down
2 changes: 1 addition & 1 deletion internal/cli/deployments/search/indexes/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ func (opts *DeleteOpts) Run(ctx context.Context) error {
}

func (opts *DeleteOpts) RunAtlas() error {
return opts.Delete(opts.store.DeleteSearchIndex, opts.ConfigProjectID(), opts.DeploymentName)
return opts.Delete(opts.store.DeleteSearchIndexDeprecated, opts.ConfigProjectID(), opts.DeploymentName)
}

func (opts *DeleteOpts) RunLocal(ctx context.Context) error {
Expand Down
2 changes: 1 addition & 1 deletion internal/cli/deployments/search/indexes/delete_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ func TestDelete_RunAtlas(t *testing.T) {

mockStore.
EXPECT().
DeleteSearchIndex(opts.ProjectID, opts.DeploymentName, opts.Entry).
DeleteSearchIndexDeprecated(opts.ProjectID, opts.DeploymentName, opts.Entry).
Return(nil).
Times(1)

Expand Down
2 changes: 1 addition & 1 deletion internal/cli/deployments/search/indexes/describe.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ func (opts *DescribeOpts) Run(ctx context.Context) error {
}

func (opts *DescribeOpts) RunAtlas() error {
r, err := opts.store.SearchIndex(opts.ConfigProjectID(), opts.DeploymentName, opts.indexID)
r, err := opts.store.SearchIndexDeprecated(opts.ConfigProjectID(), opts.DeploymentName, opts.indexID)
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion internal/cli/deployments/search/indexes/describe_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ func TestDescribe_RunAtlas(t *testing.T) {

mockStore.
EXPECT().
SearchIndex(opts.ProjectID, opts.DeploymentName, opts.indexID).
SearchIndexDeprecated(opts.ProjectID, opts.DeploymentName, opts.indexID).
Return(&atlasv2.ClusterSearchIndex{
Name: name,
Database: database,
Expand Down
Loading