Class: SensuCli::Api
- Inherits:
-
Object
- Object
- SensuCli::Api
- Defined in:
- lib/sensu-cli/api.rb
Instance Method Summary collapse
Instance Method Details
#request(opts) ⇒ Object
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/sensu-cli/api.rb', line 8 def request(opts) http = Net::HTTP.new(opts[:host], opts[:port]) http.read_timeout = 15 http.open_timeout = 5 if opts[:ssl] http.use_ssl = true http.verify_mode = OpenSSL::SSL::VERIFY_NONE end case opts[:method] when 'Get' req = Net::HTTP::Get.new(opts[:path]) when 'Delete' req = Net::HTTP::Delete.new(opts[:path]) when 'Post' req = Net::HTTP::Post.new(opts[:path],initheader = {'Content-Type' => 'application/json'}) req.body = opts[:payload] end req.basic_auth(opts[:user], opts[:password]) if opts[:user] && opts[:password] begin http.request(req) rescue Timeout::Error puts "HTTP request has timed out.".color(:red) exit rescue StandardError => e puts "An HTTP error occurred. Check your settings. #{e}".color(:red) exit end end |
#response(code, body, command = nil) ⇒ Object
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/sensu-cli/api.rb', line 37 def response(code,body,command=nil) case code when '200' JSON.parse(body) when '201' puts "The stash has been created." if command == "stashes" || command == "silence" when '202' puts "The item was submitted for processing." when '204' puts "Sensu is healthy" if command == 'health' puts "The item was successfully deleted." if command == 'aggregates' || command == 'stashes' when '400' puts "The payload is malformed.".color(:red) when '401' puts "The request requires user authentication.".color(:red) when '404' puts "The item did not exist.".color(:cyan) else (command == 'health') ? (puts "Sensu is not healthy.".color(:red)) : (puts "There was an error while trying to complete your request. Response code: #{code}".color(:red)) end end |