Class: Kennel::Importer

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

Constant Summary collapse

SORT_ORDER =

bring important fields to the top

[
  *Kennel::Models::Record::TITLE_FIELDS, :id, :kennel_id, :type, :tags, :query, :sli_specification,
  *Models::Record.subclasses.flat_map { |k| k::TRACKING_FIELDS },
  :template_variables
].freeze

Instance Method Summary collapse

Constructor Details

#initialize(api) ⇒ Importer

Returns a new instance of Importer.



12
13
14
# File 'lib/kennel/importer.rb', line 12

def initialize(api)
  @api = api
end

Instance Method Details

#import(resource, id) ⇒ Object



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
95
96
97
98
# File 'lib/kennel/importer.rb', line 16

def import(resource, id)
  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 will have the lock symbol we need to remove when re-importing
  title_field = Kennel::Models::Record::TITLE_FIELDS.detect { |f| data[f] }
  title = data.fetch(title_field)
  title.tr!(Kennel::Models::Record::LOCK, "")

  # 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
    if !!data[:notify_no_data] == !Models::Monitor::SKIP_NOTIFY_NO_DATA_TYPES.include?(data[:type])
      data.delete(:notify_no_data)
    end
    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