kong-client 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (51) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +16 -0
  3. data/.hound.yml +2 -0
  4. data/.rubocop.yml +360 -0
  5. data/.travis.yml +11 -0
  6. data/CHANGELOG.md +37 -0
  7. data/Gemfile +8 -0
  8. data/LICENSE +191 -0
  9. data/README.md +295 -0
  10. data/Rakefile +5 -0
  11. data/kong.gemspec +24 -0
  12. data/lib/kong/acl.rb +8 -0
  13. data/lib/kong/api.rb +19 -0
  14. data/lib/kong/base.rb +180 -0
  15. data/lib/kong/basic_auth.rb +8 -0
  16. data/lib/kong/belongs_to_api.rb +30 -0
  17. data/lib/kong/belongs_to_consumer.rb +30 -0
  18. data/lib/kong/client.rb +232 -0
  19. data/lib/kong/consumer.rb +103 -0
  20. data/lib/kong/error.rb +10 -0
  21. data/lib/kong/hmac_auth.rb +9 -0
  22. data/lib/kong/jwt.rb +8 -0
  23. data/lib/kong/key_auth.rb +8 -0
  24. data/lib/kong/oauth2_token.rb +14 -0
  25. data/lib/kong/oauth_app.rb +8 -0
  26. data/lib/kong/plugin.rb +31 -0
  27. data/lib/kong/server.rb +23 -0
  28. data/lib/kong/target.rb +61 -0
  29. data/lib/kong/upstream.rb +24 -0
  30. data/lib/kong/util.rb +16 -0
  31. data/lib/kong/version.rb +3 -0
  32. data/lib/kong.rb +20 -0
  33. data/spec/kong/acl_spec.rb +19 -0
  34. data/spec/kong/api_spec.rb +32 -0
  35. data/spec/kong/base_spec.rb +169 -0
  36. data/spec/kong/basic_auth_spec.rb +26 -0
  37. data/spec/kong/client_spec.rb +297 -0
  38. data/spec/kong/consumer_spec.rb +72 -0
  39. data/spec/kong/error_spec.rb +23 -0
  40. data/spec/kong/hmac_auth_spec.rb +26 -0
  41. data/spec/kong/key_auth_spec.rb +26 -0
  42. data/spec/kong/oauth2_token_spec.rb +19 -0
  43. data/spec/kong/oauth_app_spec.rb +19 -0
  44. data/spec/kong/plugin_spec.rb +62 -0
  45. data/spec/kong/server_spec.rb +39 -0
  46. data/spec/kong/target_spec.rb +53 -0
  47. data/spec/kong/upstream_spec.rb +37 -0
  48. data/spec/kong/util_spec.rb +29 -0
  49. data/spec/spec_helper.rb +19 -0
  50. data/tasks/rspec.rake +5 -0
  51. metadata +153 -0
@@ -0,0 +1,62 @@
1
+ require_relative "../spec_helper"
2
+
3
+ describe Kong::Plugin do
4
+ let(:valid_attribute_names) do
5
+ %w(id api_id name config enabled consumer_id)
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 valid end point' do
16
+ expect(subject.class::API_END_POINT).to eq('/plugins/')
17
+ end
18
+ end
19
+
20
+ describe '#init_attributes' do
21
+ it 'uses correct api end point if api_id is present' do
22
+ subject = described_class.new({ api_id: ':api_id' })
23
+ expect(subject.api_end_point).to eq('/apis/:api_id/plugins/')
24
+ end
25
+ end
26
+
27
+ describe '#create' do
28
+ it 'transforms config keys to config.key format' do
29
+ headers = { 'Content-Type' => 'application/json' }
30
+ attributes = { 'api_id' => ':api_id', 'config.anonymous' => '12345' }
31
+ expect(Kong::Client.instance).to receive(:post).with('/apis/:api_id/plugins/', attributes, nil, headers).and_return(attributes)
32
+ subject = described_class.new({ api_id: ':api_id', config: { 'anonymous' => '12345' } })
33
+ subject.create
34
+ end
35
+
36
+ it 'transforms nested config keys to config.key format' do
37
+ headers = { 'Content-Type' => 'application/json' }
38
+ attributes = { 'api_id' => ':api_id', 'config.anonymous' => '12345', 'config.first.second' => '1' }
39
+ expect(Kong::Client.instance).to receive(:post).with('/apis/:api_id/plugins/', attributes, nil, headers).and_return(attributes)
40
+ subject = described_class.new({ api_id: ':api_id', config: { 'anonymous' => '12345', 'first' => { 'second' => '1' } } })
41
+ subject.create
42
+ end
43
+ end
44
+
45
+ describe '#update' do
46
+ it 'transforms config keys to config.key format' do
47
+ headers = { 'Content-Type' => 'application/json' }
48
+ attributes = { 'api_id' => ':api_id', 'config.anonymous' => '12345' }
49
+ expect(Kong::Client.instance).to receive(:patch).with('/apis/:api_id/plugins/', attributes, nil, headers).and_return(attributes)
50
+ subject = described_class.new({ api_id: ':api_id', config: { 'anonymous' => '12345' } })
51
+ subject.update
52
+ end
53
+
54
+ it 'transforms nested config keys to config.key format' do
55
+ headers = { 'Content-Type' => 'application/json' }
56
+ attributes = { 'api_id' => ':api_id', 'config.anonymous' => '12345', 'config.first.second' => '1' }
57
+ expect(Kong::Client.instance).to receive(:patch).with('/apis/:api_id/plugins/', attributes, nil, headers).and_return(attributes)
58
+ subject = described_class.new({ api_id: ':api_id', config: { 'anonymous' => '12345', 'first' => { 'second' => '1' } } })
59
+ subject.update
60
+ end
61
+ end
62
+ end
@@ -0,0 +1,39 @@
1
+ require_relative "../spec_helper"
2
+
3
+ describe Kong::Server do
4
+ describe '.info' do
5
+ it 'makes GET / request' do
6
+ expect(Kong::Client.instance).to receive(:get).with('/')
7
+ described_class.info
8
+ end
9
+ end
10
+
11
+ describe '.version' do
12
+ it 'returns version information' do
13
+ allow(Kong::Client.instance).to receive(:get).with('/')
14
+ .and_return({ 'version' => '0.10.0' })
15
+ expect(described_class.version).to eq('0.10.0')
16
+ end
17
+ end
18
+
19
+ describe '.status' do
20
+ it 'makes GET /status request' do
21
+ expect(Kong::Client.instance).to receive(:get).with('/status')
22
+ described_class.status
23
+ end
24
+ end
25
+
26
+ describe '.cluster' do
27
+ it 'makes GET /cluster request' do
28
+ expect(Kong::Client.instance).to receive(:get).with('/cluster')
29
+ described_class.cluster
30
+ end
31
+ end
32
+
33
+ describe '.remove_node' do
34
+ it 'makes DELETE /cluster/nodes/:name request' do
35
+ expect(Kong::Client.instance).to receive(:delete).with('/cluster/nodes/:name')
36
+ described_class.remove_node(':name')
37
+ end
38
+ end
39
+ end
@@ -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
@@ -0,0 +1,29 @@
1
+ require_relative "../spec_helper"
2
+
3
+ describe Kong::Util do
4
+ describe '.flatten' do
5
+ subject { described_class.method(:flatten) }
6
+
7
+ it 'works' do
8
+ expect(subject[{
9
+ a: {
10
+ b: 1,
11
+ c: {
12
+ d: 3
13
+ }
14
+ },
15
+ b: 2
16
+ }]).to include({
17
+ "a.b" => 1,
18
+ "a.c.d" => 3,
19
+ "b" => 2,
20
+ })
21
+ end
22
+
23
+ it 'accepts a scope' do
24
+ expect(subject[{ a: "1" }, 'config']).to eq({
25
+ "config.a" => "1"
26
+ })
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,19 @@
1
+ # This file was generated by the `rspec --init` command. Conventionally, all
2
+ # specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
3
+ # Require this file using `require "spec_helper"` to ensure that it is only
4
+ # loaded once.
5
+ #
6
+ # See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
7
+
8
+ require 'kong'
9
+
10
+ RSpec.configure do |config|
11
+ config.run_all_when_everything_filtered = true
12
+ config.filter_run :focus
13
+
14
+ # Run specs in random order to surface order dependencies. If you find an
15
+ # order dependency and want to debug it, you can fix the order by providing
16
+ # the seed, which is printed after each run.
17
+ # --seed 1234
18
+ config.order = 'random'
19
+ end
data/tasks/rspec.rake ADDED
@@ -0,0 +1,5 @@
1
+ begin
2
+ require 'rspec/core/rake_task'
3
+ RSpec::Core::RakeTask.new(:spec)
4
+ rescue LoadError
5
+ end
metadata ADDED
@@ -0,0 +1,153 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: kong-client
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.4.0
5
+ platform: ruby
6
+ authors:
7
+ - Ecosystem API Team
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2023-02-06 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.17'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.17'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '12.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '12.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: excon
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ description: A Ruby client for the Kong API
56
+ email:
57
+ - ecosystem-apis@rdstation.com
58
+ - renan.porto@rdstation.com
59
+ - rogerio.angeliski@rdstation.com
60
+ executables: []
61
+ extensions: []
62
+ extra_rdoc_files: []
63
+ files:
64
+ - ".gitignore"
65
+ - ".hound.yml"
66
+ - ".rubocop.yml"
67
+ - ".travis.yml"
68
+ - CHANGELOG.md
69
+ - Gemfile
70
+ - LICENSE
71
+ - README.md
72
+ - Rakefile
73
+ - kong.gemspec
74
+ - lib/kong.rb
75
+ - lib/kong/acl.rb
76
+ - lib/kong/api.rb
77
+ - lib/kong/base.rb
78
+ - lib/kong/basic_auth.rb
79
+ - lib/kong/belongs_to_api.rb
80
+ - lib/kong/belongs_to_consumer.rb
81
+ - lib/kong/client.rb
82
+ - lib/kong/consumer.rb
83
+ - lib/kong/error.rb
84
+ - lib/kong/hmac_auth.rb
85
+ - lib/kong/jwt.rb
86
+ - lib/kong/key_auth.rb
87
+ - lib/kong/oauth2_token.rb
88
+ - lib/kong/oauth_app.rb
89
+ - lib/kong/plugin.rb
90
+ - lib/kong/server.rb
91
+ - lib/kong/target.rb
92
+ - lib/kong/upstream.rb
93
+ - lib/kong/util.rb
94
+ - lib/kong/version.rb
95
+ - spec/kong/acl_spec.rb
96
+ - spec/kong/api_spec.rb
97
+ - spec/kong/base_spec.rb
98
+ - spec/kong/basic_auth_spec.rb
99
+ - spec/kong/client_spec.rb
100
+ - spec/kong/consumer_spec.rb
101
+ - spec/kong/error_spec.rb
102
+ - spec/kong/hmac_auth_spec.rb
103
+ - spec/kong/key_auth_spec.rb
104
+ - spec/kong/oauth2_token_spec.rb
105
+ - spec/kong/oauth_app_spec.rb
106
+ - spec/kong/plugin_spec.rb
107
+ - spec/kong/server_spec.rb
108
+ - spec/kong/target_spec.rb
109
+ - spec/kong/upstream_spec.rb
110
+ - spec/kong/util_spec.rb
111
+ - spec/spec_helper.rb
112
+ - tasks/rspec.rake
113
+ homepage: https://github.com/ResultadosDigitais/kong-client-ruby
114
+ licenses:
115
+ - Apache-2.0
116
+ metadata: {}
117
+ post_install_message:
118
+ rdoc_options: []
119
+ require_paths:
120
+ - lib
121
+ required_ruby_version: !ruby/object:Gem::Requirement
122
+ requirements:
123
+ - - ">="
124
+ - !ruby/object:Gem::Version
125
+ version: 2.0.0
126
+ required_rubygems_version: !ruby/object:Gem::Requirement
127
+ requirements:
128
+ - - ">="
129
+ - !ruby/object:Gem::Version
130
+ version: '0'
131
+ requirements: []
132
+ rubygems_version: 3.4.6
133
+ signing_key:
134
+ specification_version: 4
135
+ summary: A Ruby client for the Kong API
136
+ test_files:
137
+ - spec/kong/acl_spec.rb
138
+ - spec/kong/api_spec.rb
139
+ - spec/kong/base_spec.rb
140
+ - spec/kong/basic_auth_spec.rb
141
+ - spec/kong/client_spec.rb
142
+ - spec/kong/consumer_spec.rb
143
+ - spec/kong/error_spec.rb
144
+ - spec/kong/hmac_auth_spec.rb
145
+ - spec/kong/key_auth_spec.rb
146
+ - spec/kong/oauth2_token_spec.rb
147
+ - spec/kong/oauth_app_spec.rb
148
+ - spec/kong/plugin_spec.rb
149
+ - spec/kong/server_spec.rb
150
+ - spec/kong/target_spec.rb
151
+ - spec/kong/upstream_spec.rb
152
+ - spec/kong/util_spec.rb
153
+ - spec/spec_helper.rb