-
Notifications
You must be signed in to change notification settings - Fork 55
/
provisioning_template_import.rb
94 lines (81 loc) · 3.52 KB
/
provisioning_template_import.rb
1
2
3
4
5
6
7
8
9
10
11
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
module ForemanTemplates
module ProvisioningTemplateImport
extend ActiveSupport::Concern
module ClassMethods
def import!(name, text, metadata, force = false)
# Check for snippet type
return import_snippet!(name, text, force) if metadata['snippet'] || metadata['kind'] == 'snippet'
# Get template type
kind = TemplateKind.find_by(name: metadata['kind'])
raise NoKindError unless kind
# Data
template = ProvisioningTemplate.where(:name => name).first_or_initialize
data = {
:template => text,
:snippet => false,
:template_kind_id => kind.id
}
oses = map_metadata(metadata, 'oses')
locations = map_metadata(metadata, 'locations')
organizations = map_metadata(metadata, 'organizations')
# Printout helpers
c_or_u = template.new_record? ? 'Creating' : 'Updating'
id_string = template.new_record? ? '' : "id #{template.id}"
if template.locked? && !template.new_record? && !force
return { :diff => nil,
:status => false,
:result => "Skipping Template #{id_string}:#{name} - template is locked" }
end
associate_metadata data, template, metadata, oses, organizations, locations
diff = nil
status = nil
if template_changed?(data, template)
diff = create_diff(data, template)
template.ignore_locking do
status = template.update_attributes(data)
end
result = build_associations_result c_or_u, id_string, name, oses, organizations, locations
else
status = true
result = " No change to Template #{id_string}:#{name}"
end
{ :diff => diff, :status => status, :result => result, :errors => template.errors }
end
def associate_metadata(data, template, metadata, oses, organizations, locations)
if (metadata['associate'] == 'new' && template.new_record?) || (metadata['associate'] == 'always')
data[:operatingsystem_ids] = oses.map(&:id)
data[:location_ids] = locations.map(&:id)
data[:organization_ids] = organizations.map(&:id)
end
data
end
def build_associations_result(c_or_u, id_string, name, oses, organizations, locations)
res = " #{c_or_u} Template #{id_string}:#{name}"
res += "\n Operatingsystem Associations:\n - #{oses.map(&:fullname).join("\n - ")}" unless oses.empty?
res += "\n Organizations Associations:\n - #{organizations.map(&:name).join("\n - ")}" unless organizations.empty?
res += "\n Location Associations:\n - #{locations.map(&:name).join("\n - ")}" unless locations.empty?
res
end
def create_diff(data, template)
if template_content_changed?(template.template, data[:template])
Diffy::Diff.new(
template.template,
data[:template],
:include_diff_info => true
).to_s(:color)
else
nil
end
end
def template_content_changed?(template_template, data_template)
template_template != data_template
end
def associations_changed?(data)
!(data[:operatingsystem_ids] || data[:location_ids] || data[:organization_ids]).nil?
end
def template_changed?(data, template)
template_content_changed?(template.template, data[:template]) || associations_changed?(data)
end
end
end
end