Project

General

Profile

Download (27.6 KB) Statistics
| Branch: | Tag: | Revision:

hammer-cli-csv / lib / hammer_cli_csv / base.rb @ 561a8ac9

1
# Copyright 2013-2014 Red Hat, Inc.
2
#
3
# This software is licensed to you under the GNU General Public
4
# License as published by the Free Software Foundation; either version
5
# 2 of the License (GPLv2) or (at your option) any later version.
6
# There is NO WARRANTY for this software, express or implied,
7
# including the implied warranties of MERCHANTABILITY,
8
# NON-INFRINGEMENT, or FITNESS FOR A PARTICULAR PURPOSE. You should
9
# have received a copy of GPLv2 along with this software; if not, see
10
# http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt.
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
          # rubocop:disable UselessAssignment
36
          option_csv_file = '/dev/stdout'
37
        else
38
          # rubocop:disable UselessAssignment
39
          option_csv_file = '/dev/stdin'
40
        end
41
      end
42

    
43
      server = option_server ||
44
        HammerCLI::Settings.get(:csv, :host) ||
45
        HammerCLI::Settings.get(:katello, :host) ||
46
        HammerCLI::Settings.get(:foreman, :host)
47
      username = option_username ||
48
        HammerCLI::Settings.get(:csv, :username) ||
49
        HammerCLI::Settings.get(:katello, :username) ||
50
        HammerCLI::Settings.get(:foreman, :username)
51
      password = option_password ||
52
        HammerCLI::Settings.get(:csv, :password) ||
53
        HammerCLI::Settings.get(:katello, :password) ||
54
        HammerCLI::Settings.get(:foreman, :password)
55
      @api = ApipieBindings::API.new({
56
                                       :uri => server,
57
                                       :username => username,
58
                                       :password => password,
59
                                       :api_version => 2
60
                                     })
61

    
62
      option_csv_export? ? export : import
63
      HammerCLI::EX_OK
64
    end
65

    
66
    def namify(name_format, number = 0)
67
      if name_format.index('%')
68
        name = name_format % number
69
      else
70
        name = name_format
71
      end
72
      name = "#{option_prefix}#{name}" if option_prefix
73
      name
74
    end
75

    
76
    def labelize(name)
77
      name.gsub(/[^a-z0-9\-_]/i, '_')
78
    end
79

    
80
    def thread_import(return_headers = false)
81
      csv = []
82
      CSV.foreach(option_csv_file || '/dev/stdin', {
83
                                                     :skip_blanks => true,
84
                                                     :headers => :first_row,
85
                                                     :return_headers => return_headers
86
                                                   }) do |line|
87
        csv << line
88
      end
89
      lines_per_thread = csv.length / option_threads.to_i + 1
90
      splits = []
91

    
92
      option_threads.to_i.times do |current_thread|
93
        start_index = ((current_thread) * lines_per_thread).to_i
94
        finish_index = ((current_thread + 1) * lines_per_thread).to_i
95
        finish_index = csv.length if finish_index > csv.length
96
        if start_index <= finish_index
97
          lines = csv[start_index...finish_index].clone
98
          splits << Thread.new do
99
            lines.each do |line|
100
              if line[NAME][0] != '#'
101
                yield line
102
              end
103
            end
104
          end
105
        end
106
      end
107

    
108
      splits.each do |thread|
109
        thread.join
110
      end
111
    end
112

    
113
    def hammer_context
114
      {
115
        :interactive => false,
116
        :username => 'admin', # TODO: this needs to come from config/settings
117
        :password => 'changeme' # TODO: this needs to come from config/settings
118
      }
119
    end
120

    
121
    def hammer(context = nil)
122
      HammerCLI::MainCommand.new('', context || hammer_context)
123
    end
124

    
125
    def foreman_organization(options = {})
126
      @organizations ||= {}
127

    
128
      if options[:name]
129
        return nil if options[:name].nil? || options[:name].empty?
130
        options[:id] = @organizations[options[:name]]
131
        if !options[:id]
132
          organization = @api.resource(:organizations).call(:index, {
133
                                                              :per_page => 999999,
134
                                                              'search' => "name=\"#{options[:name]}\""
135
                                                            })['results']
136
          raise "Organization '#{options[:name]}' not found" if !organization || organization.empty?
137
          options[:id] = organization[0]['id']
138
          @organizations[options[:name]] = options[:id]
139
        end
140
        result = options[:id]
141
      else
142
        return nil if options[:id].nil?
143
        options[:name] = @organizations.key(options[:id])
144
        if !options[:name]
145
          organization = @api.resource(:organizations).call(:show, {'id' => options[:id]})
146
          raise "Organization 'id=#{options[:id]}' not found" if !organization || organization.empty?
147
          options[:name] = organization['name']
148
          @organizations[options[:name]] = options[:id]
149
        end
150
        result = options[:name]
151
      end
152

    
153
      result
154
    end
155

    
156
    def foreman_location(options = {})
157
      @locations ||= {}
158

    
159
      if options[:name]
160
        return nil if options[:name].nil? || options[:name].empty?
161
        options[:id] = @locations[options[:name]]
162
        if !options[:id]
163
          location = @api.resource(:locations).call(:index, {
164
                                                      :per_page => 999999,
165
                                                      'search' => "name=\"#{options[:name]}\""
166
                                                    })['results']
167
          raise "Location '#{options[:name]}' not found" if !location || location.empty?
168
          options[:id] = location[0]['id']
169
          @locations[options[:name]] = options[:id]
170
        end
171
        result = options[:id]
172
      else
173
        return nil if options[:id].nil?
174
        options[:name] = @locations.key(options[:id])
175
        if !options[:name]
176
          location = @api.resource(:locations).call(:show, {'id' => options[:id]})
177
          raise "Location 'id=#{options[:id]}' not found" if !location || location.empty?
178
          options[:name] = location['name']
179
          @locations[options[:name]] = options[:id]
180
        end
181
        result = options[:name]
182
      end
183

    
184
      result
185
    end
186

    
187
    def foreman_role(options = {})
188
      @roles ||= {}
189

    
190
      if options[:name]
191
        return nil if options[:name].nil? || options[:name].empty?
192
        options[:id] = @roles[options[:name]]
193
        if !options[:id]
194
          role = @api.resource(:roles).call(:index, {
195
                                              :per_page => 999999,
196
                                              'search' => "name=\"#{options[:name]}\""
197
                                            })['results']
198
          raise "Role '#{options[:name]}' not found" if !role || role.empty?
199
          options[:id] = role[0]['id']
200
          @roles[options[:name]] = options[:id]
201
        end
202
        result = options[:id]
203
      else
204
        return nil if options[:id].nil?
205
        options[:name] = @roles.key(options[:id])
206
        if !options[:name]
207
          role = @api.resource(:roles).call(:show, {'id' => options[:id]})
208
          raise "Role 'id=#{options[:id]}' not found" if !role || role.empty?
209
          options[:name] = role['name']
210
          @roles[options[:name]] = options[:id]
211
        end
212
        result = options[:name]
213
      end
214

    
215
      result
216
    end
217

    
218
    def foreman_permission(options = {})
219
      @permissions ||= {}
220

    
221
      if options[:name]
222
        return nil if options[:name].nil? || options[:name].empty?
223
        options[:id] = @permissions[options[:name]]
224
        if !options[:id]
225
          permission = @api.resource(:permissions).call(:index, {
226
                                                          :per_page => 999999,
227
                                                          'name' => options[:name]
228
                                                        })['results']
229
          raise "Permission '#{options[:name]}' not found" if !permission || permission.empty?
230
          options[:id] = permission[0]['id']
231
          @permissions[options[:name]] = options[:id]
232
        end
233
        result = options[:id]
234
      else
235
        return nil if options[:id].nil?
236
        options[:name] = @permissions.key(options[:id])
237
        if !options[:name]
238
          permission = @api.resource(:permissions).call(:show, {'id' => options[:id]})
239
          raise "Permission 'id=#{options[:id]}' not found" if !permission || permission.empty?
240
          options[:name] = permission['name']
241
          @permissions[options[:name]] = options[:id]
242
        end
243
        result = options[:name]
244
      end
245

    
246
      result
247
    end
248

    
249
    def foreman_filter(role, resource, search)
250
      search = nil if search && search.empty?
251
      filters = @api.resource(:filters).call(:index, {
252
                                               :per_page => 999999,
253
                                               'search' => "role=\"#{role}\""
254
                                             })['results']
255
      filters.each do |filter|
256
        return filter['id'] if filter['resource_type'] == resource && filter['search'] == search
257
      end
258

    
259
      nil
260
    end
261

    
262
    def foreman_environment(options = {})
263
      @environments ||= {}
264

    
265
      if options[:name]
266
        return nil if options[:name].nil? || options[:name].empty?
267
        options[:id] = @environments[options[:name]]
268
        if !options[:id]
269
          environment = @api.resource(:environments).call(:index, {
270
                                                            :per_page => 999999,
271
                                                            'search' => "name=\"#{ options[:name] }\""
272
                                                          })['results']
273
          raise "Puppet environment '#{options[:name]}' not found" if !environment || environment.empty?
274
          options[:id] = environment[0]['id']
275
          @environments[options[:name]] = options[:id]
276
        end
277
        result = options[:id]
278
      else
279
        return nil if options[:id].nil?
280
        options[:name] = @environments.key(options[:id])
281
        if !options[:name]
282
          environment = @api.resource(:environments).call(:show, {'id' => options[:id]})
283
          raise "Puppet environment '#{options[:name]}' not found" if !environment || environment.empty?
284
          options[:name] = environment['name']
285
          @environments[options[:name]] = options[:id]
286
        end
287
        result = options[:name]
288
      end
289

    
290
      result
291
    end
292

    
293
    def foreman_template_kind(options = {})
294
      @template_kinds ||= {}
295

    
296
      if options[:name]
297
        return nil if options[:name].nil? || options[:name].empty?
298
        options[:id] = @template_kinds[options[:name]]
299
        if !options[:id]
300
          template_kind = @api.resource(:template_kinds).call(:index, {
301
                                              :per_page => 999999,
302
                                              'search' => "name=\"#{options[:name]}\""
303
                                            })['results']
304
          raise "Template kind '#{options[:name]}' not found" if !template_kind || template_kind.empty?
305
          options[:id] = template_kind[0]['id']
306
          @template_kinds[options[:name]] = options[:id]
307
        end
308
        result = options[:id]
309
      else
310
        return nil if options[:id].nil?
311
        options[:name] = @template_kinds.key(options[:id])
312
        if !options[:name]
313
          template_kind = @api.resource(:template_kinds).call(:show, {'id' => options[:id]})
314
          raise "Template kind 'id=#{options[:id]}' not found" if !template_kind || template_kind.empty?
315
          options[:name] = template_kind['name']
316
          @template_kinds[options[:name]] = options[:id]
317
        end
318
        result = options[:name]
319
      end
320

    
321
      result
322
    end
323

    
324
    def foreman_operatingsystem(options = {})
325
      @operatingsystems ||= {}
326

    
327
      if options[:name]
328
        return nil if options[:name].nil? || options[:name].empty?
329
        options[:id] = @operatingsystems[options[:name]]
330
        if !options[:id]
331
          (osname, major, minor) = split_os_name(options[:name])
332
          search = "name=\"#{osname}\" and major=\"#{major}\" and minor=\"#{minor}\""
333
          operatingsystems = @api.resource(:operatingsystems).call(:index, {
334
                                                                     :per_page => 999999,
335
                                                                     'search' => search
336
                                                                   })['results']
337
          operatingsystem = operatingsystems[0]
338
          raise "Operating system '#{options[:name]}' not found" if !operatingsystem || operatingsystem.empty?
339
          options[:id] = operatingsystem['id']
340
          @operatingsystems[options[:name]] = options[:id]
341
        end
342
        result = options[:id]
343
      else
344
        return nil if options[:id].nil?
345
        options[:name] = @operatingsystems.key(options[:id])
346
        if !options[:name]
347
          operatingsystem = @api.resource(:operatingsystems).call(:show, {'id' => options[:id]})
348
          raise "Operating system 'id=#{options[:id]}' not found" if !operatingsystem || operatingsystem.empty?
349
          options[:name] = build_os_name(operatingsystem['name'],
350
                                         operatingsystem['major'],
351
                                         operatingsystem['minor'])
352
          @operatingsystems[options[:name]] = options[:id]
353
        end
354
        result = options[:name]
355
      end
356

    
357
      result
358
    end
359

    
360
    def foreman_architecture(options = {})
361
      @architectures ||= {}
362

    
363
      if options[:name]
364
        return nil if options[:name].nil? || options[:name].empty?
365
        options[:id] = @architectures[options[:name]]
366
        if !options[:id]
367
          architecture = @api.resource(:architectures).call(:index, {
368
                                                              :per_page => 999999,
369
                                                              'search' => "name=\"#{options[:name]}\""
370
                                                            })['results']
371
          raise "Architecture '#{options[:name]}' not found" if !architecture || architecture.empty?
372
          options[:id] = architecture[0]['id']
373
          @architectures[options[:name]] = options[:id]
374
        end
375
        result = options[:id]
376
      else
377
        return nil if options[:id].nil?
378
        options[:name] = @architectures.key(options[:id])
379
        if !options[:name]
380
          architecture = @api.resource(:architectures).call(:show, {'id' => options[:id]})
381
          raise "Architecture 'id=#{options[:id]}' not found" if !architecture || architecture.empty?
382
          options[:name] = architecture['name']
383
          @architectures[options[:name]] = options[:id]
384
        end
385
        result = options[:name]
386
      end
387

    
388
      result
389
    end
390

    
391
    def foreman_domain(options = {})
392
      @domains ||= {}
393

    
394
      if options[:name]
395
        return nil if options[:name].nil? || options[:name].empty?
396
        options[:id] = @domains[options[:name]]
397
        if !options[:id]
398
          domain = @api.resource(:domains).call(:index, {
399
                                                  :per_page => 999999,
400
                                                  'search' => "name=\"#{options[:name]}\""
401
                                                })['results']
402
          raise "Domain '#{options[:name]}' not found" if !domain || domain.empty?
403
          options[:id] = domain[0]['id']
404
          @domains[options[:name]] = options[:id]
405
        end
406
        result = options[:id]
407
      else
408
        return nil if options[:id].nil?
409
        options[:name] = @domains.key(options[:id])
410
        if !options[:name]
411
          domain = @api.resource(:domains).call(:show, {'id' => options[:id]})
412
          raise "Domain 'id=#{options[:id]}' not found" if !domain || domain.empty?
413
          options[:name] = domain['name']
414
          @domains[options[:name]] = options[:id]
415
        end
416
        result = options[:name]
417
      end
418

    
419
      result
420
    end
421

    
422
    def foreman_partitiontable(options = {})
423
      @ptables ||= {}
424

    
425
      if options[:name]
426
        return nil if options[:name].nil? || options[:name].empty?
427
        options[:id] = @ptables[options[:name]]
428
        if !options[:id]
429
          ptable = @api.resource(:ptables).call(:index, {
430
                                                  :per_page => 999999,
431
                                                  'search' => "name=\"#{options[:name]}\""
432
                                                })['results']
433
          raise "Partition table '#{options[:name]}' not found" if !ptable || ptable.empty?
434
          options[:id] = ptable[0]['id']
435
          @ptables[options[:name]] = options[:id]
436
        end
437
        result = options[:id]
438
      elsif options[:id]
439
        return nil if options[:id].nil?
440
        options[:name] = @ptables.key(options[:id])
441
        if !options[:name]
442
          ptable = @api.resource(:ptables).call(:show, {'id' => options[:id]})
443
          options[:name] = ptable['name']
444
          @ptables[options[:name]] = options[:id]
445
        end
446
        result = options[:name]
447
      elsif !options[:name] && !options[:id]
448
        result = ''
449
      end
450

    
451
      result
452
    end
453

    
454
    def lifecycle_environment(organization, options = {})
455
      @lifecycle_environments ||= {}
456
      @lifecycle_environments[organization] ||= {
457
      }
458

    
459
      if options[:name]
460
        return nil if options[:name].nil? || options[:name].empty?
461
        options[:id] = @lifecycle_environments[organization][options[:name]]
462
        if !options[:id]
463
          @api.resource(:lifecycle_environments)\
464
            .call(:index, {
465
                    :per_page => 999999,
466
                    'organization_id' => foreman_organization(:name => organization)
467
                  })['results'].each do |environment|
468
            @lifecycle_environments[organization][environment['name']] = environment['id']
469
          end
470
          options[:id] = @lifecycle_environments[organization][options[:name]]
471
          raise "Lifecycle environment '#{options[:name]}' not found" if !options[:id]
472
        end
473
        result = options[:id]
474
      else
475
        return nil if options[:id].nil?
476
        options[:name] = @lifecycle_environments.key(options[:id])
477
        if !options[:name]
478
          environment = @api.resource(:lifecycle_environments).call(:show, {'id' => options[:id]})
479
          raise "Lifecycle environment '#{options[:name]}' not found" if !environment || environment.empty?
480
          options[:name] = environment['name']
481
          @lifecycle_environments[options[:name]] = options[:id]
482
        end
483
        result = options[:name]
484
      end
485

    
486
      result
487
    end
488

    
489
    def katello_contentview(organization, options = {})
490
      @contentviews ||= {}
491
      @contentviews[organization] ||= {}
492

    
493
      if options[:name]
494
        return nil if options[:name].nil? || options[:name].empty?
495
        options[:id] = @contentviews[organization][options[:name]]
496
        if !options[:id]
497
          @api.resource(:content_views).call(:index, {
498
                                               :per_page => 999999,
499
                                               'organization_id' => foreman_organization(:name => organization)
500
                                             })['results'].each do |contentview|
501
            @contentviews[organization][contentview['name']] = contentview['id']
502
          end
503
          options[:id] = @contentviews[organization][options[:name]]
504
          raise "Content view '#{options[:name]}' not found" if !options[:id]
505
        end
506
        result = options[:id]
507
      else
508
        return nil if options[:id].nil?
509
        options[:name] = @contentviews.key(options[:id])
510
        if !options[:name]
511
          contentview = @api.resource(:content_views).call(:show, {'id' => options[:id]})
512
          raise "Puppet contentview '#{options[:name]}' not found" if !contentview || contentview.empty?
513
          options[:name] = contentview['name']
514
          @contentviews[options[:name]] = options[:id]
515
        end
516
        result = options[:name]
517
      end
518

    
519
      result
520
    end
521

    
522
    def katello_repository(organization, options = {})
523
      @repositories ||= {}
524
      @repositories[organization] ||= {}
525

    
526
      if options[:name]
527
        return nil if options[:name].nil? || options[:name].empty?
528
        options[:id] = @repositories[organization][options[:name]]
529
        if !options[:id]
530
          @api.resource(:repositories)\
531
            .call(:index, {
532
                    :per_page => 999999,
533
                    'organization_id' => foreman_organization(:name => organization)
534
                  })['results'].each do |repository|
535
            @repositories[organization][repository['name']] = repository['id']
536
          end
537
          options[:id] = @repositories[organization][options[:name]]
538
          raise "Repository '#{options[:name]}' not found" if !options[:id]
539
        end
540
        result = options[:id]
541
      else
542
        return nil if options[:id].nil?
543
        options[:name] = @repositories.key(options[:id])
544
        if !options[:name]
545
          repository = @api.resource(:repositories).call(:show, {'id' => options[:id]})
546
          raise "Puppet repository '#{options[:name]}' not found" if !repository || repository.empty?
547
          options[:name] = repository['name']
548
          @repositoriesr[options[:name]] = options[:id]
549
        end
550
        result = options[:name]
551
      end
552

    
553
      result
554
    end
555

    
556
    def katello_subscription(organization, options = {})
557
      @subscriptions ||= {}
558
      @subscriptions[organization] ||= {}
559

    
560
      if options[:name]
561
        return nil if options[:name].nil? || options[:name].empty?
562
        options[:id] = @subscriptions[organization][options[:name]]
563
        if !options[:id]
564
          results = @api.resource(:subscriptions).call(:index, {
565
                                                         :per_page => 999999,
566
                                                         'organization_id' => foreman_organization(:name => organization),
567
                                                         'search' => "name:\"#{options[:name]}\""
568
                                                       })
569
          raise "No subscriptions match '#{options[:name]}'" if results['subtotal'] == 0
570
          raise "Too many subscriptions match '#{options[:name]}'" if results['subtotal'] > 1
571
          subscription = results['results'][0]
572
          @subscriptions[organization][options[:name]] = subscription['id']
573
          options[:id] = @subscriptions[organization][options[:name]]
574
          raise "Subscription '#{options[:name]}' not found" if !options[:id]
575
        end
576
        result = options[:id]
577
      else
578
        return nil if options[:id].nil?
579
        options[:name] = @subscriptions.key(options[:id])
580
        if !options[:name]
581
          subscription = @api.resource(:subscriptions).call(:show, {'id' => options[:id]})
582
          raise "Subscription '#{options[:name]}' not found" if !subscription || subscription.empty?
583
          options[:name] = subscription['name']
584
          @subscriptions[options[:name]] = options[:id]
585
        end
586
        result = options[:name]
587
      end
588

    
589
      result
590
    end
591

    
592
    def katello_hostcollection(organization, options = {})
593
      @hostcollections ||= {}
594
      @hostcollections[organization] ||= {}
595

    
596
      if options[:name]
597
        return nil if options[:name].nil? || options[:name].empty?
598
        options[:id] = @hostcollections[organization][options[:name]]
599
        if !options[:id]
600
          @api.resource(:host_collections).call(:index,
601
                  {
602
                    :per_page => 999999,
603
                    'organization_id' => foreman_organization(:name => organization),
604
                    'search' => "name:\"#{options[:name]}\""
605
                  })['results'].each do |hostcollection|
606
            @hostcollections[organization][hostcollection['name']] = hostcollection['id'] if hostcollection
607
          end
608
          options[:id] = @hostcollections[organization][options[:name]]
609
          raise "System group '#{options[:name]}' not found" if !options[:id]
610
        end
611
        result = options[:id]
612
      else
613
        return nil if options[:id].nil?
614
        options[:name] = @hostcollections.key(options[:id])
615
        if !options[:name]
616
          hostcollection = @api.resource(:host_collections).call(:show, {'id' => options[:id]})
617
          raise "System group '#{options[:name]}' not found" if !hostcollection || hostcollection.empty?
618
          options[:name] = hostcollection['name']
619
          @hostcollections[options[:name]] = options[:id]
620
        end
621
        result = options[:name]
622
      end
623

    
624
      result
625
    end
626

    
627
    def build_os_name(name, major, minor)
628
      name += " #{major}" if major && major != ''
629
      name += ".#{minor}" if minor && minor != ''
630
      name
631
    end
632

    
633
    # "Red Hat 6.4" => "Red Hat", "6", "4"
634
    # "Red Hat 6"   => "Red Hat", "6", ''
635
    def split_os_name(name)
636
      tokens = name.split(' ')
637
      is_number = Float(tokens[-1]) rescue false
638
      if is_number
639
        (major, minor) = tokens[-1].split('.').flatten
640
        name = tokens[0...-1].join(' ')
641
      else
642
        name = tokens.join(' ')
643
      end
644
      [name, major || '', minor || '']
645
    end
646

    
647
    def export_column(object, name, field)
648
      return '' unless object[name]
649
      values = CSV.generate do |column|
650
        column << object[name].collect do |fields|
651
          fields[field]
652
        end
653
      end
654
      values.delete!("\n")
655
    end
656

    
657
    def collect_column(column)
658
      return [] if column.nil? || column.empty?
659
      CSV.parse_line(column, {:skip_blanks => true}).collect do |value|
660
        yield value
661
      end
662
    end
663

    
664
    def pluralize(name)
665
      case name
666
      when /smart_proxy/
667
        'smart_proxies'
668
      else
669
        "#{name}s"
670
      end
671
    end
672

    
673
    def associate_organizations(id, organizations, name)
674
      return if organizations.nil?
675

    
676
      associations ||= {}
677
      CSV.parse_line(organizations).each do |organization|
678
        organization_id = foreman_organization(:name => organization)
679
        if associations[organization].nil?
680
          associations[organization] = @api.resource(:organizations).call(:show, {'id' => organization_id})[pluralize(name)].collect do |reference_object|
681
            reference_object['id']
682
          end
683
        end
684
        associations[organization] += [id] if !associations[organization].include? id
685
        @api.resource(:organizations)\
686
          .call(:update, {
687
                  'id' => organization_id,
688
                  'organization' => {
689
                    "#{name}_ids" => associations[organization]
690
                  }
691
                })
692
      end if organizations && !organizations.empty?
693
    end
694

    
695
    def associate_locations(id, locations, name)
696
      return if locations.nil?
697

    
698
      associations ||= {}
699
      CSV.parse_line(locations).each do |location|
700
        location_id = foreman_location(:name => location)
701
        if associations[location].nil?
702
          associations[location] = @api.resource(:locations).call(:show, {'id' => location_id})[pluralize(name)].collect do |reference_object|
703
            reference_object['id']
704
          end
705
        end
706
        associations[location] += [id] if !associations[location].include? id
707

    
708
        @api.resource(:locations)\
709
          .call(:update, {
710
                  'id' => location_id,
711
                  'location' => {
712
                    "#{name}_ids" => associations[location]
713
                  }
714
                })
715
      end if locations && !locations.empty?
716
    end
717
  end
718
end