Skip to content

Commit

Permalink
myriad changes in attempt to get import working against master/develop
Browse files Browse the repository at this point in the history
  • Loading branch information
Tom McKay committed Mar 11, 2016
1 parent 38a9a46 commit 5146ac6
Show file tree
Hide file tree
Showing 9 changed files with 186 additions and 149 deletions.
93 changes: 38 additions & 55 deletions lib/hammer_cli_csv/activation_keys.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,68 +9,48 @@ class ActivationKeysCommand < BaseCommand
LIMIT = 'Limit'
ENVIRONMENT = 'Environment'
CONTENTVIEW = 'Content View'
HOSTCOLLECTIONS = 'System Groups'
HOSTCOLLECTIONS = 'Host Collections'
SERVICELEVEL = "Service Level"
RELEASEVER = "Release Version"
AUTOATTACH = "Auto-Attach"
SUBSCRIPTIONS = 'Subscriptions'

def export
CSV.open(option_file || '/dev/stdout', 'wb', {:force_quotes => false}) do |csv|
csv << [NAME, 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'])
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
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
subscriptions.delete!("\n")
csv << [name, 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
subscriptions.delete!("\n")
csv << [name, count, organization['name'], description, limit, environment, contentview,
hostcollections, servicelevel, releasever, autoattach, subscriptions]
end
end
end
Expand Down Expand Up @@ -111,6 +91,9 @@ def create_activationkeys_from_csv(line)
'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)
Expand Down
2 changes: 1 addition & 1 deletion lib/hammer_cli_csv/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -748,7 +748,7 @@ def katello_subscription(organization, options = {})
results = @api.resource(:subscriptions).call(:index, {
:per_page => 999999,
'organization_id' => foreman_organization(:name => organization),
'search' => "name:\"#{options[:name]}\""
'search' => "name = \"#{options[:name]}\""
})
raise "No subscriptions match '#{options[:name]}'" if results['subtotal'] == 0
raise "Too many subscriptions match '#{options[:name]}'" if results['subtotal'] > 1
Expand Down
24 changes: 11 additions & 13 deletions lib/hammer_cli_csv/compute_resources.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,23 +45,21 @@ def import
def create_compute_resources_from_csv(line)
count(line[COUNT]).times do |number|
name = namify(line[NAME], number)
params = {
'compute_resource' => {
'name' => name,
'url' => line[URL],
'provider' => line[PROVIDER]
}
}
if !@existing.include? name
print "Creating compute resource '#{name}'..." if option_verbose?
id = @api.resource(:compute_resources).call(:create, {
'compute_resource' => {
'name' => name,
'url' => line[URL]
}
})['id']
id = @api.resource(:compute_resources).call(:create, params)['id']
else
print "Updating compute resource '#{name}'..." if option_verbose?
id = @api.resource(:compute_resources).call(:update, {
'id' => @existing[name],
'compute_resource' => {
'name' => name,
'url' => line[URL]
}
})['id']
id = @existing[name]
params['id'] = id
@api.resource(:compute_resources).call(:update, params)
end

# Update associated resources
Expand Down
104 changes: 55 additions & 49 deletions lib/hammer_cli_csv/content_hosts.rb
Original file line number Diff line number Diff line change
Expand Up @@ -165,9 +165,11 @@ def import_locally
if !@hypervisor_guests.empty?
print(_('Updating hypervisor and guest associations...')) if option_verbose?
@hypervisor_guests.each do |host_id, guest_ids|
@api.resource(:systems).call(:update, {
@api.resource(:hosts).call(:update, {
'id' => host_id,
'guest_ids' => guest_ids
'host' => {
'guest_ids' => guest_ids
}
})
end
puts _('done') if option_verbose?
Expand All @@ -178,64 +180,78 @@ def create_content_hosts_from_csv(line)
return if option_organization && line[ORGANIZATION] != option_organization

if !@existing[line[ORGANIZATION]]
@existing[line[ORGANIZATION]] = {}
@existing[line[ORGANIZATION]] = true
# Fetching all content hosts is too slow and times out due to the complexity of the data
# rendered in the json.
# http://projects.theforeman.org/issues/6307
total = @api.resource(:systems).call(:index, {
total = @api.resource(:hosts).call(:index, {
'organization_id' => foreman_organization(:name => line[ORGANIZATION]),
'per_page' => 1
})['total'].to_i
(total / 20 + 2).to_i.times do |page|
@api.resource(:systems).call(:index, {
@api.resource(:hosts).call(:index, {
'organization_id' => foreman_organization(:name => line[ORGANIZATION]),
'page' => page + 1,
'per_page' => 20
})['results'].each do |host|
@existing[line[ORGANIZATION]][host['name']] = host['uuid'] if host
@existing[host['name']] = {
:host => host['id'],
:subscription => host['subscription']['id'],
:content => host['content']['id']
}
end
end
end

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

if !@existing[line[ORGANIZATION]].include? name
if !@existing.include? name
print(_("Creating content host '%{name}'...") % {:name => name}) if option_verbose?
host_id = @api.resource(:systems).call(:create, {
host_id = @api.resource(:host_subscriptions).call(:register, {
'name' => name,
'organization_id' => foreman_organization(:name => line[ORGANIZATION]),
'environment_id' => lifecycle_environment(line[ORGANIZATION], :name => line[ENVIRONMENT]),
'lifecycle_environment_id' => lifecycle_environment(line[ORGANIZATION], :name => line[ENVIRONMENT]),
'content_view_id' => katello_contentview(line[ORGANIZATION], :name => line[CONTENTVIEW]),
'facts' => facts(name, line),
'installed_products' => products(line),
'service_level' => line[SLA],
'type' => 'system'
})['uuid']
@existing[line[ORGANIZATION]][name] = host_id
})['id']
@existing[name] = host_id
else
# TODO: remove passing facet IDs to update
# Bug #13849 - updating a host's facet should not require the facet id to be included in facet params
# http://projects.theforeman.org/issues/13849
print(_("Updating content host '%{name}'...") % {:name => name}) if option_verbose?
host_id = @api.resource(:systems).call(:update, {
'id' => @existing[line[ORGANIZATION]][name],
'system' => {
host_id = @api.resource(:hosts).call(:update, {
'id' => @existing[name][:host],
'host' => {
'name' => name,
'environment_id' => lifecycle_environment(line[ORGANIZATION], :name => line[ENVIRONMENT]),
'content_view_id' => katello_contentview(line[ORGANIZATION], :name => line[CONTENTVIEW]),
'facts' => facts(name, line),
'installed_products' => products(line)
},
'facts' => facts(name, line),
'installed_products' => products(line), # TODO: http://projects.theforeman.org/issues/9191,
'service_level' => line[SLA]
})['uuid']
'content_facet_attributes' => {
'id' => @existing[name][:content],
'lifecycle_environment_id' => lifecycle_environment(line[ORGANIZATION], :name => line[ENVIRONMENT]),
'content_view_id' => katello_contentview(line[ORGANIZATION], :name => line[CONTENTVIEW])
},
'subscription_facet_attributes' => {
'id' => @existing[name][:subscription],
'facts' => facts(name, line),
# TODO: PUT /hosts subscription_facet_attributes missing "installed_products"
# http://projects.theforeman.org/issues/13854
#'installed_products' => products(line),
'service_level' => line[SLA]
}
}
})['host_id']
end

if line[VIRTUAL] == 'Yes' && line[HOST]
raise "Content host '#{line[HOST]}' not found" if !@existing[line[ORGANIZATION]][line[HOST]]
@hypervisor_guests[@existing[line[ORGANIZATION]][line[HOST]]] ||= []
@hypervisor_guests[@existing[line[ORGANIZATION]][line[HOST]]] << "#{line[ORGANIZATION]}/#{name}"
raise "Content host '#{line[HOST]}' not found" if !@existing[line[HOST]]
@hypervisor_guests[@existing[line[HOST]]] ||= []
@hypervisor_guests[@existing[line[HOST]]] << @existing[name]
end

update_host_facts(host_id, line)
update_host_collections(host_id, line)
update_subscriptions(host_id, line)

Expand All @@ -262,12 +278,15 @@ def facts(name, line)
facts
end

def update_host_facts(host_id, line)
end

def update_host_collections(host_id, line)
return nil if !line[HOSTCOLLECTIONS]
CSV.parse_line(line[HOSTCOLLECTIONS]).each do |hostcollection_name|
@api.resource(:host_collections).call(:add_systems, {
@api.resource(:host_collections).call(:add_hosts, {
'id' => katello_hostcollection(line[ORGANIZATION], :name => hostcollection_name),
'system_ids' => [host_id]
'hosts_ids' => [host_id]
})
end
end
Expand Down Expand Up @@ -299,29 +318,16 @@ def products(line)
end

def update_subscriptions(host_id, line)
existing_subscriptions = @api.resource(:systems).call(:subscriptions, {
'per_page' => 999999,
'id' => host_id
existing_subscriptions = @api.resource(:host_subscriptions).call(:index, {
'host_id' => host_id
})['results']
if existing_subscriptions.length > 0
@api.resource(:subscriptions).call(:destroy, {
'system_id' => host_id,
'id' => existing_subscriptions[0]['id']
if existing_subscriptions.length != 0
@api.resource(:host_subscriptions).call(:remove_subscriptions, {
'host_id' => host_id,
'subscriptions' => existing_subscriptions
})
end

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

return if line[SUBSCRIPTIONS].nil? || line[SUBSCRIPTIONS].empty?

subscriptions = CSV.parse_line(line[SUBSCRIPTIONS], {:skip_blanks => true}).collect do |details|
Expand All @@ -332,8 +338,8 @@ def update_subscriptions(host_id, line)
}
end

@api.resource(:subscriptions).call(:create, {
'system_id' => host_id,
@api.resource(:host_subscriptions).call(:add_subscriptions, {
'host_id' => host_id,
'subscriptions' => subscriptions
})
end
Expand Down
32 changes: 13 additions & 19 deletions lib/hammer_cli_csv/content_views.rb
Original file line number Diff line number Diff line change
Expand Up @@ -106,32 +106,26 @@ def create_contentviews_from_csv(line)
'description' => line[DESCRIPTION],
'composite' => is_composite
}
if is_composite
options['component_ids'] = composite_ids
else
options['repository_ids'] = repository_ids
end
contentview_id = @api.resource(:content_views).call(:create, options)['id']
@existing_contentviews[line[ORGANIZATION]][name] = contentview_id
publish = true
else
print _("Updating content view '%{name}'...") % {:name => name} if option_verbose?
options = {
'id' => contentview_id,
'description' => line[DESCRIPTION]
}
if is_composite
options['component_ids'] = composite_ids
else
options['repository_ids'] = repository_ids
end
contentview = @api.resource(:content_views).call(:update, options)
contentview_id = contentview['id']
publish = contentview['versions'].empty?
end

options = {
'id' => contentview_id,
'description' => line[DESCRIPTION]
}
if is_composite
options['component_ids'] = composite_ids
else
options['repository_ids'] = repository_ids
end
contentview = @api.resource(:content_views).call(:update, options)
contentview_id = contentview['id']

# Content views cannot be used in composites unless a publish has occurred
publish_content_view(contentview_id, line) if publish
publish_content_view(contentview_id, line) if contentview['versions'].empty?
promote_content_view(contentview_id, line)

puts _('done') if option_verbose?
Expand Down
Loading

0 comments on commit 5146ac6

Please sign in to comment.