Skip to content

Commit

Permalink
Users API (#15)
Browse files Browse the repository at this point in the history
  • Loading branch information
Rukomoynikov authored Jul 27, 2024
1 parent 6cab165 commit d4db10f
Show file tree
Hide file tree
Showing 11 changed files with 263 additions and 0 deletions.
40 changes: 40 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
1 change: 1 addition & 0 deletions lib/milvus.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,5 @@ module Milvus
autoload :Indexes, "milvus/indexes"
autoload :Partitions, "milvus/partitions"
autoload :Roles, "milvus/roles"
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 @@ -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
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
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 d4db10f

Please sign in to comment.