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
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ require (
github.com/rs/zerolog v1.32.0
github.com/spf13/cobra v1.8.0
github.com/spf13/viper v1.18.2
github.com/stretchr/testify v1.10.0
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pulling this in mainly to use the assert stuff for now, because I really don't want to bloat tests further with a bunch of if statements. Eventually would like to use testify for integration suites, etc.

golang.org/x/oauth2 v0.30.0
golang.org/x/term v0.33.0
)
Expand Down Expand Up @@ -52,7 +53,6 @@ require (
github.com/spf13/afero v1.11.0 // indirect
github.com/spf13/cast v1.6.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/stretchr/testify v1.10.0 // indirect
github.com/subosito/gotenv v1.6.0 // indirect
go.uber.org/atomic v1.9.0 // indirect
go.uber.org/multierr v1.9.0 // indirect
Expand Down
53 changes: 36 additions & 17 deletions internal/pkg/cli/command/index/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,13 @@ import (
"github.com/spf13/cobra"
)

// Abstracts the Pinecone Go SDK for testing purposes
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Interface abstraction to allow passing in a mock service to represent the SDK in unit tests.

type CreateIndexService interface {
CreateServerlessIndex(ctx context.Context, req *pinecone.CreateServerlessIndexRequest) (*pinecone.Index, error)
CreatePodIndex(ctx context.Context, req *pinecone.CreatePodIndexRequest) (*pinecone.Index, error)
CreateIndexForModel(ctx context.Context, req *pinecone.CreateIndexForModelRequest) (*pinecone.Index, error)
}

type indexType string

const (
Expand Down Expand Up @@ -138,16 +145,25 @@ func runCreateIndexCmd(options createIndexOptions) {
ctx := context.Background()
pc := sdk.NewPineconeClient()

idx, err := runCreateIndexWithService(ctx, pc, options)
if err != nil {
msg.FailMsg("Failed to create index: %s\n", err)
exit.Error(err)
}

renderSuccessOutput(idx, options)
}

// This function plus the CreateIndexService interface allows for testing
func runCreateIndexWithService(ctx context.Context, service CreateIndexService, options createIndexOptions) (*pinecone.Index, error) {
// validate and derive index type from arguments
err := options.validate()
if err != nil {
exit.Error(err)
return
return nil, err
}
idxType, err := options.deriveIndexType()
if err != nil {
exit.Error(err)
return
return nil, err
}

// index tags
Expand Down Expand Up @@ -175,10 +191,11 @@ func runCreateIndexCmd(options createIndexOptions) {
SourceCollection: pointerOrNil(options.sourceCollection),
}

idx, err = pc.CreateServerlessIndex(ctx, &args)
idx, err = service.CreateServerlessIndex(ctx, &args)
if err != nil {
msg.FailMsg("Failed to create serverless index %s: %s\n", style.Emphasis(options.name), err)
exit.Error(err)
wrapped := pcio.Errorf("Failed to create serverless index %s: %w", style.Emphasis(options.name), err)
msg.FailMsg("%v", wrapped)
return nil, wrapped
}
case indexTypePod:
// create pod index
Expand All @@ -202,10 +219,11 @@ func runCreateIndexCmd(options createIndexOptions) {
MetadataConfig: metadataConfig,
}

idx, err = pc.CreatePodIndex(ctx, &args)
idx, err = service.CreatePodIndex(ctx, &args)
if err != nil {
msg.FailMsg("Failed to create pod index %s: %s\n", style.Emphasis(options.name), err)
exit.Error(err)
wrapped := pcio.Errorf("Failed to create pod index %s: %w", style.Emphasis(options.name), err)
msg.FailMsg("%v", wrapped)
return nil, wrapped
}
case indexTypeIntegrated:
// create integrated index
Expand All @@ -223,20 +241,21 @@ func runCreateIndexCmd(options createIndexOptions) {
ReadParameters: &readParams,
WriteParameters: &writeParams,
},
Tags: indexTags,
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the actual "fix" for the bug.

}

idx, err = pc.CreateIndexForModel(ctx, &args)
idx, err = service.CreateIndexForModel(ctx, &args)
if err != nil {
msg.FailMsg("Failed to create integrated index %s: %s\n", style.Emphasis(options.name), err)
exit.Error(err)
wrapped := pcio.Errorf("Failed to create integrated index %s: %w", style.Emphasis(options.name), err)
msg.FailMsg("%v", wrapped)
return nil, wrapped
}
default:
err := pcio.Errorf("invalid index type")
log.Error().Err(err).Msg("Error creating index")
exit.Error(err)
err := pcio.Errorf("Error creating index: invalid index type")
return nil, err
}

renderSuccessOutput(idx, options)
return idx, nil
}

func renderSuccessOutput(idx *pinecone.Index, options createIndexOptions) {
Expand Down
Loading