Class: Kennel::Api

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

Constant Summary collapse

CACHE_FILE =
ENV.fetch("KENNEL_API_CACHE_FILE", "tmp/cache/details")

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Api.



18
19
20
21
22
23
# File 'lib/kennel/api.rb', line 18

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

Class Method Details

.tag(api_resource, reply) ⇒ Object



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

def self.tag(api_resource, reply)
  klass = Models::Record.api_resource_map[api_resource]
  return reply unless klass # do not blow up on unknown models

  reply.merge(
    klass: klass,
    tracking_id: klass.parse_tracking_id(reply)
  )
end

Instance Method Details

#create(api_resource, attributes) ⇒ Object



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

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"
  self.class.tag(api_resource, 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)



65
66
67
68
69
70
71
72
# File 'lib/kennel/api.rb', line 65

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



74
75
76
77
78
# File 'lib/kennel/api.rb', line 74

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



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/kennel/api.rb', line 32

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.map { |r| self.class.tag(api_resource, r) }
  end
end

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



25
26
27
28
29
30
# File 'lib/kennel/api.rb', line 25

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"
  self.class.tag(api_resource, response)
end

#update(api_resource, id, attributes) ⇒ Object



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

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"
  self.class.tag(api_resource, response)
end