Class: HammerCLICsv::CsvCommand::ProductsCommand
- Inherits:
-
BaseCommand
- Object
- HammerCLI::Apipie::Command
- BaseCommand
- HammerCLICsv::CsvCommand::ProductsCommand
- Defined in:
- lib/hammer_cli_csv/products.rb
Constant Summary collapse
- LABEL =
'Label'- ORGANIZATION =
'Organization'- REPOSITORY =
'Repository'- REPOSITORY_TYPE =
'Repository Type'- REPOSITORY_URL =
'Repository Url'- DESCRIPTION =
'Description'
Constants inherited from BaseCommand
BaseCommand::COUNT, BaseCommand::NAME
Instance Method Summary collapse
-
#create_products_from_csv(line) ⇒ Object
FIXME: TODO remove this rubocop rubocop:disable CyclomaticComplexity.
- #export ⇒ Object
- #import ⇒ 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_products_from_csv(line) ⇒ Object
FIXME: TODO remove this rubocop rubocop:disable CyclomaticComplexity
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 92 93 94 95 96 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 146 147 148 149 |
# File 'lib/hammer_cli_csv/products.rb', line 65 def create_products_from_csv(line) return if option_organization && line[ORGANIZATION] != option_organization if !@existing_products[line[ORGANIZATION]] @existing_products[line[ORGANIZATION]] = {} @api.resource(:products).call(:index, { 'per_page' => 999999, 'organization_id' => foreman_organization(:name => line[ORGANIZATION]), 'enabled' => true })['results'].each do |product| @existing_products[line[ORGANIZATION]][product['name']] = product['id'] @api.resource(:repositories).call(:index, { 'page_size' => 999999, 'paged' => true, 'organization_id' => foreman_organization(:name => line[ORGANIZATION]), 'product_id' => product['id'], 'enabled' => true, 'library' => true })['results'].each do |repository| @existing_repositories[line[ORGANIZATION] + product['name']] ||= {} @existing_repositories[line[ORGANIZATION] + product['name']][repository['label']] = repository['id'] end end end line[COUNT].to_i.times do |number| name = namify(line[NAME], number) product_id = @existing_products[line[ORGANIZATION]][name] if product_id.nil? print _("Creating product '%{name}'...") % {:name => name} if option_verbose? if line[REPOSITORY_TYPE] =~ /Red Hat/ raise _("Red Hat product '%{name}' does not exist in '%{organization}'") % {:name => name, :organization => line[ORGANIZATION]} end product_id = @api.resource(:products).call(:create, { 'organization_id' => foreman_organization(:name => line[ORGANIZATION]), 'name' => name })['id'] @existing_products[line[ORGANIZATION]][name] = product_id else # Nothing to update for products print _("Updating product '%{name}'...") % {:name => name} if option_verbose? end @existing_repositories[line[ORGANIZATION] + name] = {} repository_name = namify(line[REPOSITORY], number) if !@existing_repositories[line[ORGANIZATION] + name][repository_name] @api.resource(:repositories).call(:index, { 'organization_id' => foreman_organization(:name => line[ORGANIZATION]), 'library' => true, 'all' => false, 'product_id' => product_id })['results'].each do |repository| @existing_repositories[line[ORGANIZATION] + name][repository['name']] = repository end end repository = @existing_repositories[line[ORGANIZATION] + name][repository_name] if !repository raise "Red Hat product '#{name}' does not have repository '#{repository_name}'" if line[REPOSITORY_TYPE] =~ /Red Hat/ if option_verbose? print _("Creating repository '%{repository_name}' in product '%{name}'...") % {:repository_name => repository_name, :name => name} end repository = @api.resource(:repositories).call(:create, { 'organization_id' => foreman_organization(:name => line[ORGANIZATION]), 'name' => repository_name, 'label' => labelize(repository_name), 'product_id' => product_id, 'url' => line[REPOSITORY_URL], 'content_type' => content_type(line[REPOSITORY_TYPE]) }) @existing_repositories[line[ORGANIZATION] + name][line[LABEL]] = repository end sync_repository(line, repository) puts _('done') if option_verbose? end rescue RuntimeError => e raise "#{e}\n #{line}" 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 |
# File 'lib/hammer_cli_csv/products.rb', line 28 def export CSV.open(option_csv_file || '/dev/stdout', 'wb', {:force_quotes => false}) do |csv| csv << [NAME, COUNT, LABEL, ORGANIZATION, REPOSITORY, REPOSITORY_TYPE, REPOSITORY_URL] @api.resource(:organizations).call(:index, { :per_page => 999999 })['results'].each do |organization| next if option_organization && organization['name'] != option_organization @api.resource(:products).call(:index, { 'per_page' => 999999, 'enabled' => true, 'organization_id' => organization['id'] })['results'].each do |product| @api.resource(:repositories).call(:index, { 'product_id' => product['id'], 'organization_id' => organization['id'] })['results'].each do |repository| repository_type = repository['product_type'] == 'custom' ? 'Custom' : 'Red Hat' repository_type += " #{repository['content_type'].capitalize}" csv << [product['name'], 1, product['label'], organization['name'], repository['name'], repository_type, repository['url']] end end end end end |
#import ⇒ Object
54 55 56 57 58 59 60 61 |
# File 'lib/hammer_cli_csv/products.rb', line 54 def import @existing_products = {} @existing_repositories = {} thread_import do |line| create_products_from_csv(line) end end |