Skip to content

Commit

Permalink
[Flexibility] Adding API version to plugin
Browse files Browse the repository at this point in the history
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
  • Loading branch information
Tomasz Trębski committed Dec 2, 2015
1 parent 290cf8a commit b20cbea
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 10 deletions.
20 changes: 18 additions & 2 deletions lib/logstash/outputs/monasca/monasca_log_api_client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
3 changes: 2 additions & 1 deletion lib/logstash/outputs/monasca_log_api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions logstash-output-monasca_log_api.gemspec
Original file line number Diff line number Diff line change
@@ -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'
Expand All @@ -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)/})

Expand Down
32 changes: 27 additions & 5 deletions spec/outputs/monasca/monasca_api_client_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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\"}}}}" }

Expand All @@ -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
Expand All @@ -51,33 +52,54 @@
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

it 'with application_type then it should request monasca with that value' do
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

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

0 comments on commit b20cbea

Please sign in to comment.