Class: HammerCLICsv::CsvCommand::ActivationKeysCommand
- Inherits:
-
BaseCommand
- Object
- HammerCLI::Apipie::Command
- BaseCommand
- HammerCLICsv::CsvCommand::ActivationKeysCommand
- Defined in:
- lib/hammer_cli_csv/activation_keys.rb
Constant Summary collapse
- ORGANIZATION =
'Organization'- DESCRIPTION =
'Description'- LIMIT =
'Limit'- ENVIRONMENT =
'Environment'- CONTENTVIEW =
'Content View'- HOSTCOLLECTIONS =
'System Groups'- SUBSCRIPTIONS =
'Subscriptions'
Constants inherited from BaseCommand
BaseCommand::COUNT, BaseCommand::NAME
Instance Method Summary collapse
- #create_activationkeys_from_csv(line) ⇒ Object
- #export ⇒ Object
- #import ⇒ Object
- #update_groups(activationkey, line) ⇒ Object
- #update_subscriptions(activationkey, line) ⇒ Object
- #usage_limit(limit) ⇒ Object
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_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
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 |
# File 'lib/hammer_cli_csv/activation_keys.rb', line 101 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 line[COUNT].to_i.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 } 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 |
#export ⇒ Object
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 88 89 90 91 |
# File 'lib/hammer_cli_csv/activation_keys.rb', line 28 def export CSV.open(option_csv_file || '/dev/stdout', 'wb', {:force_quotes => false}) do |csv| csv << [NAME, COUNT, ORGANIZATION, DESCRIPTION, LIMIT, ENVIRONMENT, CONTENTVIEW, HOSTCOLLECTIONS, SUBSCRIPTIONS] if @server_status['release'] == 'Headpin' @headpin.get(:organizations).each do |organization| next if option_organization && organization['name'] != option_organization @headpin.get("organizations/#{organization['label']}/activation_keys").each do |activationkey| 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| 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, 'systemGroups', 'name') 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, subscriptions] end end end end end |
#import ⇒ Object
93 94 95 96 97 98 99 |
# File 'lib/hammer_cli_csv/activation_keys.rb', line 93 def import @existing = {} thread_import do |line| create_activationkeys_from_csv(line) end end |
#update_groups(activationkey, line) ⇒ Object
145 146 147 148 149 150 151 152 153 154 155 |
# File 'lib/hammer_cli_csv/activation_keys.rb', line 145 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(: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
157 158 159 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 |
# File 'lib/hammer_cli_csv/activation_keys.rb', line 157 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
186 187 188 |
# File 'lib/hammer_cli_csv/activation_keys.rb', line 186 def usage_limit(limit) Integer(limit) rescue -1 end |