1
|
|
2
|
|
3
|
|
4
|
|
5
|
|
6
|
|
7
|
|
8
|
|
9
|
|
10
|
|
11
|
|
12
|
require 'apipie-bindings'
|
13
|
require 'hammer_cli'
|
14
|
require 'json'
|
15
|
require 'csv'
|
16
|
require 'hammer_cli_csv/csv'
|
17
|
|
18
|
module HammerCLICsv
|
19
|
class BaseCommand < HammerCLI::Apipie::Command
|
20
|
option %w(-v --verbose), :flag, 'be verbose'
|
21
|
option %w(--threads), 'THREAD_COUNT', 'Number of threads to hammer with', :default => 1
|
22
|
option %w(--csv-export), :flag, 'Export current data instead of importing'
|
23
|
option %w(--csv-file), 'FILE_NAME', 'CSV file (default to /dev/stdout with --csv-export, otherwise required)'
|
24
|
option %w(--prefix), 'PREFIX', 'Prefix for all name columns'
|
25
|
option %w(--server), 'SERVER', 'Server URL'
|
26
|
option %w(-u --username), 'USERNAME', 'Username to access server'
|
27
|
option %w(-p --password), 'PASSWORD', 'Password to access server'
|
28
|
|
29
|
NAME = 'Name'
|
30
|
COUNT = 'Count'
|
31
|
|
32
|
def execute
|
33
|
if !option_csv_file
|
34
|
if option_csv_export?
|
35
|
|
36
|
option_csv_file = '/dev/stdout'
|
37
|
else
|
38
|
|
39
|
option_csv_file = '/dev/stdin'
|
40
|
end
|
41
|
end
|
42
|
|
43
|
@api = ApipieBindings::API.new({
|
44
|
:uri => option_server || HammerCLI::Settings.get(:csv, :host),
|
45
|
:username => option_username || HammerCLI::Settings.get(:csv, :username),
|
46
|
:password => option_password || HammerCLI::Settings.get(:csv, :password),
|
47
|
:api_version => 2
|
48
|
})
|
49
|
|
50
|
option_csv_export? ? export : import
|
51
|
HammerCLI::EX_OK
|
52
|
end
|
53
|
|
54
|
def namify(name_format, number = 0)
|
55
|
if name_format.index('%')
|
56
|
name = name_format % number
|
57
|
else
|
58
|
name = name_format
|
59
|
end
|
60
|
name = "#{option_prefix}#{name}" if option_prefix
|
61
|
name
|
62
|
end
|
63
|
|
64
|
def labelize(name)
|
65
|
name.gsub(/[^a-z0-9\-_]/i, '_')
|
66
|
end
|
67
|
|
68
|
def thread_import(return_headers = false)
|
69
|
csv = []
|
70
|
CSV.foreach(option_csv_file || '/dev/stdin', {
|
71
|
:skip_blanks => true,
|
72
|
:headers => :first_row,
|
73
|
:return_headers => return_headers
|
74
|
}) do |line|
|
75
|
csv << line
|
76
|
end
|
77
|
lines_per_thread = csv.length / option_threads.to_i + 1
|
78
|
splits = []
|
79
|
|
80
|
option_threads.to_i.times do |current_thread|
|
81
|
start_index = ((current_thread) * lines_per_thread).to_i
|
82
|
finish_index = ((current_thread + 1) * lines_per_thread).to_i
|
83
|
lines = csv[start_index...finish_index].clone
|
84
|
splits << Thread.new do
|
85
|
lines.each do |line|
|
86
|
if line[NAME][0] != '#'
|
87
|
yield line
|
88
|
end
|
89
|
end
|
90
|
end
|
91
|
end
|
92
|
|
93
|
splits.each do |thread|
|
94
|
thread.join
|
95
|
end
|
96
|
end
|
97
|
|
98
|
def foreman_organization(options = {})
|
99
|
@organizations ||= {}
|
100
|
|
101
|
if options[:name]
|
102
|
return nil if options[:name].nil? || options[:name].empty?
|
103
|
options[:id] = @organizations[options[:name]]
|
104
|
if !options[:id]
|
105
|
organization = @api.resource(:organizations).call(:index, {
|
106
|
:per_page => 999999,
|
107
|
'search' => "name=\"#{options[:name]}\""
|
108
|
})['results']
|
109
|
raise "Organization '#{options[:name]}' not found" if !organization || organization.empty?
|
110
|
options[:id] = organization[0]['id']
|
111
|
@organizations[options[:name]] = options[:id]
|
112
|
end
|
113
|
result = options[:id]
|
114
|
else
|
115
|
return nil if options[:id].nil?
|
116
|
options[:name] = @organizations.key(options[:id])
|
117
|
if !options[:name]
|
118
|
organization = @api.resource(:organizations).call(:show, {'id' => options[:id]})
|
119
|
raise "Organization 'id=#{options[:id]}' not found" if !organization || organization.empty?
|
120
|
options[:name] = organization['name']
|
121
|
@organizations[options[:name]] = options[:id]
|
122
|
end
|
123
|
result = options[:name]
|
124
|
end
|
125
|
|
126
|
result
|
127
|
end
|
128
|
|
129
|
def foreman_location(options = {})
|
130
|
@locations ||= {}
|
131
|
|
132
|
if options[:name]
|
133
|
return nil if options[:name].nil? || options[:name].empty?
|
134
|
options[:id] = @locations[options[:name]]
|
135
|
if !options[:id]
|
136
|
location = @api.resource(:locations).call(:index, {
|
137
|
:per_page => 999999,
|
138
|
'search' => "name=\"#{options[:name]}\""
|
139
|
})['results']
|
140
|
raise "Location '#{options[:name]}' not found" if !location || location.empty?
|
141
|
options[:id] = location[0]['id']
|
142
|
@locations[options[:name]] = options[:id]
|
143
|
end
|
144
|
result = options[:id]
|
145
|
else
|
146
|
return nil if options[:id].nil?
|
147
|
options[:name] = @locations.key(options[:id])
|
148
|
if !options[:name]
|
149
|
location = @api.resource(:locations).call(:show, {'id' => options[:id]})
|
150
|
raise "Location 'id=#{options[:id]}' not found" if !location || location.empty?
|
151
|
options[:name] = location['name']
|
152
|
@locations[options[:name]] = options[:id]
|
153
|
end
|
154
|
result = options[:name]
|
155
|
end
|
156
|
|
157
|
result
|
158
|
end
|
159
|
|
160
|
def foreman_role(options = {})
|
161
|
@roles ||= {}
|
162
|
|
163
|
if options[:name]
|
164
|
return nil if options[:name].nil? || options[:name].empty?
|
165
|
options[:id] = @roles[options[:name]]
|
166
|
if !options[:id]
|
167
|
role = @api.resource(:roles).call(:index, {
|
168
|
:per_page => 999999,
|
169
|
'search' => "name=\"#{options[:name]}\""
|
170
|
})['results']
|
171
|
raise "Role '#{options[:name]}' not found" if !role || role.empty?
|
172
|
options[:id] = role[0]['id']
|
173
|
@roles[options[:name]] = options[:id]
|
174
|
end
|
175
|
result = options[:id]
|
176
|
else
|
177
|
return nil if options[:id].nil?
|
178
|
options[:name] = @roles.key(options[:id])
|
179
|
if !options[:name]
|
180
|
role = @api.resource(:roles).call(:show, {'id' => options[:id]})
|
181
|
raise "Role 'id=#{options[:id]}' not found" if !role || role.empty?
|
182
|
options[:name] = role['name']
|
183
|
@roles[options[:name]] = options[:id]
|
184
|
end
|
185
|
result = options[:name]
|
186
|
end
|
187
|
|
188
|
result
|
189
|
end
|
190
|
|
191
|
def foreman_permission(options = {})
|
192
|
@permissions ||= {}
|
193
|
|
194
|
if options[:name]
|
195
|
return nil if options[:name].nil? || options[:name].empty?
|
196
|
options[:id] = @permissions[options[:name]]
|
197
|
if !options[:id]
|
198
|
permission = @api.resource(:permissions).call(:index, {
|
199
|
:per_page => 999999,
|
200
|
'name' => options[:name]
|
201
|
})['results']
|
202
|
raise "Permission '#{options[:name]}' not found" if !permission || permission.empty?
|
203
|
options[:id] = permission[0]['id']
|
204
|
@permissions[options[:name]] = options[:id]
|
205
|
end
|
206
|
result = options[:id]
|
207
|
else
|
208
|
return nil if options[:id].nil?
|
209
|
options[:name] = @permissions.key(options[:id])
|
210
|
if !options[:name]
|
211
|
permission = @api.resource(:permissions).call(:show, {'id' => options[:id]})
|
212
|
raise "Permission 'id=#{options[:id]}' not found" if !permission || permission.empty?
|
213
|
options[:name] = permission['name']
|
214
|
@permissions[options[:name]] = options[:id]
|
215
|
end
|
216
|
result = options[:name]
|
217
|
end
|
218
|
|
219
|
result
|
220
|
end
|
221
|
|
222
|
def foreman_filter(role, resource, search)
|
223
|
search = nil if search && search.empty?
|
224
|
filters = @api.resource(:filters).call(:index, {
|
225
|
:per_page => 999999,
|
226
|
'search' => "role=\"#{role}\""
|
227
|
})['results']
|
228
|
filters.each do |filter|
|
229
|
return filter['id'] if filter['resource_type'] == resource && filter['search'] == search
|
230
|
end
|
231
|
|
232
|
nil
|
233
|
end
|
234
|
|
235
|
def foreman_environment(options = {})
|
236
|
@environments ||= {}
|
237
|
|
238
|
if options[:name]
|
239
|
return nil if options[:name].nil? || options[:name].empty?
|
240
|
options[:id] = @environments[options[:name]]
|
241
|
if !options[:id]
|
242
|
environment = @api.resource(:environments).call(:index, {
|
243
|
:per_page => 999999,
|
244
|
'search' => "name=\"#{ options[:name] }\""
|
245
|
})['results']
|
246
|
raise "Puppet environment '#{options[:name]}' not found" if !environment || environment.empty?
|
247
|
options[:id] = environment[0]['id']
|
248
|
@environments[options[:name]] = options[:id]
|
249
|
end
|
250
|
result = options[:id]
|
251
|
else
|
252
|
return nil if options[:id].nil?
|
253
|
options[:name] = @environments.key(options[:id])
|
254
|
if !options[:name]
|
255
|
environment = @api.resource(:environments).call(:show, {'id' => options[:id]})
|
256
|
raise "Puppet environment '#{options[:name]}' not found" if !environment || environment.empty?
|
257
|
options[:name] = environment['name']
|
258
|
@environments[options[:name]] = options[:id]
|
259
|
end
|
260
|
result = options[:name]
|
261
|
end
|
262
|
|
263
|
result
|
264
|
end
|
265
|
|
266
|
def foreman_operatingsystem(options = {})
|
267
|
@operatingsystems ||= {}
|
268
|
|
269
|
if options[:name]
|
270
|
return nil if options[:name].nil? || options[:name].empty?
|
271
|
options[:id] = @operatingsystems[options[:name]]
|
272
|
if !options[:id]
|
273
|
(osname, major, minor) = split_os_name(options[:name])
|
274
|
search = "name=\"#{osname}\" and major=\"#{major}\" and minor=\"#{minor}\""
|
275
|
operatingsystems = @api.resource(:operatingsystems).call(:index, {
|
276
|
:per_page => 999999,
|
277
|
'search' => search
|
278
|
})['results']
|
279
|
operatingsystem = operatingsystems[0]
|
280
|
raise "Operating system '#{options[:name]}' not found" if !operatingsystem || operatingsystem.empty?
|
281
|
options[:id] = operatingsystem['id']
|
282
|
@operatingsystems[options[:name]] = options[:id]
|
283
|
end
|
284
|
result = options[:id]
|
285
|
else
|
286
|
return nil if options[:id].nil?
|
287
|
options[:name] = @operatingsystems.key(options[:id])
|
288
|
if !options[:name]
|
289
|
operatingsystem = @api.resource(:operatingsystems).call(:show, {'id' => options[:id]})
|
290
|
raise "Operating system 'id=#{options[:id]}' not found" if !operatingsystem || operatingsystem.empty?
|
291
|
options[:name] = build_os_name(operatingsystem['name'],
|
292
|
operatingsystem['major'],
|
293
|
operatingsystem['minor'])
|
294
|
@operatingsystems[options[:name]] = options[:id]
|
295
|
end
|
296
|
result = options[:name]
|
297
|
end
|
298
|
|
299
|
result
|
300
|
end
|
301
|
|
302
|
def foreman_architecture(options = {})
|
303
|
@architectures ||= {}
|
304
|
|
305
|
if options[:name]
|
306
|
return nil if options[:name].nil? || options[:name].empty?
|
307
|
options[:id] = @architectures[options[:name]]
|
308
|
if !options[:id]
|
309
|
architecture = @api.resource(:architectures).call(:index, {
|
310
|
:per_page => 999999,
|
311
|
'search' => "name=\"#{options[:name]}\""
|
312
|
})['results']
|
313
|
raise "Architecture '#{options[:name]}' not found" if !architecture || architecture.empty?
|
314
|
options[:id] = architecture[0]['id']
|
315
|
@architectures[options[:name]] = options[:id]
|
316
|
end
|
317
|
result = options[:id]
|
318
|
else
|
319
|
return nil if options[:id].nil?
|
320
|
options[:name] = @architectures.key(options[:id])
|
321
|
if !options[:name]
|
322
|
architecture = @api.resource(:architectures).call(:show, {'id' => options[:id]})
|
323
|
raise "Architecture 'id=#{options[:id]}' not found" if !architecture || architecture.empty?
|
324
|
options[:name] = architecture['name']
|
325
|
@architectures[options[:name]] = options[:id]
|
326
|
end
|
327
|
result = options[:name]
|
328
|
end
|
329
|
|
330
|
result
|
331
|
end
|
332
|
|
333
|
def foreman_domain(options = {})
|
334
|
@domains ||= {}
|
335
|
|
336
|
if options[:name]
|
337
|
return nil if options[:name].nil? || options[:name].empty?
|
338
|
options[:id] = @domains[options[:name]]
|
339
|
if !options[:id]
|
340
|
domain = @api.resource(:domains).call(:index, {
|
341
|
:per_page => 999999,
|
342
|
'search' => "name=\"#{options[:name]}\""
|
343
|
})['results']
|
344
|
raise "Domain '#{options[:name]}' not found" if !domain || domain.empty?
|
345
|
options[:id] = domain[0]['id']
|
346
|
@domains[options[:name]] = options[:id]
|
347
|
end
|
348
|
result = options[:id]
|
349
|
else
|
350
|
return nil if options[:id].nil?
|
351
|
options[:name] = @domains.key(options[:id])
|
352
|
if !options[:name]
|
353
|
domain = @api.resource(:domains).call(:show, {'id' => options[:id]})
|
354
|
raise "Domain 'id=#{options[:id]}' not found" if !domain || domain.empty?
|
355
|
options[:name] = domain['name']
|
356
|
@domains[options[:name]] = options[:id]
|
357
|
end
|
358
|
result = options[:name]
|
359
|
end
|
360
|
|
361
|
result
|
362
|
end
|
363
|
|
364
|
def foreman_partitiontable(options = {})
|
365
|
@ptables ||= {}
|
366
|
|
367
|
if options[:name]
|
368
|
return nil if options[:name].nil? || options[:name].empty?
|
369
|
options[:id] = @ptables[options[:name]]
|
370
|
if !options[:id]
|
371
|
ptable = @api.resource(:ptables).call(:index, {
|
372
|
:per_page => 999999,
|
373
|
'search' => "name=\"#{options[:name]}\""
|
374
|
})['results']
|
375
|
raise "Partition table '#{options[:name]}' not found" if !ptable || ptable.empty?
|
376
|
options[:id] = ptable[0]['id']
|
377
|
@ptables[options[:name]] = options[:id]
|
378
|
end
|
379
|
result = options[:id]
|
380
|
elsif options[:id]
|
381
|
return nil if options[:id].nil?
|
382
|
options[:name] = @ptables.key(options[:id])
|
383
|
if !options[:name]
|
384
|
ptable = @api.resource(:ptables).call(:show, {'id' => options[:id]})
|
385
|
options[:name] = ptable['name']
|
386
|
@ptables[options[:name]] = options[:id]
|
387
|
end
|
388
|
result = options[:name]
|
389
|
elsif !options[:name] && !options[:id]
|
390
|
result = ''
|
391
|
end
|
392
|
|
393
|
result
|
394
|
end
|
395
|
|
396
|
def katello_environment(organization, options = {})
|
397
|
@environments ||= {}
|
398
|
@environments[organization] ||= {
|
399
|
}
|
400
|
|
401
|
if options[:name]
|
402
|
return nil if options[:name].nil? || options[:name].empty?
|
403
|
options[:id] = @environments[organization][options[:name]]
|
404
|
if !options[:id]
|
405
|
@api.resource(:lifecycle_environments).call(:index, {
|
406
|
:per_page => 999999,
|
407
|
'organization_id' => foreman_organization(:name => organization),
|
408
|
'library' => true
|
409
|
})['results'].each do |environment|
|
410
|
@environments[organization][environment['name']] = environment['id']
|
411
|
end
|
412
|
options[:id] = @environments[organization][options[:name]]
|
413
|
raise "Lifecycle environment '#{options[:name]}' not found" if !options[:id]
|
414
|
end
|
415
|
result = options[:id]
|
416
|
else
|
417
|
return nil if options[:id].nil?
|
418
|
options[:name] = @environments.key(options[:id])
|
419
|
if !options[:name]
|
420
|
environment = @api.resource(:lifecycle_environments).call(:show, {'id' => options[:id]})
|
421
|
raise "Lifecycle environment '#{options[:name]}' not found" if !environment || environment.empty?
|
422
|
options[:name] = environment['name']
|
423
|
@environments[options[:name]] = options[:id]
|
424
|
end
|
425
|
result = options[:name]
|
426
|
end
|
427
|
|
428
|
result
|
429
|
end
|
430
|
|
431
|
def katello_contentview(organization, options = {})
|
432
|
@contentviews ||= {}
|
433
|
@contentviews[organization] ||= {}
|
434
|
|
435
|
if options[:name]
|
436
|
return nil if options[:name].nil? || options[:name].empty?
|
437
|
options[:id] = @contentviews[organization][options[:name]]
|
438
|
if !options[:id]
|
439
|
@api.resource(:content_views).call(:index, {
|
440
|
:per_page => 999999,
|
441
|
'organization_id' => foreman_organization(:name => organization)
|
442
|
})['results'].each do |contentview|
|
443
|
@contentviews[organization][contentview['name']] = contentview['id']
|
444
|
end
|
445
|
options[:id] = @contentviews[organization][options[:name]]
|
446
|
raise "Content view '#{options[:name]}' not found" if !options[:id]
|
447
|
end
|
448
|
result = options[:id]
|
449
|
else
|
450
|
return nil if options[:id].nil?
|
451
|
options[:name] = @contentviews.key(options[:id])
|
452
|
if !options[:name]
|
453
|
contentview = @api.resource(:content_views).call(:show, {'id' => options[:id]})
|
454
|
raise "Puppet contentview '#{options[:name]}' not found" if !contentview || contentview.empty?
|
455
|
options[:name] = contentview['name']
|
456
|
@contentviews[options[:name]] = options[:id]
|
457
|
end
|
458
|
result = options[:name]
|
459
|
end
|
460
|
|
461
|
result
|
462
|
end
|
463
|
|
464
|
def katello_subscription(organization, options = {})
|
465
|
@subscriptions ||= {}
|
466
|
@subscriptions[organization] ||= {}
|
467
|
|
468
|
if options[:name]
|
469
|
return nil if options[:name].nil? || options[:name].empty?
|
470
|
options[:id] = @subscriptions[organization][options[:name]]
|
471
|
if !options[:id]
|
472
|
results = @api.resource(:subscriptions).call(:index, {
|
473
|
:per_page => 999999,
|
474
|
'organization_id' => foreman_organization(:name => organization),
|
475
|
'search' => "name:\"#{options[:name]}\""
|
476
|
})
|
477
|
raise "No subscriptions match '#{options[:name]}'" if results['subtotal'] == 0
|
478
|
raise "Too many subscriptions match '#{options[:name]}'" if results['subtotal'] > 1
|
479
|
subscription = results['results'][0]
|
480
|
@subscriptions[organization][options[:name]] = subscription['id']
|
481
|
options[:id] = @subscriptions[organization][options[:name]]
|
482
|
raise "Subscription '#{options[:name]}' not found" if !options[:id]
|
483
|
end
|
484
|
result = options[:id]
|
485
|
else
|
486
|
return nil if options[:id].nil?
|
487
|
options[:name] = @subscriptions.key(options[:id])
|
488
|
if !options[:name]
|
489
|
subscription = @api.resource(:subscriptions).call(:show, {'id' => options[:id]})
|
490
|
raise "Subscription '#{options[:name]}' not found" if !subscription || subscription.empty?
|
491
|
options[:name] = subscription['name']
|
492
|
@subscriptions[options[:name]] = options[:id]
|
493
|
end
|
494
|
result = options[:name]
|
495
|
end
|
496
|
|
497
|
result
|
498
|
end
|
499
|
|
500
|
def katello_hostcollection(organization, options = {})
|
501
|
@hostcollections ||= {}
|
502
|
@hostcollections[organization] ||= {}
|
503
|
|
504
|
if options[:name]
|
505
|
return nil if options[:name].nil? || options[:name].empty?
|
506
|
options[:id] = @hostcollections[organization][options[:name]]
|
507
|
if !options[:id]
|
508
|
@api.resource(:host_collections).call(:index,
|
509
|
{
|
510
|
:per_page => 999999,
|
511
|
'organization_id' => foreman_organization(:name => organization),
|
512
|
'search' => "name:\"#{options[:name]}\""
|
513
|
})['results'].each do |hostcollection|
|
514
|
@hostcollections[organization][hostcollection['name']] = hostcollection['id'] if hostcollection
|
515
|
end
|
516
|
options[:id] = @hostcollections[organization][options[:name]]
|
517
|
raise "System group '#{options[:name]}' not found" if !options[:id]
|
518
|
end
|
519
|
result = options[:id]
|
520
|
else
|
521
|
return nil if options[:id].nil?
|
522
|
options[:name] = @hostcollections.key(options[:id])
|
523
|
if !options[:name]
|
524
|
hostcollection = @api.resource(:host_collections).call(:show, {'id' => options[:id]})
|
525
|
raise "System group '#{options[:name]}' not found" if !hostcollection || hostcollection.empty?
|
526
|
options[:name] = hostcollection['name']
|
527
|
@hostcollections[options[:name]] = options[:id]
|
528
|
end
|
529
|
result = options[:name]
|
530
|
end
|
531
|
|
532
|
result
|
533
|
end
|
534
|
|
535
|
def build_os_name(name, major, minor)
|
536
|
name += " #{major}" if major && major != ''
|
537
|
name += ".#{minor}" if minor && minor != ''
|
538
|
name
|
539
|
end
|
540
|
|
541
|
|
542
|
|
543
|
def split_os_name(name)
|
544
|
tokens = name.split(' ')
|
545
|
is_number = Float(tokens[-1]) rescue false
|
546
|
if is_number
|
547
|
(major, minor) = tokens[-1].split('.').flatten
|
548
|
name = tokens[0...-1].join(' ')
|
549
|
else
|
550
|
name = tokens.join(' ')
|
551
|
end
|
552
|
[name, major || '', minor || '']
|
553
|
end
|
554
|
|
555
|
def export_column(object, name, field)
|
556
|
return '' unless object[name]
|
557
|
values = CSV.generate do |column|
|
558
|
column << object[name].collect do |fields|
|
559
|
fields[field]
|
560
|
end
|
561
|
end
|
562
|
values.delete!("\n")
|
563
|
end
|
564
|
|
565
|
def collect_column(column)
|
566
|
return [] if column.nil? || column.empty?
|
567
|
CSV.parse_line(column, {:skip_blanks => true}).collect do |value|
|
568
|
yield value
|
569
|
end
|
570
|
end
|
571
|
|
572
|
def pluralize(name)
|
573
|
case name
|
574
|
when /smart_proxy/
|
575
|
'smart_proxies'
|
576
|
else
|
577
|
"#{name}s"
|
578
|
end
|
579
|
end
|
580
|
|
581
|
def associate_organizations(id, organizations, name)
|
582
|
return if organizations.nil?
|
583
|
|
584
|
associations ||= {}
|
585
|
CSV.parse_line(organizations).each do |organization|
|
586
|
organization_id = foreman_organization(:name => organization)
|
587
|
if associations[organization].nil?
|
588
|
associations[organization] = @api.resource(:organizations).call(:show, {'id' => organization_id})[pluralize(name)].collect do |reference_object|
|
589
|
reference_object['id']
|
590
|
end
|
591
|
end
|
592
|
associations[organization] += [id] if !associations[organization].include? id
|
593
|
@api.resource(:organizations)
|
594
|
.call(:update, {
|
595
|
'id' => organization_id,
|
596
|
'organization' => {
|
597
|
"#{name}_ids" => associations[organization]
|
598
|
}
|
599
|
})
|
600
|
end if organizations && !organizations.empty?
|
601
|
end
|
602
|
|
603
|
def associate_locations(id, locations, name)
|
604
|
return if locations.nil?
|
605
|
|
606
|
associations ||= {}
|
607
|
CSV.parse_line(locations).each do |location|
|
608
|
location_id = foreman_location(:name => location)
|
609
|
if associations[location].nil?
|
610
|
associations[location] = @api.resource(:locations).call(:show, {'id' => location_id})[pluralize(name)].collect do |reference_object|
|
611
|
reference_object['id']
|
612
|
end
|
613
|
end
|
614
|
associations[location] += [id] if !associations[location].include? id
|
615
|
|
616
|
@api.resource(:locations)
|
617
|
.call(:update, {
|
618
|
'id' => location_id,
|
619
|
'location' => {
|
620
|
"#{name}_ids" => associations[location]
|
621
|
}
|
622
|
})
|
623
|
end if locations && !locations.empty?
|
624
|
end
|
625
|
end
|
626
|
end
|