Class: HammerCLICsv::CsvCommand::OperatingSystemsCommand

Inherits:
BaseCommand
  • Object
show all
Defined in:
lib/hammer_cli_csv/operating_systems.rb

Constant Summary collapse

FAMILY =
'Family'

Constants inherited from BaseCommand

BaseCommand::COUNT, BaseCommand::NAME

Instance Method Summary collapse

Methods inherited from BaseCommand

#associate_locations, #associate_organizations, #build_os_name, #check_server_status, #collect_column, #execute, #export_column, #foreman_architecture, #foreman_domain, #foreman_environment, #foreman_filter, #foreman_location, #foreman_operatingsystem, #foreman_organization, #foreman_partitiontable, #foreman_permission, #foreman_role, #foreman_template_kind, #hammer, #hammer_context, #katello_contentview, #katello_contentviewversion, #katello_hostcollection, #katello_repository, #katello_subscription, #labelize, #lifecycle_environment, #namify, #pluralize, #split_os_name, #thread_import

Instance Method Details

#create_operatingsystems_from_csv(line) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/hammer_cli_csv/operating_systems.rb', line 44

def create_operatingsystems_from_csv(line)
  line[COUNT].to_i.times do |number|
    name = namify(line[NAME], number)
    (osname, major, minor) = split_os_name(name)
    if !@existing.include? name
      print "Creating operating system '#{name}'..." if option_verbose?
      @api.resource(:operatingsystems).call(:create, {
          'operatingsystem' => {
              'name' => osname,
              'major' => major,
              'minor' => minor,
              'family' => line[FAMILY]
          }
      })
    else
      print "Updating operating system '#{name}'..." if option_verbose?
      @api.resource(:operatingsystems).call(:update, {
          'id' => @existing[name],
          'operatingsystem' => {
              'name' => osname,
              'major' => major,
              'minor' => minor,
              'family' => line[FAMILY]
          }
      })
    end
    print "done\n" if option_verbose?
  end
rescue RuntimeError => e
  raise "#{e}\n       #{line}"
end

#exportObject



20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/hammer_cli_csv/operating_systems.rb', line 20

def export
  CSV.open(option_csv_file || '/dev/stdout', 'wb', {:force_quotes => true}) do |csv|
    csv << [NAME, COUNT, FAMILY]
    @api.resource(:operatingsystems).call(:index, {:per_page => 999999})['results'].each do |operatingsystem|
      name = build_os_name(operatingsystem['name'], operatingsystem['major'], operatingsystem['minor'])
      count = 1
      description = operatingsystem['description']
      family = operatingsystem['family']
      csv << [name, count, description, family]
    end
  end
end

#importObject



33
34
35
36
37
38
39
40
41
42
# File 'lib/hammer_cli_csv/operating_systems.rb', line 33

def import
  @existing = {}
  @api.resource(:operatingsystems).call(:index, {:per_page => 999999})['results'].each do |operatingsystem|
    @existing[build_os_name(operatingsystem['name'], operatingsystem['major'], operatingsystem['minor'])] = operatingsystem['id'] if operatingsystem
  end

  thread_import do |line|
    create_operatingsystems_from_csv(line)
  end
end