Skip to content

Commit

Permalink
Test against Kong 1.1, split /apis into /routes and /services
Browse files Browse the repository at this point in the history
  • Loading branch information
mtmail committed Dec 17, 2019
1 parent 2010b83 commit c782bbc
Show file tree
Hide file tree
Showing 24 changed files with 244 additions and 142 deletions.
65 changes: 46 additions & 19 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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
Expand All @@ -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: {
Expand Down
2 changes: 1 addition & 1 deletion kong.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -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
5 changes: 3 additions & 2 deletions lib/kong.rb
Original file line number Diff line number Diff line change
@@ -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'
Expand Down
2 changes: 1 addition & 1 deletion lib/kong/acl.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
19 changes: 0 additions & 19 deletions lib/kong/api.rb

This file was deleted.

1 change: 1 addition & 0 deletions lib/kong/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion lib/kong/basic_auth.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
30 changes: 0 additions & 30 deletions lib/kong/belongs_to_api.rb

This file was deleted.

41 changes: 32 additions & 9 deletions lib/kong/belongs_to_consumer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
30 changes: 30 additions & 0 deletions lib/kong/belongs_to_service.rb
Original file line number Diff line number Diff line change
@@ -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
2 changes: 1 addition & 1 deletion lib/kong/key_auth.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 1 addition & 1 deletion lib/kong/oauth_app.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
7 changes: 5 additions & 2 deletions lib/kong/plugin.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
18 changes: 18 additions & 0 deletions lib/kong/route.rb
Original file line number Diff line number Diff line change
@@ -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<Kong::Plugin>]
def plugins
Plugin.list({ route_id: self.id })
end
end
end
18 changes: 18 additions & 0 deletions lib/kong/service.rb
Original file line number Diff line number Diff line change
@@ -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<Kong::Plugin>]
def plugins
Plugin.list({ service_id: self.id })
end
end
end
2 changes: 1 addition & 1 deletion lib/kong/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module Kong
VERSION = '0.3.4'.freeze
VERSION = '0.4.3'.freeze
end
2 changes: 1 addition & 1 deletion spec/kong/acl_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
32 changes: 0 additions & 32 deletions spec/kong/api_spec.rb

This file was deleted.

Loading

0 comments on commit c782bbc

Please sign in to comment.