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
7 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 |
# File 'lib/sensu-cli/api.rb', line 7 def request(opts) http = Net::HTTP.new(opts[:host], opts[:port], opts[:proxy_address], opts[:proxy_port]) http.read_timeout = opts[:read_timeout] http.open_timeout = opts[:open_timeout] if opts[:ssl] http.use_ssl = true http.verify_mode = OpenSSL::SSL::VERIFY_NONE end proxy_header = { 'api-proxy' => 'true' } case opts[:method] when 'Get' req = Net::HTTP::Get.new(opts[:path], proxy_header) when 'Delete' req = Net::HTTP::Delete.new(opts[:path], proxy_header) when 'Post' req = Net::HTTP::Post.new(opts[:path], proxy_header.merge!('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 SensuCli::die(1, 'HTTP request has timed out.'.color(:red)) rescue StandardError => e SensuCli::die(1, "An HTTP error occurred. Check your settings. #{e}".color(:red)) end end |
#response(code, body, command = nil) ⇒ Object
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/sensu-cli/api.rb', line 36 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 if command == 'health' puts 'Sensu is not healthy.'.color(:red) else puts "There was an error while trying to complete your request. Response code: #{code}".color(:red) end end end |