Skip to content

Commit

Permalink
Merge pull request sensu#104 from portertech/api
Browse files Browse the repository at this point in the history
[api] GET /stashes returns an array of the stash keys
  • Loading branch information
portertech committed Nov 23, 2011
2 parents be3d239 + cde0230 commit bd564e0
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 49 deletions.
97 changes: 56 additions & 41 deletions lib/sensu/api.rb
Expand Up @@ -44,17 +44,17 @@ def self.stop(signal)

aget '/clients' do
$logger.debug('[clients] -- ' + request.ip + ' -- GET -- request for client list')
current_clients = Array.new
response = Array.new
$redis.smembers('clients').callback do |clients|
unless clients.empty?
clients.each_with_index do |client, index|
$redis.get('client:' + client).callback do |client_json|
current_clients.push(JSON.parse(client_json))
body current_clients.to_json if index == clients.size - 1
response.push(JSON.parse(client_json))
body response.to_json if index == clients.size - 1
end
end
else
body current_clients.to_json
body response.to_json
end
end
end
Expand All @@ -73,10 +73,16 @@ def self.stop(signal)
if client_exists
$redis.hgetall('events:' + client).callback do |events|
events.keys.each do |check_name|
check = {:name => check_name, :issued => Time.now.to_i, :status => 0, :output => 'Client is being removed'}
check = {
:name => check_name,
:issued => Time.now.to_i,
:status => 0,
:output => 'Client is being removed on request of the API',
:force_resolve => true
}
$amq.queue('results').publish({:client => client, :check => check}.to_json)
end
EM.add_timer(5) do
EM.add_timer(8) do
$redis.srem('clients', client)
$redis.del('events:' + client)
$redis.del('client:' + client)
Expand All @@ -93,30 +99,28 @@ def self.stop(signal)

aget '/events' do
$logger.debug('[events] -- ' + request.ip + ' -- GET -- request for event list')
current_events = Hash.new
response = Hash.new
$redis.smembers('clients').callback do |clients|
unless clients.empty?
clients.each_with_index do |client, index|
$redis.hgetall('events:' + client).callback do |events|
client_events = events
client_events.each do |key, value|
client_events[key] = JSON.parse(value)
events.each do |key, value|
events[key] = JSON.parse(value)
end
current_events[client] = client_events unless client_events.empty?
body current_events.to_json if index == clients.size - 1
response[client] = events unless events.empty?
body response.to_json if index == clients.size - 1
end
end
else
body current_events.to_json
body response.to_json
end
end
end

aget '/event/:client/:check' do |client, check|
$logger.debug('[event] -- ' + request.ip + ' -- GET -- request for event -- ' + client + ' -- ' + check)
$redis.hgetall('events:' + client).callback do |events|
client_events = events
event = client_events[check]
event = events[check]
status 404 if event.nil?
body event
end
Expand Down Expand Up @@ -162,30 +166,10 @@ def self.stop(signal)
body nil
end
$redis.set('stash:' + path, stash.to_json).callback do
status 201
body nil
end
end

apost '/stashes' do
$logger.debug('[stashes] -- ' + request.ip + ' -- POST -- request for multiple stashes')
begin
paths = JSON.parse(request.body.read)
rescue JSON::ParserError
status 400
body nil
end
stashes = Hash.new
if paths.is_a?(Array) && paths.size > 0
paths.each_with_index do |path, index|
$redis.get('stash:' + path).callback do |stash|
stashes[path] = JSON.parse(stash) unless stash.nil?
body stashes.to_json if index == paths.size - 1
end
$redis.sadd('stashes', path).callback do
status 201
body nil
end
else
status 400
body nil
end
end

Expand All @@ -201,9 +185,11 @@ def self.stop(signal)
$logger.debug('[stash] -- ' + request.ip + ' -- DELETE -- request for stash -- ' + path)
$redis.exists('stash:' + path).callback do |stash_exist|
if stash_exist
$redis.del('stash:' + path).callback do
status 204
body nil
$redis.srem('stashes', path).callback do
$redis.del('stash:' + path).callback do
status 204
body nil
end
end
else
status 404
Expand All @@ -212,6 +198,35 @@ def self.stop(signal)
end
end

aget '/stashes' do
$logger.debug('[stashes] -- ' + request.ip + ' -- GET -- request for list of stashes')
$redis.smembers('stashes') do |stashes|
body stashes.to_json
end
end

apost '/stashes' do
$logger.debug('[stashes] -- ' + request.ip + ' -- POST -- request for multiple stashes')
begin
paths = JSON.parse(request.body.read)
rescue JSON::ParserError
status 400
body nil
end
response = Hash.new
if paths.is_a?(Array) && paths.size > 0
paths.each_with_index do |path, index|
$redis.get('stash:' + path).callback do |stash|
response[path] = JSON.parse(stash) unless stash.nil?
body response.to_json if index == paths.size - 1
end
end
else
status 400
body nil
end
end

def self.test(options={})
self.setup(options)
$redis.set('client:' + @settings.client.name, @settings.client.to_json).callback do
Expand Down
29 changes: 21 additions & 8 deletions test/sensu_api_test.rb
Expand Up @@ -17,11 +17,9 @@ def test_get_clients
clients = JSON.parse(http.response)
assert(clients.is_a?(Array))
assert_block "Response didn't contain the test client" do
contains_test_client = false
clients.each do |client|
contains_test_client = true if client['name'] == @settings.client.name
clients.any? do |client|
client['name'] == @settings.client.name
end
contains_test_client
end
done
end
Expand Down Expand Up @@ -172,6 +170,23 @@ def test_get_stash
end

def test_get_stashes
EM.add_timer(1) do
http = EM::HttpRequest.new(@api + '/stashes').get
http.callback do
assert_equal(200, http.response_header.status)
stashes = JSON.parse(http.response)
assert(stashes.is_a?(Array))
assert_block "Response didn't contain a test stash" do
stashes.any? do |path, stash|
['test/test', 'tester'].include?(path)
end
end
done
end
end
end

def test_multi_get_stashes
EM.add_timer(1) do
options = {
:body => '["test/test", "tester"]'
Expand All @@ -182,11 +197,9 @@ def test_get_stashes
stashes = JSON.parse(http.response)
assert(stashes.is_a?(Hash))
assert_block "Response didn't contain a test stash" do
contains_test_stash = false
stashes.each do |path, stash|
contains_test_stash = true if ['test/test', 'tester'].include?(path)
stashes.any? do |path, stash|
['test/test', 'tester'].include?(path)
end
contains_test_stash
end
done
end
Expand Down

0 comments on commit bd564e0

Please sign in to comment.