Class: HammerCLICsv::CsvCommand::ActivationKeysCommand

Inherits:
BaseCommand
  • Object
show all
Includes:
Utils::Subscriptions
Defined in:
lib/hammer_cli_csv/activation_keys.rb

Constant Summary collapse

ORGANIZATION =
'Organization'
DESCRIPTION =
'Description'
LIMIT =
'Limit'
ENVIRONMENT =
'Environment'
CONTENTVIEW =
'Content View'
HOSTCOLLECTIONS =
'Host Collections'
SERVICELEVEL =
"Service Level"
RELEASEVER =
"Release Version"
AUTOATTACH =
"Auto-Attach"

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, help_columns, #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

.supported?Boolean

Returns:

  • (Boolean)


9
10
11
# File 'lib/hammer_cli_csv/activation_keys.rb', line 9

def self.supported?
  true
end

Instance Method Details

#all_in_one_subscription(activationkey, existing_subscriptions, line) ⇒ Object



225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
# File 'lib/hammer_cli_csv/activation_keys.rb', line 225

def all_in_one_subscription(activationkey, existing_subscriptions, line)
  return if line[SUBSCRIPTIONS].nil? || line[SUBSCRIPTIONS].empty?

  organization_id = foreman_organization(:name => line[ORGANIZATION])
  hypervisor = hypervisor_from_line(line)

  subscriptions = CSV.parse_line(line[SUBSCRIPTIONS], {:skip_blanks => true}).collect do |details|
    (amount, sku, name, contract, ) = split_subscription_details(details)
    matches = get_matching_subscriptions(organization_id,
        :name => name, :contract => contract, :account => , :quantity => amount,
        :hypervisor => hypervisor)
    raise _("No matching subscriptions") if matches.empty?

    {
      :id => matches[0]['id'],
      :quantity => (amount.nil? || amount == 'Automatic') ? 0 : amount.to_i
    }
  end

  @api.resource(:activation_keys).call(:add_subscriptions, {
                                         'id' => activationkey['id'],
                                         'subscriptions' => subscriptions
                                       })
end

#create_from_csv(line) ⇒ Object



104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/hammer_cli_csv/activation_keys.rb', line 104

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

  update_existing(line)

  count(line[COUNT]).times do |number|
    name = namify(line[NAME], number)

    if option_itemized_subscriptions?
      update_itemized_subscriptions(name, line)
    else
      update_or_create(name, line)
    end
  end
end

#export(csv) ⇒ Object



25
26
27
28
29
30
31
# File 'lib/hammer_cli_csv/activation_keys.rb', line 25

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

#export_all(csv) ⇒ Object



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

def export_all(csv)
  csv << shared_headers + [SUBSCRIPTIONS]
  iterate_activationkeys(csv) do |activationkey|
    subscriptions = CSV.generate do |column|
      column << @api.resource(:subscriptions).call(:index, {
                    'organization_id' => activationkey['organization']['id'],
                    'activation_key_id' => activationkey['id']
                })['results'].collect do |subscription|
        amount = (subscription['quantity_attached'].nil? || subscription['quantity_attached'] < 1) ? 'Automatic' : subscription['quantity_attached']
        "#{amount}"\
        "|#{subscription['product_id']}"\
        "|#{subscription['product_name']}"\
        "|#{subscription['contract_number']}|#{subscription['account_number']}"
      end
    end
    subscriptions.delete!("\n")
    csv << shared_columns(activationkey) + [subscriptions]
  end
end

#export_itemized_subscriptions(csv) ⇒ Object



33
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
# File 'lib/hammer_cli_csv/activation_keys.rb', line 33

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,
                           Utils::Subscriptions::SUBS_GUESTOF]
  iterate_activationkeys(csv) do |activationkey|
    columns = shared_columns(activationkey)
    @api.resource(:subscriptions).call(:index, {
        'organization_id' => activationkey['organization']['id'],
        'activation_key_id' => activationkey['id']
    })['results'].collect do |subscription|
      subscription_type = subscription['product_id'].to_i == 0 ? 'Red Hat' : 'Custom'
      hypervisor = subscription['host']['name'] if subscription['host']
      subscription_type += ' Guest' unless hypervisor.nil?
      subscription_type += ' Temporary' if subscription['type'] == 'UNMAPPED_GUEST'
      amount = (subscription['quantity_attached'].nil? || subscription['quantity_attached'] < 1) ? 'Automatic' : subscription['quantity_attached']
      csv << columns +
        [subscription['product_name'], subscription_type, amount,
         subscription['product_id'], subscription['contract_number'], subscription['account_number'],
         DateTime.parse(subscription['start_date']),
         DateTime.parse(subscription['end_date']),
         hypervisor]
    end
  end
end

#hypervisor_from_line(line) ⇒ Object



286
287
288
289
# File 'lib/hammer_cli_csv/activation_keys.rb', line 286

def hypervisor_from_line(line)
  hypervisor = line[SUBS_GUESTOF] if !line[SUBS_GUESTOF].nil? && !line[SUBS_GUESTOF].empty?
  hypervisor
end

#importObject



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

def import
  @existing = {}

  thread_import do |line|
    create_from_csv(line)
  end
end

#iterate_activationkeys(csv) ⇒ Object



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/hammer_cli_csv/activation_keys.rb', line 80

def iterate_activationkeys(csv)
  @api.resource(:organizations).call(:index, {
      :per_page => 999999
  })['results'].each do |organization|
    next if option_organization && organization['name'] != option_organization

    @api.resource(:activation_keys).call(:index, {
        'per_page' => 999999,
        'search' => option_search,
        'organization_id' => organization['id']
    })['results'].each do |activationkey|
      yield activationkey
    end
  end
end

#shared_columns(activationkey) ⇒ Object



259
260
261
262
263
264
265
266
267
268
269
270
271
272
# File 'lib/hammer_cli_csv/activation_keys.rb', line 259

def shared_columns(activationkey)
  name = namify(activationkey['name'])
  organization = activationkey['organization']['name']
  description = activationkey['description']
  limit = activationkey['unlimited_content_hosts'] ? 'Unlimited' : activationkey['max_content_hosts']
  environment = activationkey['environment'].nil? ? nil : activationkey['environment']['label']
  contentview = activationkey['content_view'].nil? ? nil : activationkey['content_view']['name']
  hostcollections = export_column(activationkey, 'host_collections', 'name')
  autoattach = activationkey['auto_attach'] ? 'Yes' : 'No'
  servicelevel = activationkey['service_level']
  releasever = activationkey['release_version']
  [name, organization, description, limit, environment, contentview, hostcollections,
   autoattach, servicelevel, releasever]
end

#shared_headersObject



254
255
256
257
# File 'lib/hammer_cli_csv/activation_keys.rb', line 254

def shared_headers
  [NAME, ORGANIZATION, DESCRIPTION, LIMIT, ENVIRONMENT, CONTENTVIEW,
   HOSTCOLLECTIONS, AUTOATTACH, SERVICELEVEL, RELEASEVER]
end

#single_subscription(activationkey, existing_subscriptions, line) ⇒ Object



196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
# File 'lib/hammer_cli_csv/activation_keys.rb', line 196

def single_subscription(activationkey, existing_subscriptions, line)
  matches = matches_by_sku_and_name([], line[SUBS_SKU], line[SUBS_NAME], existing_subscriptions)
  matches = matches_by_hypervisor(matches, line[SUBS_GUESTOF])
  unless matches.empty?
    print _(" '%{name}' already attached...") % {:name => subscription_name(matches[0])} if option_verbose?
    return
  end

  matches = get_matching_subscriptions(activationkey['organization']['id'],
      :activation_key => activationkey, :sku => line[SUBS_SKU], :name => line[SUBS_NAME],
      :type => line[SUBS_TYPE], :account => line[SUBS_ACCOUNT],
      :contract => line[SUBS_CONTRACT], :hypervisor => hypervisor_from_line(line))

  raise _("No matching subscriptions") if matches.empty?
  match = matches[0]
  match = match_with_quantity_to_attach(match, line[SUBS_QUANTITY])

  if option_verbose?
    print _(" attaching %{quantity} of '%{name}'...") % {
      :name => subscription_name(match), :quantity => match['quantity']
    }
  end

  @api.resource(:activation_keys).call(:add_subscriptions, {
      'id' => activationkey['id'],
      'subscriptions' => [match]
  })
end

#update_existing(line) ⇒ Object



274
275
276
277
278
279
280
281
282
283
284
# File 'lib/hammer_cli_csv/activation_keys.rb', line 274

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

#update_groups(activationkey, line) ⇒ Object



160
161
162
163
164
165
166
167
168
169
170
# File 'lib/hammer_cli_csv/activation_keys.rb', line 160

def update_groups(activationkey, line)
  if line[HOSTCOLLECTIONS] && line[HOSTCOLLECTIONS] != ''
    # TODO: note that existing system groups are not removed
    CSV.parse_line(line[HOSTCOLLECTIONS], {:skip_blanks => true}).each do |name|
      @api.resource(:activation_keys).call(:add_host_collections, {
          'id' => activationkey['id'],
          'host_collection_ids' => [katello_hostcollection(line[ORGANIZATION], :name => name)]
      })
    end
  end
end

#update_itemized_subscriptions(name, line) ⇒ Object



120
121
122
123
124
125
126
127
# File 'lib/hammer_cli_csv/activation_keys.rb', line 120

def update_itemized_subscriptions(name, line)
  raise _("Activation key '%{name}' must already exist with --itemized_subscriptions") % {:name => name} unless @existing[line[ORGANIZATION]].include? name

  print(_("Updating subscriptions for activation key '%{name}'...") % {:name => name}) if option_verbose?
  activationkey = @api.resource(:activation_keys).call(:show, {:id => @existing[line[ORGANIZATION]][name]})
  update_subscriptions(activationkey, line, false)
  puts _('done') if option_verbose?
end

#update_or_create(name, line) ⇒ Object



129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
# File 'lib/hammer_cli_csv/activation_keys.rb', line 129

def update_or_create(name, line)
  params = {
             'organization_id' => foreman_organization(:name => line[ORGANIZATION]),
             'name' => name,
             'environment_id' => lifecycle_environment(line[ORGANIZATION],
                                                       :name => line[ENVIRONMENT]),
             'content_view_id' => katello_contentview(line[ORGANIZATION],
                                                      :name => line[CONTENTVIEW]),
             'description' => line[DESCRIPTION],
             'unlimited_content_hosts' => (line[LIMIT] == 'Unlimited') ? true : false,
             'max_content_hosts' => (line[LIMIT] == 'Unlimited') ? nil : line[LIMIT].to_i
           }
  params['auto_attach'] = (line[AUTOATTACH] == 'Yes' ? true : false) if params['auto_attach']
  params['service_level'] = line[SERVICELEVEL].nil? || line[SERVICELEVEL].empty? ? nil : line[SERVICELEVEL]
  params['release_version'] = line[RELEASEVER].nil? || line[RELEASEVER].empty? ? nil : line[RELEASEVER]
  if !@existing[line[ORGANIZATION]].include? name
    print _("Creating activation key '%{name}'...") % {:name => name} if option_verbose?
    activationkey = @api.resource(:activation_keys).call(:create, params)
    @existing[line[ORGANIZATION]][activationkey['name']] = activationkey['id']
  else
    print _("Updating activation key '%{name}'...") % {:name => name} if option_verbose?
    params['id'] = @existing[line[ORGANIZATION]][name]
    activationkey = @api.resource(:activation_keys).call(:update, params)
  end

  update_subscriptions(activationkey, line, true)
  update_groups(activationkey, line)

  puts _('done') if option_verbose?
end

#update_subscriptions(activationkey, line, remove_existing) ⇒ Object



172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
# File 'lib/hammer_cli_csv/activation_keys.rb', line 172

def update_subscriptions(activationkey, line, remove_existing)
  existing_subscriptions = @api.resource(:subscriptions).call(:index, {
      'organization_id' => foreman_organization(:name => line[ORGANIZATION]),
      'per_page' => 999999,
      'activation_key_id' => activationkey['id']
  })['results']
  if remove_existing && existing_subscriptions.length > 0
    existing_subscriptions.map! do |existing_subscription|
      {:id => existing_subscription['id'], :quantity => existing_subscription['quantity_consumed']}
    end
    @api.resource(:activation_keys).call(:remove_subscriptions, {
      'id' => activationkey['id'],
      'subscriptions' => existing_subscriptions
    })
    existing_subscriptions = []
  end

  if line[Utils::Subscriptions::SUBS_NAME].nil? && line[Utils::Subscriptions::SUBS_SKU].nil?
    all_in_one_subscription(activationkey, existing_subscriptions, line)
  else
    single_subscription(activationkey, existing_subscriptions, line)
  end
end

#usage_limit(limit) ⇒ Object



250
251
252
# File 'lib/hammer_cli_csv/activation_keys.rb', line 250

def usage_limit(limit)
  Integer(limit) rescue -1
end