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
|
# 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)
model.normalize({}, data)
data[:id] = id
title_field = TITLES.detect { |f| data[f] }
title = data.fetch(title_field)
title.tr!(Kennel::Models::Record::LOCK, "")
data[:kennel_id] =
if tracking_id = model.parse_tracking_id(data)
model.remove_tracking_id(data)
tracking_id.split(":").last
else
Kennel::Utils.parameterize(title)
end
case resource
when "monitor"
data.merge!(data.delete(:options))
data.merge!(data.delete(:thresholds) || {})
data.delete(:notify_no_data) if data[:notify_no_data]
data.delete(:notify_audit) unless data[:notify_audit]
data = data.slice(*model.instance_methods)
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
data[:critical] = data[:critical].to_i if data[:type] == "event alert"
data[:type] = "query alert" if data[:type] == "metric alert"
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
end
data.delete(:tags) if data[:tags] == []
if vars = data[:template_variables]
vars.map! { |v| v[:default] == "*" && v[:prefix] == v[:name] ? v[:name] : v }
end
pretty = pretty_print(data).lstrip.gsub("\\#", "#")
" \#{model.name}.new(\n self,\n \#{pretty}\n )\n RUBY\nend\n"
|