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'
HOSTCOLLECTIONS =
'Host Collections'
SERVICELEVEL =
"Service Level"
RELEASEVER =
"Release Version"
AUTOATTACH =
"Auto-Attach"
SUBSCRIPTIONS =
'Subscriptions'

Constants inherited from BaseCommand

BaseCommand::COUNT, BaseCommand::NAME

Instance Method Summary collapse

Methods inherited from BaseCommand

#apipie_check_param, #associate_locations, #associate_organizations, #build_os_name, #check_server_status, #collect_column, #count, #execute, #export_column, #foreman_architecture, #foreman_container, #foreman_domain, #foreman_environment, #foreman_filter, #foreman_host, #foreman_hostgroup, #foreman_location, #foreman_operatingsystem, #foreman_organization, #foreman_partitiontable, #foreman_permission, #foreman_provisioning_template, #foreman_role, #foreman_smart_proxy, #foreman_template_kind, #hammer, #hammer_context, #katello_contentview, #katello_contentviewversion, #katello_hostcollection, #katello_product, #katello_repository, #katello_subscription, #labelize, #lifecycle_environment, #namify, #pluralize, #split_os_name, #thread_import

Instance Method Details

#create_activationkeys_from_csv(line) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/hammer_cli_csv/activation_keys.rb', line 67

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

  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

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

    params = {
               'organization_id' => foreman_organization(:name => line[ORGANIZATION]),
               'name' => name,
               'environment_id' => lifecycle_environment(line[ORGANIZATION],
                                                         :name => line[ENVIRONMENT]),
               'content_view_id' => katello_contentview(line[ORGANIZATION],
                                                        :name => line[CONTENTVIEW]),
               'description' => line[DESCRIPTION],
               'unlimited_content_hosts' => (line[LIMIT] == 'Unlimited') ? true : false,
               'max_content_hosts' => (line[LIMIT] == 'Unlimited') ? nil : line[LIMIT].to_i
             }
    params['auto_attach'] = (line[AUTOATTACH] == 'Yes' ? true : false) if params['auto_attach']
    params['service_level'] = line[SERVICELEVEL].nil? || line[SERVICELEVEL].empty? ? nil : line[SERVICELEVEL]
    params['release_version'] = line[RELEASEVER].nil? || line[RELEASEVER].empty? ? nil : line[RELEASEVER]
    if !@existing[line[ORGANIZATION]].include? name
      print _("Creating activation key '%{name}'...") % {:name => name} if option_verbose?
      activationkey = @api.resource(:activation_keys).call(:create, params)
      @existing[line[ORGANIZATION]][activationkey['name']] = activationkey['id']
    else
      print _("Updating activation key '%{name}'...") % {:name => name} if option_verbose?
      params['id'] = @existing[line[ORGANIZATION]][name]
      activationkey = @api.resource(:activation_keys).call(:update, params)
    end

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

    puts _('done') if option_verbose?
  end
end

#exportObject



18
19
20
21
22
23
24
25
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
# File 'lib/hammer_cli_csv/activation_keys.rb', line 18

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

      @api.resource(:activation_keys).call(:index, {
          'per_page' => 999999,
          'organization_id' => organization['id']
      })['results'].each do |activationkey|
        name = namify(activationkey['name'])
        count = 1
        description = activationkey['description']
        limit = activationkey['unlimited_content_hosts'] ? 'Unlimited' : activationkey['max_content_hosts']
        environment = activationkey['environment']['label']
        contentview = activationkey['content_view']['name']
        hostcollections = export_column(activationkey, 'host_collections', 'name')
        autoattach = activationkey['auto_attach'] ? 'Yes' : 'No'
        servicelevel = activationkey['service_level']
        releasever = activationkey['release_version']
        subscriptions = CSV.generate do |column|
          column << @api.resource(:subscriptions).call(:index, {
                        'organization_id' => organization['id'],
                        'activation_key_id' => activationkey['id']
                    })['results'].collect do |subscription|
            amount = subscription['amount'] == 0 ? 'Automatic' : subscription['amount']
            sku = subscription['product_id'].match(/\A[0-9]/) ? 'Custom' : subscription['product_id']
            "#{amount}|#{sku}|#{subscription['product_name']}"
          end
        end
        subscriptions.delete!("\n")
        csv << [name, count, organization['name'], description, limit, environment, contentview,
                hostcollections, servicelevel, releasever, autoattach, subscriptions]
      end
    end
  end
end

#importObject



59
60
61
62
63
64
65
# File 'lib/hammer_cli_csv/activation_keys.rb', line 59

def import
  @existing = {}

  thread_import do |line|
    create_activationkeys_from_csv(line)
  end
end

#update_groups(activationkey, line) ⇒ Object



114
115
116
117
118
119
120
121
122
123
124
# File 'lib/hammer_cli_csv/activation_keys.rb', line 114

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

#update_subscriptions(activationkey, line) ⇒ Object



126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# File 'lib/hammer_cli_csv/activation_keys.rb', line 126

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

    existing_subscriptions = @api.resource(:subscriptions).call(:index, {
        'organization_id' => foreman_organization(:name => line[ORGANIZATION]),
        'per_page' => 999999,
        'activation_key_id' => activationkey['id']
    })['results']
    if existing_subscriptions.length > 0
      @api.resource(:activation_keys).call(:remove_subscriptions, {
        'id' => activationkey['id'],
        'subscriptions' => existing_subscriptions
      })
    end

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

#usage_limit(limit) ⇒ Object



155
156
157
# File 'lib/hammer_cli_csv/activation_keys.rb', line 155

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