Project

General

Profile

« Previous | Next » 

Revision 6434439d

Added by Thomas McKay about 8 years ago

fixes #14390 - installation-media i/o

View differences:

lib/hammer_cli_csv.rb
require 'hammer_cli_csv/hosts'
require 'hammer_cli_csv/host_groups'
require 'hammer_cli_csv/import'
require 'hammer_cli_csv/installation_medias'
require 'hammer_cli_csv/installation_media'
require 'hammer_cli_csv/job_templates'
require 'hammer_cli_csv/lifecycle_environments'
require 'hammer_cli_csv/locations'
lib/hammer_cli_csv/installation_media.rb
module HammerCLICsv
class CsvCommand
class InstallationMediaCommand < BaseCommand
command_name 'installation-media'
desc 'import or export media'
ORGANIZATIONS = 'Organizations'
LOCATIONS = 'Locations'
PATH = 'Path'
OSFAMILY = 'OS Family'
OPERATING_SYSTEMS = 'Operating Systems'
def export
CSV.open(option_file || '/dev/stdout', 'wb', {:force_quotes => true}) do |csv|
csv << [NAME, ORGANIZATIONS, LOCATIONS, PATH, OSFAMILY, OPERATING_SYSTEMS]
@api.resource(:media).call(:index, {:per_page => 999999})['results'].each do |medium|
medium = @api.resource(:media).call(:show, :id => medium['id'])
name = medium['name']
organizations = export_column(medium, 'organizations', 'name')
locations = export_column(medium, 'locations', 'name')
count = 1
path = medium['path']
os_family = medium['os_family']
operating_systems = export_column(medium, 'operatingsystems', 'title')
csv << [name, organizations, locations, path, os_family, operating_systems]
end
end
end
def import
@existing = {}
@api.resource(:media).call(:index, {:per_page => 999999})['results'].each do |medium|
@existing[medium['name']] = medium['id'] if medium
end
thread_import do |line|
create_from_csv(line)
end
end
def create_from_csv(line)
params = {
'medium' => {
'organization_ids' => collect_column(line[ORGANIZATIONS]) do |organization|
foreman_organization(:name => organization)
end,
'location_ids' => collect_column(line[LOCATIONS]) do |location|
foreman_location(:name => location)
end,
'path' => line[PATH],
'os_family' => line[OSFAMILY],
'operatingsystem_ids' => collect_column(line[OPERATING_SYSTEMS]) do |os|
foreman_operatingsystem(:name => os)
end
}
}
count(line[COUNT]).times do |number|
name = namify(line[NAME], number)
params['medium']['name'] = name
if !@existing.include? name
print _("Creating installation medium '%{name}'... ") % {:name => name} if option_verbose?
medium = @api.resource(:media).call(:create, params)
@existing[name] = medium['id']
else
print _("Updating installation medium '%{name}'... ") % {:name => name} if option_verbose?
params['id'] = @existing[name]
medium = @api.resource(:media).call(:update, params)
end
puts _('done') if option_verbose?
end
rescue RuntimeError => e
raise "#{e}\n #{line}"
end
end
end
end
lib/hammer_cli_csv/installation_medias.rb
module HammerCLICsv
class CsvCommand
class InstallationMediasCommand < BaseCommand
command_name 'installation-medias'
desc 'import or export installation media'
OSFAMILY = 'OS Family'
PATH = 'Path'
ORGANIZATIONS = 'Organizations'
def export
CSV.open(option_file || '/dev/stdout', 'wb', {:force_quotes => true}) do |csv|
csv << [NAME, PATH, OSFAMILY]
@api.resource(:media).call(:index, {:per_page => 999999})['results'].each do |installation_media|
name = installation_media['name']
count = 1
path = installation_media['path']
os_family = installation_media['os_family']
csv << [name, path, os_family]
end
end
end
def import
@existing = {}
@api.resource(:media).call(:index, {:per_page => 999999})['results'].each do |installation_media|
@existing[installation_media['name']] = installation_media['id'] if installation_media
end
thread_import do |line|
create_installation_medias_from_csv(line)
end
end
def create_installation_medias_from_csv(line)
count(line[COUNT]).times do |number|
name = namify(line[NAME], number)
if !@existing.include? name
print "Creating installation_media '#{name}'..." if option_verbose?
installation_media_id = @api.resource(:media).call(:create, {
'name' => name
})['id']
else
print "Updating installation_media '#{name}'..." if option_verbose?
installation_media_id = @api.resource(:media).call(:update, {
'id' => @existing[name],
'name' => name
})['id']
end
# Update associated resources
installation_medias ||= {}
CSV.parse_line(line[ORGANIZATIONS]).each do |organization|
organization_id = foreman_organization(:name => organization)
if installation_medias[organization].nil?
installation_medias[organization] = @api.resource(:organizations).call(:show, {'id' => organization_id})['installation_medias'].collect do |installation_media|
installation_media['id']
end
end
installation_medias[organization] += [installation_media_id] if !installation_medias[organization].include? installation_media_id
@api.resource(:organizations).call(:update, {
'id' => organization_id,
'organization' => {
'installation_media_ids' => installation_medias[organization]
}
})
end
print "done\n" if option_verbose?
end
rescue RuntimeError => e
raise "#{e}\n #{line}"
end
end
end
end

Also available in: Unified diff