Class: Kennel::Api

Inherits:
Object
  • Object
show all
Defined in:
lib/kennel/api.rb

Overview

encapsulates knowledge around how the api works

Constant Summary collapse

CACHE_FILE =
"tmp/cache/details"

Instance Method Summary collapse

Constructor Details

#initialize(app_key, api_key) ⇒ Api

Returns a new instance of Api.



7
8
9
10
11
# File 'lib/kennel/api.rb', line 7

def initialize(app_key, api_key)
  @app_key = app_key
  @api_key = api_key
  @client = Faraday.new(url: "https://app.datadoghq.com") { |c| c.adapter :net_http_persistent }
end

Instance Method Details

#create(api_resource, attributes) ⇒ Object



39
40
41
42
# File 'lib/kennel/api.rb', line 39

def create(api_resource, attributes)
  reply = request :post, "/api/v1/#{api_resource}", body: attributes
  api_resource == "slo" ? reply[:data].first : reply
end

#delete(api_resource, id) ⇒ Object

  • force=true to not dead-lock on dependent monitors+slos external dependency on kennel managed resources is their problem, we don’t block on it (?force=true did not work, force for dashboard is not documented but does not blow up)



51
52
53
# File 'lib/kennel/api.rb', line 51

def delete(api_resource, id)
  request :delete, "/api/v1/#{api_resource}/#{id}", params: { force: "true" }, ignore_404: true
end

#fill_details!(api_resource, list) ⇒ Object



55
56
57
58
59
60
# File 'lib/kennel/api.rb', line 55

def fill_details!(api_resource, list)
  return unless api_resource == "dashboard"
  details_cache do |cache|
    Utils.parallel(list) { |a| fill_detail!(api_resource, a, cache) }
  end
end

#list(api_resource, params = {}) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/kennel/api.rb', line 18

def list(api_resource, params = {})
  if api_resource == "slo"
    raise ArgumentError if params[:limit] || params[:offset]
    limit = 1000
    offset = 0
    all = []

    loop do
      result = request :get, "/api/v1/#{api_resource}", params: params.merge(limit: limit, offset: offset)
      data = result.fetch(:data)
      all.concat data
      break all if data.size < limit
      offset += limit
    end
  else
    result = request :get, "/api/v1/#{api_resource}", params: params
    result = result.fetch(:dashboards) if api_resource == "dashboard"
    result
  end
end

#show(api_resource, id, params = {}) ⇒ Object



13
14
15
16
# File 'lib/kennel/api.rb', line 13

def show(api_resource, id, params = {})
  reply = request :get, "/api/v1/#{api_resource}/#{id}", params: params
  api_resource == "slo" ? reply[:data] : reply
end

#update(api_resource, id, attributes) ⇒ Object



44
45
46
# File 'lib/kennel/api.rb', line 44

def update(api_resource, id, attributes)
  request :put, "/api/v1/#{api_resource}/#{id}", body: attributes
end