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

Instance Method Details

#create_hostcollections_from_csv(line) ⇒ Object



65
66
67
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
# File 'lib/hammer_cli_csv/host_collections.rb', line 65

def create_hostcollections_from_csv(line)
  return if option_organization && line[ORGANIZATION] != option_organization

  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)
    params =  {
                'organization_id' => foreman_organization(:name => line[ORGANIZATION]),
                'name' => name,
                'unlimited_content_hosts' => (line[LIMIT] == 'Unlimited') ? true : false,
                'max_content_hosts' => (line[LIMIT] == 'Unlimited') ? nil : line[LIMIT].to_i,
                'description' => line[DESCRIPTION]
              }
    if !@existing[line[ORGANIZATION]].include? name
      print "Creating host collection '#{name}'..." if option_verbose?
      @api.resource(:host_collections).call(:create, params)
    else
      print "Updating host collection '#{name}'..." if option_verbose?
      params['id'] = @existing[line[ORGANIZATION]][name]
      @api.resource(:host_collections).call(:update, params)
    end
    print "done\n" if option_verbose?
  end
end

#exportObject



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/hammer_cli_csv/host_collections.rb', line 29

def export
  CSV.open(option_csv_file || '/dev/stdout', 'wb') do |csv|
    csv << [NAME, COUNT, ORGANIZATION, LIMIT, DESCRIPTION]
    if @server_status['release'] == 'Headpin'
      @headpin.get(:organizations).each do |organization|
        next if option_organization && organization['name'] != option_organization
        @headpin.get("organizations/#{organization['label']}/system_groups").each do |systemgroup|
          csv << [systemgroup['name'], 1, organization['name'],
                  systemgroup['max_systems'].to_i < 0 ? 'Unlimited' : systemgroup['max_systems'],
                  systemgroup['description']]
        end
      end
    else
      @api.resource(:organizations).call(:index, {'per_page' => 999999})['results'].each do |organization|
        next if option_organization && organization['name'] != option_organization
        @api.resource(:host_collections).call(:index, {
            'organization_id' => organization['id']
        })['results'].each do |hostcollection|
          limit = hostcollection['unlimited_content_hosts'] ? 'Unlimited' : hostcollection['max_content_hosts']
          csv << [hostcollection['name'], 1, organization['name'],
                  limit,
                  hostcollection['description']]
        end
      end
    end
  end
end

#importObject



57
58
59
60
61
62
63
# File 'lib/hammer_cli_csv/host_collections.rb', line 57

def import
  @existing = {}

  thread_import do |line|
    create_hostcollections_from_csv(line)
  end
end