Class: Kennel::Importer

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

Constant Summary collapse

TITLES =
[:name, :title].freeze
SORT_ORDER =
[*TITLES, :id, :kennel_id, :type, :tags, :query, *Models::Record.subclasses.map { |k| k::TRACKING_FIELDS }, :template_variables].freeze

Instance Method Summary collapse

Constructor Details

#initialize(api) ⇒ Importer

Returns a new instance of Importer.



8
9
10
# File 'lib/kennel/importer.rb', line 8

def initialize(api)
  @api = api
end

Instance Method Details

#import(resource, id) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/kennel/importer.rb', line 12

def import(resource, id)
  if ["screen", "dash"].include?(resource)
    raise ArgumentError, "resource 'screen' and 'dash' are deprecated, use 'dashboard'"
  end

  model =
    Kennel::Models::Record.subclasses.detect { |c| c.api_resource == resource } ||
    raise(ArgumentError, "#{resource} is not supported")

  data = @api.show(model.api_resource, id)
  id = data.fetch(:id) # keep native value
  model.normalize({}, data) # removes id
  data[:id] = id

  title_field = TITLES.detect { |f| data[f] }
  title = data.fetch(title_field)
  title.tr!(Kennel::Models::Record::LOCK, "") # avoid double lock icon

  # calculate or reuse kennel_id
  data[:kennel_id] =
    if tracking_id = model.parse_tracking_id(data)
      model.remove_tracking_id(data)
      tracking_id.split(":").last
    else
      Kennel::StringUtils.parameterize(title)
    end

  case resource
  when "monitor"
    raise "Import the synthetic test page and not the monitor" if data[:type] == "synthetics alert"

    # flatten monitor options so they are all on the base which is how Monitor builds them
    data.merge!(data.delete(:options))
    data.merge!(data.delete(:thresholds) || {})

    # clean up values that are the default
    data.delete(:notify_no_data) if data[:notify_no_data] # Monitor uses true by default
    data.delete(:notify_audit) unless data[:notify_audit] # Monitor uses false by default

    # keep all values that are settable
    data = data.slice(*model.instance_methods)

    # make query use critical method if it matches
    critical = data[:critical]
    query = data[:query]
    if query && critical
      query.sub!(/([><=]) (#{Regexp.escape(critical.to_f.to_s)}|#{Regexp.escape(critical.to_i.to_s)})$/, "\\1 \#{critical}")
    end

    # using float in query is not allowed, so convert here
    data[:critical] = data[:critical].to_i if data[:type] == "event alert"

    data[:type] = "query alert" if data[:type] == "metric alert"

    link_composite_monitors(data)
  when "dashboard"
    widgets = data[:widgets]&.flat_map { |widget| widget.dig(:definition, :widgets) || [widget] }
    widgets&.each do |widget|
      convert_widget_to_compact_format!(widget)
      dry_up_widget_metadata!(widget)
      (widget.dig(:definition, :markers) || []).each { |m| m[:label]&.delete! " " }
    end
  when "synthetics/tests"
    data[:locations] = :all if data[:locations].sort == Kennel::Models::SyntheticTest::LOCATIONS.sort
  else
    # noop
  end

  data.delete(:tags) if data[:tags] == [] # do not create super + [] call

  # simplify template_variables to array of string when possible
  if vars = data[:template_variables]
    vars.map! { |v| v[:default] == "*" && v[:prefix] == v[:name] ? v[:name] : v }
  end

  pretty = pretty_print(data).lstrip.gsub("\\#", "#")
  <<~RUBY
    #{model.name}.new(
      self,
      #{pretty}
    )
  RUBY
end