Project

General

Profile

Revision 611959a3

Added by Thomas McKay over 5 years ago

fixes #16281 - add --itemized-subscriptions to activation-keys

View differences:

lib/hammer_cli_csv/content_hosts.rb
1
#require 'hammer_cli_csv/utils/subscription_utils'
2

  
1 3
module HammerCLICsv
2 4
  class CsvCommand
3 5
    class ContentHostsCommand < BaseCommand
4 6
      include ::HammerCLIForemanTasks::Helper
7
      include ::HammerCLICsv::Utils::Subscriptions
5 8

  
6 9
      command_name 'content-hosts'
7 10
      desc         'import or export content hosts'
......
10 13
        true
11 14
      end
12 15

  
13
      option %w(--subscriptions-only), :flag, 'Export only detailed subscription information'
16
      option %w(--itemized-subscriptions), :flag, _('Export one subscription per row, only process update subscriptions on import')
14 17

  
15 18
      ORGANIZATION = 'Organization'
16 19
      ENVIRONMENT = 'Environment'
......
25 28
      CORES = 'Cores'
26 29
      SLA = 'SLA'
27 30
      PRODUCTS = 'Products'
28
      SUBSCRIPTIONS = 'Subscriptions'
29
      SUBS_NAME = 'Subscription Name'
30
      SUBS_TYPE = 'Subscription Type'
31
      SUBS_QUANTITY = 'Subscription Quantity'
32
      SUBS_SKU = 'Subscription SKU'
33
      SUBS_CONTRACT = 'Subscription Contract'
34
      SUBS_ACCOUNT = 'Subscription Account'
35
      SUBS_START = 'Subscription Start'
36
      SUBS_END = 'Subscription End'
37 31

  
38 32
      def export(csv)
39
        if option_subscriptions_only?
40
          export_subscriptions csv
33
        if option_itemized_subscriptions?
34
          export_itemized_subscriptions csv
41 35
        else
42 36
          export_all csv
43 37
        end
44 38
      end
45 39

  
46
      def export_subscriptions(csv)
47
        csv << shared_headers + [SUBS_NAME, SUBS_TYPE, SUBS_QUANTITY, SUBS_SKU,
48
                                 SUBS_CONTRACT, SUBS_ACCOUNT, SUBS_START, SUBS_END]
40
      def export_itemized_subscriptions(csv)
41
        csv << shared_headers + [Utils::Subscriptions::SUBS_NAME, Utils::Subscriptions::SUBS_TYPE,
42
                                 Utils::Subscriptions::SUBS_QUANTITY, Utils::Subscriptions::SUBS_SKU,
43
                                 Utils::Subscriptions::SUBS_CONTRACT, Utils::Subscriptions::SUBS_ACCOUNT,
44
                                 Utils::Subscriptions::SUBS_START, Utils::Subscriptions::SUBS_END]
49 45
        iterate_hosts(csv) do |host|
50 46
          export_line = shared_columns(host)
51 47
          if host['subscription_facet_attributes']
......
74 70
      end
75 71

  
76 72
      def export_all(csv)
77
        csv << shared_headers + [SUBSCRIPTIONS]
73
        csv << shared_headers + [Utils::Subscriptions::SUBSCRIPTIONS]
78 74
        iterate_hosts(csv) do |host|
79 75
          if host['subscription_facet_attributes']
80 76
            subscriptions = CSV.generate do |column|
......
146 142
        count(line[COUNT]).times do |number|
147 143
          name = namify(line[NAME], number)
148 144

  
149
          if option_subscriptions_only?
150
            update_subscriptions_only(name, line)
145
          if option_itemized_subscriptions?
146
            update_itemized_subscriptions(name, line)
151 147
          else
152 148
            update_or_create(name, line)
153 149
          end
......
156 152

  
157 153
      private
158 154

  
159
      def update_subscriptions_only(name, line)
160
        raise _("Content host '%{name}' must already exist with --subscriptions-only") % {:name => name} unless @existing.include? name
155
      def update_itemized_subscriptions(name, line)
156
        raise _("Content host '%{name}' must already exist with --itemized-subscriptions") % {:name => name} unless @existing.include? name
161 157

  
162 158
        print(_("Updating subscriptions for content host '%{name}'...") % {:name => name}) if option_verbose?
163 159
        host = @api.resource(:hosts).call(:show, {:id => @existing[name]})
......
273 269
          existing_subscriptions = []
274 270
        end
275 271

  
276
        if line[SUBS_NAME].nil? && line[SUBS_SKU].nil?
272
        if line[Utils::Subscriptions::SUBS_NAME].nil? && line[Utils::Subscriptions::SUBS_SKU].nil?
277 273
          all_in_one_subscription(host, existing_subscriptions, line)
278 274
        else
279 275
          single_subscription(host, existing_subscriptions, line)
......
282 278

  
283 279
      def single_subscription(host, existing_subscriptions, line)
284 280
        already_attached = false
285
        if line[SUBS_SKU]
281
        if line[Utils::Subscriptions::SUBS_SKU]
286 282
          already_attached = existing_subscriptions.detect do |subscription|
287
            line[SUBS_SKU] == subscription['product_id']
283
            line[Utils::Subscriptions::SUBS_SKU] == subscription['product_id']
288 284
          end
289
        elsif line[SUBS_NAME]
285
        elsif line[Utils::Subscriptions::SUBS_NAME]
290 286
          already_attached = existing_subscriptions.detect do |subscription|
291
            line[SUBS_NAME] == subscription['name']
287
            line[Utils::Subscriptions::SUBS_NAME] == subscription['name']
292 288
          end
293 289
        end
294 290
        if already_attached
......
324 320
        return if line[SUBSCRIPTIONS].nil? || line[SUBSCRIPTIONS].empty?
325 321

  
326 322
        subscriptions = CSV.parse_line(line[SUBSCRIPTIONS], {:skip_blanks => true}).collect do |details|
327
          (amount, sku, name, contract, account) = details.split('|')
323
          (amount, sku, name, contract, account) = split_subscription_details(details)
328 324
          {
329
            :id => katello_subscription(line[ORGANIZATION], :name => name, :contract => contract,
330
                                                            :account => account),
325
            :id => get_subscription(line[ORGANIZATION], :name => name, :contract => contract,
326
                                                        :account => account),
331 327
            :quantity => (amount.nil? || amount.empty? || amount == 'Automatic') ? 0 : amount.to_i
332 328
          }
333 329
        end
......
361 357
        end
362 358
      end
363 359

  
364
      def get_all_subscriptions(organization)
365
        @api.resource(:subscriptions).call(:index, {
366
            :per_page => 999999,
367
            'organization_id' => foreman_organization(:name => organization)
368
        })['results']
369
      end
370

  
371 360
      def iterate_hosts(csv)
372 361
        hypervisors = []
373 362
        hosts = []
......
436 425
         operatingsystem, architecture, sockets, ram, cores, sla, products]
437 426
      end
438 427

  
439
      def matches_by_sku_and_name(matches, line, subscriptions)
440
        if line[SUBS_SKU]
441
          matches = subscriptions.select do |subscription|
442
            line[SUBS_SKU] == subscription['product_id']
443
          end
444
          raise _("No subscriptions match SKU '%{sku}'") % {:sku => line[SUBS_SKU]} if matches.empty?
445
        elsif line[SUBS_NAME]
446
          matches = subscriptions.select do |subscription|
447
            line[SUBS_NAME] == subscription['name']
448
          end
449
          raise _("No subscriptions match name '%{name}'") % {:name => line[SUBS_NAME]} if matches.empty?
450
        end
451
        matches
452
      end
453

  
454
      def matches_by_type(matches, line)
455
        if line[SUBS_TYPE] == 'Red Hat' || line[SUBS_TYPE] == 'Custom'
456
          matches = matches.select do |subscription|
457
            subscription['type'] == 'NORMAL'
458
          end
459
        elsif line[SUBS_TYPE] == 'Red Hat Guest'
460
          matches = matches.select do |subscription|
461
            subscription['type'] == 'STACK_DERIVED'
462
          end
463
        elsif line[SUBS_TYPE] == 'Red Hat Temporary'
464
          matches = matches.select do |subscription|
465
            subscription['type'] == 'UNMAPPED_GUEST'
466
          end
467
        end
468
        raise _("No subscriptions match type '%{type}'") % {:type => line[SUBS_TYPE]} if matches.empty?
469
        matches
470
      end
471

  
472
      def matches_by_account(matches, line)
473
        if matches.length > 1 && line[SUBS_ACCOUNT]
474
          refined = matches.select do |subscription|
475
            line[SUBS_ACCOUNT] == subscription['account_number']
476
          end
477
          matches = refined unless refined.empty?
478
        end
479
        matches
480
      end
481

  
482
      def matches_by_contract(matches, line)
483
        if matches.length > 1 && line[SUBS_CONTRACT]
484
          refined = matches.select do |subscription|
485
            line[SUBS_CONTRACT] == subscription['contract_number']
486
          end
487
          matches = refined unless refined.empty?
488
        end
489
        matches
490
      end
491

  
492
      def matches_by_quantity(matches, line)
493
        if line[SUBS_QUANTITY] && line[SUBS_QUANTITY] != 'Automatic'
494
          refined = matches.select do |subscription|
495
            subscription['available'] == -1 || line[SUBS_QUANTITY].to_i <= subscription['available']
496
          end
497
          raise _("No '%{name}' subscription with quantity %{quantity} or more available") %
498
                    {:name => matches[0]['name'], :quantity => line[SUBS_QUANTITY]} if refined.empty?
499
          matches = refined
500
        end
501
        matches
502
      end
503 428
    end
504 429
  end
505 430
end

Also available in: Unified diff