Class: HammerCLICsv::BaseCommand

Inherits:
HammerCLI::Apipie::Command
  • Object
show all
Defined in:
lib/hammer_cli_csv/base.rb

Constant Summary collapse

NAME =
'Name'
COUNT =
'Count'

Instance Method Summary collapse

Instance Method Details

#associate_locations(id, locations, name) ⇒ Object



683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
# File 'lib/hammer_cli_csv/base.rb', line 683

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

  associations ||= {}
  CSV.parse_line(locations).each do |location|
    location_id = foreman_location(:name => location)
    if associations[location].nil?
      associations[location] = @api.resource(:locations).call(:show, {'id' => location_id})[pluralize(name)].collect do |reference_object|
        reference_object['id']
      end
    end
    associations[location] += [id] if !associations[location].include? id

    @api.resource(:locations)
      .call(:update, {
              'id' => location_id,
              'location' => {
                "#{name}_ids" => associations[location]
              }
            })
  end if locations && !locations.empty?
end

#associate_organizations(id, organizations, name) ⇒ Object



661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
# File 'lib/hammer_cli_csv/base.rb', line 661

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

  associations ||= {}
  CSV.parse_line(organizations).each do |organization|
    organization_id = foreman_organization(:name => organization)
    if associations[organization].nil?
      associations[organization] = @api.resource(:organizations).call(:show, {'id' => organization_id})[pluralize(name)].collect do |reference_object|
        reference_object['id']
      end
    end
    associations[organization] += [id] if !associations[organization].include? id
    @api.resource(:organizations)
      .call(:update, {
              'id' => organization_id,
              'organization' => {
                "#{name}_ids" => associations[organization]
              }
            })
  end if organizations && !organizations.empty?
end

#build_os_name(name, major, minor) ⇒ Object



615
616
617
618
619
# File 'lib/hammer_cli_csv/base.rb', line 615

def build_os_name(name, major, minor)
  name += " #{major}" if major && major != ''
  name += ".#{minor}" if minor && minor != ''
  name
end

#collect_column(column) ⇒ Object



645
646
647
648
649
650
# File 'lib/hammer_cli_csv/base.rb', line 645

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

#executeObject



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/hammer_cli_csv/base.rb', line 32

def execute
  if !option_csv_file
    if option_csv_export?
      # rubocop:disable UselessAssignment
      option_csv_file = '/dev/stdout'
    else
      # rubocop:disable UselessAssignment
      option_csv_file = '/dev/stdin'
    end
  end

  @api = ApipieBindings::API.new({
                                   :uri => option_server || HammerCLI::Settings.get(:csv, :host),
                                   :username => option_username || HammerCLI::Settings.get(:csv, :username),
                                   :password => option_password || HammerCLI::Settings.get(:csv, :password),
                                   :api_version => 2
                                 })

  option_csv_export? ? export : import
  HammerCLI::EX_OK
end

#export_column(object, name, field) ⇒ Object



635
636
637
638
639
640
641
642
643
# File 'lib/hammer_cli_csv/base.rb', line 635

def export_column(object, name, field)
  return '' unless object[name]
  values = CSV.generate do |column|
    column << object[name].collect do |fields|
      fields[field]
    end
  end
  values.delete!("\n")
end

#foreman_architecture(options = {}) ⇒ Object



348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
# File 'lib/hammer_cli_csv/base.rb', line 348

def foreman_architecture(options = {})
  @architectures ||= {}

  if options[:name]
    return nil if options[:name].nil? || options[:name].empty?
    options[:id] = @architectures[options[:name]]
    if !options[:id]
      architecture = @api.resource(:architectures).call(:index, {
                                                          :per_page => 999999,
                                                          'search' => "name=\"#{options[:name]}\""
                                                        })['results']
      raise "Architecture '#{options[:name]}' not found" if !architecture || architecture.empty?
      options[:id] = architecture[0]['id']
      @architectures[options[:name]] = options[:id]
    end
    result = options[:id]
  else
    return nil if options[:id].nil?
    options[:name] = @architectures.key(options[:id])
    if !options[:name]
      architecture = @api.resource(:architectures).call(:show, {'id' => options[:id]})
      raise "Architecture 'id=#{options[:id]}' not found" if !architecture || architecture.empty?
      options[:name] = architecture['name']
      @architectures[options[:name]] = options[:id]
    end
    result = options[:name]
  end

  result
end

#foreman_domain(options = {}) ⇒ Object



379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
# File 'lib/hammer_cli_csv/base.rb', line 379

def foreman_domain(options = {})
  @domains ||= {}

  if options[:name]
    return nil if options[:name].nil? || options[:name].empty?
    options[:id] = @domains[options[:name]]
    if !options[:id]
      domain = @api.resource(:domains).call(:index, {
                                              :per_page => 999999,
                                              'search' => "name=\"#{options[:name]}\""
                                            })['results']
      raise "Domain '#{options[:name]}' not found" if !domain || domain.empty?
      options[:id] = domain[0]['id']
      @domains[options[:name]] = options[:id]
    end
    result = options[:id]
  else
    return nil if options[:id].nil?
    options[:name] = @domains.key(options[:id])
    if !options[:name]
      domain = @api.resource(:domains).call(:show, {'id' => options[:id]})
      raise "Domain 'id=#{options[:id]}' not found" if !domain || domain.empty?
      options[:name] = domain['name']
      @domains[options[:name]] = options[:id]
    end
    result = options[:name]
  end

  result
end

#foreman_environment(options = {}) ⇒ Object



250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
# File 'lib/hammer_cli_csv/base.rb', line 250

def foreman_environment(options = {})
  @environments ||= {}

  if options[:name]
    return nil if options[:name].nil? || options[:name].empty?
    options[:id] = @environments[options[:name]]
    if !options[:id]
      environment = @api.resource(:environments).call(:index, {
                                                        :per_page => 999999,
                                                        'search' => "name=\"#{ options[:name] }\""
                                                      })['results']
      raise "Puppet environment '#{options[:name]}' not found" if !environment || environment.empty?
      options[:id] = environment[0]['id']
      @environments[options[:name]] = options[:id]
    end
    result = options[:id]
  else
    return nil if options[:id].nil?
    options[:name] = @environments.key(options[:id])
    if !options[:name]
      environment = @api.resource(:environments).call(:show, {'id' => options[:id]})
      raise "Puppet environment '#{options[:name]}' not found" if !environment || environment.empty?
      options[:name] = environment['name']
      @environments[options[:name]] = options[:id]
    end
    result = options[:name]
  end

  result
end

#foreman_filter(role, resource, search) ⇒ Object



237
238
239
240
241
242
243
244
245
246
247
248
# File 'lib/hammer_cli_csv/base.rb', line 237

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

  nil
end

#foreman_location(options = {}) ⇒ Object



144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
# File 'lib/hammer_cli_csv/base.rb', line 144

def foreman_location(options = {})
  @locations ||= {}

  if options[:name]
    return nil if options[:name].nil? || options[:name].empty?
    options[:id] = @locations[options[:name]]
    if !options[:id]
      location = @api.resource(:locations).call(:index, {
                                                  :per_page => 999999,
                                                  'search' => "name=\"#{options[:name]}\""
                                                })['results']
      raise "Location '#{options[:name]}' not found" if !location || location.empty?
      options[:id] = location[0]['id']
      @locations[options[:name]] = options[:id]
    end
    result = options[:id]
  else
    return nil if options[:id].nil?
    options[:name] = @locations.key(options[:id])
    if !options[:name]
      location = @api.resource(:locations).call(:show, {'id' => options[:id]})
      raise "Location 'id=#{options[:id]}' not found" if !location || location.empty?
      options[:name] = location['name']
      @locations[options[:name]] = options[:id]
    end
    result = options[:name]
  end

  result
end

#foreman_operatingsystem(options = {}) ⇒ Object



312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
# File 'lib/hammer_cli_csv/base.rb', line 312

def foreman_operatingsystem(options = {})
  @operatingsystems ||= {}

  if options[:name]
    return nil if options[:name].nil? || options[:name].empty?
    options[:id] = @operatingsystems[options[:name]]
    if !options[:id]
      (osname, major, minor) = split_os_name(options[:name])
      search = "name=\"#{osname}\" and major=\"#{major}\" and minor=\"#{minor}\""
      operatingsystems = @api.resource(:operatingsystems).call(:index, {
                                                                 :per_page => 999999,
                                                                 'search' => search
                                                               })['results']
      operatingsystem = operatingsystems[0]
      raise "Operating system '#{options[:name]}' not found" if !operatingsystem || operatingsystem.empty?
      options[:id] = operatingsystem['id']
      @operatingsystems[options[:name]] = options[:id]
    end
    result = options[:id]
  else
    return nil if options[:id].nil?
    options[:name] = @operatingsystems.key(options[:id])
    if !options[:name]
      operatingsystem = @api.resource(:operatingsystems).call(:show, {'id' => options[:id]})
      raise "Operating system 'id=#{options[:id]}' not found" if !operatingsystem || operatingsystem.empty?
      options[:name] = build_os_name(operatingsystem['name'],
                                     operatingsystem['major'],
                                     operatingsystem['minor'])
      @operatingsystems[options[:name]] = options[:id]
    end
    result = options[:name]
  end

  result
end

#foreman_organization(options = {}) ⇒ Object



113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/hammer_cli_csv/base.rb', line 113

def foreman_organization(options = {})
  @organizations ||= {}

  if options[:name]
    return nil if options[:name].nil? || options[:name].empty?
    options[:id] = @organizations[options[:name]]
    if !options[:id]
      organization = @api.resource(:organizations).call(:index, {
                                                          :per_page => 999999,
                                                          'search' => "name=\"#{options[:name]}\""
                                                        })['results']
      raise "Organization '#{options[:name]}' not found" if !organization || organization.empty?
      options[:id] = organization[0]['id']
      @organizations[options[:name]] = options[:id]
    end
    result = options[:id]
  else
    return nil if options[:id].nil?
    options[:name] = @organizations.key(options[:id])
    if !options[:name]
      organization = @api.resource(:organizations).call(:show, {'id' => options[:id]})
      raise "Organization 'id=#{options[:id]}' not found" if !organization || organization.empty?
      options[:name] = organization['name']
      @organizations[options[:name]] = options[:id]
    end
    result = options[:name]
  end

  result
end

#foreman_partitiontable(options = {}) ⇒ Object



410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
# File 'lib/hammer_cli_csv/base.rb', line 410

def foreman_partitiontable(options = {})
  @ptables ||= {}

  if options[:name]
    return nil if options[:name].nil? || options[:name].empty?
    options[:id] = @ptables[options[:name]]
    if !options[:id]
      ptable = @api.resource(:ptables).call(:index, {
                                              :per_page => 999999,
                                              'search' => "name=\"#{options[:name]}\""
                                            })['results']
      raise "Partition table '#{options[:name]}' not found" if !ptable || ptable.empty?
      options[:id] = ptable[0]['id']
      @ptables[options[:name]] = options[:id]
    end
    result = options[:id]
  elsif options[:id]
    return nil if options[:id].nil?
    options[:name] = @ptables.key(options[:id])
    if !options[:name]
      ptable = @api.resource(:ptables).call(:show, {'id' => options[:id]})
      options[:name] = ptable['name']
      @ptables[options[:name]] = options[:id]
    end
    result = options[:name]
  elsif !options[:name] && !options[:id]
    result = ''
  end

  result
end

#foreman_permission(options = {}) ⇒ Object



206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
# File 'lib/hammer_cli_csv/base.rb', line 206

def foreman_permission(options = {})
  @permissions ||= {}

  if options[:name]
    return nil if options[:name].nil? || options[:name].empty?
    options[:id] = @permissions[options[:name]]
    if !options[:id]
      permission = @api.resource(:permissions).call(:index, {
                                                      :per_page => 999999,
                                                      'name' => options[:name]
                                                    })['results']
      raise "Permission '#{options[:name]}' not found" if !permission || permission.empty?
      options[:id] = permission[0]['id']
      @permissions[options[:name]] = options[:id]
    end
    result = options[:id]
  else
    return nil if options[:id].nil?
    options[:name] = @permissions.key(options[:id])
    if !options[:name]
      permission = @api.resource(:permissions).call(:show, {'id' => options[:id]})
      raise "Permission 'id=#{options[:id]}' not found" if !permission || permission.empty?
      options[:name] = permission['name']
      @permissions[options[:name]] = options[:id]
    end
    result = options[:name]
  end

  result
end

#foreman_role(options = {}) ⇒ Object



175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
# File 'lib/hammer_cli_csv/base.rb', line 175

def foreman_role(options = {})
  @roles ||= {}

  if options[:name]
    return nil if options[:name].nil? || options[:name].empty?
    options[:id] = @roles[options[:name]]
    if !options[:id]
      role = @api.resource(:roles).call(:index, {
                                          :per_page => 999999,
                                          'search' => "name=\"#{options[:name]}\""
                                        })['results']
      raise "Role '#{options[:name]}' not found" if !role || role.empty?
      options[:id] = role[0]['id']
      @roles[options[:name]] = options[:id]
    end
    result = options[:id]
  else
    return nil if options[:id].nil?
    options[:name] = @roles.key(options[:id])
    if !options[:name]
      role = @api.resource(:roles).call(:show, {'id' => options[:id]})
      raise "Role 'id=#{options[:id]}' not found" if !role || role.empty?
      options[:name] = role['name']
      @roles[options[:name]] = options[:id]
    end
    result = options[:name]
  end

  result
end

#foreman_template_kind(options = {}) ⇒ Object



281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
# File 'lib/hammer_cli_csv/base.rb', line 281

def foreman_template_kind(options = {})
  @template_kinds ||= {}

  if options[:name]
    return nil if options[:name].nil? || options[:name].empty?
    options[:id] = @template_kinds[options[:name]]
    if !options[:id]
      template_kind = @api.resource(:template_kinds).call(:index, {
                                          :per_page => 999999,
                                          'search' => "name=\"#{options[:name]}\""
                                        })['results']
      raise "Template kind '#{options[:name]}' not found" if !template_kind || template_kind.empty?
      options[:id] = template_kind[0]['id']
      @template_kinds[options[:name]] = options[:id]
    end
    result = options[:id]
  else
    return nil if options[:id].nil?
    options[:name] = @template_kinds.key(options[:id])
    if !options[:name]
      template_kind = @api.resource(:template_kinds).call(:show, {'id' => options[:id]})
      raise "Template kind 'id=#{options[:id]}' not found" if !template_kind || template_kind.empty?
      options[:name] = template_kind['name']
      @template_kinds[options[:name]] = options[:id]
    end
    result = options[:name]
  end

  result
end

#hammer(context = nil) ⇒ Object



109
110
111
# File 'lib/hammer_cli_csv/base.rb', line 109

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

#hammer_contextObject



101
102
103
104
105
106
107
# File 'lib/hammer_cli_csv/base.rb', line 101

def hammer_context
  {
    :interactive => false,
    :username => 'admin', # TODO: this needs to come from config/settings
    :password => 'changeme' # TODO: this needs to come from config/settings
  }
end

#katello_contentview(organization, options = {}) ⇒ Object



477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
# File 'lib/hammer_cli_csv/base.rb', line 477

def katello_contentview(organization, options = {})
  @contentviews ||= {}
  @contentviews[organization] ||= {}

  if options[:name]
    return nil if options[:name].nil? || options[:name].empty?
    options[:id] = @contentviews[organization][options[:name]]
    if !options[:id]
      @api.resource(:content_views).call(:index, {
                                           :per_page => 999999,
                                           'organization_id' => foreman_organization(:name => organization)
                                         })['results'].each do |contentview|
        @contentviews[organization][contentview['name']] = contentview['id']
      end
      options[:id] = @contentviews[organization][options[:name]]
      raise "Content view '#{options[:name]}' not found" if !options[:id]
    end
    result = options[:id]
  else
    return nil if options[:id].nil?
    options[:name] = @contentviews.key(options[:id])
    if !options[:name]
      contentview = @api.resource(:content_views).call(:show, {'id' => options[:id]})
      raise "Puppet contentview '#{options[:name]}' not found" if !contentview || contentview.empty?
      options[:name] = contentview['name']
      @contentviews[options[:name]] = options[:id]
    end
    result = options[:name]
  end

  result
end

#katello_hostcollection(organization, options = {}) ⇒ Object



580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
# File 'lib/hammer_cli_csv/base.rb', line 580

def katello_hostcollection(organization, options = {})
  @hostcollections ||= {}
  @hostcollections[organization] ||= {}

  if options[:name]
    return nil if options[:name].nil? || options[:name].empty?
    options[:id] = @hostcollections[organization][options[:name]]
    if !options[:id]
      @api.resource(:host_collections).call(:index,
              {
                :per_page => 999999,
                'organization_id' => foreman_organization(:name => organization),
                'search' => "name:\"#{options[:name]}\""
              })['results'].each do |hostcollection|
        @hostcollections[organization][hostcollection['name']] = hostcollection['id'] if hostcollection
      end
      options[:id] = @hostcollections[organization][options[:name]]
      raise "System group '#{options[:name]}' not found" if !options[:id]
    end
    result = options[:id]
  else
    return nil if options[:id].nil?
    options[:name] = @hostcollections.key(options[:id])
    if !options[:name]
      hostcollection = @api.resource(:host_collections).call(:show, {'id' => options[:id]})
      raise "System group '#{options[:name]}' not found" if !hostcollection || hostcollection.empty?
      options[:name] = hostcollection['name']
      @hostcollections[options[:name]] = options[:id]
    end
    result = options[:name]
  end

  result
end

#katello_repository(organization, options = {}) ⇒ Object



510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
# File 'lib/hammer_cli_csv/base.rb', line 510

def katello_repository(organization, options = {})
  @repositories ||= {}
  @repositories[organization] ||= {}

  if options[:name]
    return nil if options[:name].nil? || options[:name].empty?
    options[:id] = @repositories[organization][options[:name]]
    if !options[:id]
      @api.resource(:repositories)
        .call(:index, {
                :per_page => 999999,
                'organization_id' => foreman_organization(:name => organization)
              })['results'].each do |repository|
        @repositories[organization][repository['name']] = repository['id']
      end
      options[:id] = @repositories[organization][options[:name]]
      raise "Repository '#{options[:name]}' not found" if !options[:id]
    end
    result = options[:id]
  else
    return nil if options[:id].nil?
    options[:name] = @repositories.key(options[:id])
    if !options[:name]
      repository = @api.resource(:repositories).call(:show, {'id' => options[:id]})
      raise "Puppet repository '#{options[:name]}' not found" if !repository || repository.empty?
      options[:name] = repository['name']
      @repositoriesr[options[:name]] = options[:id]
    end
    result = options[:name]
  end

  result
end

#katello_subscription(organization, options = {}) ⇒ Object



544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
# File 'lib/hammer_cli_csv/base.rb', line 544

def katello_subscription(organization, options = {})
  @subscriptions ||= {}
  @subscriptions[organization] ||= {}

  if options[:name]
    return nil if options[:name].nil? || options[:name].empty?
    options[:id] = @subscriptions[organization][options[:name]]
    if !options[:id]
      results = @api.resource(:subscriptions).call(:index, {
                                                     :per_page => 999999,
                                                     'organization_id' => foreman_organization(:name => organization),
                                                     'search' => "name:\"#{options[:name]}\""
                                                   })
      raise "No subscriptions match '#{options[:name]}'" if results['subtotal'] == 0
      raise "Too many subscriptions match '#{options[:name]}'" if results['subtotal'] > 1
      subscription = results['results'][0]
      @subscriptions[organization][options[:name]] = subscription['id']
      options[:id] = @subscriptions[organization][options[:name]]
      raise "Subscription '#{options[:name]}' not found" if !options[:id]
    end
    result = options[:id]
  else
    return nil if options[:id].nil?
    options[:name] = @subscriptions.key(options[:id])
    if !options[:name]
      subscription = @api.resource(:subscriptions).call(:show, {'id' => options[:id]})
      raise "Subscription '#{options[:name]}' not found" if !subscription || subscription.empty?
      options[:name] = subscription['name']
      @subscriptions[options[:name]] = options[:id]
    end
    result = options[:name]
  end

  result
end

#labelize(name) ⇒ Object



64
65
66
# File 'lib/hammer_cli_csv/base.rb', line 64

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

#lifecycle_environment(organization, options = {}) ⇒ Object



442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
# File 'lib/hammer_cli_csv/base.rb', line 442

def lifecycle_environment(organization, options = {})
  @lifecycle_environments ||= {}
  @lifecycle_environments[organization] ||= {
  }

  if options[:name]
    return nil if options[:name].nil? || options[:name].empty?
    options[:id] = @lifecycle_environments[organization][options[:name]]
    if !options[:id]
      @api.resource(:lifecycle_environments)
        .call(:index, {
                :per_page => 999999,
                'organization_id' => foreman_organization(:name => organization)
              })['results'].each do |environment|
        @lifecycle_environments[organization][environment['name']] = environment['id']
      end
      options[:id] = @lifecycle_environments[organization][options[:name]]
      raise "Lifecycle environment '#{options[:name]}' not found" if !options[:id]
    end
    result = options[:id]
  else
    return nil if options[:id].nil?
    options[:name] = @lifecycle_environments.key(options[:id])
    if !options[:name]
      environment = @api.resource(:lifecycle_environments).call(:show, {'id' => options[:id]})
      raise "Lifecycle environment '#{options[:name]}' not found" if !environment || environment.empty?
      options[:name] = environment['name']
      @lifecycle_environments[options[:name]] = options[:id]
    end
    result = options[:name]
  end

  result
end

#namify(name_format, number = 0) ⇒ Object



54
55
56
57
58
59
60
61
62
# File 'lib/hammer_cli_csv/base.rb', line 54

def namify(name_format, number = 0)
  if name_format.index('%')
    name = name_format % number
  else
    name = name_format
  end
  name = "#{option_prefix}#{name}" if option_prefix
  name
end

#pluralize(name) ⇒ Object



652
653
654
655
656
657
658
659
# File 'lib/hammer_cli_csv/base.rb', line 652

def pluralize(name)
  case name
  when /smart_proxy/
    'smart_proxies'
  else
    "#{name}s"
  end
end

#split_os_name(name) ⇒ Object

“Red Hat 6.4” => “Red Hat”, “6”, “4” “Red Hat 6” => “Red Hat”, “6”, ”



623
624
625
626
627
628
629
630
631
632
633
# File 'lib/hammer_cli_csv/base.rb', line 623

def split_os_name(name)
  tokens = name.split(' ')
  is_number = Float(tokens[-1]) rescue false
  if is_number
    (major, minor) = tokens[-1].split('.').flatten
    name = tokens[0...-1].join(' ')
  else
    name = tokens.join(' ')
  end
  [name, major || '', minor || '']
end

#thread_import(return_headers = false) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/hammer_cli_csv/base.rb', line 68

def thread_import(return_headers = false)
  csv = []
  CSV.foreach(option_csv_file || '/dev/stdin', {
                                                 :skip_blanks => true,
                                                 :headers => :first_row,
                                                 :return_headers => return_headers
                                               }) do |line|
    csv << line
  end
  lines_per_thread = csv.length / option_threads.to_i + 1
  splits = []

  option_threads.to_i.times do |current_thread|
    start_index = ((current_thread) * lines_per_thread).to_i
    finish_index = ((current_thread + 1) * lines_per_thread).to_i
    finish_index = csv.length if finish_index > csv.length
    if start_index <= finish_index
      lines = csv[start_index...finish_index].clone
      splits << Thread.new do
        lines.each do |line|
          if line[NAME][0] != '#'
            yield line
          end
        end
      end
    end
  end

  splits.each do |thread|
    thread.join
  end
end