Class: DatadogBackup::Resources

Inherits:
Object
  • Object
show all
Includes:
LocalFilesystem, Options
Defined in:
lib/datadog_backup/resources.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

Dashboards, Monitors, SLOs, Synthetics

Constant Summary collapse

RETRY_OPTIONS =
{
  max: 5,
  interval: 0.05,
  interval_randomness: 0.5,
  backoff_factor: 2,
  rate_limit_reset_header: 'x-ratelimit-reset',
  exceptions: [Faraday::TooManyRequestsError] + Faraday::Retry::Middleware::DEFAULT_EXCEPTIONS
}.freeze

Instance Method Summary collapse

Methods included from Options

#action, #backup_dir, #concurrency_limit, #diff_format, #disable_array_sort, #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) ⇒ Resources

Returns a new instance of Resources.



78
79
80
81
82
# File 'lib/datadog_backup/resources.rb', line 78

def initialize(options)
  @options = options
  @banlist = []
  ::FileUtils.mkdir_p(mydir)
end

Instance Method Details

#backupObject



24
25
26
# File 'lib/datadog_backup/resources.rb', line 24

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



121
122
123
124
125
126
127
128
# File 'lib/datadog_backup/resources.rb', line 121

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



89
90
91
92
93
94
95
96
97
# File 'lib/datadog_backup/resources.rb', line 89

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



30
31
32
33
34
35
36
# File 'lib/datadog_backup/resources.rb', line 30

def diff(id)
  current = except(get_by_id(id)).deep_sort(array: disable_array_sort ? false : true).to_yaml
  filesystem = except(load_from_file_by_id(id)).deep_sort(array: disable_array_sort ? false : true).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



39
40
41
42
43
44
45
# File 'lib/datadog_backup/resources.rb', line 39

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



48
49
50
51
52
53
# File 'lib/datadog_backup/resources.rb', line 48

def get(id)
  params = {}
  headers = {}
  response = api_service.get("/api/#{api_version}/#{api_resource_name}/#{id}", params, headers)
  body_with_2xx(response)
end

#get_allObject

Returns a list of all resources in Datadog Do not use directly, but use the child classes’ #all method instead



57
58
59
60
61
62
63
64
# File 'lib/datadog_backup/resources.rb', line 57

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



67
68
69
70
71
# File 'lib/datadog_backup/resources.rb', line 67

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



74
75
76
# File 'lib/datadog_backup/resources.rb', line 74

def get_by_id(id)
  except(get(id))
end

#myclassObject



84
85
86
# File 'lib/datadog_backup/resources.rb', line 84

def myclass
  self.class.to_s.split(':').last.downcase
end

#restore(id) ⇒ Object

If the resource exists in Datadog, update it. Otherwise, create it.



111
112
113
114
115
116
117
118
# File 'lib/datadog_backup/resources.rb', line 111

def restore(id)
  body = load_from_file_by_id(id)
  begin
    update(id, body)
  rescue Faraday::ResourceNotFound => e
    create_newly(id, body)
  end
end

#update(id, body) ⇒ Object

Update an existing resource in Datadog



100
101
102
103
104
105
106
107
108
# File 'lib/datadog_backup/resources.rb', line 100

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