class Sensu::Run::APIClient

Public Class Methods

new(options={}) click to toggle source
# File lib/sensu/run/api_client.rb, line 13
def initialize(options={})
  @options = options
  uri = URI.parse(select_backend)
  @http = Net::HTTP.new(uri.host, uri.port)
  if uri.scheme == "https"
    @http.use_ssl = true
    @http.verify_mode = OpenSSL::SSL::VERIFY_PEER
    if options[:skip_tls_verify_peer]
      @http.verify_mode = OpenSSL::SSL::VERIFY_NONE
    end
  end
end

Public Instance Methods

create_entity(entity) click to toggle source
# File lib/sensu/run/api_client.rb, line 42
def create_entity(entity)
  request = Net::HTTP::Post.new("/api/core/v2/namespaces/#{entity.namespace}/entities")
  request["Content-Type"] = "application/json"
  request["Authorization"] = "Key #{@options[:api_key]}"
  request.body = JSON.dump(entity.to_hash)
  response = @http.request(request)
  puts response.inspect
end
create_entity_if_missing(entity) click to toggle source
# File lib/sensu/run/api_client.rb, line 51
def create_entity_if_missing(entity)
  unless entity_exists?(entity)
    create_entity(entity)
  end
end
create_event(event) click to toggle source
# File lib/sensu/run/api_client.rb, line 57
def create_event(event)
  request = Net::HTTP::Post.new("/api/core/v2/namespaces/#{event.entity.namespace}/events")
  request["Content-Type"] = "application/json"
  request["Authorization"] = "Key #{@options[:api_key]}"
  request.body = JSON.dump(event.to_hash)
  response = @http.request(request)
  puts response.inspect
end
entity_exists?(entity) click to toggle source
# File lib/sensu/run/api_client.rb, line 34
def entity_exists?(entity)
  request = Net::HTTP::Get.new("/api/core/v2/namespaces/#{entity.namespace}/entities/#{entity.name}")
  request["Content-Type"] = "application/json"
  request["Authorization"] = "Key #{@options[:api_key]}"
  response = @http.request(request)
  response.code == "200"
end
select_backend() click to toggle source
# File lib/sensu/run/api_client.rb, line 26
def select_backend
  @backends ||= []
  if @backends.empty?
    @backends = @options[:backends].shuffle
  end
  @backends.shift
end