Class: HammerCLICsv::CsvCommand::HostCollectionsCommand

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

Constant Summary collapse

ORGANIZATION =
'Organization'
LIMIT =
'Limit'
DESCRIPTION =
'Description'

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_hostcollections_from_csv(line) ⇒ Object



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
100
101
102
103
104
105
# File 'lib/hammer_cli_csv/host_collections.rb', line 69

def create_hostcollections_from_csv(line)
  if !@existing[line[ORGANIZATION]]
    @existing[line[ORGANIZATION]] = {}
    @api.resource(:host_collections)\
      .call(:index, {
              'per_page' => 999999,
              'organization_id' => foreman_organization(:name => line[ORGANIZATION])
            })['results'].each do |hostcollection|
      @existing[line[ORGANIZATION]][hostcollection['name']] = hostcollection['id']
    end
  end

  line[COUNT].to_i.times do |number|
    name = namify(line[NAME], number)
    if !@existing[line[ORGANIZATION]].include? name
      print "Creating system group '#{name}'..." if option_verbose?
      @api.resource(:host_collections)\
        .call(:create, {
                'organization_id' => foreman_organization(:name => line[ORGANIZATION]),
                'name' => name,
                'max_systems' => (line[LIMIT] == 'Unlimited') ? -1 : line[LIMIT],
                'description' => line[DESCRIPTION]
              })
    else
      print "Updating system group '#{name}'..." if option_verbose?
      @api.resource(:host_collections)\
        .call(:update, {
                'organization_id' => line[ORGANIZATION],
                'id' => @existing[line[ORGANIZATION]][name],
                'name' => name,
                'max_systems' => (line[LIMIT] == 'Unlimited') ? -1 : line[LIMIT],
                'description' => line[DESCRIPTION]
              })
    end
    print "done\n" if option_verbose?
  end
end

#exportObject



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/hammer_cli_csv/host_collections.rb', line 41

def export
  CSV.open(option_csv_file, 'wb') do |csv|
    csv << [NAME, COUNT, ORGANIZATION, LIMIT, DESCRIPTION]
    @api.resource(:organizations)\
      .call(:index, {
              'per_page' => 999999
            })['results'].each do |organization|
      @api.resource(:host_collections)\
        .call(:index, {
                'organization_id' => organization['id']
              }).each do |hostcollection|
        puts hostcollection
        csv << [hostcollection['name'], 1, organization['id'],
                hostcollection['max_systems'].to_i < 0 ? 'Unlimited' : sytemgroup['max_systems'],
                hostcollection['description']]
      end
    end
  end
end

#importObject



61
62
63
64
65
66
67
# File 'lib/hammer_cli_csv/host_collections.rb', line 61

def import
  @existing = {}

  thread_import do |line|
    create_hostcollections_from_csv(line)
  end
end