Class: HammerCLICsv::BaseCommand

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

Constant Summary collapse

NAME =
'Name'
COUNT =
'Count'

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Utils::Config

#api_connection

Class Method Details

.help_columnsObject



47
48
49
# File 'lib/hammer_cli_csv/base.rb', line 47

def self.help_columns
  ''
end

.supported?Boolean

Returns:

  • (Boolean)


34
35
36
# File 'lib/hammer_cli_csv/base.rb', line 34

def self.supported?
  false
end

Instance Method Details

#apipie_check_param(resource, action, name) ⇒ Object



983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
# File 'lib/hammer_cli_csv/base.rb', line 983

def apipie_check_param(resource, action, name)
  method = @api.resource(pluralize(resource).to_sym).apidoc[:methods].detect do |api_method|
    api_method[:name] == action.to_s
  end
  return false unless method

  found = method[:params].detect do |param|
    param[:full_name] == name
  end
  if !found
    nested =  method[:params].detect do |param|
      param[:name] == resource.to_s
    end
    if nested
      found = nested[:params].detect do |param|
        param[:full_name] == name
      end
    end
  end
  found
end

#associate_locations(id, locations, name) ⇒ Object



961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
# File 'lib/hammer_cli_csv/base.rb', line 961

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



940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
# File 'lib/hammer_cli_csv/base.rb', line 940

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

#authenticate_request(request) ⇒ Object



71
72
73
74
75
76
77
78
79
80
81
# File 'lib/hammer_cli_csv/base.rb', line 71

def authenticate_request(request)
  if HammerCLI.context[:api_connection]
    HammerCLIForeman.foreman_api_connection.authenticator.authenticate(request, {})
  else
    request.basic_auth(
      HammerCLIForeman.credentials.username,
      HammerCLIForeman.credentials.password
    )
  end
  request
end

#build_os_name(name, major, minor) ⇒ Object



894
895
896
897
898
# File 'lib/hammer_cli_csv/base.rb', line 894

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

#check_server_status(server) ⇒ Object



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/hammer_cli_csv/base.rb', line 83

def check_server_status(server)
  url = "#{server}/api/status"
  uri = URI(url)
  nethttp = Net::HTTP.new(uri.host, uri.port)
  nethttp.use_ssl = uri.scheme == 'https'
  nethttp.verify_mode = OpenSSL::SSL::VERIFY_NONE
  server_status = nethttp.start do |http|
    request = Net::HTTP::Get.new uri
    authenticate_request(request)
    response = http.request(request)
    JSON.parse(response.body)
  end

  url = "#{server}/api/v2/plugins"
  uri = URI(url)
  nethttp = Net::HTTP.new(uri.host, uri.port)
  nethttp.use_ssl = uri.scheme == 'https'
  nethttp.verify_mode = OpenSSL::SSL::VERIFY_NONE
  server_status['plugins'] = nethttp.start do |http|
    request = Net::HTTP::Get.new uri
    authenticate_request(request)
    response = http.request(request)
    JSON.parse(response.body)['results']
  end

  server_status
end

#collect_column(column) ⇒ Object



924
925
926
927
928
929
# File 'lib/hammer_cli_csv/base.rb', line 924

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

#count(value) ⇒ Object



1005
1006
1007
1008
# File 'lib/hammer_cli_csv/base.rb', line 1005

def count(value)
  return 1 if value.nil? || value.empty?
  value.to_i
end

#executeObject



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/hammer_cli_csv/base.rb', line 51

def execute
  @api = api_connection
  @server_status = check_server_status(@server)

  if option_export?
    if option_file
      CSV.open(option_file, 'wb', {:force_quotes => false}) do |csv|
        export csv
      end
    else
      CSV do |csv|
        export csv
      end
    end
  else
    import
  end
  HammerCLI::EX_OK
end

#export_column(object, name, field = nil) ⇒ Object



914
915
916
917
918
919
920
921
922
# File 'lib/hammer_cli_csv/base.rb', line 914

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

#foreman_architecture(options = {}) ⇒ Object



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
441
442
# File 'lib/hammer_cli_csv/base.rb', line 413

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 '%{name}' not found") % {:name => options[:name]} 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=%{id}' not found") % {:id => options[:id]} if !architecture || architecture.empty?
      options[:name] = architecture['name']
      @architectures[options[:name]] = options[:id]
    end
    result = options[:name]
  end

  result
end

#foreman_container(options = {}) ⇒ Object



862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
# File 'lib/hammer_cli_csv/base.rb', line 862

def foreman_container(options = {})
  @containers ||= {}

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

  result
end

#foreman_domain(options = {}) ⇒ Object



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
# File 'lib/hammer_cli_csv/base.rb', line 444

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 '%{name}' not found") % {:name => options[:name]} 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=%{id}' not found") % {:id => options[:id]} if !domain || domain.empty?
      options[:name] = domain['name']
      @domains[options[:name]] = options[:id]
    end
    result = options[:name]
  end

  result
end

#foreman_environment(options = {}) ⇒ Object



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
# File 'lib/hammer_cli_csv/base.rb', line 315

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 '%{name}' not found") % {:name => options[:name]} 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 'id=%{id}' not found") % {:id => options[:id]} 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



301
302
303
304
305
306
307
308
309
310
311
312
313
# File 'lib/hammer_cli_csv/base.rb', line 301

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|
    resource_type = (filter['resource_type'] || '').split(':')[-1] # To remove "Katello::" when present
    return filter['id'] if resource_type == resource && filter['search'] == search
  end

  nil
end

#foreman_host(options = {}) ⇒ Object



539
540
541
542
543
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
# File 'lib/hammer_cli_csv/base.rb', line 539

def foreman_host(options = {})
  @query_hosts ||= {}

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

  result
end

#foreman_hostgroup(options = {}) ⇒ Object



570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
# File 'lib/hammer_cli_csv/base.rb', line 570

def foreman_hostgroup(options = {})
  @query_hostgroups ||= {}

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

  result
end

#foreman_location(options = {}) ⇒ Object



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
236
237
# File 'lib/hammer_cli_csv/base.rb', line 208

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 '%{name}' not found") % {:name => options[:name]} 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=%{id}' not found") % {:id => options[:id]} if !location || location.empty?
      options[:name] = location['name']
      @locations[options[:name]] = options[:id]
    end
    result = options[:name]
  end

  result
end

#foreman_medium(options = {}) ⇒ Object



507
508
509
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
# File 'lib/hammer_cli_csv/base.rb', line 507

def foreman_medium(options = {})
  @media ||= {}

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

  result
end

#foreman_operatingsystem(options = {}) ⇒ Object



377
378
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
409
410
411
# File 'lib/hammer_cli_csv/base.rb', line 377

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 '%{name}' not found") % {:name => options[:name]} 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=%{id}' not found") % {:id => options[:id]} 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



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
205
206
# File 'lib/hammer_cli_csv/base.rb', line 177

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 '%{name}' not found") % {:name => options[:name]} 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=%{id}' not found") % {:id => options[:id]} if !organization || organization.empty?
      options[:name] = organization['name']
      @organizations[options[:name]] = options[:id]
    end
    result = options[:name]
  end

  result
end

#foreman_partitiontable(options = {}) ⇒ Object



475
476
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
# File 'lib/hammer_cli_csv/base.rb', line 475

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 '%{name}' not found") % {:name => options[:name]} 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



270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
# File 'lib/hammer_cli_csv/base.rb', line 270

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 '%{name}' not found") % {:name => options[:name]} 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=%{id}' not found") % {:id => options[:id]} if !permission || permission.empty?
      options[:name] = permission['name']
      @permissions[options[:name]] = options[:id]
    end
    result = options[:name]
  end

  result
end

#foreman_provisioning_template(options = {}) ⇒ Object



601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
# File 'lib/hammer_cli_csv/base.rb', line 601

def foreman_provisioning_template(options = {})
  @query_config_templates ||= {}

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

  result
end

#foreman_role(options = {}) ⇒ Object



239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
# File 'lib/hammer_cli_csv/base.rb', line 239

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 '%{name}' not found") % {:name => options[:name]} 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=%{id}' not found") % {:id => options[:id]} if !role || role.empty?
      options[:name] = role['name']
      @roles[options[:name]] = options[:id]
    end
    result = options[:name]
  end

  result
end

#foreman_smart_proxy(options = {}) ⇒ Object



632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
# File 'lib/hammer_cli_csv/base.rb', line 632

def foreman_smart_proxy(options = {})
  @query_smart_proxies ||= {}

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

  result
end

#foreman_template_kind(options = {}) ⇒ Object



346
347
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
# File 'lib/hammer_cli_csv/base.rb', line 346

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 '%{name}' not found") % {:name => options[:name]} 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=%{id}' not found") % {:id => options[:id]} 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



173
174
175
# File 'lib/hammer_cli_csv/base.rb', line 173

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

#hammer_contextObject



165
166
167
168
169
170
171
# File 'lib/hammer_cli_csv/base.rb', line 165

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

#helpObject



42
43
44
45
# File 'lib/hammer_cli_csv/base.rb', line 42

def help
  print_message _('**** This command is unsupported and is provided as tech preview. ****') unless supported?
  super + self.class.help_columns
end

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



697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
# File 'lib/hammer_cli_csv/base.rb', line 697

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 '%{name}' not found") % {:name => options[:name]} 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 'id=%{id}' not found") % {:id => options[:id]} if !contentview || contentview.empty?
      options[:name] = contentview['name']
      @contentviews[options[:name]] = options[:id]
    end
    result = options[:name]
  end

  result
end

#katello_contentviewversion(organization, name, version = 'latest') ⇒ Object



730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
# File 'lib/hammer_cli_csv/base.rb', line 730

def katello_contentviewversion(organization, name, version='latest')
  @contentviewversions ||= {}
  @contentviewversions[organization] ||= {}
  versionname = "#{version}|#{name}"

  return nil if name.nil? || name.empty?
  id = @contentviewversions[organization][versionname]
  if !id
    contentview_id = katello_contentview(organization, :name => name)
    contentviewversions = @api.resource(:content_view_versions).call(:index, {
                              :per_page => 999999,
                              'content_view_id' => contentview_id
                          })['results'].sort { |a, b| a['created_at'] <=> b['created_at'] }
    if version == 'latest'
      @contentviewversions[organization][versionname] = contentviewversions[-1]['id']
    else
      contentviewversions.each do |contentviewversion|
        if contentviewversion['version'] == version.to_f
          @contentviewversions[organization][versionname] = contentviewversion['id']
        end
      end
    end
    id = @contentviewversions[organization][versionname]
    raise _("Content view version '%{name}' with version '%{version}' not found") % {:name => name, :version => version} if !id
  end

  id
end

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



792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
# File 'lib/hammer_cli_csv/base.rb', line 792

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' => search_string('host-collections',options[:name])
              })['results'].each do |hostcollection|
        @hostcollections[organization][hostcollection['name']] = hostcollection['id'] if hostcollection
      end
      options[:id] = @hostcollections[organization][options[:name]]
      raise _("Host collection '%{name}' not found") % {:name => options[:name]} 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 _("Host collection 'id=%{id}' not found") % {:id => options[:id]} if !hostcollection || hostcollection.empty?
      options[:name] = hostcollection['name']
      @hostcollections[options[:name]] = options[:id]
    end
    result = options[:name]
  end

  result
end

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



827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
# File 'lib/hammer_cli_csv/base.rb', line 827

def katello_product(organization, options = {})
  @products ||= {}
  @products[organization] ||= {}

  if options[:name]
    return nil if options[:name].nil? || options[:name].empty?
    options[:id] = @products[organization][options[:name]]
    if !options[:id]
      @api.resource(:products).call(:index,
              {
                :per_page => 999999,
                'organization_id' => foreman_organization(:name => organization),
                'search' => search_string('host-collections',options[:name])
              })['results'].each do |product|
        @products[organization][product['name']] = product['id'] if product
      end
      options[:id] = @products[organization][options[:name]]
      raise _("Host collection '%{name}' not found") % {:name => options[:name]} if !options[:id]
    end
    result = options[:id]
  else
    return nil if options[:id].nil?
    options[:name] = @products.key(options[:id])
    if !options[:name]
      product = @api.resource(:host_collections).call(:show, {'id' => options[:id]})
      raise _("Host collection 'id=%{id}' not found") % {:id => options[:id]} if !product || product.empty?
      options[:name] = product['name']
      @products[options[:name]] = options[:id]
    end
    result = options[:name]
  end

  result
end

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



759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
# File 'lib/hammer_cli_csv/base.rb', line 759

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 '%{name}' not found") % {:name => options[:name]} 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 'id=%{id}' not found") % {:id => options[:id]} if !repository || repository.empty?
      options[:name] = repository['name']
      @repositoriesr[options[:name]] = options[:id]
    end
    result = options[:name]
  end

  result
end

#labelize(name) ⇒ Object



122
123
124
# File 'lib/hammer_cli_csv/base.rb', line 122

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

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



663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
# File 'lib/hammer_cli_csv/base.rb', line 663

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 '%{name}' not found") % {:name => options[:name]} 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 '%{name}' not found") % {:name => options[:name]} 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



111
112
113
114
115
116
117
118
119
120
# File 'lib/hammer_cli_csv/base.rb', line 111

def namify(name_format, number = 0)
  return '' unless name_format
  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



931
932
933
934
935
936
937
938
# File 'lib/hammer_cli_csv/base.rb', line 931

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”, ”



902
903
904
905
906
907
908
909
910
911
912
# File 'lib/hammer_cli_csv/base.rb', line 902

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

#supported?Boolean

Returns:

  • (Boolean)


38
39
40
# File 'lib/hammer_cli_csv/base.rb', line 38

def supported?
  self.class.supported?
end

#thread_import(return_headers = false, filename = nil, name_column = nil) ⇒ Object



126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
# File 'lib/hammer_cli_csv/base.rb', line 126

def thread_import(return_headers = false, filename=nil, name_column=nil)
  filename ||= option_file || '/dev/stdin'
  csv = []
  CSV.new(open(filename), {
      :skip_blanks => true,
      :headers => :first_row,
      :return_headers => return_headers
  }).each 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|
          next if !line[name_column || NAME].nil? && line[name_column || NAME][0] == '#'
          begin
            logger.debug(line)
            yield line
          rescue RuntimeError => e
            message = "#{e}\n#{line}"
            option_continue_on_error? ? $stderr.puts(_("Error: %{message}") % {:message => message}) : raise(message)
          end
        end
      end
    end
  end

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