From ef3b2a2ce20e210a6300f3eeb667a3b3016e7c99 Mon Sep 17 00:00:00 2001 From: Phillip Baker Date: Sun, 9 Feb 2020 22:27:37 -0500 Subject: [PATCH] [slo] Convert to ruby1.9 compatible syntax. --- lib/dogapi/common.rb | 10 +++ lib/dogapi/facade.rb | 24 ++----- lib/dogapi/v1/service_level_objective.rb | 66 +++++++++---------- .../service_level_objective_spec.rb | 24 +++---- spec/spec_helper.rb | 6 +- 5 files changed, 63 insertions(+), 67 deletions(-) diff --git a/lib/dogapi/common.rb b/lib/dogapi/common.rb index 6508d089..7d491c27 100644 --- a/lib/dogapi/common.rb +++ b/lib/dogapi/common.rb @@ -224,4 +224,14 @@ def Dogapi.validate_tags(tags) raise ArgumentError, "Each tag needs to be a string. Current value: #{tag}" unless tag.is_a? String end end + + # Very simplified hash with indifferent access - access to string or symbol + # keys via symbols. E.g.: + # my_hash = { 'foo' => 1 } + # Dogapi.symbolized_access(my_hash) + # my_hash[:foo] # => 1 + def Dogapi.symbolized_access(hash) + hash.default_proc = proc { |h, k| h.key?(k.to_s) ? h[k.to_s] : nil } + hash + end end diff --git a/lib/dogapi/facade.rb b/lib/dogapi/facade.rb index 8089392e..5949a6a1 100644 --- a/lib/dogapi/facade.rb +++ b/lib/dogapi/facade.rb @@ -680,23 +680,12 @@ def unmute_host(hostname) # SERVICE LEVEL OBJECTIVES # - def create_service_level_objective(type: , name: , description: nil, tags: nil, thresholds: nil, numerator: nil, - denominator: nil, monitor_ids: nil, monitor_search: nil, groups: nil) - @service_level_objective_svc.create_service_level_objective(type: type, name: name, description: description, - tags: tags, thresholds: thresholds, - numerator: numerator, denominator: denominator, - monitor_ids: monitor_ids, - monitor_search: monitor_search, groups: groups) + def create_service_level_objective(type, slo_name, thresholds, options = {}) + @service_level_objective_svc.create_service_level_objective(type, slo_name, thresholds, options) end - def update_service_level_objective(slo_id: , type: , name: nil, description: nil, tags: nil, thresholds: nil, - numerator: nil, denominator: nil, monitor_ids: nil, monitor_search: nil, - groups: nil) - @service_level_objective_svc.update_service_level_objective(slo_id: slo_id, type: type, name: name, - description: description, tags: tags, - thresholds: thresholds, numerator: numerator, - denominator: denominator, monitor_ids: monitor_ids, - monitor_search: monitor_search, groups: groups) + def update_service_level_objective(slo_id, type, options = {}) + @service_level_objective_svc.update_service_level_objective(slo_id, type, options) end def get_service_level_objective(slo_id) @@ -707,9 +696,8 @@ def get_service_level_objective_history(slo_id, from_ts, to_ts) @service_level_objective_svc.get_service_level_objective_history(slo_id, from_ts, to_ts) end - def search_service_level_objective(slo_ids: nil, query: nil, offset: nil, limit: nil) - @service_level_objective_svc.search_service_level_objective(slo_ids: slo_ids, query: query, offset: offset, - limit: limit) + def search_service_level_objective(slo_ids = nil, query = nil, offset = nil, limit = nil) + @service_level_objective_svc.search_service_level_objective(slo_ids, query, offset, limit) end def can_delete_service_level_objective(slo_ids) diff --git a/lib/dogapi/v1/service_level_objective.rb b/lib/dogapi/v1/service_level_objective.rb index 1d1a2ec7..ae0534e7 100644 --- a/lib/dogapi/v1/service_level_objective.rb +++ b/lib/dogapi/v1/service_level_objective.rb @@ -8,55 +8,51 @@ class ServiceLevelObjectiveService < Dogapi::APIService API_VERSION = 'v1' - def create_service_level_objective(type: , name: , description: nil, tags: nil, thresholds: nil, - numerator: nil, denominator: nil, monitor_ids: nil, monitor_search: nil, - groups: nil) + def create_service_level_objective(type, slo_name, thresholds, options = {}) body = { type: type, - name: name, + name: slo_name, thresholds: thresholds } - if type == 'metric' + + symbolized_options = Dogapi.symbolized_access(options) + if type.to_s == 'metric' body[:query] = { - numerator: numerator, - denominator: denominator + numerator: symbolized_options[:numerator], + denominator: symbolized_options[:denominator] } else - body[:monitor_search] = monitor_search unless monitor_search.nil? - body[:monitor_ids] = monitor_ids unless monitor_ids.nil? - body[:groups] = groups unless groups.nil? + body[:monitor_search] = symbolized_options[:monitor_search] if symbolized_options[:monitor_search] + body[:monitor_ids] = symbolized_options[:monitor_ids] if symbolized_options[:monitor_ids] + body[:groups] = symbolized_options[:groups] if symbolized_options[:groups] end - body[:tags] = tags unless tags.nil? - body[:description] = description unless description.nil? + body[:tags] = symbolized_options[:tags] if symbolized_options[:tags] + body[:description] = symbolized_options[:description] if symbolized_options[:description] request(Net::HTTP::Post, "/api/#{API_VERSION}/slo", nil, body, true) end - def update_service_level_objective(slo_id: , type: , name: nil, description: nil, tags: nil, - thresholds: nil, numerator: nil, denominator: nil, monitor_ids: nil, - monitor_search: nil, groups: nil) + def update_service_level_objective(slo_id, type, options = {}) body = { type: type } - body[:name] = name unless name.nil? - - body[:thresholds] = thresholds unless thresholds.nil? - + symbolized_options = Dogapi.symbolized_access(options) if type == 'metric' - if !numerator.nil? && !denominator.nil? + if symbolized_options[:numerator] && symbolized_options[:denominator] body[:query] = { - numerator: numerator, - denominator: denominator + numerator: symbolized_options[:numerator], + denominator: symbolized_options[:denominator] } end else - body[:monitor_search] = monitor_search unless monitor_search.nil? - body[:monitor_ids] = monitor_ids unless monitor_ids.nil? - body[:groups] = groups unless groups.nil? + body[:monitor_search] = symbolized_options[:monitor_search] if symbolized_options[:monitor_search] + body[:monitor_ids] = symbolized_options[:monitor_ids] if symbolized_options[:monitor_ids] + body[:groups] = symbolized_options[:groups] if symbolized_options[:groups] + end + %i[name thresholds tags description].each do |a| + body[a] = symbolized_options[a] if symbolized_options[a] end - body[:tags] = tags unless tags.nil? - body[:description] = description unless description.nil? request(Net::HTTP::Put, "/api/#{API_VERSION}/slo/#{slo_id}", nil, body, true) end @@ -65,7 +61,7 @@ def get_service_level_objective(slo_id) request(Net::HTTP::Get, "/api/#{API_VERSION}/slo/#{slo_id}", nil, nil, false) end - def search_service_level_objective(slo_ids: nil, query: nil, offset: nil, limit: nil) + def search_service_level_objective(slo_ids, query, offset, limit) params = {} params[:offset] = offset unless offset.nil? params[:limit] = limit unless limit.nil? @@ -96,19 +92,19 @@ def delete_timeframes_service_level_objective(ops) def get_service_level_objective_history(slo_id, from_ts, to_ts) params = { - from_ts: from_ts, - to_ts: to_ts + from_ts: from_ts, + to_ts: to_ts } request(Net::HTTP::Get, "/api/#{API_VERSION}/slo/#{slo_id}/history", params, nil, false) end def can_delete_service_level_objective(slo_ids) params = {} - if slo_ids.is_a?Array - params[:ids] = slo_ids.join(',') - else - params[:ids] = slo_ids - end + params[:ids] = if slo_ids.is_a? Array + slo_ids.join(',') + else + slo_ids + end request(Net::HTTP::Get, "/api/#{API_VERSION}/slo/can_delete", params, nil, false) end diff --git a/spec/integration/service_level_objective_spec.rb b/spec/integration/service_level_objective_spec.rb index 01da29fa..b34cba9b 100644 --- a/spec/integration/service_level_objective_spec.rb +++ b/spec/integration/service_level_objective_spec.rb @@ -11,19 +11,21 @@ SLO_THRESHOLDS = [{ timeframe: '7d', target: 90 }, { timeframe: '30d', target: 95 }].freeze describe '#create_service_level_objective' do - it_behaves_like 'an api method with named args', - :create_service_level_objective, {type: SLO_TYPE, name: SLO_NAME, description: SLO_DESCRIPTION, - tags: SLO_TAGS, thresholds: SLO_THRESHOLDS, - numerator: SLO_QUERY_NUMERATOR, - denominator: SLO_QUERY_DENOMINATOR}, + it_behaves_like 'an api method', + :create_service_level_objective, [SLO_TYPE, SLO_NAME, SLO_THRESHOLDS, { + description: SLO_DESCRIPTION, + tags: SLO_TAGS, + numerator: SLO_QUERY_NUMERATOR, + denominator: SLO_QUERY_DENOMINATOR + }], :post, '/slo', 'type' => SLO_TYPE, 'name' => SLO_NAME, 'thresholds' => SLO_THRESHOLDS, 'query' => { numerator: SLO_QUERY_NUMERATOR, denominator: SLO_QUERY_DENOMINATOR }, 'tags' => SLO_TAGS, 'description' => SLO_DESCRIPTION end describe '#update_service_level_objective' do - it_behaves_like 'an api method with named args', - :update_service_level_objective, {slo_id: SLO_ID, type: SLO_TYPE, name: SLO_NAME}, + it_behaves_like 'an api method', + :update_service_level_objective, [SLO_ID, SLO_TYPE, { name: SLO_NAME }], :put, "/slo/#{SLO_ID}", 'type' => SLO_TYPE, 'name' => SLO_NAME end @@ -36,18 +38,18 @@ describe '#get_service_level_objective_history' do it_behaves_like 'an api method with params', :get_service_level_objective_history, [SLO_ID], - :get, "/slo/#{SLO_ID}/history", 'from_ts' => 0, 'to_ts' => 1000000 + :get, "/slo/#{SLO_ID}/history", 'from_ts' => 0, 'to_ts' => 1_000_000 end describe '#can_delete_service_level_objective' do it_behaves_like 'an api method with params', :can_delete_service_level_objective, [], - :get, "/slo/can_delete", 'ids' => [SLO_ID] + :get, '/slo/can_delete', 'ids' => [SLO_ID] end describe '#search_service_level_objective' do - it_behaves_like 'an api method with named args making params', - :search_service_level_objective, {slo_ids: [SLO_ID]}, + it_behaves_like 'an api method with optional params', + :search_service_level_objective, [[SLO_ID]], :get, '/slo/', 'ids' => SLO_ID end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 9036a2c3..8d0d18d1 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -56,8 +56,8 @@ module SpecDog body = MultiJson.dump(body) if body expect(WebMock).to have_requested(request, /#{url}|#{old_url}/).with( - # ignore query: default_query -- here as in the test it's never properly included - body: body + # ignore query: default_query -- here as in the test it's never properly included + body: body ) end end @@ -119,7 +119,7 @@ module SpecDog # hack/note: do not merge with default_query for this test case expect(WebMock).to have_requested(request, url).with( - query: params + query: params ) end end