1
|
module HammerCLICsv
|
2
|
class CsvCommand
|
3
|
class SyncPlansCommand < BaseCommand
|
4
|
command_name 'sync-plans'
|
5
|
desc 'import or export repository sync plans'
|
6
|
|
7
|
ORGANIZATION = 'Organization'
|
8
|
DESCRIPTION = 'Description'
|
9
|
ENABLED = 'Enabled'
|
10
|
STARTDATE = 'Start Date'
|
11
|
INTERVAL = 'Interval'
|
12
|
PRODUCTS = 'Products'
|
13
|
|
14
|
def export(csv)
|
15
|
csv << [NAME, ORGANIZATION, DESCRIPTION, ENABLED, STARTDATE, INTERVAL, PRODUCTS]
|
16
|
|
17
|
@api.resource(:organizations).call(:index, {:per_page => 999999})['results'].each do |organization|
|
18
|
next if option_organization && organization['name'] != option_organization
|
19
|
|
20
|
@api.resource(:sync_plans).call(:index, {
|
21
|
'per_page' => 999999,
|
22
|
'organization_id' => foreman_organization(:name => organization['name'])
|
23
|
})['results'].each do |sync_plan|
|
24
|
name = sync_plan['name']
|
25
|
organization_name = organization['name']
|
26
|
description = sync_plan['description']
|
27
|
enabled = sync_plan['enabled'] ? 'Yes' : 'No'
|
28
|
start_date = sync_plan['sync_date']
|
29
|
interval = sync_plan['interval']
|
30
|
products = CSV.generate do |column|
|
31
|
column << sync_plan['products'].collect do |product|
|
32
|
product['name']
|
33
|
end
|
34
|
end
|
35
|
products.delete!("\n")
|
36
|
csv << [name, organization_name, description, enabled, start_date, interval,
|
37
|
products]
|
38
|
end
|
39
|
end
|
40
|
end
|
41
|
|
42
|
def import
|
43
|
@existing = {}
|
44
|
|
45
|
thread_import do |line|
|
46
|
create_content_hosts_from_csv(line)
|
47
|
end
|
48
|
end
|
49
|
|
50
|
def create_content_hosts_from_csv(line)
|
51
|
return if option_organization && line[ORGANIZATION] != option_organization
|
52
|
|
53
|
if !@existing[line[ORGANIZATION]]
|
54
|
@existing[line[ORGANIZATION]] = {}
|
55
|
@api.resource(:sync_plans).call(:index, {
|
56
|
'per_page' => 999999,
|
57
|
'organization_id' => foreman_organization(:name => line[ORGANIZATION])
|
58
|
})['results'].each do |sync_plan|
|
59
|
@existing[line[ORGANIZATION]][sync_plan['name']] = sync_plan['id']
|
60
|
end
|
61
|
end
|
62
|
|
63
|
count(line[COUNT]).times do |number|
|
64
|
name = namify(line[NAME], number)
|
65
|
if !@existing[line[ORGANIZATION]].include? name
|
66
|
print "Creating sync plan '#{name}'..." if option_verbose?
|
67
|
@api.resource(:sync_plans).call(:create, {
|
68
|
'organization_id' => foreman_organization(:name => line[ORGANIZATION]),
|
69
|
'name' => name,
|
70
|
'description' => line[DESCRIPTION],
|
71
|
'enabled' => line[ENABLED] == 'Yes' ? true : false,
|
72
|
'sync_date' => line[STARTDATE],
|
73
|
'interval' => line[INTERVAL],
|
74
|
'products' => products(line)
|
75
|
})
|
76
|
else
|
77
|
print "Updating sync plan '#{name}'..." if option_verbose?
|
78
|
|
79
|
|
80
|
|
81
|
|
82
|
|
83
|
|
84
|
|
85
|
|
86
|
end
|
87
|
puts "done" if option_verbose?
|
88
|
end
|
89
|
|
90
|
end
|
91
|
|
92
|
private
|
93
|
|
94
|
def products(line)
|
95
|
return nil if !line[PRODUCTS] || line[PRODUCTS].empty?
|
96
|
CSV.parse_line(line[PRODUCTS]).collect do |name|
|
97
|
katello_product(line[ORGANIZATION], :name => name)
|
98
|
end
|
99
|
end
|
100
|
|
101
|
end
|
102
|
end
|
103
|
end
|