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
|
# 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 =
begin
Kennel::Models.const_get(resource.capitalize)
rescue NameError
raise ArgumentError, "#{resource} is not supported"
end
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, "")
tracking_field = Syncer::TRACKING_FIELDS.detect { |f| data[f] }
data[:kennel_id] =
if tracking_field && data[tracking_field].sub!(/\n?-- Managed by kennel (\S+:\S+).*/, "")
$1.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 { |widget| dry_up_query!(widget) }
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"
|