Class: DatadogBackup::Core
- Inherits:
-
Object
- Object
- DatadogBackup::Core
- Includes:
- LocalFilesystem, Options
- Defined in:
- lib/datadog_backup/core.rb
Overview
The default options for backing up and restores. This base class is meant to be extended by specific resources, such as Dashboards, Monitors, and so on.
Direct Known Subclasses
Instance Method Summary collapse
- #api_resource_name ⇒ Object
- #api_service ⇒ Object
- #api_url ⇒ Object
- #api_version ⇒ Object
- #backup ⇒ Object
-
#body_with_2xx(response) ⇒ Object
Return the Faraday body from a response with a 2xx status code, otherwise raise an error.
-
#create(body) ⇒ Object
Create a new resource in Datadog.
-
#diff(id) ⇒ Object
Returns the diffy diff.
-
#except(hash) ⇒ Object
Returns a hash with banlist elements removed.
-
#get(id) ⇒ Object
Fetch the specified resource from Datadog.
-
#get_all ⇒ Object
Returns a list of all resources in Datadog Do not use directly, but use the child classes’ #all method instead.
-
#get_and_write_file(id) ⇒ Object
Download the resource from Datadog and write it to a file.
-
#get_by_id(id) ⇒ Object
Fetch the specified resource from Datadog and remove the banlist elements.
-
#id_keyname ⇒ Object
Some resources have a different key for the id.
-
#initialize(options) ⇒ Core
constructor
A new instance of Core.
- #myclass ⇒ Object
-
#restore(id) ⇒ Object
If the resource exists in Datadog, update it.
-
#update(id, body) ⇒ Object
Update an existing resource in Datadog.
Methods included from Options
#action, #backup_dir, #concurrency_limit, #diff_format, #force_restore, #output_format, #resources
Methods included from LocalFilesystem
#all_file_ids, #all_file_ids_for_selected_resources, #all_files, #class_from_id, #dump, #file_type, #filename, #find_file_by_id, #load_from_file, #load_from_file_by_id, #mydir, #purge, #write_file
Constructor Details
#initialize(options) ⇒ Core
Returns a new instance of Core.
113 114 115 116 117 |
# File 'lib/datadog_backup/core.rb', line 113 def initialize() = @banlist = [] ::FileUtils.mkdir_p(mydir) end |
Instance Method Details
#api_resource_name ⇒ Object
50 51 52 |
# File 'lib/datadog_backup/core.rb', line 50 def api_resource_name raise 'subclass is expected to implement #api_resource_name' end |
#api_service ⇒ Object
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/datadog_backup/core.rb', line 22 def api_service @api_service ||= Faraday.new( url: api_url, headers: { 'DD-API-KEY' => ENV.fetch('DD_API_KEY'), 'DD-APPLICATION-KEY' => ENV.fetch('DD_APP_KEY') } ) do |faraday| faraday.request :json faraday.request :retry, faraday.response(:logger, LOGGER, { headers: true, bodies: LOGGER.debug?, log_level: :debug }) do |logger| logger.filter(/(DD-API-KEY:)([^&]+)/, '\1[REDACTED]') logger.filter(/(DD-APPLICATION-KEY:)([^&]+)/, '\1[REDACTED]') end faraday.response :raise_error faraday.response :json faraday.adapter Faraday.default_adapter end end |
#api_url ⇒ Object
42 43 44 |
# File 'lib/datadog_backup/core.rb', line 42 def api_url ENV.fetch('DD_SITE_URL', 'https://api.datadoghq.com/') end |
#api_version ⇒ Object
46 47 48 |
# File 'lib/datadog_backup/core.rb', line 46 def api_version raise 'subclass is expected to implement #api_version' end |
#backup ⇒ Object
59 60 61 |
# File 'lib/datadog_backup/core.rb', line 59 def backup raise 'subclass is expected to implement #backup' end |
#body_with_2xx(response) ⇒ Object
Return the Faraday body from a response with a 2xx status code, otherwise raise an error
158 159 160 161 162 163 164 165 |
# File 'lib/datadog_backup/core.rb', line 158 def body_with_2xx(response) unless response.status.to_s =~ /^2/ raise "#{caller_locations(1, 1)[0].label} failed with error #{response.status}" end response.body end |
#create(body) ⇒ Object
Create a new resource in Datadog
124 125 126 127 128 129 130 131 132 |
# File 'lib/datadog_backup/core.rb', line 124 def create(body) headers = {} response = api_service.post("/api/#{api_version}/#{api_resource_name}", body, headers) body = body_with_2xx(response) LOGGER.warn "Successfully created #{body.fetch(id_keyname)} in datadog." LOGGER.info 'Invalidating cache' @get_all = nil body end |
#diff(id) ⇒ Object
Returns the diffy diff. Optionally, supply an array of keys to remove from comparison
65 66 67 68 69 70 71 |
# File 'lib/datadog_backup/core.rb', line 65 def diff(id) current = except(get_by_id(id)).deep_sort.to_yaml filesystem = except(load_from_file_by_id(id)).deep_sort.to_yaml result = ::Diffy::Diff.new(current, filesystem, include_plus_and_minus_in_html: true).to_s(diff_format) LOGGER.debug("Compared ID #{id} and found filesystem: #{filesystem} <=> current: #{current} == result: #{result}") result.chomp end |
#except(hash) ⇒ Object
Returns a hash with banlist elements removed
74 75 76 77 78 79 80 |
# File 'lib/datadog_backup/core.rb', line 74 def except(hash) hash.tap do # tap returns self @banlist.each do |key| hash.delete(key) # delete returns the value at the deleted key, hence the tap wrapper end end end |
#get(id) ⇒ Object
Fetch the specified resource from Datadog
83 84 85 86 87 88 |
# File 'lib/datadog_backup/core.rb', line 83 def get(id) params = {} headers = {} response = api_service.get("/api/#{api_version}/#{api_resource_name}/#{id}", params, headers) body_with_2xx(response) end |
#get_all ⇒ Object
Returns a list of all resources in Datadog Do not use directly, but use the child classes’ #all method instead
92 93 94 95 96 97 98 99 |
# File 'lib/datadog_backup/core.rb', line 92 def get_all return @get_all if @get_all params = {} headers = {} response = api_service.get("/api/#{api_version}/#{api_resource_name}", params, headers) @get_all = body_with_2xx(response) end |
#get_and_write_file(id) ⇒ Object
Download the resource from Datadog and write it to a file
102 103 104 105 106 |
# File 'lib/datadog_backup/core.rb', line 102 def get_and_write_file(id) body = get_by_id(id) write_file(dump(body), filename(id)) body end |
#get_by_id(id) ⇒ Object
Fetch the specified resource from Datadog and remove the banlist elements
109 110 111 |
# File 'lib/datadog_backup/core.rb', line 109 def get_by_id(id) except(get(id)) end |
#id_keyname ⇒ Object
Some resources have a different key for the id.
55 56 57 |
# File 'lib/datadog_backup/core.rb', line 55 def id_keyname 'id' end |
#myclass ⇒ Object
119 120 121 |
# File 'lib/datadog_backup/core.rb', line 119 def myclass self.class.to_s.split(':').last.downcase end |
#restore(id) ⇒ Object
If the resource exists in Datadog, update it. Otherwise, create it.
146 147 148 149 150 151 152 153 154 155 |
# File 'lib/datadog_backup/core.rb', line 146 def restore(id) body = load_from_file_by_id(id) begin update(id, body) rescue RuntimeError => e raise e. unless e..include?('update failed with error 404') create_newly(id, body) end end |
#update(id, body) ⇒ Object
Update an existing resource in Datadog
135 136 137 138 139 140 141 142 143 |
# File 'lib/datadog_backup/core.rb', line 135 def update(id, body) headers = {} response = api_service.put("/api/#{api_version}/#{api_resource_name}/#{id}", body, headers) body = body_with_2xx(response) LOGGER.warn "Successfully restored #{id} to datadog." LOGGER.info 'Invalidating cache' @get_all = nil body end |