Class: HammerCLICsv::CsvCommand::ActivationKeysCommand

Inherits:
BaseCommand
  • Object
show all
Defined in:
lib/hammer_cli_csv/activation_keys.rb

Constant Summary collapse

ORGANIZATION =
'Organization'
DESCRIPTION =
'Description'
LIMIT =
'Limit'
ENVIRONMENT =
'Environment'
CONTENTVIEW =
'Content View'
SYSTEMGROUPS =
'System Groups'
SUBSCRIPTIONS =
'Subscriptions'

Constants inherited from BaseCommand

BaseCommand::COUNT, BaseCommand::NAME

Instance Method Summary collapse

Methods inherited from BaseCommand

#associate_locations, #associate_organizations, #build_os_name, #check_server_status, #collect_column, #execute, #export_column, #foreman_architecture, #foreman_domain, #foreman_environment, #foreman_filter, #foreman_location, #foreman_operatingsystem, #foreman_organization, #foreman_partitiontable, #foreman_permission, #foreman_role, #foreman_template_kind, #hammer, #hammer_context, #katello_contentview, #katello_hostcollection, #katello_repository, #katello_subscription, #labelize, #lifecycle_environment, #namify, #pluralize, #split_os_name, #thread_import

Instance Method Details

#create_activationkeys_from_csv(line) ⇒ Object



97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/hammer_cli_csv/activation_keys.rb', line 97

def create_activationkeys_from_csv(line)
  if !@existing[line[ORGANIZATION]]
    @existing[line[ORGANIZATION]] = {}
    @api.resource(:activation_keys)\
      .call(:index, {
              'per_page' => 999999,
              'organization_id' => foreman_organization(:name => line[ORGANIZATION])
            })['results'].each do |activationkey|
      @existing[line[ORGANIZATION]][activationkey['name']] = activationkey['id'] if activationkey
    end
  end

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

    if !@existing[line[ORGANIZATION]].include? name
      print "Creating activation key '#{name}'..." if option_verbose?
      activationkey = @api.resource(:activation_keys)\
        .call(:create, {
                'name' => name,
                'environment_id' => lifecycle_environment(line[ORGANIZATION],
                                                          :name => line[ENVIRONMENT]),
                'content_view_id' => katello_contentview(line[ORGANIZATION],
                                                         :name => line[CONTENTVIEW]),
                'description' => line[DESCRIPTION],
                'usage_limit' => usage_limit(line[LIMIT])
              })
      @existing[line[ORGANIZATION]][activationkey['name']] = activationkey['id']
    else
      print "Updating activation key '#{name}'..." if option_verbose?
      activationkey = @api.resource(:activation_keys)\
        .call(:update, {
                'id' => @existing[line[ORGANIZATION]][name],
                'name' => name,
                'environment_id' => lifecycle_environment(line[ORGANIZATION],
                                                          :name => line[ENVIRONMENT]),
                'content_view_id' => katello_contentview(line[ORGANIZATION],
                                                         :name => line[CONTENTVIEW]),
                'description' => line[DESCRIPTION],
                'usage_limit' => usage_limit(line[LIMIT])
              })

      update_subscriptions(activationkey, line)
      update_groups(activationkey, line)
    end

    puts 'done' if option_verbose?
  end
end

#exportObject



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/hammer_cli_csv/activation_keys.rb', line 26

def export
  CSV.open(option_csv_file || '/dev/stdout', 'wb', {:force_quotes => false}) do |csv|
    csv << [NAME, COUNT, ORGANIZATION, DESCRIPTION, LIMIT, ENVIRONMENT, CONTENTVIEW,
            SYSTEMGROUPS, SUBSCRIPTIONS]
    if @server_status['release'] == 'Headpin'
      @headpin.get(:organizations).each do |organization|
        @headpin.get("organizations/#{organization['label']}/activation_keys").each do |activationkey|
          puts "Writing activation key '#{activationkey['name']}'" if option_verbose?
          name = namify(activationkey['name'])
          count = 1
          description = activationkey['description']
          limit = activationkey['usage_limit'].to_i < 0 ? 'Unlimited' : activationkey['usage_limit']
          environment = @headpin.environment(activationkey['environment_id'])['name']
          contentview = @headpin.content_view(activationkey['content_view_id'])['name']
          # TODO: https://bugzilla.redhat.com/show_bug.cgi?id=1160888
          #       Act keys in SAM-1 do not include system groups
          hostcollections = nil #???? export_column(activationkey, 'systemGroups', 'name')
          subscriptions = CSV.generate do |column|
            column << activationkey['pools'].collect do |subscription|
              amount = subscription['calculatedAttributes']['compliance_type'] == 'Stackable' ? 1 : 'Automatic'
              "#{amount}|#{subscription['productId']}|#{subscription['productName']}"
            end
          end
          subscriptions.delete!("\n")
          csv << [name, count, organization['label'], description, limit, environment, contentview,
                  hostcollections, subscriptions]
        end
      end
    else
      @api.resource(:organizations)\
        .call(:index, {
                :per_page => 999999
              })['results'].each do |organization|
        @api.resource(:activation_keys)\
          .call(:index, {
                  'per_page' => 999999,
                  'organization_id' => organization['id']
                })['results'].each do |activationkey|
          puts "Writing activation key '#{activationkey['name']}'" if option_verbose?
          name = namify(activationkey['name'])
          count = 1
          description = activationkey['description']
          limit = activationkey['usage_limit'].to_i < 0 ? 'Unlimited' : sytemgroup['usage_limit']
          environment = activationkey['environment']['label']
          contentview = activationkey['content_view']['name']
          hostcollections = export_column(activationkey, 'systemGroups', 'name')
          subscriptions = CSV.generate do |column|
            column << @api.resource(:subscriptions).call(:index, {
                                                  'activation_key_id' => activationkey['id']
                                                })['results'].collect do |subscription|
              amount = subscription['amount'] == 0 ? 'Automatic' : subscription['amount']
              "#{amount}|#{subscription['product_name']}"
            end
          end
          subscriptions.delete!("\n")
          csv << [name, count, organization['label'], description, limit, environment, contentview,
                  hostcollections, subscriptions]
        end
      end
    end
  end
end

#importObject



89
90
91
92
93
94
95
# File 'lib/hammer_cli_csv/activation_keys.rb', line 89

def import
  @existing = {}

  thread_import do |line|
    create_activationkeys_from_csv(line)
  end
end

#update_groups(activationkey, line) ⇒ Object



147
148
149
150
151
152
153
154
155
156
157
158
# File 'lib/hammer_cli_csv/activation_keys.rb', line 147

def update_groups(activationkey, line)
  if line[SYSTEMGROUPS] && line[SYSTEMGROUPS] != ''
    # TODO: note that existing system groups are not removed
    CSV.parse_line(line[SYSTEMGROUPS], {:skip_blanks => true}).each do |name|
      @api.resource(:host_collections)\
        .call(:add_activation_keys, {
                'id' => katello_hostcollection(line[ORGANIZATION], :name => name),
                'activation_key_ids' => [activationkey['id']]
              })
    end
  end
end

#update_subscriptions(activationkey, line) ⇒ Object



160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
# File 'lib/hammer_cli_csv/activation_keys.rb', line 160

def update_subscriptions(activationkey, line)
  if line[SUBSCRIPTIONS] && line[SUBSCRIPTIONS] != ''
    subscriptions = CSV.parse_line(line[SUBSCRIPTIONS], {:skip_blanks => true}).collect do |subscription_details|
      (amount, name) = subscription_details.split('|')
      {
        :id => katello_subscription(line[ORGANIZATION], :name => name),
        :quantity => amount
      }
    end

    # TODO: should there be a destroy_all similar to systems?
    @api.resource(:subscriptions)\
      .call(:index, {
              'per_page' => 999999,
              'activation_key_id' => activationkey['id']
            })['results'].each do |subscription|
      @api.resource(:subscriptions)\
        .call(:destroy, {
                'id' => subscription['id'],
                'activation_key_id' => activationkey['id']
              })
    end

    @api.resource(:subscriptions)\
      .call(:create, {
              'activation_key_id' => activationkey['id'],
              'subscriptions' => subscriptions
            })
  end
end

#usage_limit(limit) ⇒ Object



191
192
193
# File 'lib/hammer_cli_csv/activation_keys.rb', line 191

def usage_limit(limit)
  Integer(limit) rescue -1
end