Class: Sensu::Run::APIClient

Inherits:
Object
  • Object
show all
Defined in:
lib/sensu/run/api_client.rb

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ APIClient

Returns a new instance of APIClient.



13
14
15
16
17
18
19
20
21
22
23
24
# 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

Instance Method Details

#create_entity(entity) ⇒ Object



42
43
44
45
46
47
48
49
# 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) ⇒ Object



51
52
53
54
55
# 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) ⇒ Object



57
58
59
60
61
62
63
64
# 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) ⇒ Boolean

Returns:

  • (Boolean)


34
35
36
37
38
39
40
# 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_backendObject



26
27
28
29
30
31
32
# File 'lib/sensu/run/api_client.rb', line 26

def select_backend
  @backends ||= []
  if @backends.empty?
    @backends = @options[:backends].shuffle
  end
  @backends.shift
end