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
}.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.



76
77
78
79
80
# File 'lib/datadog_backup/resources.rb', line 76

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

Instance Method Details

#backupObject



22
23
24
# File 'lib/datadog_backup/resources.rb', line 22

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



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

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



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

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



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

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



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

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



46
47
48
49
50
51
# File 'lib/datadog_backup/resources.rb', line 46

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



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

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



65
66
67
68
69
# File 'lib/datadog_backup/resources.rb', line 65

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



72
73
74
# File 'lib/datadog_backup/resources.rb', line 72

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

#myclassObject



82
83
84
# File 'lib/datadog_backup/resources.rb', line 82

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

#restore(id) ⇒ Object

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



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

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



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

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