Skip to content
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

Aliases API #18

Merged
merged 1 commit into from
Jul 28, 2024
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
25 changes: 23 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ https://docs.zilliz.com/reference/restful/data-plane-v2
## TODOs
- [X] Support for [User endpoints](https://docs.zilliz.com/reference/restful/user-operations-v2)
- [X] Support for [Role endpoints](https://docs.zilliz.com/reference/restful/role-operations-v2)
- [ ] Support for [Alias endpoints](https://docs.zilliz.com/reference/restful/alias-operations-v2)
- [X] Support for [Alias endpoints](https://docs.zilliz.com/reference/restful/alias-operations-v2)

## Installation

Expand Down Expand Up @@ -311,6 +311,27 @@ client.users.grant_role(user_name: 'user_name', role_name: 'admin')
# Revoke role from the user
client.users.revoke_role(user_name: 'user_name', role_name: 'admin')
```
### Aliases
```ruby
# Lists all existing collection aliases in the specified database
client.aliases.list
```
```ruby
# Describes the details of a specific alias
client.aliases.describe
```
```ruby
# Reassigns the alias of one collection to another.
client.aliases.alter
```
```ruby
# Drops a specified alias
client.aliases.drop
```
```ruby
# Creates an alias for an existing collection
client.aliases.create
```

## Development

Expand All @@ -320,7 +341,7 @@ To install this gem onto your local machine, run `bundle exec rake install`. To

## Development with Docker

Run `docker compose run --rm ruby_app bash` and install required gems (`bundle install`). It will give you a fully working development environment with Milvius services and gem's code.
Run `docker compose run --rm ruby_app bash` and install required gems (`bundle install`). It will give you a fully working development environment with Milvus services and gem's code.

For example inside docker container run `bin/console` and inside the ruby console:
```ruby
Expand Down
1 change: 1 addition & 0 deletions lib/milvus.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,5 @@ module Milvus
autoload :Partitions, "milvus/partitions"
autoload :Roles, "milvus/roles"
autoload :Users, "milvus/users"
autoload :Aliases, "milvus/aliases"
end
78 changes: 78 additions & 0 deletions lib/milvus/aliases.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
# https://docs.zilliz.com/reference/restful/alias-operations-v2

module Milvus
class Aliases < Base
PATH = "aliases"

# Lists available roles on the server
#
# @return [Hash] The response from the server
def list
response = client.connection.post("#{PATH}/list") do |req|
req.body = {}
end

response.body
end

# Describes the details of a specific alias
#
# @param alias_name [String] The name of the alias to describe
# @return [Hash] The response from the server
def describe(alias_name:)
response = client.connection.post("#{PATH}/describe") do |req|
req.body = {
aliasName: alias_name
}
end

response.body
end

# Reassigns the alias of one collection to another
#
# @param alias_name [String] The alias of the collection
# @param collection_name [String] The name of the target collection to reassign an alias to
# @return [Hash] The response from the server
def alter(alias_name:, collection_name:)
response = client.connection.post("#{PATH}/alter") do |req|
req.body = {
aliasName: alias_name,
collectionName: collection_name
}
end

response.body
end

# This operation drops a specified alias
#
# @param alias_name [String] The alias to drop
# @return [Hash] The response from the server
def drop(alias_name:)
response = client.connection.post("#{PATH}/drop") do |req|
req.body = {
aliasName: alias_name
}
end

response.body
end

# This operation creates an alias for an existing collection. A collection can have multiple aliases, while an alias can be associated with only one collection.
#
# @param alias_name [String] The alias of the collection
# @param collection_name [String] The name of the target collection to reassign an alias to
# @return [Hash] The response from the server
def create(alias_name:, collection_name:)
response = client.connection.post("#{PATH}/create") do |req|
req.body = {
aliasName: alias_name,
collectionName: collection_name
}
end

response.body
end
end
end
4 changes: 4 additions & 0 deletions lib/milvus/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ def users
@users ||= Milvus::Users.new(client: self)
end

def aliases
@aliases ||= Milvus::Aliases.new(client: self)
end

def connection
@connection ||= Faraday.new(url: "#{url}/#{API_VERSION}/") do |faraday|
if api_key
Expand Down
1 change: 1 addition & 0 deletions spec/fixtures/aliases/alter.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"code": 0, "data": {}}
1 change: 1 addition & 0 deletions spec/fixtures/aliases/create.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"code": 0, "data": {}}
1 change: 1 addition & 0 deletions spec/fixtures/aliases/describe.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"code": 0, "data": {"aliasName": "bob_alias", "collectionName": "example_collection_2", "dbName": "default"}}
1 change: 1 addition & 0 deletions spec/fixtures/aliases/drop.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"code": 0, "data": {}}
1 change: 1 addition & 0 deletions spec/fixtures/aliases/list.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"code": 0, "data": []}
85 changes: 85 additions & 0 deletions spec/milvus/aliases_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
# spec/milvus/aliases_spec.rb

require "spec_helper"
require "faraday"

RSpec.describe Milvus::Aliases do
let(:connection) { instance_double("Faraday::Connection") }
let(:client) { instance_double("Client", connection: connection) }
let(:aliases) { described_class.new(client: client) }

describe "#list" do
let(:response_body) { File.read("spec/fixtures/aliases/list.json") }
let(:response) { instance_double("Faraday::Response", body: response_body) }

it "lists aliases" do
expect(connection).to receive(:post)
.with("aliases/list")
.and_yield(Faraday::Request.new)
.and_return(response)
result = aliases.list

expect(result).to eq(response_body)
end
end

describe "#describe" do
let(:response_body) { File.read("spec/fixtures/aliases/describe.json") }
let(:response) { instance_double("Faraday::Response", body: response_body) }

it "describes the details of a specific alias" do
expect(connection).to receive(:post)
.with("aliases/describe")
.and_yield(Faraday::Request.new)
.and_return(response)
result = aliases.describe(alias_name: "bob")

expect(result).to eq(response_body)
end
end

describe "#alter" do
let(:response_body) { File.read("spec/fixtures/aliases/alter.json") }
let(:response) { instance_double("Faraday::Response", body: response_body) }

it "reassigns the alias of one collection to another" do
expect(connection).to receive(:post)
.with("aliases/alter")
.and_yield(Faraday::Request.new)
.and_return(response)
result = aliases.alter(alias_name: "bob", collection_name: "new_collection")

expect(result).to eq(response_body)
end
end

describe "#drop" do
let(:response_body) { File.read("spec/fixtures/aliases/drop.json") }
let(:response) { instance_double("Faraday::Response", body: response_body) }

it "drops the alias" do
expect(connection).to receive(:post)
.with("aliases/drop")
.and_yield(Faraday::Request.new)
.and_return(response)
result = aliases.drop(alias_name: "bob")

expect(result).to eq(response_body)
end
end

describe "#create" do
let(:response_body) { File.read("spec/fixtures/aliases/create.json") }
let(:response) { instance_double("Faraday::Response", body: response_body) }

it "creates alias for collection" do
expect(connection).to receive(:post)
.with("aliases/create")
.and_yield(Faraday::Request.new)
.and_return(response)
result = aliases.create(alias_name: "bob", collection_name: "quick_setup")

expect(result).to eq(response_body)
end
end
end
2 changes: 1 addition & 1 deletion spec/milvus/roles_spec.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# spec/milvus/users_spec.rb
# spec/milvus/roles_spec.rb

require "spec_helper"
require "faraday"
Expand Down