Class: HammerCLICsv::CsvCommand::ContentHostsCommand

Inherits:
BaseCommand
  • Object
show all
Includes:
Utils::Subscriptions, HammerCLIForemanTasks::Helper
Defined in:
lib/hammer_cli_csv/content_hosts.rb

Constant Summary collapse

SEARCH =
'Search'
ORGANIZATION =
'Organization'
ENVIRONMENT =
'Environment'
CONTENTVIEW =
'Content View'
HOSTCOLLECTIONS =
'Host Collections'
VIRTUAL =
'Virtual'
HOST =
'Host'
OPERATINGSYSTEM =
'OS'
ARCHITECTURE =
'Arch'
SOCKETS =
'Sockets'
RAM =
'RAM'
CORES =
'Cores'
SLA =
'SLA'
PRODUCTS =
'Products'

Constants included from Utils::Subscriptions

Utils::Subscriptions::SUBSCRIPTIONS, Utils::Subscriptions::SUBS_ACCOUNT, Utils::Subscriptions::SUBS_CONTRACT, Utils::Subscriptions::SUBS_END, Utils::Subscriptions::SUBS_NAME, Utils::Subscriptions::SUBS_QUANTITY, Utils::Subscriptions::SUBS_SKU, Utils::Subscriptions::SUBS_START, Utils::Subscriptions::SUBS_TYPE

Constants inherited from BaseCommand

BaseCommand::COUNT, BaseCommand::NAME

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Utils::Subscriptions

#get_all_subscriptions, #get_subscription, #matches_by_account, #matches_by_contract, #matches_by_quantity, #matches_by_sku_and_name, #matches_by_type, #split_subscription_details

Methods inherited from BaseCommand

#apipie_check_param, #associate_locations, #associate_organizations, #build_os_name, #check_server_status, #collect_column, #count, #execute, #export_column, #foreman_architecture, #foreman_container, #foreman_domain, #foreman_environment, #foreman_filter, #foreman_host, #foreman_hostgroup, #foreman_location, #foreman_medium, #foreman_operatingsystem, #foreman_organization, #foreman_partitiontable, #foreman_permission, #foreman_provisioning_template, #foreman_role, #foreman_smart_proxy, #foreman_template_kind, #hammer, #hammer_context, #help, #katello_contentview, #katello_contentviewversion, #katello_hostcollection, #katello_product, #katello_repository, #labelize, #lifecycle_environment, #namify, #pluralize, #split_os_name, #supported?, #thread_import

Methods included from Utils::Config

#api_connection, #credentials, #resource_config

Class Method Details

.supported?Boolean

Returns:

  • (Boolean)


10
11
12
# File 'lib/hammer_cli_csv/content_hosts.rb', line 10

def self.supported?
  true
end

Instance Method Details

#create_from_csv(line) ⇒ Object



136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
# File 'lib/hammer_cli_csv/content_hosts.rb', line 136

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

  update_existing(line)

  count(line[COUNT]).times do |number|
    if  !line[SEARCH].nil? && !line[SEARCH].empty?
      search = namify(line[SEARCH], number)
      @api.resource(:hosts).call(:index, {
          'organization_id' => foreman_organization(:name => line[ORGANIZATION]),
          'search' => search
      })['results'].each do |host|
        if host['subscription_facet_attributes']
          create_named_from_csv(host['name'], line)
        end
      end
    else
      name = namify(line[NAME], number)
      create_named_from_csv(name, line)
    end
  end
end

#export(csv) ⇒ Object



31
32
33
34
35
36
37
# File 'lib/hammer_cli_csv/content_hosts.rb', line 31

def export(csv)
  if option_itemized_subscriptions?
    export_itemized_subscriptions csv
  else
    export_all csv
  end
end

#export_all(csv) ⇒ Object



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/hammer_cli_csv/content_hosts.rb', line 71

def export_all(csv)
  csv << shared_headers + [Utils::Subscriptions::SUBSCRIPTIONS]
  iterate_hosts(csv) do |host|
    if host['subscription_facet_attributes']
      subscriptions = CSV.generate do |column|
        column << @api.resource(:host_subscriptions).call(:index, {
            'organization_id' => host['organization_id'],
            'host_id' => host['id']
        })['results'].collect do |subscription|
          "#{subscription['quantity_consumed']}"\
          "|#{subscription['product_id']}"\
          "|#{subscription['product_name']}"\
          "|#{subscription['contract_number']}|#{subscription['account_number']}"
        end
      end
      subscriptions.delete!("\n")
    else
      subscriptions = nil
    end

    csv << shared_columns(host) + [subscriptions]
  end
end

#export_itemized_subscriptions(csv) ⇒ Object



39
40
41
42
43
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
# File 'lib/hammer_cli_csv/content_hosts.rb', line 39

def export_itemized_subscriptions(csv)
  csv << shared_headers + [Utils::Subscriptions::SUBS_NAME, Utils::Subscriptions::SUBS_TYPE,
                           Utils::Subscriptions::SUBS_QUANTITY, Utils::Subscriptions::SUBS_SKU,
                           Utils::Subscriptions::SUBS_CONTRACT, Utils::Subscriptions::SUBS_ACCOUNT,
                           Utils::Subscriptions::SUBS_START, Utils::Subscriptions::SUBS_END]
  iterate_hosts(csv) do |host|
    export_line = shared_columns(host)
    if host['subscription_facet_attributes']
      subscriptions = @api.resource(:host_subscriptions).call(:index, {
          'organization_id' => host['organization_id'],
          'host_id' => host['id']
      })['results']
      if subscriptions.empty?
        csv << export_line + [nil, nil, nil, nil, nil, nil]
      else
        subscriptions.each do |subscription|
          subscription_type = subscription['product_id'].to_i == 0 ? 'Red Hat' : 'Custom'
          subscription_type += ' Guest' if subscription['type'] == 'STACK_DERIVED'
          subscription_type += ' Temporary' if subscription['type'] == 'UNMAPPED_GUEST'
          csv << export_line + [subscription['product_name'], subscription_type,
                                subscription['quantity_consumed'], subscription['product_id'],
                                subscription['contract_number'], subscription['account_number'],
                                DateTime.parse(subscription['start_date']).strftime('%m/%d/%Y'),
                                DateTime.parse(subscription['end_date']).strftime('%m/%d/%Y')]
        end
      end
    else
      csv << export_line + [nil, nil, nil, nil, nil, nil]
    end
  end
end

#importObject



95
96
97
98
99
100
101
102
# File 'lib/hammer_cli_csv/content_hosts.rb', line 95

def import
  remote = @server_status['plugins'].detect { |plugin| plugin['name'] == 'foreman_csv' }
  if remote.nil?
    import_locally
  else
    import_remotely
  end
end

#import_locallyObject



110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/hammer_cli_csv/content_hosts.rb', line 110

def import_locally
  @existing = {}
  @hypervisor_guests = {}
  @all_subscriptions = {}

  thread_import do |line|
    create_from_csv(line)
  end

  if !@hypervisor_guests.empty?
    print(_('Updating hypervisor and guest associations...')) if option_verbose?
    @hypervisor_guests.each do |host_id, guest_ids|
      @api.resource(:hosts).call(:update, {
        'id' => host_id,
        'host' => {
          'subscription_facet_attributes' => {
            'autoheal' => false,
            'hypervisor_guest_uuids' => guest_ids
          }
        }
      })
    end
    puts _('done') if option_verbose?
  end
end

#import_remotelyObject



104
105
106
107
108
# File 'lib/hammer_cli_csv/content_hosts.rb', line 104

def import_remotely
  params = {'content' => ::File.new(::File.expand_path(option_file), 'rb')}
  headers = {:content_type => 'multipart/form-data', :multipart => true}
  task_progress(@api.resource(:csv).call(:import_content_hosts, params, headers))
end