Project

General

Profile

Revision 3061c66a

Added by Thomas McKay over 6 years ago

fixes #____ - subscriptions manifest, dates

View differences:

lib/hammer_cli_csv/subscriptions.rb
3 3
module HammerCLICsv
4 4
  class CsvCommand
5 5
    class SubscriptionsCommand < BaseCommand
6
      include ::HammerCLICsv::Utils::Subscriptions
7

  
6 8
      command_name 'subscriptions'
7 9
      desc         'import or export subscriptions'
8 10

  
......
24 26

  
25 27
      ORGANIZATION = 'Organization'
26 28
      MANIFEST = 'Manifest File'
27
      SUBSCRIPTION = 'Subscription Name'
28
      QUANTITY = 'Quantity'
29
      SKU = 'Product SKU'
30
      CONTRACT = 'Contract Number'
31
      ACCOUNT = 'Account Number'
32 29

  
33 30
      def export(csv)
34
        csv << [NAME, ORGANIZATION, MANIFEST, SUBSCRIPTION, QUANTITY, SKU, CONTRACT, ACCOUNT]
31
        csv << [NAME, ORGANIZATION, MANIFEST, SUBS_NAME, SUBS_QUANTITY, SUBS_SKU, SUBS_CONTRACT, SUBS_ACCOUNT, SUBS_START, SUBS_END]
35 32
        @api.resource(:organizations).call(:index, {:per_page => 999999})['results'].each do |organization|
36 33
          next if option_organization && organization['name'] != option_organization
37 34
          organization = @api.resource(:organizations).call(:show, {'id' => organization['id']})
......
69 66
        else
70 67
          thread_import do |line|
71 68
            if line[NAME] == 'Manifest' && line[MANIFEST] && !line[MANIFEST].empty?
72
              import_manifest(line)
69
              import_manifest(line[ORGANIZATION], line[MANIFEST])
73 70
            end
74 71
          end
75 72
        end
76 73
      end
77 74

  
78
      def import_manifest(line)
79
        return if option_organization && line[ORGANIZATION] != option_organization
75
      def import_manifest(organization_name, filename)
76
        return if option_organization && organization_name != option_organization
77
        print(_("Importing manifest '%{filename}' into organization '%{organization}'...") % {:filename => filename, :organization => organization_name}) if option_verbose?
80 78
        args = %W{
81 79
          --server #{ @server } --username #{ @username } --password #{ @password }
82
          subscription upload --file #{ line[MANIFEST] }
83
          --organization-id #{ foreman_organization(:name => line[ORGANIZATION]) }
80
          subscription upload --file #{ filename }
81
          --organization-id #{ foreman_organization(:name => organization_name) }
84 82
        }
85 83
        hammer.run(args)
84
        puts(_("done")) if option_verbose?
86 85
      end
87 86

  
88 87
      def import_into_portal
......
109 108
            f.write data
110 109
          end
111 110
          puts _("done") if option_verbose?
111
          import_manifest(organization, filename)
112 112
        end
113 113
      end
114 114

  
......
126 126
        when "Subscription"
127 127
          manifest = @manifests[line[ORGANIZATION]][:manifest]
128 128
          raise _('Manifest Name row is required before updating from Subscription rows') unless manifest
129
          line[QUANTITY] = line[QUANTITY].to_i  #guarantee integer for future calculations
129
          line[SUBS_QUANTITY] = line[SUBS_QUANTITY].to_i  #guarantee integer for future calculations
130 130
          add_subscription(line, manifest)
131 131
        else
132 132
          # ignore
......
136 136
      def add_subscription(line, manifest)
137 137
        if find_existing_subscription(line, manifest)
138 138
          puts _("'%{name}' of quantity %{quantity} already attached") %
139
                  {:name => line[SUBSCRIPTION], :quantity => line[QUANTITY]} if option_verbose?
139
                  {:name => line[SUBS_NAME], :quantity => line[SUBS_QUANTITY]} if option_verbose?
140 140
          return
141 141
        end
142 142
        print _("Attaching '%{name}' of quantity %{quantity}...") %
143
                {:name => line[SUBSCRIPTION], :quantity => line[QUANTITY]} if option_verbose?
143
                {:name => line[SUBS_NAME], :quantity => line[SUBS_QUANTITY]} if option_verbose?
144 144
        manifest['available_subscriptions'] ||= get_available_subscriptions(manifest)
145 145
        attach_subscription(line, manifest)
146 146
        puts _('done')
......
148 148

  
149 149
      def attach_subscription(line, manifest)
150 150
        manifest['available_subscriptions'].each do |subscription|
151
          if subscription['productId'] == line[SKU] && subscription['quantity'] >= line[QUANTITY]
152
            api = rest_client("/subscription/consumers/#{manifest['uuid']}/entitlements?pool=#{subscription['id']}&quantity=#{line[QUANTITY]}")
151
          if subscription['productId'] == line[SUBS_SKU] && subscription['quantity'] >= line[SUBS_QUANTITY]
152
            api = rest_client("/subscription/consumers/#{manifest['uuid']}/entitlements?pool=#{subscription['id']}&quantity=#{line[SUBS_QUANTITY]}")
153 153
            results = api.post({}.to_json)
154
            subscription['quantity'] -= line[QUANTITY]
154
            subscription['quantity'] -= line[SUBS_QUANTITY]
155 155
            break
156 156
          end
157 157
        end
......
164 164

  
165 165
      def find_existing_subscription(line, manifest)
166 166
        manifest['subscriptions'].each do |subscription|
167
          if !subscription['csv_matched'] && subscription['pool']['productId'] == line[SKU] && subscription['quantity'] == line[QUANTITY]
167
          if !subscription['csv_matched'] && subscription['pool']['productId'] == line[SUBS_SKU] && subscription['quantity'] == line[SUBS_QUANTITY]
168 168
            subscription['csv_matched'] = true
169 169
            return true
170 170
          end

Also available in: Unified diff