Skip to content

Commit

Permalink
Users API
Browse files Browse the repository at this point in the history
  • Loading branch information
Rukomoynikov committed Jul 27, 2024
1 parent 057888f commit c321398
Show file tree
Hide file tree
Showing 11 changed files with 229 additions and 0 deletions.
1 change: 1 addition & 0 deletions lib/milvus.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,5 @@ module Milvus
autoload :Entities, "milvus/entities"
autoload :Indexes, "milvus/indexes"
autoload :Partitions, "milvus/partitions"
autoload :Users, "milvus/users"
end
4 changes: 4 additions & 0 deletions lib/milvus/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ def indexes
@indexes ||= Milvus::Indexes.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
Expand Down
96 changes: 96 additions & 0 deletions lib/milvus/users.rb
Original file line number Diff line number Diff line change
@@ -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
6 changes: 6 additions & 0 deletions spec/fixtures/roles/describe.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{"code": 0,
"data": [
{"dbName": "*", "grantor": "root", "objectName": "*", "objectType": "Collection", "privilege": "IndexDetail"},
{"dbName": "*", "grantor": "root", "objectName": "*", "objectType": "Global", "privilege": "DescribeCollection"}
]
}
1 change: 1 addition & 0 deletions spec/fixtures/users/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/users/describe.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/users/drop.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"code": 0, "data": []}
Empty file.
1 change: 1 addition & 0 deletions spec/fixtures/users/revoke_role.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/users/update_password.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"code": 0, "data": {}}
117 changes: 117 additions & 0 deletions spec/milvus/users_spec.rb
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit c321398

Please sign in to comment.