Project

General

Profile

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

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

1 611959a3 Tom McKay
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
14
      def get_all_subscriptions(organization)
15
        @api.resource(:subscriptions).call(:index, {
16
            :per_page => 999999,
17
            'organization_id' => foreman_organization(:name => organization)
18
        })['results']
19
      end
20
21
      def get_subscription(organization, options = {})
22
        @subscriptions ||= {}
23
        @subscriptions[organization] ||= {}
24
25
        if options[:name]
26
          return nil if options[:name].nil? || options[:name].empty?
27
          options[:id] = @subscriptions[organization][options[:name]]
28
          if !options[:id]
29
            results = @api.resource(:subscriptions).call(:index, {
30
                :per_page => 999999,
31
                'organization_id' => foreman_organization(:name => organization),
32
                'search' => "name = \"#{options[:name]}\""
33
            })
34
            raise "No subscriptions match '#{options[:name]}'" if results['subtotal'] == 0
35
            raise "Too many subscriptions match '#{options[:name]}'" if results['subtotal'] > 1
36
            subscription = results['results'][0]
37
            @subscriptions[organization][options[:name]] = subscription['id']
38
            options[:id] = @subscriptions[organization][options[:name]]
39
            raise "Subscription '#{options[:name]}' not found" if !options[:id]
40
          end
41
          result = options[:id]
42
        else
43
          return nil if options[:id].nil?
44
          options[:name] = @subscriptions.key(options[:id])
45
          if !options[:name]
46
            subscription = @api.resource(:subscriptions).call(:show, {'id' => options[:id]})
47
            raise "Subscription '#{options[:name]}' not found" if !subscription || subscription.empty?
48
            options[:name] = subscription['name']
49
            @subscriptions[options[:name]] = options[:id]
50
          end
51
          result = options[:name]
52
        end
53
54
        result
55
      end
56
57
      def matches_by_sku_and_name(matches, line, subscriptions)
58
        if line[SUBS_SKU]
59
          matches = subscriptions.select do |subscription|
60
            line[SUBS_SKU] == subscription['product_id']
61
          end
62
          raise _("No subscriptions match SKU '%{sku}'") % {:sku => line[SUBS_SKU]} if matches.empty?
63
        elsif line[SUBS_NAME]
64
          matches = subscriptions.select do |subscription|
65
            line[SUBS_NAME] == subscription['name']
66
          end
67
          raise _("No subscriptions match name '%{name}'") % {:name => line[SUBS_NAME]} if matches.empty?
68
        end
69
        matches
70
      end
71
72
      def matches_by_type(matches, line)
73
        if line[SUBS_TYPE] == 'Red Hat' || line[SUBS_TYPE] == 'Custom'
74
          matches = matches.select do |subscription|
75
            subscription['type'] == 'NORMAL'
76
          end
77
        elsif line[SUBS_TYPE] == 'Red Hat Guest'
78
          matches = matches.select do |subscription|
79
            subscription['type'] == 'STACK_DERIVED'
80
          end
81
        elsif line[SUBS_TYPE] == 'Red Hat Temporary'
82
          matches = matches.select do |subscription|
83
            subscription['type'] == 'UNMAPPED_GUEST'
84
          end
85
        end
86
        raise _("No subscriptions match type '%{type}'") % {:type => line[SUBS_TYPE]} if matches.empty?
87
        matches
88
      end
89
90
      def matches_by_account(matches, line)
91
        if matches.length > 1 && line[SUBS_ACCOUNT]
92
          refined = matches.select do |subscription|
93
            line[SUBS_ACCOUNT] == subscription['account_number']
94
          end
95
          matches = refined unless refined.empty?
96
        end
97
        matches
98
      end
99
100
      def matches_by_contract(matches, line)
101
        if matches.length > 1 && line[SUBS_CONTRACT]
102
          refined = matches.select do |subscription|
103
            line[SUBS_CONTRACT] == subscription['contract_number']
104
          end
105
          matches = refined unless refined.empty?
106
        end
107
        matches
108
      end
109
110
      def matches_by_quantity(matches, line)
111
        if line[SUBS_QUANTITY] && line[SUBS_QUANTITY] != 'Automatic'
112
          refined = matches.select do |subscription|
113
            subscription['available'] == -1 || line[SUBS_QUANTITY].to_i <= subscription['available']
114
          end
115
          raise _("No '%{name}' subscription with quantity %{quantity} or more available") %
116
            {:name => matches[0]['name'], :quantity => line[SUBS_QUANTITY]} if refined.empty?
117
          matches = refined
118
        end
119
        matches
120
      end
121
122 d76bdbc0 Andrew Kofink
      def match_with_quantity_to_attach(match, line)
123
        if line[SUBS_QUANTITY] && line[SUBS_QUANTITY] != 'Automatic' && !line[SUBS_QUANTITY].empty?
124
          match['quantity'] = line[SUBS_QUANTITY]
125
        else
126
          match['quantity'] = -1
127
        end
128
        match
129
      end
130
131 611959a3 Tom McKay
      # Subscription amount, SKU, name, contract number, and account number separated by '|'
132
      # or simply the subscription name.
133
      def split_subscription_details(details)
134
        details = details.split('|')
135
        details.length == 1 ? ['Automatic', nil, details[0], nil, nil] : details
136
      end
137
    end
138
  end
139
end