Project

General

Profile

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

hammer-cli-csv / lib / hammer_cli_csv / activation_keys.rb @ 5146ac68

1
module HammerCLICsv
2
  class CsvCommand
3
    class ActivationKeysCommand < BaseCommand
4
      command_name 'activation-keys'
5
      desc         _('import or export activation keys')
6

    
7
      ORGANIZATION = 'Organization'
8
      DESCRIPTION = 'Description'
9
      LIMIT = 'Limit'
10
      ENVIRONMENT = 'Environment'
11
      CONTENTVIEW = 'Content View'
12
      HOSTCOLLECTIONS = 'Host Collections'
13
      SERVICELEVEL = "Service Level"
14
      RELEASEVER = "Release Version"
15
      AUTOATTACH = "Auto-Attach"
16
      SUBSCRIPTIONS = 'Subscriptions'
17

    
18
      def export
19
        CSV.open(option_file || '/dev/stdout', 'wb', {:force_quotes => false}) do |csv|
20
          csv << [NAME, ORGANIZATION, DESCRIPTION, LIMIT, ENVIRONMENT, CONTENTVIEW,
21
                  HOSTCOLLECTIONS, AUTOATTACH, SERVICELEVEL, RELEASEVER, SUBSCRIPTIONS]
22
          @api.resource(:organizations).call(:index, {
23
              :per_page => 999999
24
          })['results'].each do |organization|
25
            next if option_organization && organization['name'] != option_organization
26

    
27
            @api.resource(:activation_keys).call(:index, {
28
                'per_page' => 999999,
29
                'organization_id' => organization['id']
30
            })['results'].each do |activationkey|
31
              name = namify(activationkey['name'])
32
              count = 1
33
              description = activationkey['description']
34
              limit = activationkey['unlimited_content_hosts'] ? 'Unlimited' : activationkey['max_content_hosts']
35
              environment = activationkey['environment']['label']
36
              contentview = activationkey['content_view']['name']
37
              hostcollections = export_column(activationkey, 'host_collections', 'name')
38
              autoattach = activationkey['auto_attach'] ? 'Yes' : 'No'
39
              servicelevel = activationkey['service_level']
40
              releasever = activationkey['release_version']
41
              subscriptions = CSV.generate do |column|
42
                column << @api.resource(:subscriptions).call(:index, {
43
                              'organization_id' => organization['id'],
44
                              'activation_key_id' => activationkey['id']
45
                          })['results'].collect do |subscription|
46
                  amount = subscription['amount'] == 0 ? 'Automatic' : subscription['amount']
47
                  sku = subscription['product_id'].match(/\A[0-9]/) ? 'Custom' : subscription['product_id']
48
                  "#{amount}|#{sku}|#{subscription['product_name']}"
49
                end
50
              end
51
              subscriptions.delete!("\n")
52
              csv << [name, count, organization['name'], description, limit, environment, contentview,
53
                      hostcollections, servicelevel, releasever, autoattach, subscriptions]
54
            end
55
          end
56
        end
57
      end
58

    
59
      def import
60
        @existing = {}
61

    
62
        thread_import do |line|
63
          create_activationkeys_from_csv(line)
64
        end
65
      end
66

    
67
      def create_activationkeys_from_csv(line)
68
        return if option_organization && line[ORGANIZATION] != option_organization
69

    
70
        if !@existing[line[ORGANIZATION]]
71
          @existing[line[ORGANIZATION]] = {}
72
          @api.resource(:activation_keys).call(:index, {
73
              'per_page' => 999999,
74
              'organization_id' => foreman_organization(:name => line[ORGANIZATION])
75
          })['results'].each do |activationkey|
76
            @existing[line[ORGANIZATION]][activationkey['name']] = activationkey['id'] if activationkey
77
          end
78
        end
79

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

    
83
          params = {
84
                     'organization_id' => foreman_organization(:name => line[ORGANIZATION]),
85
                     'name' => name,
86
                     'environment_id' => lifecycle_environment(line[ORGANIZATION],
87
                                                               :name => line[ENVIRONMENT]),
88
                     'content_view_id' => katello_contentview(line[ORGANIZATION],
89
                                                              :name => line[CONTENTVIEW]),
90
                     'description' => line[DESCRIPTION],
91
                     'unlimited_content_hosts' => (line[LIMIT] == 'Unlimited') ? true : false,
92
                     'max_content_hosts' => (line[LIMIT] == 'Unlimited') ? nil : line[LIMIT].to_i
93
                   }
94
          params['auto_attach'] = (line[AUTOATTACH] == 'Yes' ? true : false) if params['auto_attach']
95
          params['service_level'] = line[SERVICELEVEL].nil? || line[SERVICELEVEL].empty? ? nil : line[SERVICELEVEL]
96
          params['release_version'] = line[RELEASEVER].nil? || line[RELEASEVER].empty? ? nil : line[RELEASEVER]
97
          if !@existing[line[ORGANIZATION]].include? name
98
            print _("Creating activation key '%{name}'...") % {:name => name} if option_verbose?
99
            activationkey = @api.resource(:activation_keys).call(:create, params)
100
            @existing[line[ORGANIZATION]][activationkey['name']] = activationkey['id']
101
          else
102
            print _("Updating activation key '%{name}'...") % {:name => name} if option_verbose?
103
            params['id'] = @existing[line[ORGANIZATION]][name]
104
            activationkey = @api.resource(:activation_keys).call(:update, params)
105
          end
106

    
107
          update_subscriptions(activationkey, line)
108
          update_groups(activationkey, line)
109

    
110
          puts _('done') if option_verbose?
111
        end
112
      end
113

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

    
126
      def update_subscriptions(activationkey, line)
127
        if line[SUBSCRIPTIONS] && line[SUBSCRIPTIONS] != ''
128
          subscriptions = CSV.parse_line(line[SUBSCRIPTIONS], {:skip_blanks => true}).collect do |subscription_details|
129
            (amount, sku, name) = subscription_details.split('|')
130
            {
131
              :id => katello_subscription(line[ORGANIZATION], :name => name),
132
              :quantity => (amount.nil? || amount == 'Automatic') ? 0 : amount
133
            }
134
          end
135

    
136
          existing_subscriptions = @api.resource(:subscriptions).call(:index, {
137
              'organization_id' => foreman_organization(:name => line[ORGANIZATION]),
138
              'per_page' => 999999,
139
              'activation_key_id' => activationkey['id']
140
          })['results']
141
          if existing_subscriptions.length > 0
142
            @api.resource(:activation_keys).call(:remove_subscriptions, {
143
              'id' => activationkey['id'],
144
              'subscriptions' => existing_subscriptions
145
            })
146
          end
147

    
148
          @api.resource(:activation_keys).call(:add_subscriptions, {
149
              'id' => activationkey['id'],
150
              'subscriptions' => subscriptions
151
          })
152
        end
153
      end
154

    
155
      def usage_limit(limit)
156
        Integer(limit) rescue -1
157
      end
158
    end
159
  end
160
end