Project

General

Profile

Download (7.67 KB) Statistics
| Branch: | Tag: | Revision:

hammer-cli-csv / lib / hammer_cli_csv / utils / subscriptions.rb @ f83015fe

1
module HammerCLICsv
2
  module Utils
3
    module Subscriptions
4
      SUBSCRIPTIONS = 'Subscriptions'
5
      SUBS_NAME = 'Subscription Name'
6
      SUBS_TYPE = 'Subscription Type'
7
      SUBS_QUANTITY = 'Subscription Quantity'
8
      SUBS_SKU = 'Subscription SKU'
9
      SUBS_CONTRACT = 'Subscription Contract'
10
      SUBS_ACCOUNT = 'Subscription Account'
11
      SUBS_START = 'Subscription Start'
12
      SUBS_END = 'Subscription End'
13
      SUBS_GUESTOF = 'Subscription Guest'
14

    
15
      def get_all_subscriptions(organization)
16
        @api.resource(:subscriptions).call(:index, {
17
            'full_results' => true,
18
            'organization_id' => foreman_organization(:name => organization)
19
        })['results']
20
      end
21

    
22
      def get_matching_subscriptions(organization_id, options = {})
23
        logger.debug("get_matching_subscriptions: #{options}")
24
        if options[:host]
25
          available_subscriptions = @api.resource(:subscriptions).call(:index, {
26
            'full_results' => true,
27
            'organization_id' => organization_id,
28
            'host_id' => options[:host]['id'],
29
            'available_for' => 'host',
30
            'match_host' => true
31
          })['results']
32
        elsif options[:activation_key]
33
          available_subscriptions = @api.resource(:subscriptions).call(:index, {
34
            'full_results' => true,
35
            'organization_id' => organization_id,
36
            'activation_key_id' => options[:activation_key]['id'],
37
            'available_for' => 'activation_key'
38
          })['results']
39
        else
40
          available_subscriptions = @api.resource(:subscriptions).call(:index, {
41
              'full_results' => true,
42
              'organization_id' => organization_id
43
          })['results']
44
        end
45

    
46
        debug_subscriptions('available_subscriptions', available_subscriptions)
47
        matches = matches_by_sku_and_name([], options[:sku], options[:name], available_subscriptions)
48
        matches = matches_by_type(matches, options[:type])
49
        matches = matches_by_hypervisor(matches, options[:hypervisor])
50
        matches = matches_by_account(matches, options[:account])
51
        matches = matches_by_contract(matches, options[:contract])
52
        matches = matches_by_sla(matches, options[:sla])
53
        matches = matches_by_quantity(matches, options[:quantity]) unless options[:activation_key]
54

    
55
        matches
56
      end
57

    
58
      def matches_by_sku_and_name(matches, subs_sku, subs_name, subscriptions)
59
        return matches if subscriptions.empty?
60

    
61
        if subs_sku
62
          matches = subscriptions.select do |subscription|
63
            subs_sku == subscription['product_id']
64
          end
65
          raise _("No subscriptions match SKU '%{sku}'") % {:sku => subs_sku} if matches.empty?
66
        elsif subs_name
67
          matches = subscriptions.select do |subscription|
68
            subs_name == subscription['name']
69
          end
70
        end
71
        debug_subscriptions("matches_by_sku_and_name: #{subs_sku}|#{subs_name}", matches)
72
        matches
73
      end
74

    
75
      def matches_by_hypervisor(matches, subs_hypervisor)
76
        return matches if matches.empty?
77

    
78
        if !subs_hypervisor.nil? && !subs_hypervisor.empty?
79
          matches.select! do |subscription|
80
            !subscription['host'].nil? && subscription['host']['name'] == subs_hypervisor
81
          end
82
          if matches.empty? && subs_hypervisor =~ /virt-who-/
83
            subs_hypervisor = subs_hypervisor.split('-')[2..-2].join('-')
84
            matches.select! do |subscription|
85
              !subscription['host'].nil? && subscription['host']['name'] == subs_hypervisor
86
            end
87
          end
88
        else
89
          matches.select! do |subscription|
90
            subscription['host'].nil?
91
          end
92
        end
93
        debug_subscriptions("matches_by_hypervisor: #{subs_hypervisor}", matches)
94
        matches
95
      end
96

    
97
      def matches_by_sla(matches, subs_sla)
98
        return matches if matches.empty?
99

    
100
        if !subs_sla.nil? && !subs_sla.empty?
101
          found = matches.select do |subscription|
102
            subscription['sla'] == subs_sla
103
          end
104
          # Fallback to subscriptions w/o sla set
105
          if found.empty?
106
            found = matches.select do |subscription|
107
              subscription['sla'].nil? || subscription['sla'].empty?
108
            end
109
          end
110
          matches = found
111
        end
112
        debug_subscriptions("matches_by_sla: #{subs_sla}", matches)
113
        matches
114
      end
115

    
116
      def matches_by_type(matches, subs_type)
117
        return matches if matches.empty?
118

    
119
        if subs_type == 'Red Hat' || subs_type == 'Custom'
120
          matches.select! do |subscription|
121
            subscription['type'] == 'NORMAL'
122
          end
123
        elsif subs_type == 'Red Hat Guest'
124
          matches.select! do |subscription|
125
            !subscription['host'].nil? && !subscription['host'].empty?
126
          end
127
        elsif subs_type == 'Red Hat Temporary'
128
          matches.select! do |subscription|
129
            subscription['type'] == 'UNMAPPED_GUEST'
130
          end
131
        end
132
        debug_subscriptions("matches_type: #{subs_type}", matches)
133
        matches
134
      end
135

    
136
      def matches_by_account(matches, subs_account)
137
        return matches if matches.empty?
138

    
139
        if matches.length > 1 && subs_account
140
          refined = matches.select do |subscription|
141
            subs_account == subscription['account_number']
142
          end
143
          matches = refined unless refined.empty?
144
        end
145
        debug_subscriptions("matches_by_account: #{subs_account}", matches)
146
        matches
147
      end
148

    
149
      def matches_by_contract(matches, subs_contract)
150
        return matches if matches.empty?
151

    
152
        if matches.length > 1 && subs_contract
153
          refined = matches.select do |subscription|
154
            subs_contract == subscription['contract_number']
155
          end
156
          matches = refined unless refined.empty?
157
        end
158
        debug_subscriptions("matches_by_contract: #{subs_contract}", matches)
159
        matches
160
      end
161

    
162
      def matches_by_quantity(matches, subs_quantity)
163
        return matches if matches.empty?
164

    
165
        matches.select! do |subscription|
166
          subscription['available'] != 0
167
        end
168

    
169
        if !subs_quantity.nil? && !subs_quantity.empty? && subs_quantity != 'Automatic'
170
          subs_quantity = subs_quantity.to_i
171
          matches.select! do |subscription|
172
            subscription['available'] < 0 || subs_quantity <= subscription['available']
173
          end
174
        end
175
        debug_subscriptions("matches_by_quantity: #{subs_quantity}", matches)
176
        matches
177
      end
178

    
179
      def match_with_quantity_to_attach(match, subs_quantity)
180
        if subs_quantity && subs_quantity != 'Automatic' && !subs_quantity.empty?
181
          match['quantity'] = subs_quantity
182
        else
183
          match['quantity'] = -1
184
        end
185
        match
186
      end
187

    
188
      def subscription_name(subscription)
189
        if subscription['host'].nil?
190
          subscription['name']
191
        else
192
          "#{subscription['name']} - Guest of #{subscription['host']['name']}"
193
        end
194
      end
195

    
196
      # Subscription amount, SKU, name, contract number, and account number separated by '|'
197
      # or simply the subscription name.
198
      def split_subscription_details(details)
199
        details = details.split('|')
200
        details.length == 1 ? ['Automatic', nil, details[0], nil, nil] : details
201
      end
202

    
203
      def debug_subscriptions(description, subscriptions)
204
        logger.debug(description)
205
        subscriptions.each do |subscription|
206
          logger.debug "#{subscription['quantity_consumed']}"\
207
                       "|#{subscription['product_id']}"\
208
                       "|#{subscription['product_name']}"\
209
                       "|#{subscription['contract_number']}|#{subscription['account_number']}"
210
        end
211
      end
212
    end
213
  end
214
end