From 04ded533bf9b3713bd7c58fe48a18478e81562f2 Mon Sep 17 00:00:00 2001 From: Max Rukomoynikov Date: Sun, 28 Jul 2024 22:24:02 +0100 Subject: [PATCH] Aliases API --- README.md | 25 ++++++++- lib/milvus.rb | 1 + lib/milvus/aliases.rb | 78 ++++++++++++++++++++++++++ lib/milvus/client.rb | 4 ++ spec/fixtures/aliases/alter.json | 1 + spec/fixtures/aliases/create.json | 1 + spec/fixtures/aliases/describe.json | 1 + spec/fixtures/aliases/drop.json | 1 + spec/fixtures/aliases/list.json | 1 + spec/milvus/aliases_spec.rb | 85 +++++++++++++++++++++++++++++ spec/milvus/roles_spec.rb | 2 +- 11 files changed, 197 insertions(+), 3 deletions(-) create mode 100644 lib/milvus/aliases.rb create mode 100644 spec/fixtures/aliases/alter.json create mode 100644 spec/fixtures/aliases/create.json create mode 100644 spec/fixtures/aliases/describe.json create mode 100644 spec/fixtures/aliases/drop.json create mode 100644 spec/fixtures/aliases/list.json create mode 100644 spec/milvus/aliases_spec.rb diff --git a/README.md b/README.md index 1c921d8..2ec9daf 100644 --- a/README.md +++ b/README.md @@ -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 @@ -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 @@ -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 diff --git a/lib/milvus.rb b/lib/milvus.rb index 5197263..78b7515 100644 --- a/lib/milvus.rb +++ b/lib/milvus.rb @@ -13,4 +13,5 @@ module Milvus autoload :Partitions, "milvus/partitions" autoload :Roles, "milvus/roles" autoload :Users, "milvus/users" + autoload :Aliases, "milvus/aliases" end diff --git a/lib/milvus/aliases.rb b/lib/milvus/aliases.rb new file mode 100644 index 0000000..9d2b6f6 --- /dev/null +++ b/lib/milvus/aliases.rb @@ -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 diff --git a/lib/milvus/client.rb b/lib/milvus/client.rb index 92c73a7..9062382 100644 --- a/lib/milvus/client.rb +++ b/lib/milvus/client.rb @@ -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 diff --git a/spec/fixtures/aliases/alter.json b/spec/fixtures/aliases/alter.json new file mode 100644 index 0000000..4cfb27b --- /dev/null +++ b/spec/fixtures/aliases/alter.json @@ -0,0 +1 @@ +{"code": 0, "data": {}} \ No newline at end of file diff --git a/spec/fixtures/aliases/create.json b/spec/fixtures/aliases/create.json new file mode 100644 index 0000000..4cfb27b --- /dev/null +++ b/spec/fixtures/aliases/create.json @@ -0,0 +1 @@ +{"code": 0, "data": {}} \ No newline at end of file diff --git a/spec/fixtures/aliases/describe.json b/spec/fixtures/aliases/describe.json new file mode 100644 index 0000000..444fa7f --- /dev/null +++ b/spec/fixtures/aliases/describe.json @@ -0,0 +1 @@ +{"code": 0, "data": {"aliasName": "bob_alias", "collectionName": "example_collection_2", "dbName": "default"}} \ No newline at end of file diff --git a/spec/fixtures/aliases/drop.json b/spec/fixtures/aliases/drop.json new file mode 100644 index 0000000..4cfb27b --- /dev/null +++ b/spec/fixtures/aliases/drop.json @@ -0,0 +1 @@ +{"code": 0, "data": {}} \ No newline at end of file diff --git a/spec/fixtures/aliases/list.json b/spec/fixtures/aliases/list.json new file mode 100644 index 0000000..1aac39f --- /dev/null +++ b/spec/fixtures/aliases/list.json @@ -0,0 +1 @@ +{"code": 0, "data": []} \ No newline at end of file diff --git a/spec/milvus/aliases_spec.rb b/spec/milvus/aliases_spec.rb new file mode 100644 index 0000000..c457fc5 --- /dev/null +++ b/spec/milvus/aliases_spec.rb @@ -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 diff --git a/spec/milvus/roles_spec.rb b/spec/milvus/roles_spec.rb index 72cfa0a..cba40a1 100644 --- a/spec/milvus/roles_spec.rb +++ b/spec/milvus/roles_spec.rb @@ -1,4 +1,4 @@ -# spec/milvus/users_spec.rb +# spec/milvus/roles_spec.rb require "spec_helper" require "faraday"