diff --git a/lib/kong/plugin.rb b/lib/kong/plugin.rb index 2bac142..66fbb36 100644 --- a/lib/kong/plugin.rb +++ b/lib/kong/plugin.rb @@ -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 diff --git a/spec/kong/plugin_spec.rb b/spec/kong/plugin_spec.rb index fe02913..fe1d268 100644 --- a/spec/kong/plugin_spec.rb +++ b/spec/kong/plugin_spec.rb @@ -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