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 =

deprecated for GUESTOF

'Host'
GUESTOF =
'Guest of 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_GUESTOF, 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

#debug_subscriptions, #get_all_subscriptions, #get_matching_subscriptions, #match_with_quantity_to_attach, #matches_by_account, #matches_by_contract, #matches_by_hypervisor, #matches_by_quantity, #matches_by_sku_and_name, #matches_by_sla, #matches_by_type, #split_subscription_details, #subscription_name

Methods inherited from BaseCommand

#apipie_check_param, #associate_locations, #associate_organizations, #authenticate_request, #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

Class Method Details

.help_columnsObject



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/hammer_cli_csv/content_hosts.rb', line 34

def self.help_columns
  ['', _('Columns:'),
   _(" %{name} - Name of resource") % {:name => NAME},
   _(" %{name} - Search for matching names during import (overrides '%{name_col}' column)") % {:name => SEARCH, :name_col => NAME},
   _(" %{name} - Organization name") % {:name => ORGANIZATION},
   _(" %{name} - Lifecycle environment name") % {:name => ENVIRONMENT},
   _(" %{name} - Content view name") % {:name => CONTENTVIEW},
   _(" %{name} - Comma separated list of host collection names") % {:name => HOSTCOLLECTIONS},
   _(" %{name} - Is a virtual host, %{yes} or %{no}") % {:name => VIRTUAL, :yes => 'Yes', :no => 'No'},
   _(" %{name} - Hypervisor host name for virtual hosts") % {:name => GUESTOF},
   _(" %{name} - Operating system") % {:name => OPERATINGSYSTEM},
   _(" %{name} - Architecture") % {:name => ARCHITECTURE},
   _(" %{name} - Number of sockets") % {:name => SOCKETS},
   _(" %{name} - Quantity of RAM in bytes") % {:name => RAM},
   _(" %{name} - Number of cores") % {:name => CORES},
   _(" %{name} - Service Level Agreement value") % {:name => SLA},
   _(" %{name} - Comma separated list of products, each of the format \"<sku>|<name>\"") % {:name => PRODUCTS},
   _(" %{name} - Comma separated list of subscriptions, each of the format \"<quantity>|<sku>|<name>|<contract>|<account>\"") % {:name => SUBSCRIPTIONS},
   _(" %{name} - Subscription name (only applicable for --itemized-subscriptions)") % {:name => SUBS_NAME},
   _(" %{name} - Subscription type (only applicable for --itemized-subscriptions)") % {:name => SUBS_TYPE},
   _(" %{name} - Subscription quantity (only applicable for --itemized-subscriptions)") % {:name => SUBS_QUANTITY},
   _(" %{name} - Subscription SKU (only applicable for --itemized-subscriptions)") % {:name => SUBS_SKU},
   _(" %{name} - Subscription contract number (only applicable for --itemized-subscriptions)") % {:name => SUBS_CONTRACT},
   _(" %{name} - Subscription account number (only applicable for --itemized-subscriptions)") % {:name => SUBS_ACCOUNT},
   _(" %{name} - Subscription start date (only applicable for --itemized-subscriptions)") % {:name => SUBS_START},
   _(" %{name} - Subscription end date (only applicable for --itemized-subscriptions)") % {:name => SUBS_END}
  ].join("\n")
end

.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

#column_headersObject



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/hammer_cli_csv/content_hosts.rb', line 63

def column_headers
  @column_values = {}
  if option_columns.nil?
    if ::HammerCLI::Settings.settings[:csv][:columns] &&
        ::HammerCLI::Settings.settings[:csv][:columns]['content-hosts'.to_sym] &&
        ::HammerCLI::Settings.settings[:csv][:columns]['content-hosts'.to_sym][:export]
      @columns = ::HammerCLI::Settings.settings[:csv][:columns]['content-hosts'.to_sym][:export]
    else
      if option_itemized_subscriptions?
        @columns = shared_headers + [SUBS_NAME, SUBS_TYPE, SUBS_QUANTITY, SUBS_SKU,
                                     SUBS_CONTRACT, SUBS_ACCOUNT, SUBS_START, SUBS_END, SUBS_GUESTOF]
      else
        @columns = shared_headers + [SUBSCRIPTIONS]
      end
    end
  else
    @columns = option_columns.split(',')
  end

  if ::HammerCLI::Settings.settings[:csv][:columns] && ::HammerCLI::Settings.settings[:csv][:columns]['content-hosts'.to_sym][:define]
    @column_definitions = ::HammerCLI::Settings.settings[:csv][:columns]['content-hosts'.to_sym][:define]
  end

  @columns
end

#create_from_csv(line) ⇒ Object



182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
# File 'lib/hammer_cli_csv/content_hosts.rb', line 182

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,
          'per_page' => 999999
      })['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



89
90
91
92
93
94
95
96
97
# File 'lib/hammer_cli_csv/content_hosts.rb', line 89

def export(csv)
  raise _("--clear-subscriptions option only relevant during import") if option_clear_subscriptions?

  if option_itemized_subscriptions?
    export_itemized_subscriptions csv
  else
    export_all csv
  end
end

#export_all(csv) ⇒ Object



143
144
145
146
147
148
149
150
151
# File 'lib/hammer_cli_csv/content_hosts.rb', line 143

def export_all(csv)
  csv << column_headers
  iterate_hosts(csv) do |host|
    all_subscription_column(host)
    shared_columns(host)
    custom_columns(host)
    columns_to_csv(csv)
  end
end

#export_itemized_subscriptions(csv) ⇒ Object



99
100
101
102
103
104
105
106
107
108
109
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
135
136
137
138
139
140
141
# File 'lib/hammer_cli_csv/content_hosts.rb', line 99

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

#importObject



153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
# File 'lib/hammer_cli_csv/content_hosts.rb', line 153

def import
  raise _("--columns option only relevant with --export") unless option_columns.nil?

  @existing = {}
  @visited = {}
  @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