hammer_cli_csv 0.0.6 → 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -121,18 +121,15 @@ module HammerCLICsv
121
121
  end
122
122
 
123
123
  def export_foretello(csv)
124
- @api.resource(:organizations)\
125
- .call(:index, {:per_page => 999999})['results'].each do |organization|
126
- @api.resource(:systems)\
127
- .call(:index, {
128
- 'per_page' => 999999,
129
- 'organization_id' => foreman_organization(:name => organization['name'])
130
- })['results'].each do |system|
131
- system = @api.resource(:systems)\
132
- .call(:show, {
133
- 'id' => system['uuid'],
134
- 'fields' => 'full'
135
- })
124
+ @api.resource(:organizations).call(:index, {:per_page => 999999})['results'].each do |organization|
125
+ @api.resource(:systems).call(:index, {
126
+ 'per_page' => 999999,
127
+ 'organization_id' => foreman_organization(:name => organization['name'])
128
+ })['results'].each do |system|
129
+ system = @api.resource(:systems).call(:show, {
130
+ 'id' => system['uuid'],
131
+ 'fields' => 'full'
132
+ })
136
133
 
137
134
  name = system['name']
138
135
  count = 1
@@ -140,13 +137,13 @@ module HammerCLICsv
140
137
  environment = system['environment']['label']
141
138
  contentview = system['content_view']['name']
142
139
  hostcollections = CSV.generate do |column|
143
- column << system['systemGroups'].collect do |hostcollection|
140
+ column << system['hostCollections'].collect do |hostcollection|
144
141
  hostcollection['name']
145
142
  end
146
143
  end
147
144
  hostcollections.delete!("\n")
148
145
  virtual = system['facts']['virt.is_guest'] == 'true' ? 'Yes' : 'No'
149
- host = system['virtual_host']
146
+ host = system['virtual_host'].nil? ? nil : system['virtual_host']['name']
150
147
  operatingsystem = "#{system['facts']['distribution.name']} " if system['facts']['distribution.name']
151
148
  operatingsystem += system['facts']['distribution.version'] if system['facts']['distribution.version']
152
149
  architecture = system['facts']['uname.machine']
@@ -161,11 +158,11 @@ module HammerCLICsv
161
158
  end
162
159
  products.delete!("\n")
163
160
  subscriptions = CSV.generate do |column|
164
- column << @api.resource(:subscriptions)\
165
- .call(:index, {
166
- 'system_id' => system['uuid']
167
- })['results'].collect do |subscription|
168
- "#{subscription['quantity']}|#{subscription['product_id']}|#{subscription['product_name']}"
161
+ column << @api.resource(:subscriptions).call(:index, {
162
+ 'organization_id' => organization['id'],
163
+ 'system_id' => system['uuid']
164
+ })['results'].collect do |subscription|
165
+ "#{subscription['consumed']}|#{subscription['product_id']}|#{subscription['product_name']}"
169
166
  end
170
167
  end
171
168
  subscriptions.delete!("\n")
@@ -183,13 +180,12 @@ module HammerCLICsv
183
180
  create_systems_from_csv(line)
184
181
  end
185
182
 
186
- print 'Updating host and guest associations...' if option_verbose?
183
+ print 'Updating hypervisor and guest associations...' if option_verbose?
187
184
  @host_guests.each do |host_id, guest_ids|
188
- @api.resource(:systems)\
189
- .call(:update, {
190
- 'id' => host_id,
191
- 'guest_ids' => guest_ids
192
- })
185
+ @api.resource(:systems).call(:update, {
186
+ 'id' => host_id,
187
+ 'guest_ids' => guest_ids
188
+ })
193
189
  end
194
190
  puts 'done' if option_verbose?
195
191
  end
@@ -200,18 +196,16 @@ module HammerCLICsv
200
196
  # Fetching all content hosts is too slow and times out due to the complexity of the data
201
197
  # rendered in the json.
202
198
  # http://projects.theforeman.org/issues/6307
203
- total = @api.resource(:systems)\
204
- .call(:index, {
205
- 'organization_id' => foreman_organization(:name => line[ORGANIZATION]),
206
- 'per_page' => 1
207
- })['total'].to_i
199
+ total = @api.resource(:systems).call(:index, {
200
+ 'organization_id' => foreman_organization(:name => line[ORGANIZATION]),
201
+ 'per_page' => 1
202
+ })['total'].to_i
208
203
  (total / 20 + 2).to_i.times do |page|
209
- @api.resource(:systems)\
210
- .call(:index, {
211
- 'organization_id' => foreman_organization(:name => line[ORGANIZATION]),
212
- 'page' => page,
213
- 'per_page' => 20
214
- })['results'].each do |system|
204
+ @api.resource(:systems).call(:index, {
205
+ 'organization_id' => foreman_organization(:name => line[ORGANIZATION]),
206
+ 'page' => page,
207
+ 'per_page' => 20
208
+ })['results'].each do |system|
215
209
  @existing[line[ORGANIZATION]][system['name']] = system['uuid'] if system
216
210
  end
217
211
  end
@@ -220,45 +214,43 @@ module HammerCLICsv
220
214
  line[COUNT].to_i.times do |number|
221
215
  name = namify(line[NAME], number)
222
216
 
223
- # TODO: w/ @daviddavis p-r
224
- #subscriptions(line).each do |subscription|
225
- # katello_subscription(line[ORGANIZATION], :name => subscription[:number])
226
- #end
227
-
228
217
  if !@existing[line[ORGANIZATION]].include? name
229
- print "Creating system '#{name}'..." if option_verbose?
230
- system_id = @api.resource(:systems)\
231
- .call(:create, {
232
- 'name' => name,
233
- 'organization_id' => foreman_organization(:name => line[ORGANIZATION]),
234
- 'environment_id' => lifecycle_environment(line[ORGANIZATION], :name => line[ENVIRONMENT]),
235
- 'content_view_id' => katello_contentview(line[ORGANIZATION], :name => line[CONTENTVIEW]),
236
- 'facts' => facts(name, line),
237
- 'installed_products' => products(line)
238
- })['uuid']
218
+ print "Creating content host '#{name}'..." if option_verbose?
219
+ system_id = @api.resource(:systems).call(:create, {
220
+ 'name' => name,
221
+ 'organization_id' => foreman_organization(:name => line[ORGANIZATION]),
222
+ 'environment_id' => lifecycle_environment(line[ORGANIZATION], :name => line[ENVIRONMENT]),
223
+ 'content_view_id' => katello_contentview(line[ORGANIZATION], :name => line[CONTENTVIEW]),
224
+ 'facts' => facts(name, line),
225
+ 'installed_products' => products(line),
226
+ 'service_level' => line[SLA],
227
+ 'type' => 'system'
228
+ })['uuid']
239
229
  @existing[line[ORGANIZATION]][name] = system_id
240
230
  else
241
- print "Updating system '#{name}'..." if option_verbose?
242
- system_id = @api.resource(:systems)\
243
- .call(:update, {
244
- 'id' => @existing[line[ORGANIZATION]][name],
245
- 'system' => {
246
- 'name' => name,
247
- 'environment_id' => lifecycle_environment(line[ORGANIZATION], :name => line[ENVIRONMENT]),
248
- 'content_view_id' => katello_contentview(line[ORGANIZATION], :name => line[CONTENTVIEW]),
249
- 'facts' => facts(name, line),
250
- 'installed_products' => products(line)
251
- }
252
- })['uuid']
231
+ print "Updating content host '#{name}'..." if option_verbose?
232
+ system_id = @api.resource(:systems).call(:update, {
233
+ 'id' => @existing[line[ORGANIZATION]][name],
234
+ 'system' => {
235
+ 'name' => name,
236
+ 'environment_id' => lifecycle_environment(line[ORGANIZATION], :name => line[ENVIRONMENT]),
237
+ 'content_view_id' => katello_contentview(line[ORGANIZATION], :name => line[CONTENTVIEW]),
238
+ 'facts' => facts(name, line),
239
+ 'installed_products' => products(line)
240
+ },
241
+ 'installed_products' => products(line), # TODO: http://projects.theforeman.org/issues/9191,
242
+ 'service_level' => line[SLA]
243
+ })['uuid']
253
244
  end
254
245
 
255
246
  if line[VIRTUAL] == 'Yes' && line[HOST]
256
- raise "Host system '#{line[HOST]}' not found" if !@existing[line[ORGANIZATION]][line[HOST]]
247
+ raise "Content host '#{line[HOST]}' not found" if !@existing[line[ORGANIZATION]][line[HOST]]
257
248
  @host_guests[@existing[line[ORGANIZATION]][line[HOST]]] ||= []
258
249
  @host_guests[@existing[line[ORGANIZATION]][line[HOST]]] << "#{line[ORGANIZATION]}/#{name}"
259
250
  end
260
251
 
261
- set_host_collections(system_id, line)
252
+ update_host_collections(system_id, line)
253
+ update_subscriptions(system_id, line)
262
254
 
263
255
  puts 'done' if option_verbose?
264
256
  end
@@ -270,35 +262,41 @@ module HammerCLICsv
270
262
 
271
263
  def facts(name, line)
272
264
  facts = {}
265
+ facts['system.certificate_version'] = '3.2' # Required for auto-attach to work
273
266
  facts['network.hostname'] = name
274
267
  facts['cpu.core(s)_per_socket'] = line[CORES]
275
268
  facts['cpu.cpu_socket(s)'] = line[SOCKETS]
276
269
  facts['memory.memtotal'] = line[RAM]
277
270
  facts['uname.machine'] = line[ARCHITECTURE]
278
- if line[OPERATINGSYSTEM].nil?
279
- facts['distribution.name'] = nil
280
- facts['distribution.version'] = nil
281
- elsif line[OPERATINGSYSTEM].index(' ')
282
- (facts['distribution.name'], facts['distribution.version']) = line[OPERATINGSYSTEM].split(' ')
283
- else
284
- (facts['distribution.name'], facts['distribution.version']) = ['RHEL', line[OPERATINGSYSTEM]]
285
- end
271
+ (facts['distribution.name'], facts['distribution.version']) = os_name_version(line[OPERATINGSYSTEM])
286
272
  facts['virt.is_guest'] = line[VIRTUAL] == 'Yes' ? true : false
287
273
  facts['virt.uuid'] = "#{line[ORGANIZATION]}/#{name}" if facts['virt.is_guest']
274
+ facts['cpu.cpu(s)'] = 1
288
275
  facts
289
276
  end
290
277
 
291
- def set_host_collections(system_id, line)
278
+ def update_host_collections(system_id, line)
292
279
  return nil if !line[HOSTCOLLECTIONS]
293
280
  CSV.parse_line(line[HOSTCOLLECTIONS]).each do |hostcollection_name|
294
- @api.resource(:host_collections)\
295
- .call(:add_systems, {
296
- 'id' => katello_hostcollection(line[ORGANIZATION], :name => hostcollection_name),
297
- 'system_ids' => [system_id]
298
- })
281
+ @api.resource(:host_collections).call(:add_systems, {
282
+ 'id' => katello_hostcollection(line[ORGANIZATION], :name => hostcollection_name),
283
+ 'system_ids' => [system_id]
284
+ })
299
285
  end
300
286
  end
301
287
 
288
+ def os_name_version(operatingsystem)
289
+ if operatingsystem.nil?
290
+ name = nil
291
+ version = nil
292
+ elsif operatingsystem.index(' ')
293
+ (name, version) = operatingsystem.split(' ')
294
+ else
295
+ (name, version) = ['RHEL', operatingsystem]
296
+ end
297
+ [name, version]
298
+ end
299
+
302
300
  def products(line)
303
301
  return nil if !line[PRODUCTS]
304
302
  products = CSV.parse_line(line[PRODUCTS]).collect do |product_details|
@@ -306,19 +304,40 @@ module HammerCLICsv
306
304
  # TODO: these get passed straight through to candlepin; probably would be better to process in server
307
305
  # to allow underscore product_id here
308
306
  (product['productId'], product['productName']) = product_details.split('|')
307
+ product['arch'] = line[ARCHITECTURE]
308
+ product['version'] = os_name_version(line[OPERATINGSYSTEM])[1]
309
309
  product
310
310
  end
311
311
  products
312
312
  end
313
313
 
314
- def subscriptions(line)
315
- return nil if !line[SUBSCRIPTIONS]
316
- subscriptions = CSV.parse_line(line[SUBSCRIPTIONS]).collect do |subscription_details|
317
- subscription = {}
318
- (subscription[:quantity], subscription[:number], subscription[:name]) = subscription_details.split('|')
319
- subscription
314
+ def update_subscriptions(content_host_id, line)
315
+ existing_subscriptions = @api.resource(:subscriptions).call(:index, {
316
+ 'organization_id' => foreman_organization(:name => line[ORGANIZATION]),
317
+ 'per_page' => 999999,
318
+ 'system_id' => content_host_id
319
+ })['results']
320
+ if existing_subscriptions.length > 0
321
+ @api.resource(:systems).call(:remove_subscriptions, {
322
+ 'id' => content_host_id,
323
+ 'subscriptions' => existing_subscriptions
324
+ })
320
325
  end
321
- subscriptions
326
+
327
+ return if line[SUBSCRIPTIONS].nil? || line[SUBSCRIPTIONS].empty?
328
+
329
+ subscriptions = CSV.parse_line(line[SUBSCRIPTIONS], {:skip_blanks => true}).collect do |details|
330
+ (amount, sku, name) = details.split('|')
331
+ {
332
+ :id => katello_subscription(line[ORGANIZATION], :name => name),
333
+ :quantity => (amount.nil? || amount.empty? || amount == 'Automatic') ? 0 : amount
334
+ }
335
+ end
336
+
337
+ @api.resource(:subscriptions).call(:create, {
338
+ 'system_id' => content_host_id,
339
+ 'subscriptions' => subscriptions
340
+ })
322
341
  end
323
342
  end
324
343
  end
@@ -20,13 +20,59 @@ module HammerCLICsv
20
20
 
21
21
  CONTENTVIEW = 'Content View'
22
22
  ORGANIZATION = 'Organization'
23
- TYPE = 'Type'
24
23
  DESCRIPTION = 'Description'
24
+ TYPE = 'Type'
25
25
  REPOSITORIES = 'Repositories'
26
26
  RULES = 'Rules'
27
27
 
28
28
  def export
29
- # TODO
29
+ CSV.open(option_csv_file || '/dev/stdout', 'wb', {:force_quotes => false}) do |csv|
30
+ csv << [NAME, COUNT, CONTENTVIEW, ORGANIZATION, TYPE, DESCRIPTION, REPOSITORIES, RULES]
31
+ @api.resource(:organizations).call(:index, {
32
+ :per_page => 999999
33
+ })['results'].each do |organization|
34
+ composite_contentviews = []
35
+ @api.resource(:content_views).call(:index, {
36
+ 'per_page' => 999999,
37
+ 'organization_id' => organization['id'],
38
+ 'nondefault' => true
39
+ })['results'].each do |contentview|
40
+ @api.resource(:content_view_filters).call(:index, {
41
+ 'content_view_id' => contentview['id']
42
+ })['results'].collect do |filter|
43
+ filter_type = "#{filter['inclusion'] == true ? 'Include' : 'Exclude'} #{filter['type']}"
44
+
45
+ case filter['type']
46
+ when /rpm/
47
+ rules = CSV.generate do |column|
48
+ column << filter['rules'].collect do |rule|
49
+ if rule['version']
50
+ "#{rule['name']}|=|#{rule['version']}"
51
+ elsif rule['min_version'] && rule['max_version']
52
+ "#{rule['name']}|-|#{rule['min_version']},#{rule['max_version']}"
53
+ elsif rule['min_version']
54
+ "#{rule['name']}|>|#{rule['min_version']}"
55
+ elsif rule['max_version']
56
+ "#{rule['name']}|<|#{rule['max_version']}"
57
+ end
58
+ end
59
+ end
60
+ rules.delete!("\n")
61
+ when /erratum/
62
+ when /package_group/
63
+ else
64
+ raise "Unknown filter rule type '#{filter['type']}'"
65
+ end
66
+ #puts "#{filter['type']} -> #{rule}"
67
+
68
+ name = contentview['name']
69
+ repositories = export_column(filter, 'repositories', 'name')
70
+ csv << [name, 1, contentview['name'], organization['name'], filter_type, filter['description'],
71
+ repositories, rules]
72
+ end
73
+ end
74
+ end
75
+ end
30
76
  end
31
77
 
32
78
  def import
@@ -41,11 +87,10 @@ module HammerCLICsv
41
87
  @existing_filters[line[ORGANIZATION]] ||= {}
42
88
  if !@existing_filters[line[ORGANIZATION]][line[CONTENTVIEW]]
43
89
  @existing_filters[line[ORGANIZATION]][line[CONTENTVIEW]] ||= {}
44
- @api.resource(:content_view_filters)\
45
- .call(:index, {
46
- 'per_page' => 999999,
47
- 'content_view_id' => katello_contentview(line[ORGANIZATION], :name => line[CONTENTVIEW])
48
- })['results'].each do |filter|
90
+ @api.resource(:content_view_filters).call(:index, {
91
+ 'per_page' => 999999,
92
+ 'content_view_id' => katello_contentview(line[ORGANIZATION], :name => line[CONTENTVIEW])
93
+ })['results'].each do |filter|
49
94
  @existing_filters[line[ORGANIZATION]][line[CONTENTVIEW]][filter['name']] = filter['id'] if filter
50
95
  end
51
96
  end
@@ -60,36 +105,33 @@ module HammerCLICsv
60
105
  filter_id = @existing_filters[line[ORGANIZATION]][line[CONTENTVIEW]][name]
61
106
  if !filter_id
62
107
  print "Creating filter '#{name}' for content view filter '#{line[CONTENTVIEW]}'..." if option_verbose?
63
- filter_id = @api.resource(:content_view_filters)\
64
- .call(:create, {
65
- 'content_view_id' => katello_contentview(line[ORGANIZATION], :name => line[CONTENTVIEW]),
66
- 'name' => name,
67
- 'description' => line[DESCRIPTION],
68
- 'type' => filter_type(line[TYPE]),
69
- 'inclusion' => filter_inclusion?(line[TYPE]),
70
- 'repository_ids' => repository_ids
71
- })['id']
108
+ filter_id = @api.resource(:content_view_filters).call(:create, {
109
+ 'content_view_id' => katello_contentview(line[ORGANIZATION], :name => line[CONTENTVIEW]),
110
+ 'name' => name,
111
+ 'description' => line[DESCRIPTION],
112
+ 'type' => filter_type(line[TYPE]),
113
+ 'inclusion' => filter_inclusion?(line[TYPE]),
114
+ 'repository_ids' => repository_ids
115
+ })['id']
72
116
  @existing_filters[line[ORGANIZATION]][name] = filter_id
73
117
  else
74
118
  print "Updating filter '#{name}' for content view filter '#{line[CONTENTVIEW]}'..." if option_verbose?
75
- @api.resource(:content_view_filters)\
76
- .call(:update, {
77
- 'id' => filter_id,
78
- 'description' => line[DESCRIPTION],
79
- 'type' => filter_type(line[TYPE]),
80
- 'inclusion' => filter_inclusion?(line[TYPE]),
81
- 'repository_ids' => repository_ids
82
- })
119
+ @api.resource(:content_view_filters).call(:update, {
120
+ 'id' => filter_id,
121
+ 'description' => line[DESCRIPTION],
122
+ 'type' => filter_type(line[TYPE]),
123
+ 'inclusion' => filter_inclusion?(line[TYPE]),
124
+ 'repository_ids' => repository_ids
125
+ })
83
126
  end
84
127
 
85
128
  @existing_rules ||= {}
86
129
  @existing_rules[line[ORGANIZATION]] ||= {}
87
130
  @existing_rules[line[ORGANIZATION]][line[CONTENTVIEW]] ||= {}
88
- @api.resource(:content_view_filter_rules)\
89
- .call(:index, {
90
- 'per_page' => 999999,
91
- 'content_view_filter_id' => filter_id
92
- })['results'].each do |rule|
131
+ @api.resource(:content_view_filter_rules).call(:index, {
132
+ 'per_page' => 999999,
133
+ 'content_view_filter_id' => filter_id
134
+ })['results'].each do |rule|
93
135
  @existing_rules[line[ORGANIZATION]][line[CONTENTVIEW]][rule['name']] = rule
94
136
  end
95
137
 
@@ -119,11 +161,11 @@ module HammerCLICsv
119
161
 
120
162
  rule = @existing_rules[line[ORGANIZATION]][line[CONTENTVIEW]][name]
121
163
  if !rule
122
- print "creating rule '#{rule}'..." if option_verbose?
164
+ print "." if option_verbose?
123
165
  rule = @api.resource(:content_view_filter_rules).call(:create, params)
124
166
  @existing_rules[line[ORGANIZATION]][line[CONTENTVIEW]][rule['name']] = rule
125
167
  else
126
- print "updating rule '#{rule}'..." if option_verbose?
168
+ print "." if option_verbose?
127
169
  params['id'] = rule['id']
128
170
  @api.resource(:content_view_filter_rules).call(:update, params)
129
171
  end
@@ -15,13 +15,67 @@ module HammerCLICsv
15
15
  command_name 'content-views'
16
16
  desc 'import or export content-views'
17
17
 
18
+ LABEL = 'Label'
18
19
  ORGANIZATION = 'Organization'
19
20
  DESCRIPTION = 'Description'
20
21
  COMPOSITE = 'Composite'
21
- REPOSITORIES = 'Repositories'
22
+ REPOSITORIES = 'Repositories or Composites'
23
+ FILTERS = 'Filters'
22
24
 
23
25
  def export
24
- # TODO
26
+ CSV.open(option_csv_file || '/dev/stdout', 'wb', {:force_quotes => false}) do |csv|
27
+ csv << [NAME, COUNT, LABEL, ORGANIZATION, COMPOSITE, REPOSITORIES, FILTERS]
28
+ @api.resource(:organizations).call(:index, {
29
+ :per_page => 999999
30
+ })['results'].each do |organization|
31
+ composite_contentviews = []
32
+ @api.resource(:content_views).call(:index, {
33
+ 'per_page' => 999999,
34
+ 'organization_id' => organization['id'],
35
+ 'nondefault' => true
36
+ })['results'].each do |contentview|
37
+
38
+ filters = CSV.generate do |column|
39
+ column << @api.resource(:content_view_filters).call(:index, {
40
+ 'content_view_id' => contentview['id']
41
+ })['results'].collect do |filter|
42
+ rules = filter['rules'].collect do |rule|
43
+ rule['name']
44
+ end
45
+ in_or_out = filter['inclusion'] == true ? 'Include' : 'Exclude'
46
+ if filter['type'] == 'rpm'
47
+ "#{ in_or_out }|#{ filter['type'] }|#{ rules.join(',')}"
48
+ elsif filter['type'] == 'erratum'
49
+ "#{ in_or_out }|#{ filter['type'] }|#{ rules['types'].join(',')}"
50
+ else
51
+ "???? #{filter['type']}"
52
+ end
53
+ end
54
+ end
55
+ filters.delete!("\n")
56
+
57
+ name = contentview['name']
58
+ label = contentview['label']
59
+ orgname = organization['name']
60
+ composite = contentview['composite'] == true ? 'Yes' : 'No'
61
+ if composite == 'Yes'
62
+ contentviews = CSV.generate do |column|
63
+ column << contentview['components'].collect do |component|
64
+ component['content_view']['name']
65
+ end
66
+ end
67
+ contentviews.delete!("\n")
68
+ composite_contentviews << [name, 1, label, orgname, composite, contentviews, filters]
69
+ else
70
+ repositories = export_column(contentview, 'repositories', 'name')
71
+ csv << [name, 1, label, orgname, composite, repositories, filters]
72
+ end
73
+ end
74
+ composite_contentviews.each do |contentview|
75
+ csv << contentview
76
+ end
77
+ end
78
+ end
25
79
  end
26
80
 
27
81
  def import
@@ -35,46 +89,75 @@ module HammerCLICsv
35
89
  def create_contentviews_from_csv(line)
36
90
  if !@existing_contentviews[line[ORGANIZATION]]
37
91
  @existing_contentviews[line[ORGANIZATION]] ||= {}
38
- @api.resource(:content_views)\
39
- .call(:index, {
40
- 'per_page' => 999999,
41
- 'organization_id' => foreman_organization(:name => line[ORGANIZATION]),
42
- 'nondefault' => true
43
- })['results'].each do |contentview|
92
+ @api.resource(:content_views).call(:index, {
93
+ 'per_page' => 999999,
94
+ 'organization_id' => foreman_organization(:name => line[ORGANIZATION]),
95
+ 'nondefault' => true
96
+ })['results'].each do |contentview|
44
97
  @existing_contentviews[line[ORGANIZATION]][contentview['name']] = contentview['id'] if contentview
45
98
  end
46
99
  end
47
100
 
48
- repository_ids = collect_column(line[REPOSITORIES]) do |repository|
49
- katello_repository(line[ORGANIZATION], :name => repository)
101
+ is_composite = line[COMPOSITE] == 'Yes' ? true : false
102
+
103
+ if is_composite
104
+ composite_ids = collect_column(line[REPOSITORIES]) do |composite|
105
+ # TODO: export version and use it here
106
+ katello_contentviewversion(line[ORGANIZATION], composite, 1)
107
+ end
108
+ else
109
+ repository_ids = collect_column(line[REPOSITORIES]) do |repository|
110
+ katello_repository(line[ORGANIZATION], :name => repository)
111
+ end
50
112
  end
51
113
 
52
114
  line[COUNT].to_i.times do |number|
53
115
  name = namify(line[NAME], number)
54
- composite = line[COMPOSITE] == 'Yes' ? true : false
55
116
 
56
117
  contentview_id = @existing_contentviews[line[ORGANIZATION]][name]
57
118
  if !contentview_id
58
119
  print "Creating content view '#{name}'..." if option_verbose?
59
- contentview_id = @api.resource(:content_views)\
60
- .call(:create, {
61
- 'organization_id' => foreman_organization(:name => line[ORGANIZATION]),
62
- 'name' => name,
63
- 'label' => labelize(name),
64
- 'description' => line[DESCRIPTION],
65
- 'composite' => composite,
66
- 'repository_ids' => repository_ids
67
- })['id']
120
+ options = {
121
+ 'organization_id' => foreman_organization(:name => line[ORGANIZATION]),
122
+ 'name' => name,
123
+ 'label' => labelize(name),
124
+ 'description' => line[DESCRIPTION],
125
+ 'composite' => is_composite
126
+ }
127
+ if is_composite
128
+ options['component_ids'] = composite_ids
129
+ else
130
+ options['repository_ids'] = repository_ids
131
+ end
132
+ contentview_id = @api.resource(:content_views).call(:create, options)['id']
68
133
  @existing_contentviews[line[ORGANIZATION]][name] = contentview_id
134
+ publish = true
69
135
  else
70
136
  print "Updating content view '#{name}'..." if option_verbose?
71
- @api.resource(:content_views)\
72
- .call(:update, {
73
- 'id' => contentview_id,
74
- 'description' => line[DESCRIPTION],
75
- 'repository_ids' => repository_ids
76
- })
137
+ options = {
138
+ 'id' => contentview_id,
139
+ 'description' => line[DESCRIPTION]
140
+ }
141
+ if is_composite
142
+ options['component_ids'] = composite_ids
143
+ else
144
+ options['repository_ids'] = repository_ids
145
+ end
146
+ contentview = @api.resource(:content_views).call(:update, options)
147
+ publish = contentview['versions'].empty?
77
148
  end
149
+
150
+ # Content views cannot be used in composites unless a publish has occurred
151
+ # TODO: this command cannot be called more than once during a run, why?
152
+ if publish
153
+ args = %W{
154
+ --server #{ @server } --username #{ @username } --password #{ @server }
155
+ content-view publish --id #{ contentview_id }
156
+ --organization-id #{ foreman_organization(:name => line[ORGANIZATION]) }
157
+ }
158
+ hammer.run(args)
159
+ end
160
+
78
161
  puts 'done' if option_verbose?
79
162
  end
80
163