Class: HammerCLICsv::CsvCommand::LocationsCommand

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

Constant Summary collapse

PARENT =
'Parent Location'

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_hostcollection, #katello_repository, #katello_subscription, #labelize, #lifecycle_environment, #namify, #pluralize, #split_os_name, #thread_import

Instance Method Details

#create_locations_from_csv(line) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/hammer_cli_csv/locations.rb', line 58

def create_locations_from_csv(line)
  line[COUNT].to_i.times do |number|
    name = namify(line[NAME], number)
    location_id = @existing[name]
    if !location_id
      print "Creating location '#{name}'... " if option_verbose?
      @api.resource(:locations).call(:create, {
                                       'location' => {
                                         'name' => name,
                                         'parent_id' => foreman_location(:name => line[PARENT])
                                       }
                                     })
    else
      print "Updating location '#{name}'... " if option_verbose?
      @api.resource(:locations).call(:update, {
                                       'id' => location_id,
                                       'location' => {
                                         'parent_id' => foreman_location(:name => line[PARENT])
                                       }
                                     })
    end
    print "done\n" if option_verbose?
  end
end

#exportObject



38
39
40
41
42
43
44
45
# File 'lib/hammer_cli_csv/locations.rb', line 38

def export
  CSV.open(option_csv_file || '/dev/stdout', 'wb', {:force_quotes => true}) do |csv|
    csv << [NAME, COUNT, PARENT]
    @api.resource(:locations).call(:index, {:per_page => 999999})['results'].each do |location|
      csv << [location['name'], 1, '']
    end
  end
end

#importObject



47
48
49
50
51
52
53
54
55
56
# File 'lib/hammer_cli_csv/locations.rb', line 47

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

  thread_import do |line|
    create_locations_from_csv(line)
  end
end