1
|
|
2
|
|
3
|
|
4
|
|
5
|
|
6
|
|
7
|
|
8
|
|
9
|
|
10
|
|
11
|
|
12
|
module HammerCLICsv
|
13
|
class CsvCommand
|
14
|
class ProvisioningTemplatesCommand < BaseCommand
|
15
|
command_name 'provisioning-templates'
|
16
|
desc 'import or export provisioning templates'
|
17
|
|
18
|
ORGANIZATIONS = 'Organizations'
|
19
|
LOCATIONS = 'Locations'
|
20
|
KIND = 'Kind'
|
21
|
TEMPLATE = 'Template'
|
22
|
|
23
|
def export
|
24
|
CSV.open(option_csv_file || '/dev/stdout', 'wb', {:force_quotes => true}) do |csv|
|
25
|
csv << [NAME, COUNT, ORGANIZATIONS, LOCATIONS, KIND, TEMPLATE]
|
26
|
@api.resource(:config_templates)\
|
27
|
.call(:index, {
|
28
|
:per_page => 999999
|
29
|
})['results'].each do |template_id|
|
30
|
template = @api.resource(:config_templates).call(:show, {:id => template_id['id']})
|
31
|
name = template['name']
|
32
|
count = 1
|
33
|
kind = template['snippet'] ? 'snippet' : template['template_kind_name']
|
34
|
organizations = export_column(template, 'organizations', 'name')
|
35
|
locations = export_column(template, 'locations', 'name')
|
36
|
unless name == 'Boot disk iPXE - generic host' || name == 'Boot disk iPXE - host'
|
37
|
csv << [name, count, organizations, locations, kind, template['template']]
|
38
|
end
|
39
|
end
|
40
|
end
|
41
|
end
|
42
|
|
43
|
def import
|
44
|
@existing = {}
|
45
|
@api.resource(:config_templates)\
|
46
|
.call(:index, {
|
47
|
:per_page => 999999
|
48
|
})['results'].each do |template|
|
49
|
@existing[template['name']] = template['id'] if template
|
50
|
end
|
51
|
|
52
|
thread_import do |line|
|
53
|
create_templates_from_csv(line)
|
54
|
end
|
55
|
end
|
56
|
|
57
|
def create_templates_from_csv(line)
|
58
|
organizations = collect_column(line[ORGANIZATIONS]) do |organization|
|
59
|
foreman_organization(:name => organization)
|
60
|
end
|
61
|
locations = collect_column(line[LOCATIONS]) do |location|
|
62
|
foreman_location(:name => location)
|
63
|
end
|
64
|
|
65
|
line[COUNT].to_i.times do |number|
|
66
|
name = namify(line[NAME], number)
|
67
|
if !@existing.include? name
|
68
|
print "Creating provisioning template '#{name}'..." if option_verbose?
|
69
|
template_id = @api.resource(:config_templates)\
|
70
|
.call(:create, {
|
71
|
'name' => name,
|
72
|
'snippet' => line[KIND] == 'snippet',
|
73
|
'template_kind_id' => line[KIND] == 'snippet' ? nil : foreman_template_kind(:name => line[KIND]),
|
74
|
'organization_ids' => organizations,
|
75
|
'location_ids' => locations,
|
76
|
'template' => line[TEMPLATE]
|
77
|
})['id']
|
78
|
else
|
79
|
print "Updating provisioning template '#{name}'..." if option_verbose?
|
80
|
template_id = @api.resource(:config_templates)\
|
81
|
.call(:update, {
|
82
|
'id' => @existing[name],
|
83
|
'name' => name,
|
84
|
'snippet' => line[KIND] == 'snippet',
|
85
|
'template_kind_id' => line[KIND] == 'snippet' ? nil : foreman_template_kind(:name => line[KIND]),
|
86
|
'organization_ids' => organizations,
|
87
|
'location_ids' => locations,
|
88
|
'template' => line[TEMPLATE]
|
89
|
})['id']
|
90
|
end
|
91
|
@existing[name] = template_id
|
92
|
|
93
|
|
94
|
template_organizations ||= {}
|
95
|
organizations.each do |organization_id|
|
96
|
if template_organizations[organization_id].nil?
|
97
|
template_organizations[organization_id] = @api.resource(:organizations)\
|
98
|
.call(:show, {
|
99
|
'id' => organization_id
|
100
|
})['config_templates'].collect do |template|
|
101
|
template['id']
|
102
|
end
|
103
|
end
|
104
|
if !template_organizations[organization_id].include? template_id
|
105
|
template_organizations[organization_id] += [template_id]
|
106
|
@api.resource(:organizations)\
|
107
|
.call(:update, {
|
108
|
'id' => organization_id,
|
109
|
'organization' => {
|
110
|
'config_template_ids' => template_organizations[organization_id]
|
111
|
}
|
112
|
})
|
113
|
end
|
114
|
end
|
115
|
template_locations ||= {}
|
116
|
locations.each do |location_id|
|
117
|
if template_locations[location_id].nil?
|
118
|
template_locations[location_id] = @api.resource(:locations)\
|
119
|
.call(:show, {
|
120
|
'id' => location_id
|
121
|
})['config_templates'].collect do |template|
|
122
|
template['id']
|
123
|
end
|
124
|
end
|
125
|
if !template_locations[location_id].include? template_id
|
126
|
template_locations[location_id] += [template_id]
|
127
|
@api.resource(:locations)\
|
128
|
.call(:update, {
|
129
|
'id' => location_id,
|
130
|
'location' => {
|
131
|
'config_template_ids' => template_locations[location_id]
|
132
|
}
|
133
|
})
|
134
|
end
|
135
|
end
|
136
|
|
137
|
print "done\n" if option_verbose?
|
138
|
end
|
139
|
rescue RuntimeError => e
|
140
|
raise "#{e}\n #{line[NAME]}"
|
141
|
end
|
142
|
end
|
143
|
end
|
144
|
end
|