Class: Kennel::Api
- Inherits:
-
Object
- Object
- Kennel::Api
- Defined in:
- lib/kennel/api.rb
Constant Summary collapse
- CACHE_FILE =
ENV.fetch("KENNEL_API_CACHE_FILE", "tmp/cache/details")
- RateLimitParams =
Data.define(:limit, :period, :remaining, :reset, :name)
Class Method Summary collapse
Instance Method Summary collapse
- #create(api_resource, attributes) ⇒ Object
-
#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).
-
- #fill_details!(api_resource, list) ⇒ Object
-
#initialize(app_key = nil, api_key = nil) ⇒ Api
constructor
A new instance of Api.
- #list(api_resource, params = {}) ⇒ Object
- #show(api_resource, id, params = {}) ⇒ Object
- #update(api_resource, id, attributes) ⇒ Object
Constructor Details
#initialize(app_key = nil, api_key = nil) ⇒ Api
Returns a new instance of Api.
20 21 22 23 24 25 |
# File 'lib/kennel/api.rb', line 20 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("") @client = Faraday.new(url: url) { |c| c.adapter :net_http_persistent } end |
Class Method Details
.with_tracking(api_resource, reply) ⇒ Object
10 11 12 13 14 15 16 17 18 |
# File 'lib/kennel/api.rb', line 10 def self.with_tracking(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
51 52 53 54 55 56 |
# File 'lib/kennel/api.rb', line 51 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.with_tracking(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)
67 68 69 70 71 72 73 74 |
# File 'lib/kennel/api.rb', line 67 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
76 77 78 79 80 |
# File 'lib/kennel/api.rb', line 76 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
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/kennel/api.rb', line 34 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.with_tracking(api_resource, r) } end end |
#show(api_resource, id, params = {}) ⇒ Object
27 28 29 30 31 32 |
# File 'lib/kennel/api.rb', line 27 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.with_tracking(api_resource, response) end |
#update(api_resource, id, attributes) ⇒ Object
58 59 60 61 62 |
# File 'lib/kennel/api.rb', line 58 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.with_tracking(api_resource, response) end |