stacker_bee 2.1.0.pre236 → 2.1.1.pre247

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,15 +1,15 @@
1
1
  ---
2
2
  !binary "U0hBMQ==":
3
3
  metadata.gz: !binary |-
4
- ZGMwMTczNmU3MjVmMjM2ZGM2OGI1MzU1M2NhNDI1MTE0NDdiYjhiYQ==
4
+ ZTU4NzkwYjgxZGRjZTQ0MzBiMmY0Nzc2YThkMzRiYjA3ZGJmMzIyNw==
5
5
  data.tar.gz: !binary |-
6
- ZDZkZWJjMDkyNWNjMTc2YmUxYmFkYzYxOTgzZTlkMDNlMjEzY2NmMQ==
6
+ OGJiOTIwNzY3ODM4NzBkNGRkNGVjNzNmN2UzMzgxNzM0NTBkYzZhMg==
7
7
  SHA512:
8
8
  metadata.gz: !binary |-
9
- NDhhNmFjNWJlY2FjMTQzNDk1MzFjOGU5NGFhZDA3ODMwYzU1YjBlNzJkNmFl
10
- ZWUyNTY5YmVjM2VkZWE3Y2VmYjk1YTdhODczOGFhMWM2ZGYzNmRjZWQyN2Q0
11
- ZmYxNzE5OTJiZDVlZWJkZTZjYzA2ZGI1ZDI3YWUxMGNmNGIwN2M=
9
+ MmVkYWNhOTQ1N2IxNDNhMTRkMWE3NzQyZmViM2Q1MTg3YWNjMzdmMGUyMDBh
10
+ MTc1OTNiNTAyMThkY2NhNmM4MmU4ZWQyNmM4NjJhNzZmZGM1YTYzY2ZiZDZj
11
+ NThhOTgzOGVmNDVmNTM4ODAzNmE4NWJmYTAyNDRhNzZhM2FkNmU=
12
12
  data.tar.gz: !binary |-
13
- ZjUxZTg3YmE0ZmVlYTBmNjUwNmE1ZWNkYjg3MWM4ODYwOGFkOTc3ZmQ1Mjdl
14
- MGE0NzkzNTJhZTYzYjZjOTI2ZWIwNWY2ZTc3ZmZkODY0MGY2ZGYzMjNlY2I1
15
- YzA0MDdmYTBlZGE4MzJiMTAzNjkyOGQyYmY1NDEwNzU3N2UwODI=
13
+ MDkyNDVmZDhiOGZmMGI2OTU5YjQ5MjNjMGNhNGMwOTg5MGFiMzVhYmUxN2Ix
14
+ OTdiYTBhNzA0OGQwYmIzZWU1NzljNmI4NWZiZjY1MTY5MTcxMGE3MzkxOWJh
15
+ NGY0ZmUwZWI5Y2Y3YmU3YWE3YTc5ZGNmZGQyNTQ5MTAzNzc3YmE=
data/README.md CHANGED
@@ -204,31 +204,7 @@ StackerBee itself puts some middlewares on Faraday. Any middlewares you add will
204
204
 
205
205
  ### Logging
206
206
 
207
- Logging is best handled with Faraday middleware.
208
-
209
- #### GELF/Graylog2
210
-
211
- If you're using the Graylog2 GELF format, you're in luck because StackerBee currently ships with a Faraday middleware for that. Here's an example of logging to Graylog2:
212
-
213
- ```ruby
214
- logger = GELF::Notifier.new("localhost", 12201)
215
-
216
- StackerBee::Client.configuration = {
217
- faraday_middlewares: ->(faraday) do
218
- faraday.use StackerBee::HTTPMiddleware::Graylog, logger
219
- end
220
- }
221
- ```
222
-
223
- #### Basic logging
224
-
225
- To log to a file or STDOUT, Faraday has a built-in logger. You can use it like so:
226
-
227
- ```ruby
228
- StackerBee::Client.configuration = {
229
- faraday_middlewares: ->(faraday) { faraday.response :logger }
230
- }
231
- ```
207
+ You can configure logging by passing in a logger object that adheres to the standard log4* logging conventions
232
208
 
233
209
  ### Bulk Configuration
234
210
 
@@ -21,6 +21,7 @@ require "stacker_bee/middleware/raise_on_http_error"
21
21
  require "stacker_bee/middleware/http_status"
22
22
  require "stacker_bee/middleware/console_access"
23
23
  require "stacker_bee/middleware/error_message"
24
+ require "stacker_bee/middleware/log_response"
24
25
 
25
26
  # rubocop:disable ClassLength
26
27
  module StackerBee
@@ -48,6 +49,7 @@ module StackerBee
48
49
  # response
49
50
  builder.use Middleware::RaiseOnHTTPError
50
51
  builder.use Middleware::ErrorMessage
52
+ builder.use Middleware::LogResponse
51
53
  builder.use Middleware::HTTPStatus
52
54
  builder.use Middleware::CleanResponse
53
55
  builder.use Middleware::RashifyResponse
@@ -110,7 +112,8 @@ module StackerBee
110
112
  env = Middleware::Environment.new(
111
113
  endpoint_name: endpoint_name,
112
114
  api_key: configuration.api_key,
113
- params: params
115
+ params: params,
116
+ logger: configuration.logger
114
117
  )
115
118
 
116
119
  middleware_app.call(env)
@@ -9,12 +9,12 @@ module StackerBee
9
9
  :secret_key,
10
10
  :api_key,
11
11
  :middlewares,
12
- :faraday_middlewares
12
+ :faraday_middlewares,
13
+ :logger
13
14
  ]
14
15
 
15
16
  def initialize(attrs = nil)
16
17
  @attributes = attrs || {}
17
-
18
18
  @attributes.each_pair do |key, value|
19
19
  unless ATTRIBUTES.include?(key)
20
20
  fail NoAttributeError, "No attribute defined: '#{key}'"
@@ -46,6 +46,10 @@ module StackerBee
46
46
  attribute :faraday_middlewares, proc {}
47
47
  end
48
48
 
49
+ def logger
50
+ attribute :logger
51
+ end
52
+
49
53
  def to_hash
50
54
  @attributes
51
55
  end
@@ -27,7 +27,6 @@ module StackerBee
27
27
  @faraday = Faraday.new(options) do |faraday|
28
28
  faraday.use HTTPMiddleware::Detokenizer
29
29
  faraday.use HTTPMiddleware::SignedQuery, configuration.secret_key
30
-
31
30
  configuration.faraday_middlewares.call faraday
32
31
 
33
32
  unless using_adapter?(faraday.builder.handlers)
@@ -45,6 +44,7 @@ module StackerBee
45
44
  def get(path, params)
46
45
  @faraday.get(path, params)
47
46
  rescue Faraday::Error::ConnectionFailed => error
47
+ configuration.logger.error error if configuration.logger
48
48
  raise ConnectionError,
49
49
  "Failed to connect to #{configuration.url}, #{error}"
50
50
  end
@@ -10,6 +10,7 @@ module StackerBee
10
10
  self.request = Request.new(defaults.merge(request_attributes))
11
11
 
12
12
  self.response = Response.new
13
+ self.logger = request_attributes[:logger]
13
14
  end
14
15
 
15
16
  class Request < OpenStruct
@@ -0,0 +1,36 @@
1
+ module StackerBee
2
+ module Middleware
3
+ class LogResponse < Base
4
+ def after(env)
5
+ return unless env.logger
6
+ params = env.request.params.to_a.sort
7
+ command = params.find { |key, _| key == "command" }.last
8
+
9
+ log(env, params, command)
10
+ end
11
+
12
+ def log(env, params, command)
13
+ log_data = base_log_data(env, params)
14
+ if env.response.success?
15
+ log_data[:short_message] = command
16
+ env.logger.info log_data
17
+ else
18
+ log_data[:short_message] = "#{command} failed: #{env.response.error}"
19
+ env.logger.error log_data
20
+ end
21
+ end
22
+
23
+ def base_log_data(env, params)
24
+ {
25
+ request_path: env.request.path,
26
+ params: params,
27
+ response_body: env.raw_response[:body]
28
+ }
29
+ end
30
+
31
+ def content_types
32
+ /javascript/
33
+ end
34
+ end
35
+ end
36
+ end
@@ -1,3 +1,3 @@
1
1
  module StackerBee
2
- VERSION = "2.1.0"
2
+ VERSION = "2.1.1"
3
3
  end
data/lib/stacker_bee.rb CHANGED
@@ -1,5 +1,2 @@
1
1
  require_relative "stacker_bee/version"
2
2
  require_relative "stacker_bee/client"
3
-
4
- require_relative "faraday_middleware/response/graylog"
5
- require_relative "stacker_bee/http_middleware/graylog"
data/spec/spec_helper.rb CHANGED
@@ -17,6 +17,11 @@ CONFIG.merge!(YAML.load(File.read(config_file))) if File.exist?(config_file)
17
17
 
18
18
  require 'webmock/rspec'
19
19
 
20
+ support_files = Dir[File.join(
21
+ File.expand_path("../../spec/support/**/*.rb", __FILE__)
22
+ )]
23
+ support_files.each { |f| require f }
24
+
20
25
  RSpec.configure do |config|
21
26
  config.treat_symbols_as_metadata_keys_with_true_values = true
22
27
  config.run_all_when_everything_filtered = true
@@ -32,37 +37,3 @@ RSpec.configure do |config|
32
37
  StackerBee::Client.reset!
33
38
  end
34
39
  end
35
-
36
- require 'vcr'
37
-
38
- VCR.configure do |c|
39
- c.hook_into :webmock
40
- c.cassette_library_dir = 'spec/cassettes'
41
-
42
- c.filter_sensitive_data('<CLOUD_STACK_URL>') do
43
- CONFIG["url"]
44
- end
45
-
46
- c.filter_sensitive_data('<CLOUD_STACK_HOST>') do
47
- uri = URI.parse(CONFIG["url"])
48
- "#{uri.scheme}://#{uri.host}:#{uri.port}"
49
- end
50
-
51
- c.filter_sensitive_data('<CLOUD_STACK_API_KEY>') do
52
- CONFIG["api_key"]
53
- end
54
-
55
- c.filter_sensitive_data('<CLOUD_STACK_SECRET_KEY>') do
56
- CONFIG["secret_key"]
57
- end
58
-
59
- c.default_cassette_options = {
60
- record: :new_episodes,
61
- match_requests_on: [
62
- :method,
63
- VCR.request_matchers.uri_without_param(:signature)
64
- ]
65
- }
66
-
67
- c.configure_rspec_metadata!
68
- end
@@ -0,0 +1,26 @@
1
+ class FakeLogger
2
+ attr_accessor :logs
3
+ def initialize
4
+ @logs = []
5
+ end
6
+
7
+ def debug(obj)
8
+ logs << obj
9
+ end
10
+
11
+ def info(obj)
12
+ logs << obj
13
+ end
14
+
15
+ def warn(obj)
16
+ logs << obj
17
+ end
18
+
19
+ def error(obj)
20
+ logs << obj
21
+ end
22
+
23
+ def fatal(obj)
24
+ logs << obj
25
+ end
26
+ end
@@ -0,0 +1,33 @@
1
+ require 'vcr'
2
+
3
+ VCR.configure do |c|
4
+ c.hook_into :webmock
5
+ c.cassette_library_dir = 'spec/cassettes'
6
+
7
+ c.filter_sensitive_data('<CLOUD_STACK_URL>') do
8
+ CONFIG["url"]
9
+ end
10
+
11
+ c.filter_sensitive_data('<CLOUD_STACK_HOST>') do
12
+ uri = URI.parse(CONFIG["url"])
13
+ "#{uri.scheme}://#{uri.host}:#{uri.port}"
14
+ end
15
+
16
+ c.filter_sensitive_data('<CLOUD_STACK_API_KEY>') do
17
+ CONFIG["api_key"]
18
+ end
19
+
20
+ c.filter_sensitive_data('<CLOUD_STACK_SECRET_KEY>') do
21
+ CONFIG["secret_key"]
22
+ end
23
+
24
+ c.default_cassette_options = {
25
+ record: :new_episodes,
26
+ match_requests_on: [
27
+ :method,
28
+ VCR.request_matchers.uri_without_param(:signature)
29
+ ]
30
+ }
31
+
32
+ c.configure_rspec_metadata!
33
+ end
@@ -7,11 +7,12 @@ describe StackerBee::Middleware::CloudStackAPI do
7
7
  path: path
8
8
  )
9
9
  end
10
-
11
10
  let(:middleware) { described_class.new(api_key: "API-KEY", params: {}) }
12
11
  let(:path) { nil }
13
12
 
14
- before { middleware.before(env) }
13
+ before do
14
+ middleware.before(env)
15
+ end
15
16
 
16
17
  describe "request" do
17
18
  subject { env.request }
@@ -0,0 +1,51 @@
1
+ require 'spec_helper'
2
+ describe StackerBee::Middleware::LogResponse do
3
+ describe "after" do
4
+ let(:logger) { FakeLogger.new }
5
+
6
+ let(:env) do
7
+ env = StackerBee::Middleware::Environment.new(logger: logger)
8
+ env.raw_response = { body: "some body" }
9
+ env.request.params = { "command" => "some command" }
10
+ env.request.path = "some/path"
11
+ env
12
+ end
13
+
14
+ shared_examples_for "all logs" do
15
+ it "logs the details" do
16
+ logger.logs.length.should eq 1
17
+ logger.logs.last[:request_path].should eq env.request.path
18
+ logger.logs.last[:params].should eq env.request.params.to_a
19
+ logger.logs.last[:response_body].should eq env.raw_response[:body]
20
+ end
21
+ end
22
+
23
+ context "error response" do
24
+ before do
25
+ env.response.success = false
26
+ env.response.error = "invalid request"
27
+ subject.after(env)
28
+ end
29
+
30
+ it_should_behave_like "all logs"
31
+ it "should have logged the details" do
32
+ logger.logs.last[:short_message].should eq \
33
+ "some command failed: invalid request"
34
+ end
35
+ end
36
+
37
+ context "success response" do
38
+ before do
39
+ env.response.success = true
40
+ subject.after(env)
41
+ end
42
+
43
+ it_should_behave_like "all logs"
44
+ it "should have logged the details" do
45
+ logger.logs.length.should eq 1
46
+ logger.logs.last[:short_message].should eq "some command"
47
+ end
48
+
49
+ end
50
+ end
51
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: stacker_bee
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.1.0.pre236
4
+ version: 2.1.1.pre247
5
5
  platform: ruby
6
6
  authors:
7
7
  - Greg Sterndale
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2014-06-24 00:00:00.000000000 Z
12
+ date: 2014-07-03 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: faraday
@@ -176,7 +176,6 @@ files:
176
176
  - bin/stacker_bee
177
177
  - config.default.yml
178
178
  - config/4.2.json
179
- - lib/faraday_middleware/response/graylog.rb
180
179
  - lib/stacker_bee.rb
181
180
  - lib/stacker_bee/api.rb
182
181
  - lib/stacker_bee/builder.rb
@@ -200,6 +199,7 @@ files:
200
199
  - lib/stacker_bee/middleware/format_values.rb
201
200
  - lib/stacker_bee/middleware/http_status.rb
202
201
  - lib/stacker_bee/middleware/json_body.rb
202
+ - lib/stacker_bee/middleware/log_response.rb
203
203
  - lib/stacker_bee/middleware/raise_on_http_error.rb
204
204
  - lib/stacker_bee/middleware/rashify_response.rb
205
205
  - lib/stacker_bee/middleware/remove_empty_strings.rb
@@ -232,12 +232,12 @@ files:
232
232
  - spec/integration/console_spec.rb
233
233
  - spec/integration/request_spec.rb
234
234
  - spec/spec_helper.rb
235
- - spec/units/faraday_graylog_middleware_spec.rb
235
+ - spec/support/fake_logger.rb
236
+ - spec/support/vcr.rb
236
237
  - spec/units/stacker_bee/api_spec.rb
237
238
  - spec/units/stacker_bee/client_spec.rb
238
239
  - spec/units/stacker_bee/configuration_spec.rb
239
240
  - spec/units/stacker_bee/connection_spec.rb
240
- - spec/units/stacker_bee/http_middleware/graylog_spec.rb
241
241
  - spec/units/stacker_bee/middleware/adapter_spec.rb
242
242
  - spec/units/stacker_bee/middleware/base_spec.rb
243
243
  - spec/units/stacker_bee/middleware/cloudstack_api_spec.rb
@@ -248,6 +248,7 @@ files:
248
248
  - spec/units/stacker_bee/middleware/format_keys_spec.rb
249
249
  - spec/units/stacker_bee/middleware/format_values_spec.rb
250
250
  - spec/units/stacker_bee/middleware/http_status_spec.rb
251
+ - spec/units/stacker_bee/middleware/log_response_spec.rb
251
252
  - spec/units/stacker_bee/middleware/raise_on_http_errors_spec.rb
252
253
  - spec/units/stacker_bee/middleware/remove_empty_strings_spec.rb
253
254
  - spec/units/stacker_bee/middleware/remove_nils_spec.rb
@@ -305,12 +306,12 @@ test_files:
305
306
  - spec/integration/console_spec.rb
306
307
  - spec/integration/request_spec.rb
307
308
  - spec/spec_helper.rb
308
- - spec/units/faraday_graylog_middleware_spec.rb
309
+ - spec/support/fake_logger.rb
310
+ - spec/support/vcr.rb
309
311
  - spec/units/stacker_bee/api_spec.rb
310
312
  - spec/units/stacker_bee/client_spec.rb
311
313
  - spec/units/stacker_bee/configuration_spec.rb
312
314
  - spec/units/stacker_bee/connection_spec.rb
313
- - spec/units/stacker_bee/http_middleware/graylog_spec.rb
314
315
  - spec/units/stacker_bee/middleware/adapter_spec.rb
315
316
  - spec/units/stacker_bee/middleware/base_spec.rb
316
317
  - spec/units/stacker_bee/middleware/cloudstack_api_spec.rb
@@ -321,6 +322,7 @@ test_files:
321
322
  - spec/units/stacker_bee/middleware/format_keys_spec.rb
322
323
  - spec/units/stacker_bee/middleware/format_values_spec.rb
323
324
  - spec/units/stacker_bee/middleware/http_status_spec.rb
325
+ - spec/units/stacker_bee/middleware/log_response_spec.rb
324
326
  - spec/units/stacker_bee/middleware/raise_on_http_errors_spec.rb
325
327
  - spec/units/stacker_bee/middleware/remove_empty_strings_spec.rb
326
328
  - spec/units/stacker_bee/middleware/remove_nils_spec.rb
@@ -1,35 +0,0 @@
1
- require 'faraday_middleware'
2
- require 'faraday_middleware/response_middleware'
3
-
4
- module FaradayMiddleware
5
- class Graylog < ResponseMiddleware
6
- INFO = 1
7
- ERROR = 3
8
-
9
- attr_accessor :facility
10
-
11
- def initialize(app, logger, options = {})
12
- @logger = logger
13
- self.facility = options[:facility] || "faraday-middleware-graylog"
14
-
15
- super app, options
16
- end
17
-
18
- def process_response(env)
19
- @logger.info(
20
- facility: facility,
21
- short_message: short_message(env),
22
- level: level(env),
23
- _data: env.dup.tap { |e| e.delete(:response) }
24
- )
25
- end
26
-
27
- def short_message(env)
28
- facility + " Request"
29
- end
30
-
31
- def level(env)
32
- env[:status] < 400 ? INFO : ERROR
33
- end
34
- end
35
- end
@@ -1,80 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe FaradayMiddleware::Graylog do
4
- subject { log_data }
5
-
6
- class DummyLogger
7
- attr_accessor :data
8
- alias_method :info, :data=
9
- end
10
-
11
- let(:log_data) { logger.data }
12
- let(:logger) { DummyLogger.new }
13
-
14
- let(:dummy_adapter) { ->(env) { Faraday::Response.new(env) } }
15
- let(:middleware) do
16
- described_class.new(dummy_adapter, logger, facility: facility)
17
- end
18
-
19
- let(:env) do
20
- {
21
- body: "DATA",
22
- response_headers: {},
23
- response: {},
24
- status: status
25
- }
26
- end
27
- let(:facility) { nil }
28
-
29
- before { middleware.call(env) }
30
-
31
- context "a basic request" do
32
- let(:status) { 200 }
33
-
34
- its([:facility]) { should eq "faraday-middleware-graylog" }
35
- its([:short_message]) { should eq "faraday-middleware-graylog Request" }
36
- its([:_data]) do
37
- should eq(body: "DATA", response_headers: {}, status: status)
38
- end
39
- end
40
-
41
- context "given a facility" do
42
- let(:status) { 200 }
43
- let(:facility) { "stacker_bee" }
44
-
45
- its([:facility]) { should eq "stacker_bee" }
46
- end
47
-
48
- context "a successful request" do
49
- let(:status) { 200 }
50
- its([:level]) { should eq FaradayMiddleware::Graylog::INFO }
51
- end
52
-
53
- context "a failed request" do
54
- let(:status) { 500 }
55
- its([:level]) { should eq FaradayMiddleware::Graylog::ERROR }
56
- end
57
-
58
- describe "a subclass" do
59
- class GrayLogSubclass < FaradayMiddleware::Graylog
60
- def short_message(env)
61
- "Short message: #{env[:status]}"
62
- end
63
-
64
- def level(env)
65
- env[:status]
66
- end
67
- end
68
-
69
- let(:middleware) { GrayLogSubclass.new(dummy_adapter, logger) }
70
- let(:status) { 401 }
71
-
72
- it "can override the level determining logic" do
73
- log_data[:level].should eq 401
74
- end
75
-
76
- it "can override the short_message logic" do
77
- log_data[:short_message].should eq "Short message: 401"
78
- end
79
- end
80
- end
@@ -1,51 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe StackerBee::HTTPMiddleware::Graylog do
4
- subject { log_data }
5
-
6
- class DummyLogger
7
- attr_accessor :data
8
- alias_method :info, :data=
9
- end
10
-
11
- let(:log_data) { logger.data }
12
- let(:logger) { DummyLogger.new }
13
-
14
- let(:dummy_adapter) { ->(env) { Faraday::Response.new(env) } }
15
- let(:middleware) { described_class.new(dummy_adapter, logger) }
16
- let(:status) { 200 }
17
-
18
- let(:env) do
19
- {
20
- body: "DATA",
21
- response_headers: {},
22
- response: {},
23
- status: status,
24
- url: URI.parse("http://a.b/?key=KEY&command=listVirtualMachines&val=val")
25
- }
26
- end
27
-
28
- before { middleware.call(env) }
29
-
30
- it "sets a custom short message" do
31
- log_data[:short_message].should eq "StackerBee listVirtualMachines"
32
- end
33
-
34
- its([:facility]) { should == "stacker-bee" }
35
-
36
- context "without a command in the url" do
37
- let(:env) do
38
- {
39
- body: "DATA",
40
- response_headers: {},
41
- response: {},
42
- status: status,
43
- url: URI.parse("http://a.b/?key=KEY&&val=val")
44
- }
45
- end
46
-
47
- it "sets a custom short message" do
48
- log_data[:short_message].should eq "StackerBee"
49
- end
50
- end
51
- end