From b20cbea935709d5fa18a343eb0dc2da504f28d4f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tomasz=20Tr=C4=99bski?= Date: Fri, 6 Nov 2015 14:03:34 +0100 Subject: [PATCH] [Flexibility] Adding API version to plugin Log-API is a REST Api that is versioned. Such version may change in future so therefore a mechanism do specify such version is required as well as detecting unsupported versions. Change-Id: Ib36284e882591ce86ef7c66e550cf9f44e3ef8ba --- .../outputs/monasca/monasca_log_api_client.rb | 20 ++++++++++-- lib/logstash/outputs/monasca_log_api.rb | 3 +- logstash-output-monasca_log_api.gemspec | 4 +-- .../monasca/monasca_api_client_spec.rb | 32 ++++++++++++++++--- 4 files changed, 49 insertions(+), 10 deletions(-) diff --git a/lib/logstash/outputs/monasca/monasca_log_api_client.rb b/lib/logstash/outputs/monasca/monasca_log_api_client.rb index 4593312..42da5b4 100644 --- a/lib/logstash/outputs/monasca/monasca_log_api_client.rb +++ b/lib/logstash/outputs/monasca/monasca_log_api_client.rb @@ -25,9 +25,12 @@ module LogStash::Outputs module Monasca class MonascaLogApiClient - def initialize(host) + SUPPORTED_API_VERSION = %w(2.0) + + def initialize(host, version) @logger = Cabin::Channel.get(LogStash) - @rest_client = RestClient::Resource.new(LogStash::Outputs::Helper::UrlHelper.generate_url(host, '/v2.0').to_s) + @rest_client_url = LogStash::Outputs::Helper::UrlHelper.generate_url(host, '/' + check_version(version)).to_s + @rest_client = RestClient::Resource.new(@rest_client_url) end # Send log events to monasca-api, requires token @@ -54,9 +57,22 @@ def request(event, data, token, dimensions, application_type) if application_type post_headers[:x_application_type] = application_type end + + @logger.debug('Sending data to ', :url => @rest_client_url) @rest_client['log']['single'].post(data, post_headers) end + + def check_version(version) + tmp_version = version.sub('v','') + + unless SUPPORTED_API_VERSION.include? tmp_version + raise "#{tmp_version} is not supported, supported versions are #{SUPPORTED_API_VERSION}" + end + + version + end + end end end diff --git a/lib/logstash/outputs/monasca_log_api.rb b/lib/logstash/outputs/monasca_log_api.rb index 5177771..89963ec 100644 --- a/lib/logstash/outputs/monasca_log_api.rb +++ b/lib/logstash/outputs/monasca_log_api.rb @@ -30,6 +30,7 @@ class LogStash::Outputs::MonascaLogApi < LogStash::Outputs::Base # monasca-api host and port configuration config :monasca_log_api, :validate => :string, :required => true + config :monasca_log_api_version, :validate => :string, :required => false, :default => "v2.0" # keystone host and port configuration config :keystone_api, :validate => :string, :required => true @@ -49,7 +50,7 @@ class LogStash::Outputs::MonascaLogApi < LogStash::Outputs::Base public def register @keystone_client = LogStash::Outputs::Keystone::KeystoneClient.new keystone_api - @monasca_log_api_client = LogStash::Outputs::Monasca::MonascaLogApiClient.new monasca_log_api + @monasca_log_api_client = LogStash::Outputs::Monasca::MonascaLogApiClient.new monasca_log_api, monasca_log_api_version @token = get_token @logger.info('Registering keystone user', :username => @username, :project_name => @project_name) diff --git a/logstash-output-monasca_log_api.gemspec b/logstash-output-monasca_log_api.gemspec index 156cbd2..fbfb3a3 100644 --- a/logstash-output-monasca_log_api.gemspec +++ b/logstash-output-monasca_log_api.gemspec @@ -1,6 +1,6 @@ Gem::Specification.new do |s| s.name = 'logstash-output-monasca_log_api' - s.version = '0.3.2' + s.version = '0.3.3' s.licenses = ['Apache License 2.0'] s.summary = 'This gem is a logstash output plugin to connect via http to monasca-log-api.' s.description = 'This gem is a logstash plugin required to be installed on top of the Logstash core pipeline using $LS_HOME/bin/plugin install gemname. This gem is not a stand-alone program' @@ -11,7 +11,7 @@ Gem::Specification.new do |s| # Files s.files = Dir["lib/**/*", "spec/**/*", "*.gemspec", "*.md", "Gemfile", "LICENSE"] - + # Tests s.test_files = s.files.grep(%r{^(test|spec|features)/}) diff --git a/spec/outputs/monasca/monasca_api_client_spec.rb b/spec/outputs/monasca/monasca_api_client_spec.rb index 41e455a..ebce151 100644 --- a/spec/outputs/monasca/monasca_api_client_spec.rb +++ b/spec/outputs/monasca/monasca_api_client_spec.rb @@ -17,6 +17,7 @@ require_relative '../spec_helper' describe LogStash::Outputs::Monasca::MonascaLogApiClient do + let (:version) { "v2.0" } let (:auth_hash) { "{\"auth\":{\"identity\":{\"methods\":[\"password\"],\"password\":{\"user\":{\"domain\":{\"id\":\"1051bd27b9394120b26d8b08847325c0\"},\"name\":\"csi-operator\",\"password\":\"password\"}}},\"scope\":{\"project\":{\"domain\":{\"id\":\"1051bd27b9394120b26d8b08847325c0\"},\"name\":\"csi\"}}}}" } @@ -38,7 +39,7 @@ context 'when initializing' do it 'then it should register without exceptions' do - expect {LogStash::Outputs::Monasca::MonascaLogApiClient.new('hostname:8080')}.to_not raise_error + expect {LogStash::Outputs::Monasca::MonascaLogApiClient.new('hostname:8080', version)}.to_not raise_error end it "returns a failure if arguments are missing" do @@ -51,14 +52,14 @@ it 'with dimensions then it should request monasca' do expect_any_instance_of(RestClient::Resource).to receive(:post) .with(data, :x_auth_token => token, :content_type => 'application/json', :x_dimensions => dimensions) - monasca_log_api_client = LogStash::Outputs::Monasca::MonascaLogApiClient.new('hostname:8080') + monasca_log_api_client = LogStash::Outputs::Monasca::MonascaLogApiClient.new('hostname:8080', version) monasca_log_api_client.send_event(nil, data, token, dimensions) end it 'without dimensions then it should request monasca' do expect_any_instance_of(RestClient::Resource).to receive(:post) .with(data, :x_auth_token => token, :content_type => 'application/json') - monasca_log_api_client = LogStash::Outputs::Monasca::MonascaLogApiClient.new('hostname:8080') + monasca_log_api_client = LogStash::Outputs::Monasca::MonascaLogApiClient.new('hostname:8080', version) monasca_log_api_client.send_event(nil, data, token, nil) end @@ -66,7 +67,7 @@ app_type = 'someapp' expect_any_instance_of(RestClient::Resource).to receive(:post) .with(data, :x_auth_token => token, :content_type => 'application/json', :x_application_type => app_type) - monasca_log_api_client = LogStash::Outputs::Monasca::MonascaLogApiClient.new('hostname:8080') + monasca_log_api_client = LogStash::Outputs::Monasca::MonascaLogApiClient.new('hostname:8080', version) monasca_log_api_client.send_event(nil, data, token, nil, app_type) end end @@ -74,10 +75,31 @@ context 'when sending events failes' do it 'then it should be rescued and a warn log printed' do expect_any_instance_of(Cabin::Channel).to receive(:warn) - monasca_log_api_client = LogStash::Outputs::Monasca::MonascaLogApiClient.new('hostname:8080') + monasca_log_api_client = LogStash::Outputs::Monasca::MonascaLogApiClient.new('hostname:8080', version) allow(monasca_log_api_client).to receive(:request).and_raise('an_error') monasca_log_api_client.send_event(nil, data, token, dimensions) end end + context 'api version checking' do + + let (:supported_version) { %w(v2.0) } + + it 'should pass for correct version' do + supported_version.each { |ver| + expect {LogStash::Outputs::Monasca::MonascaLogApiClient.new('hostname:8080', ver)}.to_not raise_error + } + end + + it 'should pass if version does not specify v' do + ver = '2.0' + expect {LogStash::Outputs::Monasca::MonascaLogApiClient.new('hostname:8080', ver)}.to_not raise_error + end + + it 'should fail for unsupported version' do + ver = 'v4.0' + expect {LogStash::Outputs::Monasca::MonascaLogApiClient.new('hostname:8080', ver)}.to raise_exception + end + end + end