Class: Kennel::Api

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

Constant Summary collapse

CACHE_FILE =
"tmp/cache/details"

Instance Method Summary collapse

Constructor Details

#initialize(app_key = nil, api_key = nil) ⇒ Api

Returns a new instance of Api.



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

def initialize(app_key = nil, api_key = nil)
  @app_key = app_key || ENV.fetch("DATADOG_APP_KEY")
  @api_key = api_key || ENV.fetch("DATADOG_API_KEY")
  url = Utils.path_to_url("", subdomain: "app")
  @client = Faraday.new(url: url) { |c| c.adapter :net_http_persistent }
end

Instance Method Details

#create(api_resource, attributes) ⇒ Object



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

def create(api_resource, attributes)
  response = request :post, "/api/v1/#{api_resource}", body: attributes
  response = response.fetch(:data).first if api_resource == "slo"
  response[:id] = response.delete(:public_id) if api_resource == "synthetics/tests"
  response
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)



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

def delete(api_resource, id)
  if api_resource == "synthetics/tests"
    # https://docs.datadoghq.com/api/latest/synthetics/#delete-tests
    request :post, "/api/v1/#{api_resource}/delete", body: { public_ids: [id] }, ignore_404: true
  else
    request :delete, "/api/v1/#{api_resource}/#{id}", params: { force: "true" }, ignore_404: true
  end
end

#fill_details!(api_resource, list) ⇒ Object



64
65
66
67
68
# File 'lib/kennel/api.rb', line 64

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

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



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

def list(api_resource, params = {})
  with_pagination api_resource == "slo", params do |paginated_params|
    response = request :get, "/api/v1/#{api_resource}", params: paginated_params
    response = response.fetch(:dashboards) if api_resource == "dashboard"
    response = response.fetch(:data) if api_resource == "slo"
    if api_resource == "synthetics/tests"
      response = response.fetch(:tests)
      response.each { |r| r[:id] = r.delete(:public_id) }
    end

    # ignore monitor synthetics create and that inherit the kennel_id, we do not directly manage them
    response.reject! { |m| m[:type] == "synthetics alert" } if api_resource == "monitor"

    response
  end
end

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



15
16
17
18
19
20
# File 'lib/kennel/api.rb', line 15

def show(api_resource, id, params = {})
  response = request :get, "/api/v1/#{api_resource}/#{id}", params: params
  response = response.fetch(:data) if api_resource == "slo"
  response[:id] = response.delete(:public_id) if api_resource == "synthetics/tests"
  response
end

#update(api_resource, id, attributes) ⇒ Object



46
47
48
49
50
# File 'lib/kennel/api.rb', line 46

def update(api_resource, id, attributes)
  response = request :put, "/api/v1/#{api_resource}/#{id}", body: attributes
  response[:id] = response.delete(:public_id) if api_resource == "synthetics/tests"
  response
end