diff --git a/README.md b/README.md index 7de05dc..a0b336f 100644 --- a/README.md +++ b/README.md @@ -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 ) diff --git a/lib/kong.rb b/lib/kong.rb index 71af3c3..5fe2702 100644 --- a/lib/kong.rb +++ b/lib/kong.rb @@ -13,3 +13,4 @@ require_relative 'kong/key_auth' require_relative 'kong/jwt' require_relative 'kong/acl' +require_relative 'kong/server' diff --git a/lib/kong/plugin.rb b/lib/kong/plugin.rb index 2bac142..66fbb36 100644 --- a/lib/kong/plugin.rb +++ b/lib/kong/plugin.rb @@ -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 diff --git a/lib/kong/server.rb b/lib/kong/server.rb new file mode 100644 index 0000000..0c8e943 --- /dev/null +++ b/lib/kong/server.rb @@ -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 diff --git a/spec/kong/plugin_spec.rb b/spec/kong/plugin_spec.rb index fe02913..fe1d268 100644 --- a/spec/kong/plugin_spec.rb +++ b/spec/kong/plugin_spec.rb @@ -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 diff --git a/spec/kong/server_spec.rb b/spec/kong/server_spec.rb new file mode 100644 index 0000000..1a1fe2b --- /dev/null +++ b/spec/kong/server_spec.rb @@ -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