diff --git a/README.md b/README.md index e15f372..b921d62 100644 --- a/README.md +++ b/README.md @@ -272,6 +272,46 @@ client.partitions.drop( ) ``` +### Roles +```ruby +# List roles available on the server +client.roles.list +``` +```ruby +# Describe the role +client.roles.describe(role_name: 'public') +``` + +### Users +```ruby +# Create new user +client.users.create(user_name: 'user_name', password: 'password') +``` +```ruby +# List of roles assigned to the user +client.users.describe(user_name: 'user_name') +``` +```ruby +# List all users in the specified database. +client.users.list +``` +```ruby +# Drop existing user +client.users.drop(user_name: 'user_name') +``` +```ruby +# Update password for the user +client.users.update_password(user_name: 'user_name', password: 'old_password', new_password: 'new_password') +``` +```ruby +# Grant role to the user +client.users.grant_role(user_name: 'user_name', role_name: 'admin') +``` +```ruby +# Revoke role from the user +client.users.revoke_role(user_name: 'user_name', role_name: 'admin') +``` + ## Development After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment. diff --git a/lib/milvus.rb b/lib/milvus.rb index 606e59b..5197263 100644 --- a/lib/milvus.rb +++ b/lib/milvus.rb @@ -12,4 +12,5 @@ module Milvus autoload :Indexes, "milvus/indexes" autoload :Partitions, "milvus/partitions" autoload :Roles, "milvus/roles" + autoload :Users, "milvus/users" end diff --git a/lib/milvus/client.rb b/lib/milvus/client.rb index 8a677b0..92c73a7 100644 --- a/lib/milvus/client.rb +++ b/lib/milvus/client.rb @@ -33,6 +33,10 @@ def roles @roles ||= Milvus::Roles.new(client: self) end + def users + @users ||= Milvus::Users.new(client: self) + end + def connection @connection ||= Faraday.new(url: "#{url}/#{API_VERSION}/") do |faraday| if api_key diff --git a/lib/milvus/users.rb b/lib/milvus/users.rb new file mode 100644 index 0000000..b278406 --- /dev/null +++ b/lib/milvus/users.rb @@ -0,0 +1,96 @@ +# frozen_string_literal: true + +module Milvus + class Users < Base + PATH = "users" + + # Create a user + # + # @param user_name [String] Username for the user + # @param password [String] Password for the user + # @return [Hash] Server response + def create(user_name:, password:) + response = client.connection.post("#{PATH}/create") do |req| + req.body = { + userName: user_name, + password: password + } + end + + response.body + end + + # Describe a user + # + # @param user_name [String] Username for the user + # @return [Hash] Server response + def describe(user_name:) + response = client.connection.post("#{PATH}/describe") do |req| + req.body = { + userName: user_name + } + end + + response.body + end + + # List users + # + # @return [Hash] Server response + def list + response = client.connection.post("#{PATH}/list") do |req| + req.body = {} + end + + response.body + end + + # Drops a user + # + # @param user_name [String] Username for the user + # @return [Hash] Server response + def drop(user_name:) + response = client.connection.post("#{PATH}/drop") do |req| + req.body = { + userName: user_name + } + end + + response.body + end + + def update_password(user_name:, password:, new_password:) + response = client.connection.post("#{PATH}/update_password") do |req| + req.body = { + userName: user_name, + password: password, + newPassword: new_password + } + end + + response.body + end + + def grant_role(user_name:, role_name:) + response = client.connection.post("#{PATH}/grant_role") do |req| + req.body = { + userName: user_name, + roleName: role_name + } + end + + response.body + end + + def revoke_role(user_name:, role_name:) + response = client.connection.post("#{PATH}/revoke_role") do |req| + req.body = { + userName: user_name, + roleName: role_name + } + end + + response.body + end + end +end diff --git a/spec/fixtures/users/create.json b/spec/fixtures/users/create.json new file mode 100644 index 0000000..4cfb27b --- /dev/null +++ b/spec/fixtures/users/create.json @@ -0,0 +1 @@ +{"code": 0, "data": {}} \ No newline at end of file diff --git a/spec/fixtures/users/describe.json b/spec/fixtures/users/describe.json new file mode 100644 index 0000000..1aac39f --- /dev/null +++ b/spec/fixtures/users/describe.json @@ -0,0 +1 @@ +{"code": 0, "data": []} \ No newline at end of file diff --git a/spec/fixtures/users/drop.json b/spec/fixtures/users/drop.json new file mode 100644 index 0000000..1aac39f --- /dev/null +++ b/spec/fixtures/users/drop.json @@ -0,0 +1 @@ +{"code": 0, "data": []} \ No newline at end of file diff --git a/spec/fixtures/users/grant_role.json b/spec/fixtures/users/grant_role.json new file mode 100644 index 0000000..e69de29 diff --git a/spec/fixtures/users/revoke_role.json b/spec/fixtures/users/revoke_role.json new file mode 100644 index 0000000..4cfb27b --- /dev/null +++ b/spec/fixtures/users/revoke_role.json @@ -0,0 +1 @@ +{"code": 0, "data": {}} \ No newline at end of file diff --git a/spec/fixtures/users/update_password.json b/spec/fixtures/users/update_password.json new file mode 100644 index 0000000..4cfb27b --- /dev/null +++ b/spec/fixtures/users/update_password.json @@ -0,0 +1 @@ +{"code": 0, "data": {}} \ No newline at end of file diff --git a/spec/milvus/users_spec.rb b/spec/milvus/users_spec.rb new file mode 100644 index 0000000..59be15b --- /dev/null +++ b/spec/milvus/users_spec.rb @@ -0,0 +1,117 @@ +# spec/milvus/users_spec.rb + +require "spec_helper" +require "faraday" + +RSpec.describe Milvus::Users do + let(:connection) { instance_double("Faraday::Connection") } + let(:client) { instance_double("Client", connection: connection) } + let(:users) { described_class.new(client: client) } + + describe "#create" do + let(:response_body) { File.read("spec/fixtures/users/create.json") } + let(:response) { instance_double("Faraday::Response", body: response_body) } + + it "creates a user" do + expect(connection).to receive(:post) + .with("users/create") + .and_yield(Faraday::Request.new) + .and_return(response) + result = users.create(user_name: "user_name", password: "password") + + expect(result).to eq(response_body) + end + end + + describe "#describe" do + let(:response_body) { File.read("spec/fixtures/users/describe.json") } + let(:response) { instance_double("Faraday::Response", body: response_body) } + + it "describes the user" do + expect(connection).to receive(:post) + .with("users/describe") + .and_yield(Faraday::Request.new) + .and_return(response) + result = users.describe(user_name: "root") + + expect(result).to eq(response_body) + end + end + + describe "#list" do + let(:response_body) { File.read("spec/fixtures/users/list.json") } + let(:response) { instance_double("Faraday::Response", body: response_body) } + + it "responds with a list of users" do + expect(connection).to receive(:post) + .with("users/list") + .and_yield(Faraday::Request.new) + .and_return(response) + result = users.list + + expect(result).to eq(response_body) + end + end + + describe "#drop" do + let(:response_body) { File.read("spec/fixtures/users/drop.json") } + let(:response) { instance_double("Faraday::Response", body: response_body) } + + it "drops the user" do + expect(connection).to receive(:post) + .with("users/drop") + .and_yield(Faraday::Request.new) + .and_return(response) + result = users.drop(user_name: "root") + + expect(result).to eq(response_body) + end + end + + describe "#update_password" do + let(:response_body) { File.read("spec/fixtures/users/update_password.json") } + let(:response) { instance_double("Faraday::Response", body: response_body) } + + it "updates the users's password" do + expect(connection).to receive(:post) + .with("users/update_password") + .and_yield(Faraday::Request.new) + .and_return(response) + result = users.update_password user_name: "username", + password: "old_password", + new_password: "new_password" + + expect(result).to eq(response_body) + end + end + + describe "#grant_role" do + let(:response_body) { File.read("spec/fixtures/users/grant_role.json") } + let(:response) { instance_double("Faraday::Response", body: response_body) } + + it "sets role to the user" do + expect(connection).to receive(:post) + .with("users/grant_role") + .and_yield(Faraday::Request.new) + .and_return(response) + result = users.grant_role user_name: "user_name", role_name: "admin" + + expect(result).to eq(response_body) + end + end + + describe "#revoke_role" do + let(:response_body) { File.read("spec/fixtures/users/revoke_role.json") } + let(:response) { instance_double("Faraday::Response", body: response_body) } + + it "revokes the role" do + expect(connection).to receive(:post) + .with("users/revoke_role") + .and_yield(Faraday::Request.new) + .and_return(response) + result = users.revoke_role user_name: "user_name", role_name: "admin" + + expect(result).to eq(response_body) + end + end +end