Skip to content

Commit

Permalink
[slo] Convert to ruby1.9 compatible syntax.
Browse files Browse the repository at this point in the history
  • Loading branch information
phillip-dd committed Feb 10, 2020
1 parent ac9cf81 commit 5fb0157
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 94 deletions.
10 changes: 10 additions & 0 deletions lib/dogapi/common.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
24 changes: 6 additions & 18 deletions lib/dogapi/facade.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand Down
66 changes: 31 additions & 35 deletions lib/dogapi/v1/service_level_objective.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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?
Expand Down Expand Up @@ -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

Expand Down
24 changes: 13 additions & 11 deletions spec/integration/service_level_objective_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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

Expand Down
30 changes: 0 additions & 30 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,22 +46,6 @@ module SpecDog
end
end

shared_examples 'an api method with named args' do |command, args, request, endpoint, body|
it 'queries the api' do
url = api_url + endpoint
old_url = old_api_url + endpoint
stub_request(request, /#{url}|#{old_url}/).to_return(body: '{}').then.to_raise(StandardError)
expect(dog.send(command, **args)).to eq ['200', {}]

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
)
end
end

shared_examples 'an api method with options' do |command, args, request, endpoint, body|
include_examples 'an api method', command, args, request, endpoint, body
it 'queries the api with options' do
Expand Down Expand Up @@ -110,20 +94,6 @@ module SpecDog
end
end

shared_examples 'an api method with named args making params' do |command, args, request, endpoint, params|
it 'queries the api with params' do
url = api_url + endpoint
stub_request(request, /#{url}/).to_return(body: '{}').then.to_raise(StandardError)
expect(dog.send(command, **args)).to eq ['200', {}]
params.each { |k, v| params[k] = v.join(',') if v.is_a? Array }
# hack/note: do not merge with default_query for this test case

expect(WebMock).to have_requested(request, url).with(
query: params
)
end
end

shared_examples 'an api method with optional params' do |command, args, request, endpoint, opt_params|
include_examples 'an api method', command, args, request, endpoint
it 'queries the api with optional params' do
Expand Down

0 comments on commit 5fb0157

Please sign in to comment.