Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ACL support #9

Merged
merged 1 commit into from
Jan 12, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,24 @@ token.delete
token.oauth_app
```

#### ACL

```ruby

acl = Kong::Acl.new({
consumer_id: 'a3dX2dh2-1adb-40a5-c042-63b19dbx83hF4',
group: 'group1'
})

acl.create
acl.update
acl.save # requests create_or_update action
acl.delete

consumer = Kong::Consumer.find_by_username('testuser')
consumer.acls
```

## Contributing

1. Fork it ( https://github.com/kontena/kong-client-ruby/fork )
Expand Down
27 changes: 14 additions & 13 deletions lib/kong.rb
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
require 'kong/version'
require 'kong/base'
require 'kong/api'
require 'kong/belongs_to_api'
require 'kong/client'
require 'kong/consumer'
require 'kong/belongs_to_consumer'
require 'kong/plugin'
require 'kong/error'
require 'kong/oauth_app'
require 'kong/oauth2_token'
require 'kong/basic_auth'
require 'kong/key_auth'
require 'kong/jwt'
require_relative 'kong/base'
require_relative 'kong/api'
require_relative 'kong/belongs_to_api'
require_relative 'kong/client'
require_relative 'kong/consumer'
require_relative 'kong/belongs_to_consumer'
require_relative 'kong/plugin'
require_relative 'kong/error'
require_relative 'kong/oauth_app'
require_relative 'kong/oauth2_token'
require_relative 'kong/basic_auth'
require_relative 'kong/key_auth'
require_relative 'kong/jwt'
require_relative 'kong/acl'
8 changes: 8 additions & 0 deletions lib/kong/acl.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
module Kong
class Acl
include Base
include BelongsToConsumer
ATTRIBUTE_NAMES = %w(id group consumer_id).freeze
API_END_POINT = "/acls/".freeze
end
end
14 changes: 14 additions & 0 deletions lib/kong/consumer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,20 @@ def oauth2_tokens
end
end

# List Acls
#
# @return [Array<Kong::Acl>]
def acls
acls = []
response = client.get("#{@api_end_point}#{self.username}/acls") rescue nil
if response
response['data'].each do |attributes|
acls << Kong::Acl.new(attributes)
end
end
acls
end

# List JWTs
#
# @return [Array<Kong::JWT>]
Expand Down
19 changes: 19 additions & 0 deletions spec/kong/acl_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
require_relative "../spec_helper"

describe Kong::Acl do
let(:valid_attribute_names) do
%w(id group consumer_id)
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('/acls/')
end
end
end
17 changes: 17 additions & 0 deletions spec/kong/consumer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,4 +52,21 @@
end
end
end

describe '#acls' do
it 'requests consumer\'s acls' do
subject.username = ':username'
expect(Kong::Client.instance).to receive(:get).with("/consumers/:username/acls")
.and_return({ 'data' => [{ 'id' => '123456', 'group' => 'group1' }] })
subject.acls
end

it 'returns list of Acls' do
subject.username = ':username'
allow(Kong::Client.instance).to receive(:get).with("/consumers/:username/acls")
.and_return({ 'data' => [{ 'id' => '123456', 'group' => 'group1' }] })
result = subject.acls
expect(result.first.is_a?(Kong::Acl)).to be_truthy
end
end
end