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

Implement Kong server info endpoints #14

Merged
merged 4 commits into from
Apr 20, 2017
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
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,16 @@ consumer = Kong::Consumer.find_by_username('testuser')
consumer.acls
```

#### Server Information

```ruby
Kong::Server.info
Kong::Server.version
Kong::Server.status
Kong::Server.cluster
Kong::Server.remove_node(node_name)
```

## Contributing

1. Fork it ( https://github.com/kontena/kong-client-ruby/fork )
Expand Down
1 change: 1 addition & 0 deletions lib/kong.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,4 @@
require_relative 'kong/key_auth'
require_relative 'kong/jwt'
require_relative 'kong/acl'
require_relative 'kong/server'
22 changes: 22 additions & 0 deletions lib/kong/plugin.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,27 @@ class Plugin

ATTRIBUTE_NAMES = %w(id api_id name config enabled consumer_id).freeze
API_END_POINT = '/plugins/'.freeze

# Create resource
def create
if attributes['config']
attributes['config'].each do |key, value|
attributes["config.#{key}"] = value
end
attributes.delete('config')
end
super
end

# update resource
def update
if attributes['config']
attributes['config'].each do |key, value|
attributes["config.#{key}"] = value
end
attributes.delete('config')
end
super
end
end
end
23 changes: 23 additions & 0 deletions lib/kong/server.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
module Kong
class Server
def self.version
self.info['version'] rescue nil
end

def self.info
Client.instance.get('/')
end

def self.status
Client.instance.get('/status')
end

def self.cluster
Client.instance.get('/cluster')
end

def self.remove_node(name)
Client.instance.delete("/cluster/nodes/#{name}")
end
end
end
20 changes: 20 additions & 0 deletions spec/kong/plugin_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,24 @@
expect(subject.api_end_point).to eq('/apis/:api_id/plugins/')
end
end

describe '#create' do
it 'transforms config keys to config.key format' do
headers = { 'Content-Type' => 'application/x-www-form-urlencoded' }
attributes = { 'api_id' => ':api_id', 'config.anonymous' => '12345' }
expect(Kong::Client.instance).to receive(:post).with('/apis/:api_id/plugins/', nil, attributes, headers).and_return(attributes)
subject = described_class.new({ api_id: ':api_id', config: { 'anonymous' => '12345' } })
subject.create
end
end

describe '#update' do
it 'transforms config keys to config.key format' do
headers = { 'Content-Type' => 'application/x-www-form-urlencoded' }
attributes = { 'api_id' => ':api_id', 'config.anonymous' => '12345' }
expect(Kong::Client.instance).to receive(:patch).with('/apis/:api_id/plugins/', nil, attributes, headers).and_return(attributes)
subject = described_class.new({ api_id: ':api_id', config: { 'anonymous' => '12345' } })
subject.update
end
end
end
39 changes: 39 additions & 0 deletions spec/kong/server_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
require_relative "../spec_helper"

describe Kong::Server do
describe '.info' do
it 'makes GET / request' do
expect(Kong::Client.instance).to receive(:get).with('/')
described_class.info
end
end

describe '.version' do
it 'returns version information' do
allow(Kong::Client.instance).to receive(:get).with('/')
.and_return({ 'version' => '0.10.0' })
expect(described_class.version).to eq('0.10.0')
end
end

describe '.status' do
it 'makes GET /status request' do
expect(Kong::Client.instance).to receive(:get).with('/status')
described_class.status
end
end

describe '.cluster' do
it 'makes GET /cluster request' do
expect(Kong::Client.instance).to receive(:get).with('/cluster')
described_class.cluster
end
end

describe '.remove_node' do
it 'makes DELETE /cluster/nodes/:name request' do
expect(Kong::Client.instance).to receive(:delete).with('/cluster/nodes/:name')
described_class.remove_node(':name')
end
end
end