-
Notifications
You must be signed in to change notification settings - Fork 126
[DATA-4707] Add CLI for Custom Indexes #5384
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
danielbotros
merged 8 commits into
viamrobotics:main
from
danielbotros:DATA-4707-custom-indexes-cli
Oct 29, 2025
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
39d5123
Add CLI for custom indexes
danielbotros 235b9a9
Lint
danielbotros a6df656
make errors private
danielbotros 3f1fe20
Consistent usage text
danielbotros 831531d
Remove confirmation and fix reading JSON file into index spec, improv…
danielbotros d289825
Merge branch 'main' into DATA-4707-custom-indexes-cli
danielbotros 3795a29
Use - instead of _
danielbotros 4abd4ae
Recapitalize test case
danielbotros File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,197 @@ | ||
| package cli | ||
|
|
||
| import ( | ||
| "context" | ||
| "encoding/json" | ||
| "errors" | ||
| "fmt" | ||
| "os" | ||
|
|
||
| "github.com/urfave/cli/v2" | ||
| pb "go.viam.com/api/app/data/v1" | ||
| ) | ||
|
|
||
| const ( | ||
| hotStoreCollectionType = pb.IndexableCollection_INDEXABLE_COLLECTION_HOT_STORE | ||
| pipelineSinkCollectionType = pb.IndexableCollection_INDEXABLE_COLLECTION_PIPELINE_SINK | ||
| unspecifiedCollectionType = pb.IndexableCollection_INDEXABLE_COLLECTION_UNSPECIFIED | ||
|
|
||
| hotStoreCollectionTypeStr = "hot-storage" | ||
| pipelineSinkCollectionTypeStr = "pipeline-sink" | ||
| ) | ||
|
|
||
| var ( | ||
| errInvalidCollectionType = errors.New("invalid collection type, must be one of: hot-storage, pipeline-sink") | ||
| errPipelineNameRequired = errors.New("--pipeline-name is required when --collection-type is 'pipeline-sink'") | ||
| errPipelineNameNotAllowed = errors.New("--pipeline-name can only be used when --collection-type is 'pipeline-sink'") | ||
| ) | ||
|
|
||
| type createCustomIndexArgs struct { | ||
| OrgID string | ||
| CollectionType string | ||
| PipelineName string | ||
| IndexSpecPath string | ||
| } | ||
|
|
||
| // CreateCustomIndexAction creates a custom index for a specified organization and collection type | ||
| // using the provided index specification file in the arguments. | ||
| func CreateCustomIndexAction(c *cli.Context, args createCustomIndexArgs) error { | ||
| client, err := newViamClient(c) | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| collectionType, err := validateCollectionTypeArgs(c, args.CollectionType) | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| indexSpec, err := readJSONToByteSlices(args.IndexSpecPath) | ||
| if err != nil { | ||
| return fmt.Errorf("failed to read index spec from file: %w", err) | ||
| } | ||
|
|
||
| _, err = client.dataClient.CreateIndex(context.Background(), &pb.CreateIndexRequest{ | ||
| OrganizationId: args.OrgID, | ||
| CollectionType: collectionType, | ||
| PipelineName: &args.PipelineName, | ||
| IndexSpec: indexSpec, | ||
| }) | ||
| if err != nil { | ||
| return fmt.Errorf("failed to create index: %w", err) | ||
| } | ||
|
|
||
| printf(c.App.Writer, "Create index request sent successfully") | ||
|
|
||
| return nil | ||
| } | ||
|
|
||
| type deleteCustomIndexArgs struct { | ||
| OrgID string | ||
| CollectionType string | ||
| PipelineName string | ||
| IndexName string | ||
| } | ||
|
|
||
| // DeleteCustomIndexAction deletes a custom index for a specified organization and collection type using the provided index name. | ||
| func DeleteCustomIndexAction(c *cli.Context, args deleteCustomIndexArgs) error { | ||
| client, err := newViamClient(c) | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| collectionType, err := validateCollectionTypeArgs(c, args.CollectionType) | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| _, err = client.dataClient.DeleteIndex(context.Background(), &pb.DeleteIndexRequest{ | ||
| OrganizationId: args.OrgID, | ||
| CollectionType: collectionType, | ||
| PipelineName: &args.PipelineName, | ||
| IndexName: args.IndexName, | ||
| }) | ||
| if err != nil { | ||
| return fmt.Errorf("failed to delete index: %w", err) | ||
| } | ||
|
|
||
| printf(c.App.Writer, "Index (name: %s) deleted successfully", args.IndexName) | ||
|
|
||
| return nil | ||
| } | ||
|
|
||
| type listCustomIndexesArgs struct { | ||
| OrgID string | ||
| CollectionType string | ||
| PipelineName string | ||
| } | ||
|
|
||
| // ListCustomIndexesAction lists all custom indexes for a specified organization and collection type. | ||
| func ListCustomIndexesAction(c *cli.Context, args listCustomIndexesArgs) error { | ||
| client, err := newViamClient(c) | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| collectionType, err := validateCollectionTypeArgs(c, args.CollectionType) | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| resp, err := client.dataClient.ListIndexes(context.Background(), &pb.ListIndexesRequest{ | ||
| OrganizationId: args.OrgID, | ||
| CollectionType: collectionType, | ||
| PipelineName: &args.PipelineName, | ||
| }) | ||
| if err != nil { | ||
| return fmt.Errorf("failed to list indexes: %w", err) | ||
| } | ||
|
|
||
| if len(resp.Indexes) == 0 { | ||
| printf(c.App.Writer, "No indexes found") | ||
| return nil | ||
| } | ||
|
|
||
| printf(c.App.Writer, "Indexes:\n") | ||
| for _, index := range resp.Indexes { | ||
| printf(c.App.Writer, "- Name: %s\n", index.IndexName) | ||
| printf(c.App.Writer, " Spec: %s\n", index.IndexSpec) | ||
| } | ||
|
|
||
| return nil | ||
| } | ||
|
|
||
| func validateCollectionTypeArgs(c *cli.Context, collectionType string) (pb.IndexableCollection, error) { | ||
| var collectionTypeProto pb.IndexableCollection | ||
| switch collectionType { | ||
| case hotStoreCollectionTypeStr: | ||
| collectionTypeProto = hotStoreCollectionType | ||
| case pipelineSinkCollectionTypeStr: | ||
| collectionTypeProto = pipelineSinkCollectionType | ||
| default: | ||
| return unspecifiedCollectionType, errInvalidCollectionType | ||
| } | ||
|
|
||
| collectionTypeFlag := c.String(dataFlagCollectionType) | ||
| pipelineName := c.String(dataFlagPipelineName) | ||
|
|
||
| if collectionTypeFlag == pipelineSinkCollectionTypeStr && pipelineName == "" { | ||
| return unspecifiedCollectionType, errPipelineNameRequired | ||
| } | ||
|
|
||
| if collectionTypeFlag != pipelineSinkCollectionTypeStr && pipelineName != "" { | ||
| return unspecifiedCollectionType, errPipelineNameNotAllowed | ||
| } | ||
|
|
||
| return collectionTypeProto, nil | ||
| } | ||
|
|
||
| func readJSONToByteSlices(filePath string) ([][]byte, error) { | ||
| //nolint:gosec // filePath is a user-provided path for a JSON file containing an index spec | ||
| data, err := os.ReadFile(filePath) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| var indexSpec struct { | ||
| Key json.RawMessage `json:"key"` | ||
| Options json.RawMessage `json:"options,omitempty"` | ||
| } | ||
|
|
||
| if err = json.Unmarshal(data, &indexSpec); err != nil { | ||
| return nil, fmt.Errorf("failed to unmarshal JSON: %w", err) | ||
| } | ||
|
|
||
| if len(indexSpec.Key) == 0 { | ||
| return nil, fmt.Errorf("missing required 'key' field in index spec") | ||
| } | ||
|
|
||
| result := make([][]byte, 0, 2) | ||
| result = append(result, []byte(indexSpec.Key)) | ||
|
|
||
| if len(indexSpec.Options) > 0 { | ||
| result = append(result, []byte(indexSpec.Options)) | ||
| } | ||
|
|
||
| return result, nil | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure if we wanna print more info here like what the collection type / pipeline name for each index is
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, name and spec should be fine for now. The collection type / pipeline name will be the same for each index, and the customer will have just entered those in the request, so those fields would make the response longer without adding new info.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
True