1
|
module HammerCLICsv
|
2
|
class CsvCommand
|
3
|
class InstallationMediaCommand < BaseCommand
|
4
|
command_name 'installation-media'
|
5
|
desc 'import or export media'
|
6
|
|
7
|
ORGANIZATIONS = 'Organizations'
|
8
|
LOCATIONS = 'Locations'
|
9
|
PATH = 'Path'
|
10
|
OSFAMILY = 'OS Family'
|
11
|
OPERATING_SYSTEMS = 'Operating Systems'
|
12
|
|
13
|
def export(csv)
|
14
|
csv << [NAME, ORGANIZATIONS, LOCATIONS, PATH, OSFAMILY, OPERATING_SYSTEMS]
|
15
|
@api.resource(:media).call(:index, {:per_page => 999999})['results'].each do |medium|
|
16
|
medium = @api.resource(:media).call(:show, :id => medium['id'])
|
17
|
name = medium['name']
|
18
|
organizations = export_column(medium, 'organizations', 'name')
|
19
|
locations = export_column(medium, 'locations', 'name')
|
20
|
count = 1
|
21
|
path = medium['path']
|
22
|
os_family = medium['os_family']
|
23
|
operating_systems = export_column(medium, 'operatingsystems', 'title')
|
24
|
csv << [name, organizations, locations, path, os_family, operating_systems]
|
25
|
end
|
26
|
end
|
27
|
|
28
|
def import
|
29
|
@existing = {}
|
30
|
@api.resource(:media).call(:index, {:per_page => 999999})['results'].each do |medium|
|
31
|
@existing[medium['name']] = medium['id'] if medium
|
32
|
end
|
33
|
|
34
|
thread_import do |line|
|
35
|
create_from_csv(line)
|
36
|
end
|
37
|
end
|
38
|
|
39
|
def create_from_csv(line)
|
40
|
params = {
|
41
|
'medium' => {
|
42
|
'organization_ids' => collect_column(line[ORGANIZATIONS]) do |organization|
|
43
|
foreman_organization(:name => organization)
|
44
|
end,
|
45
|
'location_ids' => collect_column(line[LOCATIONS]) do |location|
|
46
|
foreman_location(:name => location)
|
47
|
end,
|
48
|
'path' => line[PATH],
|
49
|
'os_family' => line[OSFAMILY],
|
50
|
'operatingsystem_ids' => collect_column(line[OPERATING_SYSTEMS]) do |os|
|
51
|
foreman_operatingsystem(:name => os)
|
52
|
end
|
53
|
}
|
54
|
}
|
55
|
|
56
|
count(line[COUNT]).times do |number|
|
57
|
name = namify(line[NAME], number)
|
58
|
params['medium']['name'] = name
|
59
|
|
60
|
if !@existing.include? name
|
61
|
print _("Creating installation medium '%{name}'... ") % {:name => name} if option_verbose?
|
62
|
medium = @api.resource(:media).call(:create, params)
|
63
|
@existing[name] = medium['id']
|
64
|
else
|
65
|
print _("Updating installation medium '%{name}'... ") % {:name => name} if option_verbose?
|
66
|
params['id'] = @existing[name]
|
67
|
medium = @api.resource(:media).call(:update, params)
|
68
|
end
|
69
|
puts _('done') if option_verbose?
|
70
|
end
|
71
|
end
|
72
|
end
|
73
|
end
|
74
|
end
|