lib/ardtweeno/restapi.rb in ardtweeno-0.4.0 vs lib/ardtweeno/restapi.rb in ardtweeno-0.5.0

- old
+ new

@@ -114,10 +114,15 @@ erb :topology, :locals => {:theTopology=>theResponse} end + get '/api' do + erb :api + end + + get '/graph/v1/punchcard/:node' do |node| begin theData, theDays, theRange= @@theDispatcher.constructPunchcard(params) rescue Exception => e @@ -162,24 +167,35 @@ ######################################################################################################### get '/api/v1/zones' do - throw :halt, [ 404, "404 Page Not Found" ] unless @@theDispatcher.authenticate?(params[:key]) + auth, zonedata = @@theDispatcher.authenticate?(params[:key]) + throw :halt, [ 404, "404 Page Not Found" ] unless auth settings.log.debug "The retrieve zones hook has been called" + + unless zonedata.nil? + params["role"] = zonedata[:role] + unless params.has_key?("zonename") then params["zonename"] = zonedata["zonename"]; end + end begin @@theDispatcher.retrieve_zones(params).to_json # Returns String in JSON form rescue Exception => e throw :halt, [ 500, "500 Internal Server Error" ] end end - get '/api/v1/zones/:zonename' do |zoneid| - throw :halt, [ 404, "404 Page Not Found" ] unless @@theDispatcher.authenticate?(params[:key]) + get '/api/v1/zones/:zonename' do |zonename| + auth, zonedata = @@theDispatcher.authenticate?(params[:key]) + throw :halt, [ 404, "404 Page Not Found" ] unless auth settings.log.debug "The retrieve zones hook has been called" + + unless zonedata.nil? + params["role"] = zonedata[:role] + end begin @@theDispatcher.retrieve_zones(params).to_json # Returns String in JSON form rescue Exception => e throw :halt, [ 500, "500 Internal Server Error" ] @@ -189,11 +205,12 @@ ######################################################################################################### get '/api/v1/packets' do - throw :halt, [ 404, "404 Page Not Found" ] unless @@theDispatcher.authenticate?(params[:key]) + auth, zonedata = @@theDispatcher.authenticate?(params[:key]) + throw :halt, [ 404, "404 Page Not Found" ] unless auth settings.log.debug "The retrieve packets hook has been called" begin @@theDispatcher.retrieve_packets(params).to_json # Returns String in JSON form rescue Exception => e @@ -201,11 +218,12 @@ end end post '/api/v1/packets' do - throw :halt, [ 404, "404 Page Not Found" ] unless @@theDispatcher.authenticate?(params[:key]) + auth, zonedata = @@theDispatcher.authenticate?(params[:key]) + throw :halt, [ 404, "404 Page Not Found" ] unless auth settings.log.debug "The add packets hook has been called" settings.log.debug "Add packet API request: " + params[:payload] begin @@theDispatcher.store(params[:payload]) @@ -219,26 +237,28 @@ ######################################################################################################### get '/api/v1/nodes' do - throw :halt, [ 404, "404 Page Not Found" ] unless @@theDispatcher.authenticate?(params[:key]) + auth, zonedata = @@theDispatcher.authenticate?(params[:key]) + throw :halt, [ 404, "404 Page Not Found" ] unless auth settings.log.debug "The retrieve nodes hook has been called" begin @@theDispatcher.retrieve_nodes(params).to_json # Returns String in JSON form rescue Exception => e - throw :halt, [ 500, "500 Internal Server Error" ] + raise e + #throw :halt, [ 500, "500 Internal Server Error" ] end end ######################################################################################################### post '/api/v1/watch/:node' do |node| - settings.log.debug params.inspect - throw :halt, [ 404, "404 Page Not Found" ] unless @@theDispatcher.authenticate?(params[:key]) + auth, zonedata = @@theDispatcher.authenticate?(params[:key]) + throw :halt, [ 404, "404 Page Not Found" ] unless auth settings.log.debug "The add watch to node hook has been called" begin @@theDispatcher.addWatch(params) rescue Exception => e @@ -246,12 +266,12 @@ end end get '/api/v1/watch/:node' do |node| - settings.log.debug params.inspect - throw :halt, [ 404, "404 Page Not Found" ] unless @@theDispatcher.authenticate?(params[:key]) + auth, zonedata = @@theDispatcher.authenticate?(params[:key]) + throw :halt, [ 404, "404 Page Not Found" ] unless auth settings.log.debug "Check if a node is being watched" begin @@theDispatcher.watched?(params).to_json rescue Exception => e @@ -259,12 +279,12 @@ end end get '/api/v1/watch' do - settings.log.debug params.inspect - throw :halt, [ 404, "404 Page Not Found" ] unless @@theDispatcher.authenticate?(params[:key]) + auth, zonedata = @@theDispatcher.authenticate?(params[:key]) + throw :halt, [ 404, "404 Page Not Found" ] unless auth settings.log.debug "Check if a node is being watched" begin @@theDispatcher.watchList.to_json rescue Exception => e @@ -274,11 +294,13 @@ end ######################################################################################################### get '/api/v1/system/config' do - throw :halt, [ 404, "404 Page Not Found" ] unless @@theDispatcher.authenticate?(params[:key]) + auth, zonedata = @@theDispatcher.authenticate?(params[:key]) + throw :halt, [ 404, "404 Page Not Found" ] unless auth + throw :halt, [ 401, "401 Not Authorised" ] unless zonedata[:role] == "admin" settings.log.debug "The system config hook has been called, querying the Ardtweeno gateway to retrieve config" begin return @@theDispatcher.config.to_json rescue Exception => e @@ -287,49 +309,39 @@ end get '/api/v1/system/start' do - throw :halt, [ 404, "404 Page Not Found" ] unless @@theDispatcher.authenticate?(params[:key]) + auth, zonedata = @@theDispatcher.authenticate?(params[:key]) + throw :halt, [ 404, "404 Page Not Found" ] unless auth + throw :halt, [ 401, "401 Not Authorised" ] unless zonedata[:role] == "admin" settings.log.debug "The system start hook has been called, launching the Ardtweeno system" begin - @@theDispatcher.start + theResponse = @@theDispatcher.start rescue Exception => e throw :halt, [ 500, "500 Internal Server Error" ] end - "The Ardtweeno system is launching, this will take a moment..." + return {:response=>theResponse, :running=>@@theDispatcher.running?}.to_json end get '/api/v1/system/stop' do - throw :halt, [ 404, "404 Page Not Found" ] unless @@theDispatcher.authenticate?(params[:key]) + auth, zonedata = @@theDispatcher.authenticate?(params[:key]) + throw :halt, [ 404, "404 Page Not Found" ] unless auth + throw :halt, [ 401, "401 Not Authorised" ] unless zonedata[:role] == "admin" settings.log.debug "The system stop hook has been called, shutting the Ardtweeno system down..." begin - @@theDispatcher.stop + theResponse = @@theDispatcher.stop rescue Exception => e throw :halt, [ 500, "500 Internal Server Error" ] end - "The Ardtweeno system is shutting down, this will take a moment..." + return {:response=>theResponse, :running=>@@theDispatcher.running?}.to_json end - - # This is currently not implemented correctly - get '/api/v1/system/reboot' do - throw :halt, [ 404, "404 Page Not Found" ] unless @@theDispatcher.authenticate?(params[:key]) - settings.log.debug "The system reboot hook has been called, rebooting the host" - - begin - @@theDispatcher.reboot - rescue Exception => e - throw :halt, [ 500, "500 Internal Server Error" ] - end - - "The host is rebooting, this will take a moment..." - end get '/api/v1/system/status' do settings.log.debug "The system status hook has been called, reading the host configuration"