Project

General

Profile

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

hammer-cli-csv / lib / hammer_cli_csv / activation_keys.rb @ 561a8ac9

1
# Copyright 2013-2014 Red Hat, Inc.
2
#
3
# This software is licensed to you under the GNU General Public
4
# License as published by the Free Software Foundation; either version
5
# 2 of the License (GPLv2) or (at your option) any later version.
6
# There is NO WARRANTY for this software, express or implied,
7
# including the implied warranties of MERCHANTABILITY,
8
# NON-INFRINGEMENT, or FITNESS FOR A PARTICULAR PURPOSE. You should
9
# have received a copy of GPLv2 along with this software; if not, see
10
# http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt.
11

    
12
module HammerCLICsv
13
  class CsvCommand
14
    class ActivationKeysCommand < BaseCommand
15
      command_name 'activation-keys'
16
      desc         'import or export activation keys'
17

    
18
      ORGANIZATION = 'Organization'
19
      DESCRIPTION = 'Description'
20
      LIMIT = 'Limit'
21
      ENVIRONMENT = 'Environment'
22
      CONTENTVIEW = 'Content View'
23
      SYSTEMGROUPS = 'System Groups'
24
      SUBSCRIPTIONS = 'Subscriptions'
25

    
26
      def export
27
        CSV.open(option_csv_file || '/dev/stdout', 'wb', {:force_quotes => false}) do |csv|
28
          csv << [NAME, COUNT, ORGANIZATION, DESCRIPTION, LIMIT, ENVIRONMENT, CONTENTVIEW,
29
                  SYSTEMGROUPS, SUBSCRIPTIONS]
30
          @api.resource(:organizations)\
31
            .call(:index, {
32
                    :per_page => 999999
33
                  })['results'].each do |organization|
34
            @api.resource(:activation_keys)\
35
              .call(:index, {
36
                      'per_page' => 999999,
37
                      'organization_id' => organization['id']
38
                    })['results'].each do |activationkey|
39
              puts "Writing activation key '#{activationkey['name']}'" if option_verbose?
40
              name = namify(activationkey['name'])
41
              count = 1
42
              description = activationkey['description']
43
              limit = activationkey['usage_limit'].to_i < 0 ? 'Unlimited' : sytemgroup['usage_limit']
44
              environment = activationkey['environment']['label']
45
              contentview = activationkey['content_view']['name']
46
              hostcollections = export_column(activationkey, 'systemGroups', 'name')
47
              subscriptions = CSV.generate do |column|
48
                column << @api.resource(:subscriptions).call(:index, {
49
                                                      'activation_key_id' => activationkey['id']
50
                                                    })['results'].collect do |subscription|
51
                  amount = subscription['amount'] == 0 ? 'Automatic' : subscription['amount']
52
                  "#{amount}|#{subscription['product_name']}"
53
                end
54
              end
55
              subscriptions.delete!("\n")
56
              csv << [name, count, organization['label'], description, limit, environment, contentview,
57
                      hostcollections, subscriptions]
58
            end
59
          end
60
        end
61
      end
62

    
63
      def import
64
        @existing = {}
65

    
66
        thread_import do |line|
67
          create_activationkeys_from_csv(line)
68
        end
69
      end
70

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

    
83
        line[COUNT].to_i.times do |number|
84
          name = namify(line[NAME], number)
85

    
86
          if !@existing[line[ORGANIZATION]].include? name
87
            print "Creating activation key '#{name}'..." if option_verbose?
88
            activationkey = @api.resource(:activation_keys)\
89
              .call(:create, {
90
                      'name' => name,
91
                      'environment_id' => lifecycle_environment(line[ORGANIZATION],
92
                                                                :name => line[ENVIRONMENT]),
93
                      'content_view_id' => katello_contentview(line[ORGANIZATION],
94
                                                               :name => line[CONTENTVIEW]),
95
                      'description' => line[DESCRIPTION],
96
                      'usage_limit' => usage_limit(line[LIMIT])
97
                    })
98
            @existing[line[ORGANIZATION]][activationkey['name']] = activationkey['id']
99
          else
100
            print "Updating activation key '#{name}'..." if option_verbose?
101
            activationkey = @api.resource(:activation_keys)\
102
              .call(:update, {
103
                      'id' => @existing[line[ORGANIZATION]][name],
104
                      'name' => name,
105
                      'environment_id' => lifecycle_environment(line[ORGANIZATION],
106
                                                                :name => line[ENVIRONMENT]),
107
                      'content_view_id' => katello_contentview(line[ORGANIZATION],
108
                                                               :name => line[CONTENTVIEW]),
109
                      'description' => line[DESCRIPTION],
110
                      'usage_limit' => usage_limit(line[LIMIT])
111
                    })
112
          end
113

    
114
          update_subscriptions(activationkey, line)
115
          update_groups(activationkey, line)
116

    
117
          puts 'done' if option_verbose?
118
        end
119
      end
120

    
121
      def update_groups(activationkey, line)
122
        if line[SYSTEMGROUPS] && line[SYSTEMGROUPS] != ''
123
          # TODO: note that existing system groups are not removed
124
          CSV.parse_line(line[SYSTEMGROUPS], {:skip_blanks => true}).each do |name|
125
            @api.resource(:host_collections)\
126
              .call(:add_activation_keys, {
127
                      'id' => katello_hostcollection(line[ORGANIZATION], :name => name),
128
                      'activation_key_ids' => [activationkey['id']]
129
                    })
130
          end
131
        end
132
      end
133

    
134
      def update_subscriptions(activationkey, line)
135
        if line[SUBSCRIPTIONS] && line[SUBSCRIPTIONS] != ''
136
          subscriptions = CSV.parse_line(line[SUBSCRIPTIONS], {:skip_blanks => true}).collect do |subscription_details|
137
            (amount, name) = subscription_details.split('|')
138
            {
139
              :id => katello_subscription(line[ORGANIZATION], :name => name),
140
              :quantity => amount
141
            }
142
          end
143

    
144
          # TODO: should there be a destroy_all similar to systems?
145
          @api.resource(:subscriptions)\
146
            .call(:index, {
147
                    'per_page' => 999999,
148
                    'activation_key_id' => activationkey['id']
149
                  })['results'].each do |subscription|
150
            @api.resource(:subscriptions)\
151
              .call(:destroy, {
152
                      'id' => subscription['id'],
153
                      'activation_key_id' => activationkey['id']
154
                    })
155
          end
156

    
157
          @api.resource(:subscriptions)\
158
            .call(:create, {
159
                    'activation_key_id' => activationkey['id'],
160
                    'subscriptions' => subscriptions
161
                  })
162
        end
163
      end
164

    
165
      def usage_limit(limit)
166
        Integer(limit) rescue -1
167
      end
168
    end
169
  end
170
end