Skip to content

Commit

Permalink
Transform config keys to config.key format on plugin create and update (
Browse files Browse the repository at this point in the history
  • Loading branch information
nevalla authored Apr 20, 2017
1 parent fdc1f9f commit c6eaca9
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 0 deletions.
22 changes: 22 additions & 0 deletions lib/kong/plugin.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,27 @@ class Plugin

ATTRIBUTE_NAMES = %w(id api_id name config enabled consumer_id).freeze
API_END_POINT = '/plugins/'.freeze

# Create resource
def create
if attributes['config']
attributes['config'].each do |key, value|
attributes["config.#{key}"] = value
end
attributes.delete('config')
end
super
end

# update resource
def update
if attributes['config']
attributes['config'].each do |key, value|
attributes["config.#{key}"] = value
end
attributes.delete('config')
end
super
end
end
end
20 changes: 20 additions & 0 deletions spec/kong/plugin_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,24 @@
expect(subject.api_end_point).to eq('/apis/:api_id/plugins/')
end
end

describe '#create' do
it 'transforms config keys to config.key format' do
headers = { 'Content-Type' => 'application/x-www-form-urlencoded' }
attributes = { 'api_id' => ':api_id', 'config.anonymous' => '12345' }
expect(Kong::Client.instance).to receive(:post).with('/apis/:api_id/plugins/', nil, attributes, headers).and_return(attributes)
subject = described_class.new({ api_id: ':api_id', config: { 'anonymous' => '12345' } })
subject.create
end
end

describe '#update' do
it 'transforms config keys to config.key format' do
headers = { 'Content-Type' => 'application/x-www-form-urlencoded' }
attributes = { 'api_id' => ':api_id', 'config.anonymous' => '12345' }
expect(Kong::Client.instance).to receive(:patch).with('/apis/:api_id/plugins/', nil, attributes, headers).and_return(attributes)
subject = described_class.new({ api_id: ':api_id', config: { 'anonymous' => '12345' } })
subject.update
end
end
end

0 comments on commit c6eaca9

Please sign in to comment.