From c782bbc96703236f98444955778e47dfabddcfaa Mon Sep 17 00:00:00 2001 From: marc tobias Date: Wed, 24 Apr 2019 19:18:17 +0200 Subject: [PATCH] Test against Kong 1.1, split /apis into /routes and /services --- README.md | 65 +++++++++++++++++++++++---------- kong.gemspec | 2 +- lib/kong.rb | 5 ++- lib/kong/acl.rb | 2 +- lib/kong/api.rb | 19 ---------- lib/kong/base.rb | 1 + lib/kong/basic_auth.rb | 2 +- lib/kong/belongs_to_api.rb | 30 --------------- lib/kong/belongs_to_consumer.rb | 41 ++++++++++++++++----- lib/kong/belongs_to_service.rb | 30 +++++++++++++++ lib/kong/key_auth.rb | 2 +- lib/kong/oauth_app.rb | 2 +- lib/kong/plugin.rb | 7 +++- lib/kong/route.rb | 18 +++++++++ lib/kong/service.rb | 18 +++++++++ lib/kong/version.rb | 2 +- spec/kong/acl_spec.rb | 2 +- spec/kong/api_spec.rb | 32 ---------------- spec/kong/basic_auth_spec.rb | 4 +- spec/kong/key_auth_spec.rb | 6 +-- spec/kong/oauth_app_spec.rb | 2 +- spec/kong/plugin_spec.rb | 32 ++++++++-------- spec/kong/route_spec.rb | 31 ++++++++++++++++ spec/kong/service_spec.rb | 31 ++++++++++++++++ 24 files changed, 244 insertions(+), 142 deletions(-) delete mode 100644 lib/kong/api.rb delete mode 100644 lib/kong/belongs_to_api.rb create mode 100644 lib/kong/belongs_to_service.rb create mode 100644 lib/kong/route.rb create mode 100644 lib/kong/service.rb delete mode 100644 spec/kong/api_spec.rb create mode 100644 spec/kong/route_spec.rb create mode 100644 spec/kong/service_spec.rb diff --git a/README.md b/README.md index f4eaea5..192c98c 100644 --- a/README.md +++ b/README.md @@ -53,7 +53,7 @@ consumer = Kong::Consumer.find_by_custom_id('custom_id') ### All Resources and Actions -To see the complete Kong Admin API documentation, please visit: https://getkong.org/docs/0.11.x/admin-api/ +To see the complete Kong Admin API documentation, please visit: https://getkong.org/docs/1.1.x/admin-api/ #### Consumer @@ -80,31 +80,58 @@ consumer.oauth2_tokens #### API +Starting Kong 0.14 API resources are split into Services and Routes. A Route it attached to a +Service. Kong 1.x no longer supports API resources. + +#### Service + +```ruby +Kong::Service.list(filters) +Kong::Service.all() +Kong::Service.find(id) +Kong::Service.find_by_*(value) +Kong::Service.create(attributes) + +service = Kong::Service.new({ + name: 'Mockbin', + protocol: 'https', + host: 'mockbin.com', # upstream + path: '/someremoteservice' +}) +service.get # reloads resource +service.create +service.update +service.save # requests create_or_update action +service.delete + +service.plugins +``` + +#### Route + ```ruby -Kong::Api.list(filters) -Kong::Api.all() -Kong::Api.find(id) -Kong::Api.find_by_*(value) -Kong::Api.create(attributes) +Kong::Route.list(filters) +Kong::Route.all() +Kong::Route.find(id) +Kong::Route.find_by_*(value) +Kong::Route.create(attributes) -api = Kong::Api.new({ +route = Kong::Route.new({ name: 'Mockbin', - hosts: ['example.com'], + service_id: '5fd1z584-1adb-40a5-c042-63b19db49x21', uris: ['/someservice'], methods: ['GET'], - strip_uri: false, - preserve_host: false, - upstream_url: 'https://mockbin.com' + strip_path: false, + preserve_host: false }) -api.get # reloads resource -api.create -api.update -api.save # requests create_or_update action -api.delete - -api.plugins +route.get # reloads resource +route.create +route.update +route.save # requests create_or_update action +route.delete ``` + #### Plugin ```ruby @@ -115,7 +142,7 @@ Kong::Plugin.find_by_*(value) Kong::Plugin.create(attributes) plugin = Kong::Plugin.new({ - api_id: '5fd1z584-1adb-40a5-c042-63b19db49x21', + service_id: '5fd1z584-1adb-40a5-c042-63b19db49x21', consumer_id: 'a3dX2dh2-1adb-40a5-c042-63b19dbx83hF4', name: 'rate-limiting', config: { diff --git a/kong.gemspec b/kong.gemspec index 1466638..20e11e4 100644 --- a/kong.gemspec +++ b/kong.gemspec @@ -18,7 +18,7 @@ Gem::Specification.new do |spec| spec.required_ruby_version = ">= 2.0.0" - spec.add_development_dependency "bundler", "~> 1.7" + spec.add_development_dependency "bundler", "~> 1.17" spec.add_development_dependency "rake", "~> 10.0" spec.add_runtime_dependency "excon" end diff --git a/lib/kong.rb b/lib/kong.rb index e212460..04cc2c4 100644 --- a/lib/kong.rb +++ b/lib/kong.rb @@ -1,7 +1,8 @@ require 'kong/version' require_relative 'kong/base' -require_relative 'kong/api' -require_relative 'kong/belongs_to_api' +require_relative 'kong/route' +require_relative 'kong/service' +require_relative 'kong/belongs_to_service' require_relative 'kong/client' require_relative 'kong/consumer' require_relative 'kong/belongs_to_consumer' diff --git a/lib/kong/acl.rb b/lib/kong/acl.rb index ccee4bb..c12f68f 100644 --- a/lib/kong/acl.rb +++ b/lib/kong/acl.rb @@ -2,7 +2,7 @@ module Kong class Acl include Base include BelongsToConsumer - ATTRIBUTE_NAMES = %w(id group consumer_id).freeze + ATTRIBUTE_NAMES = %w(id group consumer_attrs).freeze API_END_POINT = "/acls/".freeze end end diff --git a/lib/kong/api.rb b/lib/kong/api.rb deleted file mode 100644 index 3cba30a..0000000 --- a/lib/kong/api.rb +++ /dev/null @@ -1,19 +0,0 @@ -module Kong - class Api - include Base - - ATTRIBUTE_NAMES = %w( - id name request_host request_path strip_request_path - hosts uris strip_uri preserve_host upstream_url retries - upstream_connect_timeout upstream_send_timeout upstream_read_timeout - https_only http_if_terminated methods - ).freeze - API_END_POINT = '/apis/'.freeze - - ## - # @return [Array] - def plugins - Plugin.list({ api_id: self.id }) - end - end -end diff --git a/lib/kong/base.rb b/lib/kong/base.rb index 0a5e7bd..0675aae 100644 --- a/lib/kong/base.rb +++ b/lib/kong/base.rb @@ -160,6 +160,7 @@ def init_attributes(attributes) end use_consumer_end_point if respond_to?(:use_consumer_end_point) use_api_end_point if respond_to?(:use_api_end_point) + use_service_end_point if respond_to?(:use_service_end_point) use_upstream_end_point if respond_to?(:use_upstream_end_point) end end diff --git a/lib/kong/basic_auth.rb b/lib/kong/basic_auth.rb index e9e3c6c..82c9b2b 100644 --- a/lib/kong/basic_auth.rb +++ b/lib/kong/basic_auth.rb @@ -2,7 +2,7 @@ module Kong class BasicAuth include Base include BelongsToConsumer - ATTRIBUTE_NAMES = %w(id username password consumer_id).freeze + ATTRIBUTE_NAMES = %w(id username password consumer).freeze API_END_POINT = "/basic-auth/".freeze end end diff --git a/lib/kong/belongs_to_api.rb b/lib/kong/belongs_to_api.rb deleted file mode 100644 index 76c1228..0000000 --- a/lib/kong/belongs_to_api.rb +++ /dev/null @@ -1,30 +0,0 @@ -module Kong - module BelongsToApi - attr_accessor :api - - # Convert API end point relative to Kong API resource - def use_api_end_point - self.api_end_point = "/apis/#{self.api_id}#{self.class::API_END_POINT}" if self.api_id - end - - # Get Api resource - # @return [Kong::Api] - def api - @api ||= Api.find(self.api_id) - end - - # Set Api resource - # @param [Kong::Api] api - def api=(api) - @api = api - self.api_id = api.id - end - - # Set Api id - # @param [String] id - def api_id=(id) - super(id) - use_api_end_point - end - end -end diff --git a/lib/kong/belongs_to_consumer.rb b/lib/kong/belongs_to_consumer.rb index ec35493..24cd482 100644 --- a/lib/kong/belongs_to_consumer.rb +++ b/lib/kong/belongs_to_consumer.rb @@ -4,27 +4,50 @@ module BelongsToConsumer # Convert API end point relative to Kong Consumer resource def use_consumer_end_point - self.api_end_point = "/consumers/#{self.consumer_id}#{self.class::API_END_POINT}" if self.consumer_id + self.api_end_point = "/consumers/#{self.consumer.id}#{self.class::API_END_POINT}" if self.consumer && self.consumer.id end # Get Consumer resource # @return [Kong::Consumer] def consumer - @consumer ||= Consumer.find(self.consumer_id) + if @consumer + return @consumer + elsif self.attributes['consumer'] && self.attributes['consumer'].instance_of?(Hash) + return @consumer = Consumer.new(attributes['consumer']) + elsif self.attributes['consumer'] && self.attributes['consumer'].instance_of?(Kong::Consumer) + @consumer = self.attributes['consumer'] + return @consumer + end + # @consumer ||= Consumer.find(self.consumer.id) + # @consumer ||= self.consumer end + # def initialize(attributes = {}) + # super(attributes) + # use_consumer_end_point + # end + # Set Consumer resource # @param [Kong::Consumer] consumer def consumer=(consumer) @consumer = consumer - self.consumer_id = consumer.id - end - - # Set Consumer id - # @param [String] id - def consumer_id=(id) - super(id) + # self.consumer_attrs = { id: consumer.id } + # super(consumer) + # self.consumer = consumer use_consumer_end_point end + + # # Set Consumer id + # # @param [String] id + # def consumer_id=(id) + # super(id) + # end + + # Set Consumer attributes + # @param [Hash] attributes + # def consumer_attr=(attrs) + # super(attrs) + # use_consumer_end_point + # end end end diff --git a/lib/kong/belongs_to_service.rb b/lib/kong/belongs_to_service.rb new file mode 100644 index 0000000..831b1c0 --- /dev/null +++ b/lib/kong/belongs_to_service.rb @@ -0,0 +1,30 @@ +module Kong + module BelongsToService + attr_accessor :service + + # Convert Service end point relative to Kong API resource + def use_service_end_point + self.api_end_point = "/services/#{self.service_id}#{self.class::API_END_POINT}" if self.service_id + end + + # Get Service resource + # @return [Kong::Service] + def service + @service ||= Service.find(self.service_id) + end + + # Set Service resource + # @param [Kong::Service] service + def service=(service) + @service = service + self.service_id = service.id + end + + # Set Service id + # @param [String] id + def service_id=(id) + super(id) + use_service_end_point + end + end +end diff --git a/lib/kong/key_auth.rb b/lib/kong/key_auth.rb index 7454bfd..0a348fc 100644 --- a/lib/kong/key_auth.rb +++ b/lib/kong/key_auth.rb @@ -2,7 +2,7 @@ module Kong class KeyAuth include Base include BelongsToConsumer - ATTRIBUTE_NAMES = %w(id key consumer_id).freeze + ATTRIBUTE_NAMES = %w(id key consumer).freeze API_END_POINT = "/key-auth/".freeze end end diff --git a/lib/kong/oauth_app.rb b/lib/kong/oauth_app.rb index 948e659..9db67b2 100644 --- a/lib/kong/oauth_app.rb +++ b/lib/kong/oauth_app.rb @@ -2,7 +2,7 @@ module Kong class OAuthApp include Base include BelongsToConsumer - ATTRIBUTE_NAMES = %w(id name client_id client_secret redirect_uri consumer_id).freeze + ATTRIBUTE_NAMES = %w(id name client_id client_secret redirect_uri consumer_attrs).freeze API_END_POINT = "/oauth2/".freeze end end diff --git a/lib/kong/plugin.rb b/lib/kong/plugin.rb index 8d26f97..c1b700b 100644 --- a/lib/kong/plugin.rb +++ b/lib/kong/plugin.rb @@ -3,9 +3,12 @@ module Kong class Plugin include Base - include BelongsToApi + include BelongsToService - ATTRIBUTE_NAMES = %w(id api_id name config enabled consumer_id).freeze + ATTRIBUTE_NAMES = %w( + id name config enabled route_id service_id consumer_id protocols + tags + ).freeze API_END_POINT = '/plugins/'.freeze # Create resource diff --git a/lib/kong/route.rb b/lib/kong/route.rb new file mode 100644 index 0000000..b8458c6 --- /dev/null +++ b/lib/kong/route.rb @@ -0,0 +1,18 @@ +module Kong + class Route + include Base + + ATTRIBUTE_NAMES = %w( + id name protocols methods hosts paths + regex_priority strip_path preserve_host + service_id tags + ).freeze + API_END_POINT = '/routes/'.freeze + + ## + # @return [Array] + def plugins + Plugin.list({ route_id: self.id }) + end + end +end diff --git a/lib/kong/service.rb b/lib/kong/service.rb new file mode 100644 index 0000000..3bf9e4e --- /dev/null +++ b/lib/kong/service.rb @@ -0,0 +1,18 @@ +module Kong + class Service + include Base + + ATTRIBUTE_NAMES = %w( + id name retries protocol host port path + connect_timeout write_timeout upstream_read_timeout + tags + ).freeze + API_END_POINT = '/services/'.freeze + + ## + # @return [Array] + def plugins + Plugin.list({ service_id: self.id }) + end + end +end diff --git a/lib/kong/version.rb b/lib/kong/version.rb index add4d39..413601c 100644 --- a/lib/kong/version.rb +++ b/lib/kong/version.rb @@ -1,3 +1,3 @@ module Kong - VERSION = '0.3.4'.freeze + VERSION = '0.4.3'.freeze end diff --git a/spec/kong/acl_spec.rb b/spec/kong/acl_spec.rb index cb21c45..2b6e844 100644 --- a/spec/kong/acl_spec.rb +++ b/spec/kong/acl_spec.rb @@ -2,7 +2,7 @@ describe Kong::Acl do let(:valid_attribute_names) do - %w(id group consumer_id) + %w(id group consumer_attrs) end describe '::ATTRIBUTE_NAMES' do diff --git a/spec/kong/api_spec.rb b/spec/kong/api_spec.rb deleted file mode 100644 index e7bfe08..0000000 --- a/spec/kong/api_spec.rb +++ /dev/null @@ -1,32 +0,0 @@ -require_relative "../spec_helper" - -describe Kong::Api do - let(:valid_attribute_names) do - %w( - id name request_host request_path strip_request_path - hosts uris strip_uri preserve_host upstream_url retries - upstream_connect_timeout upstream_send_timeout upstream_read_timeout - https_only http_if_terminated methods - ) - end - - describe 'ATTRIBUTE_NAMES' do - it 'contains valid names' do - expect(subject.class::ATTRIBUTE_NAMES).to eq(valid_attribute_names) - end - end - - describe 'API_END_POINT' do - it 'contains valid end point' do - expect(subject.class::API_END_POINT).to eq('/apis/') - end - end - - describe '.plugins' do - it 'requests plugins attached to Api' do - subject.id = '12345' - expect(Kong::Plugin).to receive(:list).with({ api_id: subject.id }) - subject.plugins - end - end -end diff --git a/spec/kong/basic_auth_spec.rb b/spec/kong/basic_auth_spec.rb index 8c674ae..39a4384 100644 --- a/spec/kong/basic_auth_spec.rb +++ b/spec/kong/basic_auth_spec.rb @@ -2,7 +2,7 @@ describe Kong::BasicAuth do let(:valid_attribute_names) do - %w(id username password consumer_id) + %w(id username password consumer) end describe 'ATTRIBUTE_NAMES' do @@ -19,7 +19,7 @@ describe '#init_attributes' do it 'uses correct api end point if api_id is present' do - subject = described_class.new({ consumer_id: ':consumer_id' }) + subject = described_class.new({ consumer: { id: ':consumer_id' } }) expect(subject.api_end_point).to eq('/consumers/:consumer_id/basic-auth/') end end diff --git a/spec/kong/key_auth_spec.rb b/spec/kong/key_auth_spec.rb index 93b8a03..3bce258 100644 --- a/spec/kong/key_auth_spec.rb +++ b/spec/kong/key_auth_spec.rb @@ -2,7 +2,7 @@ describe Kong::KeyAuth do let(:valid_attribute_names) do - %w(id key consumer_id) + %w(id key consumer) end describe 'ATTRIBUTE_NAMES' do @@ -18,8 +18,8 @@ end describe '#init_attributes' do - it 'uses correct api end point if api_id is present' do - subject = described_class.new({ consumer_id: ':consumer_id' }) + it 'uses correct api end point if consumer is present' do + subject = described_class.new({ consumer: Kong::Consumer.new(id: ':consumer_id') }) expect(subject.api_end_point).to eq('/consumers/:consumer_id/key-auth/') end end diff --git a/spec/kong/oauth_app_spec.rb b/spec/kong/oauth_app_spec.rb index 528a18e..7e1e1e4 100644 --- a/spec/kong/oauth_app_spec.rb +++ b/spec/kong/oauth_app_spec.rb @@ -2,7 +2,7 @@ describe Kong::OAuthApp do let(:valid_attribute_names) do - %w(id name client_id client_secret redirect_uri consumer_id) + %w(id name client_id client_secret redirect_uri consumer_attrs) end describe '::ATTRIBUTE_NAMES' do diff --git a/spec/kong/plugin_spec.rb b/spec/kong/plugin_spec.rb index 3119773..b43f874 100644 --- a/spec/kong/plugin_spec.rb +++ b/spec/kong/plugin_spec.rb @@ -2,7 +2,7 @@ describe Kong::Plugin do let(:valid_attribute_names) do - %w(id api_id name config enabled consumer_id) + %w(id name config enabled route_id service_id consumer_id protocols tags) end describe 'ATTRIBUTE_NAMES' do @@ -18,26 +18,26 @@ end describe '#init_attributes' do - it 'uses correct api end point if api_id is present' do - subject = described_class.new({ api_id: ':api_id' }) - expect(subject.api_end_point).to eq('/apis/:api_id/plugins/') + it 'uses correct service end point if service_id is present' do + subject = described_class.new({ service_id: ':service_id' }) + expect(subject.api_end_point).to eq('/services/:service_id/plugins/') end end describe '#create' do it 'transforms config keys to config.key format' do headers = { 'Content-Type' => 'application/json' } - attributes = { 'api_id' => ':api_id', 'config.anonymous' => '12345' } - expect(Kong::Client.instance).to receive(:post).with('/apis/:api_id/plugins/', attributes, nil, headers).and_return(attributes) - subject = described_class.new({ api_id: ':api_id', config: { 'anonymous' => '12345' } }) + attributes = { 'service_id' => ':service_id', 'config.anonymous' => '12345' } + expect(Kong::Client.instance).to receive(:post).with('/services/:service_id/plugins/', attributes, nil, headers).and_return(attributes) + subject = described_class.new({ service_id: ':service_id', config: { 'anonymous' => '12345' } }) subject.create end it 'transforms nested config keys to config.key format' do headers = { 'Content-Type' => 'application/json' } - attributes = { 'api_id' => ':api_id', 'config.anonymous' => '12345', 'config.first.second' => '1' } - expect(Kong::Client.instance).to receive(:post).with('/apis/:api_id/plugins/', attributes, nil, headers).and_return(attributes) - subject = described_class.new({ api_id: ':api_id', config: { 'anonymous' => '12345', 'first' => { 'second' => '1' } } }) + attributes = { 'service_id' => ':service_id', 'config.anonymous' => '12345', 'config.first.second' => '1' } + expect(Kong::Client.instance).to receive(:post).with('/services/:service_id/plugins/', attributes, nil, headers).and_return(attributes) + subject = described_class.new({ service_id: ':service_id', config: { 'anonymous' => '12345', 'first' => { 'second' => '1' } } }) subject.create end end @@ -45,17 +45,17 @@ describe '#update' do it 'transforms config keys to config.key format' do headers = { 'Content-Type' => 'application/json' } - attributes = { 'api_id' => ':api_id', 'config.anonymous' => '12345' } - expect(Kong::Client.instance).to receive(:patch).with('/apis/:api_id/plugins/', attributes, nil, headers).and_return(attributes) - subject = described_class.new({ api_id: ':api_id', config: { 'anonymous' => '12345' } }) + attributes = { 'service_id' => ':service_id', 'config.anonymous' => '12345' } + expect(Kong::Client.instance).to receive(:patch).with('/services/:service_id/plugins/', attributes, nil, headers).and_return(attributes) + subject = described_class.new({ service_id: ':service_id', config: { 'anonymous' => '12345' } }) subject.update end it 'transforms nested config keys to config.key format' do headers = { 'Content-Type' => 'application/json' } - attributes = { 'api_id' => ':api_id', 'config.anonymous' => '12345', 'config.first.second' => '1' } - expect(Kong::Client.instance).to receive(:patch).with('/apis/:api_id/plugins/', attributes, nil, headers).and_return(attributes) - subject = described_class.new({ api_id: ':api_id', config: { 'anonymous' => '12345', 'first' => { 'second' => '1' } } }) + attributes = { 'service_id' => ':service_id', 'config.anonymous' => '12345', 'config.first.second' => '1' } + expect(Kong::Client.instance).to receive(:patch).with('/services/:service_id/plugins/', attributes, nil, headers).and_return(attributes) + subject = described_class.new({ service_id: ':service_id', config: { 'anonymous' => '12345', 'first' => { 'second' => '1' } } }) subject.update end end diff --git a/spec/kong/route_spec.rb b/spec/kong/route_spec.rb new file mode 100644 index 0000000..80eee01 --- /dev/null +++ b/spec/kong/route_spec.rb @@ -0,0 +1,31 @@ +require_relative "../spec_helper" + +describe Kong::Route do + let(:valid_attribute_names) do + %w( + id name protocols methods hosts paths + regex_priority strip_path preserve_host + service_id tags + ) + end + + describe 'ATTRIBUTE_NAMES' do + it 'contains valid names' do + expect(subject.class::ATTRIBUTE_NAMES).to eq(valid_attribute_names) + end + end + + describe 'API_END_POINT' do + it 'contains valid end point' do + expect(subject.class::API_END_POINT).to eq('/routes/') + end + end + + describe '.plugins' do + it 'requests plugins attached to Route' do + subject.id = '12345' + expect(Kong::Plugin).to receive(:list).with({ route_id: subject.id }) + subject.plugins + end + end +end diff --git a/spec/kong/service_spec.rb b/spec/kong/service_spec.rb new file mode 100644 index 0000000..f3741f9 --- /dev/null +++ b/spec/kong/service_spec.rb @@ -0,0 +1,31 @@ +require_relative "../spec_helper" + +describe Kong::Service do + let(:valid_attribute_names) do + %w( + id name retries protocol host port path + connect_timeout write_timeout upstream_read_timeout + tags + ) + end + + describe 'ATTRIBUTE_NAMES' do + it 'contains valid names' do + expect(subject.class::ATTRIBUTE_NAMES).to eq(valid_attribute_names) + end + end + + describe 'API_END_POINT' do + it 'contains valid end point' do + expect(subject.class::API_END_POINT).to eq('/services/') + end + end + + describe '.plugins' do + it 'requests plugins attached to Service' do + subject.id = '12345' + expect(Kong::Plugin).to receive(:list).with({ service_id: subject.id }) + subject.plugins + end + end +end