kong 0.2.0 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 2eb96feeae50f935d3faf48b18bffde840ad3ca0
4
- data.tar.gz: bc4e0aa0c3125d227135ff82db8168a154b773cf
3
+ metadata.gz: 65344f942e47dd8c8607a2c59df13230abeff63d
4
+ data.tar.gz: b0e5d1f544da750dc29e73fe64162185e87435e3
5
5
  SHA512:
6
- metadata.gz: 20914d65262adc1f8592ed02eb689aeba49b5b9b3aca132bf7cd4b9e5bdd1899e0556b1463fea60d3e7ad8dcab16ea8456876086ab94a54d8cf80952c92ff5c0
7
- data.tar.gz: 5be4a32b52ec29a31a5fcda2d40784881fc9f0ac009bb68eeaf132e1831062822eb76e6554b1d1317326e800f2e8ff25fe2b90770ef6806d1993808daf87d1d2
6
+ metadata.gz: e0e3bb31a1d6b3236e56f449f6ee98cd05492081dcf5504a7b3715ec72cfe24d5b0da360a07925ec5bd5befdbf6af2826bffd4fc913159024bbd6a2e2f691ac8
7
+ data.tar.gz: b51843df1c0d1962d7db7928cb042a286a3d6ad897a658341c59e9f0bb605e38283e06d10626a5fadcaa989ddaae4e121f5c01485d00fc05a247a546f5dea588
data/CHANGELOG.md CHANGED
@@ -1,3 +1,6 @@
1
+ # 0.3.0 (2017-07-31)
2
+ - Add support for Upstream and Target resources ([#15](https://github.com/kontena/kong-client-ruby/pull/15))
3
+
1
4
  # 0.2.0 (2017-04-20)
2
5
  - Add Server information support ([#14](https://github.com/kontena/kong-client-ruby/pull/14))
3
6
  - Add optional attributes to Api
data/README.md CHANGED
@@ -53,6 +53,8 @@ consumer = Kong::Consumer.find_by_custom_id('custom_id')
53
53
 
54
54
  ### All Resources and Actions
55
55
 
56
+ To see the complete Kong Admin API documentation, please visit: https://getkong.org/docs/0.10.x/admin-api/
57
+
56
58
  #### Consumer
57
59
 
58
60
  ```ruby
@@ -87,9 +89,9 @@ Kong::Api.create(attributes)
87
89
 
88
90
  api = Kong::Api.new({
89
91
  name: 'Mockbin',
90
- request_host: 'mockbin.com',
91
- request_path: '/someservice',
92
- strip_request_path: false,
92
+ hosts: ['mockbin.com'],
93
+ uris: ['/someservice'],
94
+ strip_uri: false,
93
95
  preserve_host: false,
94
96
  upstream_url: 'https://mockbin.com'
95
97
  })
@@ -118,7 +120,7 @@ plugin = Kong::Plugin.new({
118
120
  config: {
119
121
  minute: 20,
120
122
  hour: 500
121
- }
123
+ }
122
124
  })
123
125
 
124
126
  plugin.get # reloads resource
@@ -128,6 +130,36 @@ plugin.save # requests create_or_update action
128
130
  plugin.delete
129
131
  ```
130
132
 
133
+ #### Upstream and Targets (for load-balanced APIs)
134
+
135
+ ```
136
+ Kong::Upstream.list(filters)
137
+ Kong::Upstream.all()
138
+ Kong::Upstream.find(id)
139
+ Kong::Upstream.find_by_*(value)
140
+ Kong::Upstream.create(attributes)
141
+
142
+ upstream = Kong::Upstream.new({ name: 'myservice' })
143
+
144
+ upstream.get # reloads resource
145
+ upstream.create
146
+ upstream.update
147
+ upstream.save # requests create_or_update action
148
+ upstream.delete
149
+
150
+ upstream.targets # lists active targets
151
+
152
+ # Add targets
153
+ Kong::Target.new({ upstream_id: upstream.id, target: 'appserver1:80' }).save
154
+ Kong::Target.new({ upstream_id: upstream.id, target: 'appserver2:80' }).save
155
+
156
+ # Add the API
157
+ Kong::Api.new({
158
+ ...
159
+ upstream_url: 'http://myservice'
160
+ }).save
161
+ ```
162
+
131
163
  #### OAuthApp
132
164
 
133
165
  ```ruby
@@ -137,8 +169,8 @@ Kong::OAuthApp.find(consumer_id)
137
169
  Kong::OAuthApp.find_by_*(value)
138
170
  Kong::OAuthApp.create(attributes)
139
171
 
140
- app = Kong::OAuthApp.new({
141
- consumer_id: 'a3dX2dh2-1adb-40a5-c042-63b19dbx83hF4',
172
+ app = Kong::OAuthApp.new({
173
+ consumer_id: 'a3dX2dh2-1adb-40a5-c042-63b19dbx83hF4',
142
174
  redirect_uri: 'http://some-domain/endpoint/'
143
175
  })
144
176
 
@@ -154,8 +186,8 @@ app.delete
154
186
  ```ruby
155
187
  Kong::KeyAuth.create(attributes)
156
188
 
157
- auth = Kong::KeyAuth.new({
158
- consumer_id: 'a3dX2dh2-1adb-40a5-c042-63b19dbx83hF4',
189
+ auth = Kong::KeyAuth.new({
190
+ consumer_id: 'a3dX2dh2-1adb-40a5-c042-63b19dbx83hF4',
159
191
  })
160
192
 
161
193
  auth.create
@@ -170,7 +202,7 @@ auth.delete
170
202
  ```ruby
171
203
  Kong::BasicAuth.create(attributes)
172
204
 
173
- auth = Kong::BasicAuth.new({
205
+ auth = Kong::BasicAuth.new({
174
206
  consumer_id: 'a3dX2dh2-1adb-40a5-c042-63b19dbx83hF4',
175
207
  username: 'user123',
176
208
  password: 'secret'
@@ -188,7 +220,7 @@ auth.delete
188
220
  ```ruby
189
221
  token = Kong::OAuth2Token.find_by_access_token('SOME-TOKEN')
190
222
 
191
- token = Kong::OAuth2Token.new({
223
+ token = Kong::OAuth2Token.new({
192
224
  credential_id: 'KONG-APPLICATION-ID',
193
225
  token_type: 'bearer',
194
226
  access_token: 'SOME-TOKEN',
@@ -208,7 +240,7 @@ token.oauth_app
208
240
 
209
241
  ```ruby
210
242
 
211
- jwt = Kong::JWT.new({
243
+ jwt = Kong::JWT.new({
212
244
  consumer_id: 'a3dX2dh2-1adb-40a5-c042-63b19dbx83hF4',
213
245
  key: 'a36c3049b36249a3c9f8891cb127243c',
214
246
  secret: 'e71829c351aa4242c2719cbfbe671c09'
@@ -227,7 +259,7 @@ consumer.jwts
227
259
 
228
260
  ```ruby
229
261
 
230
- acl = Kong::Acl.new({
262
+ acl = Kong::Acl.new({
231
263
  consumer_id: 'a3dX2dh2-1adb-40a5-c042-63b19dbx83hF4',
232
264
  group: 'group1'
233
265
  })
data/lib/kong/base.rb CHANGED
@@ -161,6 +161,7 @@ module Kong
161
161
  end
162
162
  use_consumer_end_point if respond_to?(:use_consumer_end_point)
163
163
  use_api_end_point if respond_to?(:use_api_end_point)
164
+ use_upstream_end_point if respond_to?(:use_upstream_end_point)
164
165
  end
165
166
  end
166
167
  end
@@ -0,0 +1,61 @@
1
+ module Kong
2
+ class Target
3
+ include Base
4
+
5
+ ATTRIBUTE_NAMES = %w(id upstream_id target weight).freeze
6
+ API_END_POINT = '/targets/'.freeze
7
+
8
+ def self.find(id)
9
+ raise NotImplementedError, 'Kong does not support direct access to targets, you must go via an upstream'
10
+ end
11
+
12
+ def self.list(params = {})
13
+ raise NotImplementedError, 'Kong does not support direct access to targets, you must go via an upstream'
14
+ end
15
+
16
+ def initialize(attributes = {})
17
+ super(attributes)
18
+ raise ArgumentError, 'You must specify an upstream_id' unless self.upstream_id
19
+ end
20
+
21
+ def active?
22
+ self.weight > 0
23
+ end
24
+
25
+ def save
26
+ create
27
+ end
28
+
29
+ def create_or_update
30
+ raise NotImplementedError, 'Kong does not support updating targets, you must delete and re-create'
31
+ end
32
+
33
+ def update
34
+ raise NotImplementedError, 'Kong does not support updating targets, you must delete and re-create'
35
+ end
36
+
37
+ def use_upstream_end_point
38
+ self.api_end_point = "/upstreams/#{self.upstream_id}#{self.class::API_END_POINT}" if self.upstream_id
39
+ end
40
+
41
+ # Get Upstream resource
42
+ # @return [Kong::Upstream]
43
+ def upstream
44
+ @upstream ||= Upstream.find(self.upstream_id)
45
+ end
46
+
47
+ # Set Upstream resource
48
+ # @param [Kong::Upstream] upstream
49
+ def upstream=(upstream)
50
+ @upstream = upstream
51
+ self.upstream_id = upstream.id
52
+ end
53
+
54
+ # Set Upstream id
55
+ # @param [String] id
56
+ def upstream_id=(id)
57
+ super(id)
58
+ use_upstream_end_point
59
+ end
60
+ end
61
+ end
@@ -0,0 +1,24 @@
1
+ module Kong
2
+ class Upstream
3
+ include Base
4
+
5
+ ATTRIBUTE_NAMES = %w(id name slots orderlist).freeze
6
+ API_END_POINT = '/upstreams/'.freeze
7
+
8
+ ##
9
+ # @return [Array<Kong::Target>]
10
+ def targets
11
+ targets = []
12
+ json_data = Client.instance.get("#{API_END_POINT}#{self.id}/targets")
13
+
14
+ if json_data['data']
15
+ json_data['data'].each do |target_data|
16
+ target = Target.new(target_data)
17
+ targets << target if target.active?
18
+ end
19
+ end
20
+
21
+ targets
22
+ end
23
+ end
24
+ end
data/lib/kong/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Kong
2
- VERSION = '0.2.0'.freeze
2
+ VERSION = '0.3.0'.freeze
3
3
  end
data/lib/kong.rb CHANGED
@@ -13,4 +13,6 @@ require_relative 'kong/basic_auth'
13
13
  require_relative 'kong/key_auth'
14
14
  require_relative 'kong/jwt'
15
15
  require_relative 'kong/acl'
16
+ require_relative 'kong/target'
17
+ require_relative 'kong/upstream'
16
18
  require_relative 'kong/server'
@@ -0,0 +1,53 @@
1
+ require_relative "../spec_helper"
2
+
3
+ describe Kong::Target do
4
+ let(:upstream_id) { '1234' }
5
+
6
+ let(:valid_attribute_names) do
7
+ %w(id upstream_id target weight)
8
+ end
9
+
10
+ subject { described_class.new(upstream_id: upstream_id) }
11
+
12
+ context 'without an upstream_id' do
13
+ it 'raises an ArgumentError' do
14
+ expect { described_class.new }
15
+ .to raise_error(ArgumentError)
16
+ end
17
+ end
18
+
19
+ describe 'ATTRIBUTE_NAMES' do
20
+ it 'contains valid names' do
21
+ expect(subject.class::ATTRIBUTE_NAMES).to eq(valid_attribute_names)
22
+ end
23
+ end
24
+
25
+ describe 'API_END_POINT' do
26
+ it 'contains valid end point' do
27
+ expect(subject.class::API_END_POINT).to eq('/targets/')
28
+ end
29
+ end
30
+
31
+ describe '#init_attributes' do
32
+ it 'uses the correct api end point if the upstream_id is present' do
33
+ expect(subject.api_end_point).to eq("/upstreams/#{upstream_id}/targets/")
34
+ end
35
+ end
36
+
37
+ describe '.upstream' do
38
+ it 'requests the attached Upstream' do
39
+ expect(Kong::Upstream).to receive(:find).with(upstream_id)
40
+ subject.upstream
41
+ end
42
+ end
43
+
44
+ describe '.active?' do
45
+ it 'returns true if the weight is > 0' do
46
+ target1 = described_class.new(upstream_id: upstream_id, target: 'google.com', weight: 100)
47
+ target2 = described_class.new(upstream_id: upstream_id, target: 'google.com', weight: 0)
48
+
49
+ expect(target1).to be_active
50
+ expect(target2).to_not be_active
51
+ end
52
+ end
53
+ end
@@ -0,0 +1,37 @@
1
+ require_relative "../spec_helper"
2
+
3
+ describe Kong::Upstream do
4
+ let(:valid_attribute_names) do
5
+ %w(id name slots orderlist)
6
+ end
7
+
8
+ describe 'ATTRIBUTE_NAMES' do
9
+ it 'contains valid names' do
10
+ expect(subject.class::ATTRIBUTE_NAMES).to eq(valid_attribute_names)
11
+ end
12
+ end
13
+
14
+ describe 'API_END_POINT' do
15
+ it 'contains a valid end point' do
16
+ expect(subject.class::API_END_POINT).to eq('/upstreams/')
17
+ end
18
+ end
19
+
20
+ describe '.targets' do
21
+ it 'requests targets attached to the Upstream' do
22
+ subject.id = '12345'
23
+
24
+ expect(Kong::Client.instance)
25
+ .to receive(:get).with("/upstreams/12345/targets")
26
+ .and_return({
27
+ 'data' => [{ 'upstream_id' => 12345, 'target' => 'google.com:80', 'weight' => 100 }],
28
+ 'total' => 1
29
+ })
30
+
31
+ targets = subject.targets
32
+
33
+ expect(targets.size).to eq(1)
34
+ expect(targets.first).to be_a(Kong::Target)
35
+ end
36
+ end
37
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: kong
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Lauri Nevala
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-04-20 00:00:00.000000000 Z
11
+ date: 2017-07-31 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -85,6 +85,8 @@ files:
85
85
  - lib/kong/oauth_app.rb
86
86
  - lib/kong/plugin.rb
87
87
  - lib/kong/server.rb
88
+ - lib/kong/target.rb
89
+ - lib/kong/upstream.rb
88
90
  - lib/kong/version.rb
89
91
  - spec/kong/acl_spec.rb
90
92
  - spec/kong/api_spec.rb
@@ -97,6 +99,8 @@ files:
97
99
  - spec/kong/oauth_app_spec.rb
98
100
  - spec/kong/plugin_spec.rb
99
101
  - spec/kong/server_spec.rb
102
+ - spec/kong/target_spec.rb
103
+ - spec/kong/upstream_spec.rb
100
104
  - spec/spec_helper.rb
101
105
  - tasks/rspec.rake
102
106
  homepage: https://github.com/kontena/kong-client-ruby
@@ -119,7 +123,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
119
123
  version: '0'
120
124
  requirements: []
121
125
  rubyforge_project:
122
- rubygems_version: 2.5.2
126
+ rubygems_version: 2.6.12
123
127
  signing_key:
124
128
  specification_version: 4
125
129
  summary: A Ruby client for the Kong API
@@ -135,4 +139,6 @@ test_files:
135
139
  - spec/kong/oauth_app_spec.rb
136
140
  - spec/kong/plugin_spec.rb
137
141
  - spec/kong/server_spec.rb
142
+ - spec/kong/target_spec.rb
143
+ - spec/kong/upstream_spec.rb
138
144
  - spec/spec_helper.rb