diff --git a/lib/kong/plugin.rb b/lib/kong/plugin.rb index 8d26f97..9f50baf 100644 --- a/lib/kong/plugin.rb +++ b/lib/kong/plugin.rb @@ -14,7 +14,13 @@ def create super end - # update resource + # Create or update resource + def create_or_update + flatten_config + super + end + + # Update resource def update flatten_config super diff --git a/spec/kong/plugin_spec.rb b/spec/kong/plugin_spec.rb index 3119773..9a55910 100644 --- a/spec/kong/plugin_spec.rb +++ b/spec/kong/plugin_spec.rb @@ -42,6 +42,24 @@ end end + describe '#create_or_update' do + it 'transforms config keys to config.key format' do + headers = { 'Content-Type' => 'application/json' } + attributes = { 'api_id' => ':api_id', 'config.anonymous' => '12345' } + expect(Kong::Client.instance).to receive(:put).with('/apis/:api_id/plugins/', attributes, nil, headers).and_return(attributes) + subject = described_class.new({ api_id: ':api_id', config: { 'anonymous' => '12345' } }) + subject.create_or_update + end + + it 'transforms nested config keys to config.key format' do + headers = { 'Content-Type' => 'application/json' } + attributes = { 'api_id' => ':api_id', 'config.anonymous' => '12345', 'config.first.second' => '1' } + expect(Kong::Client.instance).to receive(:put).with('/apis/:api_id/plugins/', attributes, nil, headers).and_return(attributes) + subject = described_class.new({ api_id: ':api_id', config: { 'anonymous' => '12345', 'first' => { 'second' => '1' } } }) + subject.create_or_update + end + end + describe '#update' do it 'transforms config keys to config.key format' do headers = { 'Content-Type' => 'application/json' }