1
|
require 'hammer_cli_foreman'
|
2
|
require 'hammer_cli_foreman_tasks'
|
3
|
|
4
|
module HammerCLICsv
|
5
|
class CsvCommand
|
6
|
class ContentViewsCommand < BaseCommand
|
7
|
include ::HammerCLIForemanTasks::Helper
|
8
|
|
9
|
command_name 'content-views'
|
10
|
desc 'import or export content-views'
|
11
|
|
12
|
LABEL = 'Label'
|
13
|
ORGANIZATION = 'Organization'
|
14
|
DESCRIPTION = 'Description'
|
15
|
COMPOSITE = 'Composite'
|
16
|
REPOSITORIES = 'Repositories or Composites'
|
17
|
ENVIRONMENTS = "Lifecycle Environments"
|
18
|
|
19
|
def export(csv)
|
20
|
csv << [NAME, LABEL, ORGANIZATION, COMPOSITE, REPOSITORIES, ENVIRONMENTS]
|
21
|
@api.resource(:organizations).call(:index, {
|
22
|
:per_page => 999999
|
23
|
})['results'].each do |organization|
|
24
|
next if option_organization && organization['name'] != option_organization
|
25
|
|
26
|
composite_contentviews = []
|
27
|
@api.resource(:content_views).call(:index, {
|
28
|
'per_page' => 999999,
|
29
|
'organization_id' => organization['id'],
|
30
|
'nondefault' => true
|
31
|
})['results'].each do |contentview|
|
32
|
name = contentview['name']
|
33
|
label = contentview['label']
|
34
|
orgname = organization['name']
|
35
|
environments = CSV.generate do |column|
|
36
|
column << environment_names(contentview)
|
37
|
end
|
38
|
environments.delete!("\n")
|
39
|
composite = contentview['composite'] == true ? 'Yes' : 'No'
|
40
|
if composite == 'Yes'
|
41
|
contentviews = CSV.generate do |column|
|
42
|
column << contentview['components'].collect do |component|
|
43
|
component['content_view']['name']
|
44
|
end
|
45
|
end
|
46
|
contentviews.delete!("\n")
|
47
|
composite_contentviews << [name, 1, label, orgname, composite, contentviews, environments]
|
48
|
else
|
49
|
repositories = export_column(contentview, 'repositories', 'name')
|
50
|
csv << [name, label, orgname, composite, repositories, environments]
|
51
|
end
|
52
|
end
|
53
|
composite_contentviews.each do |contentview|
|
54
|
csv << contentview
|
55
|
end
|
56
|
end
|
57
|
end
|
58
|
|
59
|
def import
|
60
|
@existing_contentviews = {}
|
61
|
|
62
|
thread_import do |line|
|
63
|
create_contentviews_from_csv(line)
|
64
|
end
|
65
|
end
|
66
|
|
67
|
def create_contentviews_from_csv(line)
|
68
|
return if option_organization && line[ORGANIZATION] != option_organization
|
69
|
|
70
|
if !@existing_contentviews[line[ORGANIZATION]]
|
71
|
@existing_contentviews[line[ORGANIZATION]] ||= {}
|
72
|
@api.resource(:content_views).call(:index, {
|
73
|
'per_page' => 999999,
|
74
|
'organization_id' => foreman_organization(:name => line[ORGANIZATION]),
|
75
|
'nondefault' => true
|
76
|
})['results'].each do |contentview|
|
77
|
@existing_contentviews[line[ORGANIZATION]][contentview['name']] = contentview['id'] if contentview
|
78
|
end
|
79
|
end
|
80
|
|
81
|
is_composite = line[COMPOSITE] == 'Yes' ? true : false
|
82
|
|
83
|
if is_composite
|
84
|
composite_ids = collect_column(line[REPOSITORIES]) do |composite|
|
85
|
|
86
|
katello_contentviewversion(line[ORGANIZATION], composite)
|
87
|
end
|
88
|
else
|
89
|
repository_ids = collect_column(line[REPOSITORIES]) do |repository|
|
90
|
katello_repository(line[ORGANIZATION], :name => repository)
|
91
|
end
|
92
|
end
|
93
|
|
94
|
count(line[COUNT]).times do |number|
|
95
|
name = namify(line[NAME], number)
|
96
|
|
97
|
contentview_id = @existing_contentviews[line[ORGANIZATION]][name]
|
98
|
if !contentview_id
|
99
|
print _("Creating content view '%{name}'...") % {:name => name} if option_verbose?
|
100
|
options = {
|
101
|
'organization_id' => foreman_organization(:name => line[ORGANIZATION]),
|
102
|
'name' => name,
|
103
|
'label' => labelize(name),
|
104
|
'description' => line[DESCRIPTION],
|
105
|
'composite' => is_composite
|
106
|
}
|
107
|
contentview_id = @api.resource(:content_views).call(:create, options)['id']
|
108
|
@existing_contentviews[line[ORGANIZATION]][name] = contentview_id
|
109
|
else
|
110
|
print _("Updating content view '%{name}'...") % {:name => name} if option_verbose?
|
111
|
end
|
112
|
|
113
|
options = {
|
114
|
'id' => contentview_id,
|
115
|
'description' => line[DESCRIPTION]
|
116
|
}
|
117
|
if is_composite
|
118
|
options['component_ids'] = composite_ids
|
119
|
else
|
120
|
options['repository_ids'] = repository_ids
|
121
|
end
|
122
|
contentview = @api.resource(:content_views).call(:update, options)
|
123
|
contentview_id = contentview['id']
|
124
|
|
125
|
|
126
|
publish_content_view(contentview_id, line) if contentview['versions'].empty?
|
127
|
promote_content_view(contentview_id, line)
|
128
|
|
129
|
puts _('done') if option_verbose?
|
130
|
end
|
131
|
|
132
|
end
|
133
|
|
134
|
def environment_names(contentview)
|
135
|
names = []
|
136
|
contentview['versions'].each do |version|
|
137
|
version['environment_ids'].each do |environment_id|
|
138
|
names << lifecycle_environment(contentview['organization']['name'], :id => environment_id)
|
139
|
end
|
140
|
end
|
141
|
names.uniq
|
142
|
end
|
143
|
|
144
|
def publish_content_view(contentview_id, line)
|
145
|
task_progress(@api.resource(:content_views).call(:publish, {
|
146
|
'id' => contentview_id
|
147
|
}))
|
148
|
end
|
149
|
|
150
|
def promote_content_view(contentview_id, line)
|
151
|
contentview = @api.resource(:content_views).call(:show, {'id' => contentview_id})
|
152
|
existing_names = environment_names(contentview)
|
153
|
|
154
|
CSV.parse_line(line[ENVIRONMENTS]).each do |environment_name|
|
155
|
next if environment_name == 'Library' || existing_names.include?(environment_name)
|
156
|
|
157
|
version = contentview['versions'][-1]
|
158
|
task_progress(@api.resource(:content_view_versions).call(:promote, {
|
159
|
'id' => version['id'],
|
160
|
'environment_id' => lifecycle_environment(line[ORGANIZATION], :name => environment_name),
|
161
|
'force' => true
|
162
|
}))
|
163
|
end
|
164
|
end
|
165
|
end
|
166
|
end
|
167
|
end
|