diff --git a/.gitignore b/.gitignore index 059a1a0..88a82d9 100644 --- a/.gitignore +++ b/.gitignore @@ -21,6 +21,7 @@ pickle-email-*.html .project config/initializers/secret_token.rb config/cli.modules.d/*.yml +config/cli_config.yml !config/*.template.yml !config/cli.modules.d/*.template.yml test/data/*.zip diff --git a/README.md b/README.md index ac23241..b8f13a6 100644 --- a/README.md +++ b/README.md @@ -133,6 +133,7 @@ Creating organization 'abcMega Corporation'... done ## Puppet Environments **Overview** +* Due to the implications of removing a puppet environment from an organization or location, this column only adds to what is present already. * [Open Issues](https://github.com/Katello/hammer-cli-csv/issues?labels=puppet-environments&state=open) * [Tests](https://github.com/Katello/hammer-cli-csv/blob/master/test/puppet_environments_test.rb) * Sample data @@ -226,6 +227,8 @@ Creating organization 'abcMega Corporation'... done ## Partition Tables **Overview** +* Import and export of the Organizations and Locations columns does not apply to all versions and will be silently ignored when unsupported. +* Importing Operating Systems column does not apply to all versions and will be silently ignored when unsupported. * [Open Issues](https://github.com/Katello/hammer-cli-csv/issues?labels=partition-tables&state=open) * [Tests](https://github.com/Katello/hammer-cli-csv/blob/master/test/partition_tables_test.rb) * Sample data @@ -306,10 +309,10 @@ Creating organization 'abcMega Corporation'... done | Additional arguments | Description | | ---------------:| :--------------| | --organization | Only process organization matching this name | -| --sync | Sync product repositories (default true) | +| --[no-]sync | Sync product repositories (default true) | **Overview** -* Due to the length of time that syncing repositories can take, the --sync=false option may be used to skip this step. To always disable syncing, ':products_sync: false' may be specified in configuration file. +* Due to the length of time that syncing repositories can take, the --no-sync option may be used to skip this step. To always disable syncing, ':products_sync: false' may be specified in configuration file. * [Open Issues](https://github.com/Katello/hammer-cli-csv/issues?labels=products&state=open) * [Tests](https://github.com/Katello/hammer-cli-csv/blob/master/test/products_test.rb) * Sample data diff --git a/lib/hammer_cli_csv.rb b/lib/hammer_cli_csv.rb index e06f5e4..63ebb80 100644 --- a/lib/hammer_cli_csv.rb +++ b/lib/hammer_cli_csv.rb @@ -1,14 +1,3 @@ -# Copyright 2013-2014 Red Hat, Inc. -# -# This software is licensed to you under the GNU General Public -# License as published by the Free Software Foundation; either version -# 2 of the License (GPLv2) or (at your option) any later version. -# There is NO WARRANTY for this software, express or implied, -# including the implied warranties of MERCHANTABILITY, -# NON-INFRINGEMENT, or FITNESS FOR A PARTICULAR PURPOSE. You should -# have received a copy of GPLv2 along with this software; if not, see -# http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt. - require 'hammer_cli' require 'hammer_cli/exit_codes' @@ -45,6 +34,7 @@ module HammerCLICsv require 'hammer_cli_csv/puppet_reports' require 'hammer_cli_csv/reports' require 'hammer_cli_csv/roles' + require 'hammer_cli_csv/settings' require 'hammer_cli_csv/smart_proxies' require 'hammer_cli_csv/splice' require 'hammer_cli_csv/subnets' diff --git a/lib/hammer_cli_csv/activation_keys.rb b/lib/hammer_cli_csv/activation_keys.rb index edcb7eb..9631ca4 100644 --- a/lib/hammer_cli_csv/activation_keys.rb +++ b/lib/hammer_cli_csv/activation_keys.rb @@ -1,22 +1,9 @@ -# Copyright 2013-2014 Red Hat, Inc. -# -# This software is licensed to you under the GNU General Public -# License as published by the Free Software Foundation; either version -# 2 of the License (GPLv2) or (at your option) any later version. -# There is NO WARRANTY for this software, express or implied, -# including the implied warranties of MERCHANTABILITY, -# NON-INFRINGEMENT, or FITNESS FOR A PARTICULAR PURPOSE. You should -# have received a copy of GPLv2 along with this software; if not, see -# http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt. - module HammerCLICsv class CsvCommand class ActivationKeysCommand < BaseCommand command_name 'activation-keys' desc _('import or export activation keys') - option %w(--organization), 'ORGANIZATION', _('Only process organization matching this name') - ORGANIZATION = 'Organization' DESCRIPTION = 'Description' LIMIT = 'Limit' @@ -26,7 +13,7 @@ class ActivationKeysCommand < BaseCommand SUBSCRIPTIONS = 'Subscriptions' def export - CSV.open(option_csv_file || '/dev/stdout', 'wb', {:force_quotes => false}) do |csv| + CSV.open(option_file || '/dev/stdout', 'wb', {:force_quotes => false}) do |csv| csv << [NAME, COUNT, ORGANIZATION, DESCRIPTION, LIMIT, ENVIRONMENT, CONTENTVIEW, HOSTCOLLECTIONS, SUBSCRIPTIONS] if @server_status['release'] == 'Headpin' @@ -146,9 +133,9 @@ 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']] + @api.resource(:activation_keys).call(:add_host_collections, { + 'id' => activationkey['id'], + 'host_collection_ids' => [katello_hostcollection(line[ORGANIZATION], :name => name)] }) end end diff --git a/lib/hammer_cli_csv/architectures.rb b/lib/hammer_cli_csv/architectures.rb index c6af8f0..4edd710 100644 --- a/lib/hammer_cli_csv/architectures.rb +++ b/lib/hammer_cli_csv/architectures.rb @@ -1,30 +1,3 @@ -# Copyright 2013-2014 Red Hat, Inc. -# -# This software is licensed to you under the GNU General Public -# License as published by the Free Software Foundation; either version -# 2 of the License (GPLv2) or (at your option) any later version. -# There is NO WARRANTY for this software, express or implied, -# including the implied warranties of MERCHANTABILITY, -# NON-INFRINGEMENT, or FITNESS FOR A PARTICULAR PURPOSE. You should -# have received a copy of GPLv2 along with this software; if not, see -# http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt. - -# -# -= Architectures CSV =- -# -# Columns -# Name -# - Architecture name -# - May contain '%d' which will be replaced with current iteration number of Count -# - eg. "os%d" -> "os1" -# Count -# - Number of times to iterate on this line of the CSV file -# - -require 'hammer_cli' -require 'json' -require 'csv' - module HammerCLICsv class CsvCommand class ArchitecturesCommand < BaseCommand @@ -34,16 +7,13 @@ class ArchitecturesCommand < BaseCommand OPERATINGSYSTEMS = 'Operating Systems' def export - CSV.open(option_csv_file || '/dev/stdout', 'wb', {:force_quotes => true}) do |csv| - csv << [NAME, COUNT, ORGANIZATIONS, OPERATINGSYSTEMS] + CSV.open(option_file || '/dev/stdout', 'wb', {:force_quotes => true}) do |csv| + csv << [NAME, COUNT, OPERATINGSYSTEMS] @api.resource(:architectures).call(:index, {:per_page => 999999})['results'].each do |architecture| + architecture = @api.resource(:architectures).call(:show, {:id => architecture['id']}) name = architecture['name'] count = 1 - # TODO: http://projects.theforeman.org/issues/4198 - #operatingsystems = architecture['operatingsystem_ids'].collect do |operatingsystem_id| - # foreman_operatingsystem(:id => operatingsystem_id) - #end.join(',') - operatingsystems = '' + operatingsystems = export_column(architecture, 'operatingsystems', 'title') csv << [name, count, operatingsystems] end end diff --git a/lib/hammer_cli_csv/base.rb b/lib/hammer_cli_csv/base.rb index d6bdae2..bad03d3 100644 --- a/lib/hammer_cli_csv/base.rb +++ b/lib/hammer_cli_csv/base.rb @@ -1,14 +1,3 @@ -# Copyright 2013-2014 Red Hat, Inc. -# -# This software is licensed to you under the GNU General Public -# License as published by the Free Software Foundation; either version -# 2 of the License (GPLv2) or (at your option) any later version. -# There is NO WARRANTY for this software, express or implied, -# including the implied warranties of MERCHANTABILITY, -# NON-INFRINGEMENT, or FITNESS FOR A PARTICULAR PURPOSE. You should -# have received a copy of GPLv2 along with this software; if not, see -# http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt. - require 'apipie-bindings' require 'hammer_cli' require 'json' @@ -20,9 +9,18 @@ module HammerCLICsv class BaseCommand < HammerCLI::Apipie::Command option %w(-v --verbose), :flag, 'be verbose' option %w(--threads), 'THREAD_COUNT', 'Number of threads to hammer with', :default => 1 - option %w(--csv-export), :flag, 'Export current data instead of importing' - option %w(--csv-file), 'FILE_NAME', 'CSV file (default to /dev/stdout with --csv-export, otherwise required)' + option %w(--export), :flag, 'Export current data instead of importing' + option %w(--file), 'FILE_NAME', 'CSV file (default to /dev/stdout with --csv-export, otherwise required)' option %w(--prefix), 'PREFIX', 'Prefix for all name columns' + option %w(--organization), 'ORGANIZATION', _('Only process organization matching this name') + + option %w(--csv-file), 'FILE_NAME', 'Option --csv-file is deprecated. Use --file', + :deprecated => "Use --file", :hidden => true, + :attribute_name => :option_file + option %w(--csv-export), :flag, 'Option --csv-export is deprecated. Use --export', + :deprecated => "Use --export", :hidden => true, + :attribute_name => :option_export + NAME = 'Name' COUNT = 'Count' @@ -58,7 +56,7 @@ def execute }) end - option_csv_export? ? export : import + option_export? ? export : import HammerCLI::EX_OK end @@ -105,7 +103,7 @@ def labelize(name) end def thread_import(return_headers = false, filename=nil, name_column=nil) - filename ||= option_csv_file || '/dev/stdin' + filename ||= option_file || '/dev/stdin' csv = [] CSV.foreach(filename, { :skip_blanks => true, @@ -804,6 +802,28 @@ def associate_locations(id, locations, name) end if locations && !locations.empty? end + def apipie_check_param(resource, action, name) + method = @api.resource(pluralize(resource).to_sym).apidoc[:methods].detect do |api_method| + api_method[:name] == action.to_s + end + return false unless method + + found = method[:params].detect do |param| + param[:full_name] == name + end + if !found + nested = method[:params].detect do |param| + param[:name] == resource.to_s + end + if nested + found = nested[:params].detect do |param| + param[:full_name] == name + end + end + end + found + end + private def search_string(resource, name) diff --git a/lib/hammer_cli_csv/compute_profiles.rb b/lib/hammer_cli_csv/compute_profiles.rb index 5edf419..3230736 100644 --- a/lib/hammer_cli_csv/compute_profiles.rb +++ b/lib/hammer_cli_csv/compute_profiles.rb @@ -1,20 +1,4 @@ -# Copyright 2013-2014 Red Hat, Inc. -# -# This software is licensed to you under the GNU General Public -# License as published by the Free Software Foundation; either version -# 2 of the License (GPLv2) or (at your option) any later version. -# There is NO WARRANTY for this software, express or implied, -# including the implied warranties of MERCHANTABILITY, -# NON-INFRINGEMENT, or FITNESS FOR A PARTICULAR PURPOSE. You should -# have received a copy of GPLv2 along with this software; if not, see -# http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt. - -require 'hammer_cli' -require 'json' -require 'csv' - # TODO: waiting for https://github.com/theforeman/foreman/pull/1326 - module HammerCLICsv class CsvCommand class ComputeProfilesCommand < BaseCommand @@ -28,7 +12,7 @@ class ComputeProfilesCommand < BaseCommand URL = 'URL' def export - CSV.open(option_csv_file || '/dev/stdout', 'wb', {:force_quotes => true}) do |csv| + CSV.open(option_file || '/dev/stdout', 'wb', {:force_quotes => true}) do |csv| csv << [NAME, COUNT, ORGANIZATIONS, LOCATIONS, DESCRIPTION, PROVIDER, URL] @api.resource(:compute_profiles).call(:index, {:per_page => 999999})['results'].each do |compute_profile| puts compute_profile diff --git a/lib/hammer_cli_csv/compute_resources.rb b/lib/hammer_cli_csv/compute_resources.rb index a7bd7c6..7abbc39 100644 --- a/lib/hammer_cli_csv/compute_resources.rb +++ b/lib/hammer_cli_csv/compute_resources.rb @@ -1,14 +1,3 @@ -# Copyright 2013-2014 Red Hat, Inc. -# -# This software is licensed to you under the GNU General Public -# License as published by the Free Software Foundation; either version -# 2 of the License (GPLv2) or (at your option) any later version. -# There is NO WARRANTY for this software, express or implied, -# including the implied warranties of MERCHANTABILITY, -# NON-INFRINGEMENT, or FITNESS FOR A PARTICULAR PURPOSE. You should -# have received a copy of GPLv2 along with this software; if not, see -# http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt. - require 'hammer_cli' require 'json' require 'csv' @@ -26,7 +15,7 @@ class ComputeResourcesCommand < BaseCommand URL = 'URL' def export - CSV.open(option_csv_file || '/dev/stdout', 'wb', {:force_quotes => true}) do |csv| + CSV.open(option_file || '/dev/stdout', 'wb', {:force_quotes => true}) do |csv| csv << [NAME, COUNT, ORGANIZATIONS, LOCATIONS, DESCRIPTION, PROVIDER, URL] @api.resource(:compute_resources).call(:index, {:per_page => 999999})['results'].each do |compute_resource| compute_resource = @api.resource(:compute_resources).call(:show, {'id' => compute_resource['id']}) diff --git a/lib/hammer_cli_csv/content_hosts.rb b/lib/hammer_cli_csv/content_hosts.rb index f2ae06f..6d84932 100644 --- a/lib/hammer_cli_csv/content_hosts.rb +++ b/lib/hammer_cli_csv/content_hosts.rb @@ -1,14 +1,3 @@ -# Copyright 2013-2014 Red Hat, Inc. -# -# This software is licensed to you under the GNU General Public -# License as published by the Free Software Foundation; either version -# 2 of the License (GPLv2) or (at your option) any later version. -# There is NO WARRANTY for this software, express or implied, -# including the implied warranties of MERCHANTABILITY, -# NON-INFRINGEMENT, or FITNESS FOR A PARTICULAR PURPOSE. You should -# have received a copy of GPLv2 along with this software; if not, see -# http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt. - module HammerCLICsv class CsvCommand class ContentHostsCommand < BaseCommand @@ -17,8 +6,6 @@ class ContentHostsCommand < BaseCommand command_name 'content-hosts' desc 'import or export content hosts' - option %w(--organization), 'ORGANIZATION', 'Only process organization matching this name' - ORGANIZATION = 'Organization' ENVIRONMENT = 'Environment' CONTENTVIEW = 'Content View' @@ -35,7 +22,7 @@ class ContentHostsCommand < BaseCommand SUBSCRIPTIONS = 'Subscriptions' def export - CSV.open(option_csv_file || '/dev/stdout', 'wb', {:force_quotes => false}) do |csv| + CSV.open(option_file || '/dev/stdout', 'wb', {:force_quotes => false}) do |csv| csv << [NAME, COUNT, ORGANIZATION, ENVIRONMENT, CONTENTVIEW, HOSTCOLLECTIONS, VIRTUAL, HOST, OPERATINGSYSTEM, ARCHITECTURE, SOCKETS, RAM, CORES, SLA, PRODUCTS, SUBSCRIPTIONS] if @server_status['release'] == 'Headpin' @@ -164,7 +151,7 @@ def import end def import_remotely - params = {'content' => ::File.new(::File.expand_path(option_csv_file), 'rb')} + params = {'content' => ::File.new(::File.expand_path(option_file), 'rb')} headers = {:content_type => 'multipart/form-data', :multipart => true} task_progress(@api.resource(:csv).call(:import_content_hosts, params, headers)) end diff --git a/lib/hammer_cli_csv/content_view_filters.rb b/lib/hammer_cli_csv/content_view_filters.rb index cb466c2..ae0a516 100644 --- a/lib/hammer_cli_csv/content_view_filters.rb +++ b/lib/hammer_cli_csv/content_view_filters.rb @@ -1,14 +1,3 @@ -# Copyright 2013-2014 Red Hat, Inc. -# -# This software is licensed to you under the GNU General Public -# License as published by the Free Software Foundation; either version -# 2 of the License (GPLv2) or (at your option) any later version. -# There is NO WARRANTY for this software, express or implied, -# including the implied warranties of MERCHANTABILITY, -# NON-INFRINGEMENT, or FITNESS FOR A PARTICULAR PURPOSE. You should -# have received a copy of GPLv2 along with this software; if not, see -# http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt. - # NOTE: # rpm -qa --queryformat "%{NAME}|=|%{VERSION}-%{RELEASE}," @@ -18,8 +7,6 @@ class ContentViewFiltersCommand < BaseCommand command_name 'content-view-filters' desc 'import or export content-view-filters' - option %w(--organization), 'ORGANIZATION', 'Only process organization matching this name' - CONTENTVIEW = 'Content View' ORGANIZATION = 'Organization' DESCRIPTION = 'Description' @@ -28,7 +15,7 @@ class ContentViewFiltersCommand < BaseCommand RULES = 'Rules' def export - CSV.open(option_csv_file || '/dev/stdout', 'wb', {:force_quotes => false}) do |csv| + CSV.open(option_file || '/dev/stdout', 'wb', {:force_quotes => false}) do |csv| csv << [NAME, COUNT, CONTENTVIEW, ORGANIZATION, TYPE, DESCRIPTION, REPOSITORIES, RULES] @api.resource(:organizations).call(:index, { :per_page => 999999 diff --git a/lib/hammer_cli_csv/content_views.rb b/lib/hammer_cli_csv/content_views.rb index 163cdc5..1efeeb7 100644 --- a/lib/hammer_cli_csv/content_views.rb +++ b/lib/hammer_cli_csv/content_views.rb @@ -1,14 +1,3 @@ -# Copyright 2013-2014 Red Hat, Inc. -# -# This software is licensed to you under the GNU General Public -# License as published by the Free Software Foundation; either version -# 2 of the License (GPLv2) or (at your option) any later version. -# There is NO WARRANTY for this software, express or implied, -# including the implied warranties of MERCHANTABILITY, -# NON-INFRINGEMENT, or FITNESS FOR A PARTICULAR PURPOSE. You should -# have received a copy of GPLv2 along with this software; if not, see -# http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt. - require 'hammer_cli_foreman' require 'hammer_cli_foreman_tasks' @@ -20,8 +9,6 @@ class ContentViewsCommand < BaseCommand command_name 'content-views' desc 'import or export content-views' - option %w(--organization), 'ORGANIZATION', 'Only process organization matching this name' - LABEL = 'Label' ORGANIZATION = 'Organization' DESCRIPTION = 'Description' @@ -30,7 +17,7 @@ class ContentViewsCommand < BaseCommand ENVIRONMENTS = "Lifecycle Environments" def export - CSV.open(option_csv_file || '/dev/stdout', 'wb', {:force_quotes => false}) do |csv| + CSV.open(option_file || '/dev/stdout', 'wb', {:force_quotes => false}) do |csv| csv << [NAME, COUNT, LABEL, ORGANIZATION, COMPOSITE, REPOSITORIES, ENVIRONMENTS] @api.resource(:organizations).call(:index, { :per_page => 999999 diff --git a/lib/hammer_cli_csv/csv.rb b/lib/hammer_cli_csv/csv.rb index d24fc1d..4b8c7ce 100644 --- a/lib/hammer_cli_csv/csv.rb +++ b/lib/hammer_cli_csv/csv.rb @@ -1,15 +1,3 @@ - -# Copyright 2013-2014 Red Hat, Inc. -# -# This software is licensed to you under the GNU General Public -# License as published by the Free Software Foundation; either version -# 2 of the License (GPLv2) or (at your option) any later version. -# There is NO WARRANTY for this software, express or implied, -# including the implied warranties of MERCHANTABILITY, -# NON-INFRINGEMENT, or FITNESS FOR A PARTICULAR PURPOSE. You should -# have received a copy of GPLv2 along with this software; if not, see -# http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt. - require 'hammer_cli' require 'hammer_cli/exit_codes' diff --git a/lib/hammer_cli_csv/domains.rb b/lib/hammer_cli_csv/domains.rb index 7078c01..c525c49 100644 --- a/lib/hammer_cli_csv/domains.rb +++ b/lib/hammer_cli_csv/domains.rb @@ -1,30 +1,3 @@ -# Copyright 2013-2014 Red Hat, Inc. -# -# This software is licensed to you under the GNU General Public -# License as published by the Free Software Foundation; either version -# 2 of the License (GPLv2) or (at your option) any later version. -# There is NO WARRANTY for this software, express or implied, -# including the implied warranties of MERCHANTABILITY, -# NON-INFRINGEMENT, or FITNESS FOR A PARTICULAR PURPOSE. You should -# have received a copy of GPLv2 along with this software; if not, see -# http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt. - -# -# -= Domains CSV =- -# -# Columns -# Name -# - Domain name -# - May contain '%d' which will be replaced with current iteration number of Count -# - eg. "os%d" -> "os1" -# Count -# - Number of times to iterate on this line of the CSV file -# - -require 'hammer_cli' -require 'json' -require 'csv' - module HammerCLICsv class CsvCommand class DomainsCommand < BaseCommand @@ -35,7 +8,7 @@ class DomainsCommand < BaseCommand ORGANIZATIONS = 'Organizations' def export - CSV.open(option_csv_file || '/dev/stdout', 'wb', {:force_quotes => true}) do |csv| + CSV.open(option_file || '/dev/stdout', 'wb', {:force_quotes => true}) do |csv| csv << [NAME, COUNT, FULLNAME] @api.resource(:domains).call(:index, {:per_page => 999999})['results'].each do |domain| puts domain @@ -64,13 +37,17 @@ def create_domains_from_csv(line) if !@existing.include? name print "Creating domain '#{name}'..." if option_verbose? domain_id = @api.resource(:domains).call(:create, { - 'name' => name + 'domain' => { + 'name' => name + } })['id'] else print "Updating domain '#{name}'..." if option_verbose? domain_id = @api.resource(:domains).call(:update, { 'id' => @existing[name], - 'name' => name + 'domain' => { + 'name' => name + } })['id'] end diff --git a/lib/hammer_cli_csv/exception_handler.rb b/lib/hammer_cli_csv/exception_handler.rb index 8845791..84c81df 100644 --- a/lib/hammer_cli_csv/exception_handler.rb +++ b/lib/hammer_cli_csv/exception_handler.rb @@ -1,14 +1,3 @@ -# Copyright 2013-2014 Red Hat, Inc. -# -# This software is licensed to you under the GNU General Public -# License as published by the Free Software Foundation; either version -# 2 of the License (GPLv2) or (at your option) any later version. -# There is NO WARRANTY for this software, express or implied, -# including the implied warranties of MERCHANTABILITY, -# NON-INFRINGEMENT, or FITNESS FOR A PARTICULAR PURPOSE. You should -# have received a copy of GPLv2 along with this software; if not, see -# http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt. - require 'hammer_cli/exception_handler' module HammerCLICsv diff --git a/lib/hammer_cli_csv/export.rb b/lib/hammer_cli_csv/export.rb index 8910e77..7513aa6 100644 --- a/lib/hammer_cli_csv/export.rb +++ b/lib/hammer_cli_csv/export.rb @@ -1,15 +1,3 @@ -# Copyright 2013-2014 Red Hat, Inc. -# -# This software is licensed to you under the GNU General Public -# License as published by the Free Software Foundation; either version -# 2 of the License (GPLv2) or (at your option) any later version. -# There is NO WARRANTY for this software, express or implied, -# including the implied warranties of MERCHANTABILITY, -# NON-INFRINGEMENT, or FITNESS FOR A PARTICULAR PURPOSE. You should -# have received a copy of GPLv2 along with this software; if not, see -# http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt. - - module HammerCLICsv class CsvCommand class ExportCommand < HammerCLI::Apipie::Command @@ -91,7 +79,7 @@ def hammer_resource(resource) options_file = self.send("option_#{resource}") || "#{option_dir}/#{resource.sub('_', '-')}.csv" args = [] args += %W( --server #{@server} ) if @server - args += %W( csv #{resource.sub('_', '-')} --csv-export --csv-file #{options_file} ) + args += %W( csv #{resource.sub('_', '-')} --csv-export --file #{options_file} ) args << '-v' if option_verbose? args += %W( --threads #{option_threads} ) puts "Exporting '#{args.join(' ')}'" if option_verbose? diff --git a/lib/hammer_cli_csv/headpin_api.rb b/lib/hammer_cli_csv/headpin_api.rb index f50ab00..a813d1f 100644 --- a/lib/hammer_cli_csv/headpin_api.rb +++ b/lib/hammer_cli_csv/headpin_api.rb @@ -1,14 +1,3 @@ -# Copyright 2014 Red Hat, Inc. -# -# This software is licensed to you under the GNU General Public -# License as published by the Free Software Foundation; either version -# 2 of the License (GPLv2) or (at your option) any later version. -# There is NO WARRANTY for this software, express or implied, -# including the implied warranties of MERCHANTABILITY, -# NON-INFRINGEMENT, or FITNESS FOR A PARTICULAR PURPOSE. You should -# have received a copy of GPLv2 along with this software; if not, see -# http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt. - require 'csv' module HammerCLICsv diff --git a/lib/hammer_cli_csv/host_collections.rb b/lib/hammer_cli_csv/host_collections.rb index f971e33..71114c0 100644 --- a/lib/hammer_cli_csv/host_collections.rb +++ b/lib/hammer_cli_csv/host_collections.rb @@ -1,33 +1,15 @@ -# Copyright 2013-2014 Red Hat, Inc. -# -# This software is licensed to you under the GNU General Public -# License as published by the Free Software Foundation; either version -# 2 of the License (GPLv2) or (at your option) any later version. -# There is NO WARRANTY for this software, express or implied, -# including the implied warranties of MERCHANTABILITY, -# NON-INFRINGEMENT, or FITNESS FOR A PARTICULAR PURPOSE. You should -# have received a copy of GPLv2 along with this software; if not, see -# http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt. - - -require 'hammer_cli' -require 'json' -require 'csv' - module HammerCLICsv class CsvCommand class HostCollectionsCommand < BaseCommand command_name 'host-collections' desc 'import or export host collections' - option %w(--organization), 'ORGANIZATION', 'Only process organization matching this name' - ORGANIZATION = 'Organization' LIMIT = 'Limit' DESCRIPTION = 'Description' def export - CSV.open(option_csv_file || '/dev/stdout', 'wb') do |csv| + CSV.open(option_file || '/dev/stdout', 'wb') do |csv| csv << [NAME, COUNT, ORGANIZATION, LIMIT, DESCRIPTION] if @server_status['release'] == 'Headpin' @headpin.get(:organizations).each do |organization| diff --git a/lib/hammer_cli_csv/hosts.rb b/lib/hammer_cli_csv/hosts.rb index 13e1823..7dbe920 100644 --- a/lib/hammer_cli_csv/hosts.rb +++ b/lib/hammer_cli_csv/hosts.rb @@ -1,36 +1,3 @@ -# Copyright 2013-2014 Red Hat, Inc. -# -# This software is licensed to you under the GNU General Public -# License as published by the Free Software Foundation; either version -# 2 of the License (GPLv2) or (at your option) any later version. -# There is NO WARRANTY for this software, express or implied, -# including the implied warranties of MERCHANTABILITY, -# NON-INFRINGEMENT, or FITNESS FOR A PARTICULAR PURPOSE. You should -# have received a copy of GPLv2 along with this software; if not, see -# http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt. - -# -# -= Hosts CSV =- -# -# Columns -# Name -# - Host name -# - May contain '%d' which will be replaced with current iteration number of Count -# - eg. "os%d" -> "os1" -# Count -# - Number of times to iterate on this line of the CSV file -# MAC Address -# - MAC address -# - May contain '%d' which will be replaced with current iteration number of Count -# - eg. "FF:FF:FF:FF:FF:%02x" -> "FF:FF:FF:FF:FF:0A" -# - Warning: be sure to keep count below 255 or MAC hex will exceed limit -# - -require 'hammer_cli' -require 'json' -require 'csv' -require 'uri' - module HammerCLICsv class CsvCommand class HostsCommand < BaseCommand @@ -38,6 +5,7 @@ class HostsCommand < BaseCommand desc 'import or export hosts' ORGANIZATION = 'Organization' + LOCATION = 'Location' ENVIRONMENT = 'Environment' OPERATINGSYSTEM = 'Operating System' ARCHITECTURE = 'Architecture' @@ -46,8 +14,8 @@ class HostsCommand < BaseCommand PARTITIONTABLE = 'Partition Table' def export - CSV.open(option_csv_file || '/dev/stdout', 'wb', {:force_quotes => true}) do |csv| - csv << [NAME, COUNT, ORGANIZATION, ENVIRONMENT, OPERATINGSYSTEM, ARCHITECTURE, MACADDRESS, DOMAIN, PARTITIONTABLE] + CSV.open(option_file || '/dev/stdout', 'wb', {:force_quotes => true}) do |csv| + csv << [NAME, COUNT, ORGANIZATION, LOCATION, ENVIRONMENT, OPERATINGSYSTEM, ARCHITECTURE, MACADDRESS, DOMAIN, PARTITIONTABLE] @api.resource(:hosts).call(:index, {:per_page => 999999})['results'].each do |host| host = @api.resource(:hosts).call(:show, {'id' => host['id']}) raise "Host 'id=#{host['id']}' not found" if !host || host.empty? @@ -84,28 +52,33 @@ def create_hosts_from_csv(line) if !@existing.include? name print "Creating host '#{name}'..." if option_verbose? @api.resource(:hosts).call(:create, { - 'name' => name, - 'root_pass' => 'changeme', - 'mac' => namify(line[MACADDRESS], number), - 'organization_id' => foreman_organization(:name => line[ORGANIZATION]), - 'environment_id' => foreman_environment(:name => line[ENVIRONMENT]), - 'operatingsystem_id' => foreman_operatingsystem(:name => line[OPERATINGSYSTEM]), - 'architecture_id' => foreman_architecture(:name => line[ARCHITECTURE]), - 'domain_id' => foreman_domain(:name => line[DOMAIN]), - 'ptable_id' => foreman_partitiontable(:name => line[PARTITIONTABLE]) + 'host' => { + 'name' => name, + 'root_pass' => 'changeme', + 'mac' => namify(line[MACADDRESS], number), + 'organization_id' => foreman_organization(:name => line[ORGANIZATION]), + 'location_id' => foreman_location(:name => line[LOCATION]), + 'environment_id' => foreman_environment(:name => line[ENVIRONMENT]), + 'operatingsystem_id' => foreman_operatingsystem(:name => line[OPERATINGSYSTEM]), + 'architecture_id' => foreman_architecture(:name => line[ARCHITECTURE]), + 'domain_id' => foreman_domain(:name => line[DOMAIN]), + 'ptable_id' => foreman_partitiontable(:name => line[PARTITIONTABLE]) + } }) else print "Updating host '#{name}'..." if option_verbose? @api.resource(:hosts).call(:update, { 'id' => @existing[name], - 'name' => name, - 'mac' => namify(line[MACADDRESS], number), - 'organization_id' => foreman_organization(:name => line[ORGANIZATION]), - 'environment_id' => foreman_environment(:name => line[ENVIRONMENT]), - 'operatingsystem_id' => foreman_operatingsystem(:name => line[OPERATINGSYSTEM]), - 'architecture_id' => foreman_architecture(:name => line[ARCHITECTURE]), - 'domain_id' => foreman_domain(:name => line[DOMAIN]), - 'ptable_id' => foreman_partitiontable(:name => line[PARTITIONTABLE]) + 'host' => { + 'name' => name, + 'mac' => namify(line[MACADDRESS], number), + 'organization_id' => foreman_organization(:name => line[ORGANIZATION]), + 'environment_id' => foreman_environment(:name => line[ENVIRONMENT]), + 'operatingsystem_id' => foreman_operatingsystem(:name => line[OPERATINGSYSTEM]), + 'architecture_id' => foreman_architecture(:name => line[ARCHITECTURE]), + 'domain_id' => foreman_domain(:name => line[DOMAIN]), + 'ptable_id' => foreman_partitiontable(:name => line[PARTITIONTABLE]) + } }) end print "done\n" if option_verbose? diff --git a/lib/hammer_cli_csv/import.rb b/lib/hammer_cli_csv/import.rb index 897f901..53a534e 100644 --- a/lib/hammer_cli_csv/import.rb +++ b/lib/hammer_cli_csv/import.rb @@ -1,15 +1,3 @@ -# Copyright 2013-2014 Red Hat, Inc. -# -# This software is licensed to you under the GNU General Public -# License as published by the Free Software Foundation; either version -# 2 of the License (GPLv2) or (at your option) any later version. -# There is NO WARRANTY for this software, express or implied, -# including the implied warranties of MERCHANTABILITY, -# NON-INFRINGEMENT, or FITNESS FOR A PARTICULAR PURPOSE. You should -# have received a copy of GPLv2 along with this software; if not, see -# http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt. - - module HammerCLICsv class CsvCommand class ImportCommand < HammerCLI::Apipie::Command @@ -60,7 +48,7 @@ def hammer_resource(resource) raise "File for #{resource} '#{options_file}' does not exist" end - args = %W( csv #{resource.sub('_', '-')} --csv-file #{options_file} ) + args = %W( csv #{resource.sub('_', '-')} --file #{options_file} ) args << '-v' if option_verbose? args += %W( --threads #{option_threads} ) hammer.run(args) diff --git a/lib/hammer_cli_csv/installation_medias.rb b/lib/hammer_cli_csv/installation_medias.rb index 32e6af1..464d52d 100644 --- a/lib/hammer_cli_csv/installation_medias.rb +++ b/lib/hammer_cli_csv/installation_medias.rb @@ -1,14 +1,3 @@ -# Copyright 2013-2014 Red Hat, Inc. -# -# This software is licensed to you under the GNU General Public -# License as published by the Free Software Foundation; either version -# 2 of the License (GPLv2) or (at your option) any later version. -# There is NO WARRANTY for this software, express or implied, -# including the implied warranties of MERCHANTABILITY, -# NON-INFRINGEMENT, or FITNESS FOR A PARTICULAR PURPOSE. You should -# have received a copy of GPLv2 along with this software; if not, see -# http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt. - module HammerCLICsv class CsvCommand class InstallationMediasCommand < BaseCommand @@ -20,7 +9,7 @@ class InstallationMediasCommand < BaseCommand ORGANIZATIONS = 'Organizations' def export - CSV.open(option_csv_file || '/dev/stdout', 'wb', {:force_quotes => true}) do |csv| + CSV.open(option_file || '/dev/stdout', 'wb', {:force_quotes => true}) do |csv| csv << [NAME, COUNT, PATH, OSFAMILY] @api.resource(:media).call(:index, {:per_page => 999999})['results'].each do |installation_media| name = installation_media['name'] diff --git a/lib/hammer_cli_csv/lifecycle_environments.rb b/lib/hammer_cli_csv/lifecycle_environments.rb index c06e52d..51e6052 100644 --- a/lib/hammer_cli_csv/lifecycle_environments.rb +++ b/lib/hammer_cli_csv/lifecycle_environments.rb @@ -1,44 +1,15 @@ -# Copyright 2013-2014 Red Hat, Inc. -# -# This software is licensed to you under the GNU General Public -# License as published by the Free Software Foundation; either version -# 2 of the License (GPLv2) or (at your option) any later version. -# There is NO WARRANTY for this software, express or implied, -# including the implied warranties of MERCHANTABILITY, -# NON-INFRINGEMENT, or FITNESS FOR A PARTICULAR PURPOSE. You should -# have received a copy of GPLv2 along with this software; if not, see -# http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt. - -# -# -= Environments CSV =- -# -# Columns -# Name -# - Environment name -# - May contain '%d' which will be replaced with current iteration number of Count -# - eg. "os%d" -> "os1" -# Count -# - Number of times to iterate on this line of the CSV file -# - -require 'hammer_cli' -require 'json' -require 'csv' - module HammerCLICsv class CsvCommand class LifecycleEnvironmentsCommand < BaseCommand command_name 'lifecycle-environments' desc 'import or export lifecycle environments' - option %w(--organization), 'ORGANIZATION', 'Only process organization matching this name' - ORGANIZATION = 'Organization' PRIORENVIRONMENT = 'Prior Environment' DESCRIPTION = 'Description' def export - CSV.open(option_csv_file || '/dev/stdout', 'wb', {:force_quotes => true}) do |csv| + CSV.open(option_file || '/dev/stdout', 'wb', {:force_quotes => true}) do |csv| csv << [NAME, COUNT, ORGANIZATION, PRIORENVIRONMENT, DESCRIPTION] @api.resource(:organizations).call(:index, { 'per_page' => 999999 @@ -92,7 +63,7 @@ def create_environments_from_csv(line) @api.resource(:lifecycle_environments).call(:create, { 'organization_id' => foreman_organization(:name => line[ORGANIZATION]), 'name' => name, - 'prior' => lifecycle_environment(line[ORGANIZATION], :name => prior), + #'prior' => lifecycle_environment(line[ORGANIZATION], :name => prior), 'prior_id' => lifecycle_environment(line[ORGANIZATION], :name => prior), 'description' => line[DESCRIPTION] }) @@ -100,9 +71,6 @@ def create_environments_from_csv(line) print "Updating environment '#{name}'..." if option_verbose? @api.resource(:lifecycle_environments).call(:update, { 'id' => @existing[line[ORGANIZATION]][name], - 'name' => name, - 'new_name' => name, - 'organization_id' => foreman_organization(:name => line[ORGANIZATION]), 'description' => line[DESCRIPTION] }) end diff --git a/lib/hammer_cli_csv/locations.rb b/lib/hammer_cli_csv/locations.rb index 0c3132d..113f76b 100644 --- a/lib/hammer_cli_csv/locations.rb +++ b/lib/hammer_cli_csv/locations.rb @@ -1,32 +1,3 @@ -# Copyright 2013-2014 Red Hat, Inc. -# -# This software is licensed to you under the GNU General Public -# License as published by the Free Software Foundation; either version -# 2 of the License (GPLv2) or (at your option) any later version. -# There is NO WARRANTY for this software, express or implied, -# including the implied warranties of MERCHANTABILITY, -# NON-INFRINGEMENT, or FITNESS FOR A PARTICULAR PURPOSE. You should -# have received a copy of GPLv2 along with this software; if not, see -# http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt. - -# -# -= Locations CSV =- -# -# Columns -# Name -# - Name of the location. -# - May contain '%d' which will be replaced with current iteration number of Count -# - eg. "location%d" -> "location1" -# Count -# - Number of times to iterate on this line of the CSV file -# Parent -# - Parent location -# - -require 'hammer_cli' -require 'json' -require 'csv' - module HammerCLICsv class CsvCommand class LocationsCommand < BaseCommand @@ -36,7 +7,7 @@ class LocationsCommand < BaseCommand PARENT = 'Parent Location' def export - CSV.open(option_csv_file || '/dev/stdout', 'wb', {:force_quotes => true}) do |csv| + CSV.open(option_file || '/dev/stdout', 'wb', {:force_quotes => true}) do |csv| csv << [NAME, COUNT, PARENT] @api.resource(:locations).call(:index, {:per_page => 999999})['results'].each do |location| csv << [location['name'], 1, ''] diff --git a/lib/hammer_cli_csv/operating_systems.rb b/lib/hammer_cli_csv/operating_systems.rb index d92a934..4732d84 100644 --- a/lib/hammer_cli_csv/operating_systems.rb +++ b/lib/hammer_cli_csv/operating_systems.rb @@ -1,14 +1,3 @@ -# Copyright 2013-2014 Red Hat, Inc. -# -# This software is licensed to you under the GNU General Public -# License as published by the Free Software Foundation; either version -# 2 of the License (GPLv2) or (at your option) any later version. -# There is NO WARRANTY for this software, express or implied, -# including the implied warranties of MERCHANTABILITY, -# NON-INFRINGEMENT, or FITNESS FOR A PARTICULAR PURPOSE. You should -# have received a copy of GPLv2 along with this software; if not, see -# http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt. - module HammerCLICsv class CsvCommand class OperatingSystemsCommand < BaseCommand @@ -16,10 +5,11 @@ class OperatingSystemsCommand < BaseCommand desc 'import or export operating systems' FAMILY = 'Family' + DESCRIPTION = 'Description' def export - CSV.open(option_csv_file || '/dev/stdout', 'wb', {:force_quotes => true}) do |csv| - csv << [NAME, COUNT, FAMILY] + CSV.open(option_file || '/dev/stdout', 'wb', {:force_quotes => true}) do |csv| + csv << [NAME, COUNT, DESCRIPTION, FAMILY] @api.resource(:operatingsystems).call(:index, {:per_page => 999999})['results'].each do |operatingsystem| name = build_os_name(operatingsystem['name'], operatingsystem['major'], operatingsystem['minor']) count = 1 @@ -42,30 +32,25 @@ def import end def create_operatingsystems_from_csv(line) + params = { + 'operatingsystem' => { + 'family' => line[FAMILY], + 'description' => line[DESCRIPTION] + } + } line[COUNT].to_i.times do |number| name = namify(line[NAME], number) (osname, major, minor) = split_os_name(name) + params['operatingsystem']['name'] = osname + params['operatingsystem']['major'] = major + params['operatingsystem']['minor'] = minor if !@existing.include? name print "Creating operating system '#{name}'..." if option_verbose? - @api.resource(:operatingsystems).call(:create, { - 'operatingsystem' => { - 'name' => osname, - 'major' => major, - 'minor' => minor, - 'family' => line[FAMILY] - } - }) + @api.resource(:operatingsystems).call(:create, params) else print "Updating operating system '#{name}'..." if option_verbose? - @api.resource(:operatingsystems).call(:update, { - 'id' => @existing[name], - 'operatingsystem' => { - 'name' => osname, - 'major' => major, - 'minor' => minor, - 'family' => line[FAMILY] - } - }) + params['id'] = @existing[name] + @api.resource(:operatingsystems).call(:update, params) end print "done\n" if option_verbose? end diff --git a/lib/hammer_cli_csv/organizations.rb b/lib/hammer_cli_csv/organizations.rb index 08ee2b5..a7480af 100644 --- a/lib/hammer_cli_csv/organizations.rb +++ b/lib/hammer_cli_csv/organizations.rb @@ -1,36 +1,3 @@ -# Copyright 2013-2014 Red Hat, Inc. -# -# This software is licensed to you under the GNU General Public -# License as published by the Free Software Foundation; either version -# 2 of the License (GPLv2) or (at your option) any later version. -# There is NO WARRANTY for this software, express or implied, -# including the implied warranties of MERCHANTABILITY, -# NON-INFRINGEMENT, or FITNESS FOR A PARTICULAR PURPOSE. You should -# have received a copy of GPLv2 along with this software; if not, see -# http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt. - -# -# -= Organizations CSV =- -# -# Columns -# Name -# - Name of the organization. -# - May contain '%d' which will be replaced with current iteration number of Count -# - eg. "organization%d" -> "organization1" -# Count -# - Number of times to iterate on this line of the CSV file -# Org Label -# - Label of the organization. -# - May contain '%d' which will be replaced with current iteration number of Count -# - eg. "organization%d" -> "organization1" -# Description -# - -require 'hammer_cli' -#require 'net/http' -require 'json' -require 'csv' - module HammerCLICsv class CsvCommand class OrganizationsCommand < BaseCommand @@ -41,15 +8,17 @@ class OrganizationsCommand < BaseCommand DESCRIPTION = 'Description' def export - CSV.open(option_csv_file || '/dev/stdout', 'wb', {:force_quotes => true}) do |csv| + CSV.open(option_file || '/dev/stdout', 'wb', {:force_quotes => true}) do |csv| csv << [NAME, COUNT, LABEL, DESCRIPTION] if @server_status['release'] == 'Headpin' @headpin.get(:organizations).each do |organization| + next if option_organization && organization['name'] != option_organization csv << [organization['name'], 1, organization['label'], organization['description']] end else @api.resource(:organizations).call(:index, {:per_page => 999999})['results'].each do |organization| + next if option_organization && organization['name'] != option_organization csv << [organization['name'], 1, organization['label'], organization['description']] end end @@ -68,6 +37,8 @@ def import end def create_organizations_from_csv(line) + return if option_organization && line[ORGANIZATION] != option_organization + line[COUNT].to_i.times do |number| name = namify(line[NAME], number) label = namify(line[LABEL], number) diff --git a/lib/hammer_cli_csv/partition_tables.rb b/lib/hammer_cli_csv/partition_tables.rb index 8538c6e..cc1a1d1 100644 --- a/lib/hammer_cli_csv/partition_tables.rb +++ b/lib/hammer_cli_csv/partition_tables.rb @@ -1,50 +1,53 @@ -# Copyright 2013-2014 Red Hat, Inc. -# -# This software is licensed to you under the GNU General Public -# License as published by the Free Software Foundation; either version -# 2 of the License (GPLv2) or (at your option) any later version. -# There is NO WARRANTY for this software, express or implied, -# including the implied warranties of MERCHANTABILITY, -# NON-INFRINGEMENT, or FITNESS FOR A PARTICULAR PURPOSE. You should -# have received a copy of GPLv2 along with this software; if not, see -# http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt. - -# -# -= Partition Tables CSV =- -# -# Columns -# Name -# - Partition table name -# - May contain '%d' which will be replaced with current iteration number of Count -# - eg. "os%d" -> "os1" -# Count -# - Number of times to iterate on this line of the CSV file -# - -require 'hammer_cli' -require 'json' -require 'csv' - module HammerCLICsv class CsvCommand class PartitionTablesCommand < BaseCommand command_name 'partition-tables' desc 'import or export partition tables' + ORGANIZATIONS = 'Organizations' + LOCATIONS = 'Locations' OSFAMILY = 'OS Family' OPERATINGSYSTEMS = 'Operating Systems' LAYOUT = 'Layout' def export - CSV.open(option_csv_file || '/dev/stdout', 'wb', {:force_quotes => true}) do |csv| - csv << [NAME, COUNT, OSFAMILY, OPERATINGSYSTEMS, LAYOUT] + # TODO: partition-tables do not return their organizations or locations + # http://projects.theforeman.org/issues/11175 + organizations_map = {} + @api.resource(:organizations).call(:index, {:per_page => 999999})['results'].each do |organization| + ptables = @api.resource(:ptables).call(:index, {'organization_id' => organization['id']})['results'].each do |ptable| + organizations_map[ptable['name']] ||= [] + organizations_map[ptable['name']] << organization['name'] + end + end + locations_map = {} + @api.resource(:locations).call(:index, {:per_page => 999999})['results'].each do |location| + ptables = @api.resource(:ptables).call(:index, {'location_id' => location['id']})['results'].each do |ptable| + locations_map[ptable['name']] ||= [] + locations_map[ptable['name']] << location['name'] + end + end + + CSV.open(option_file || '/dev/stdout', 'wb', {:force_quotes => true}) do |csv| + csv << [NAME, COUNT, ORGANIZATIONS, LOCATIONS, OSFAMILY, OPERATINGSYSTEMS, LAYOUT] @api.resource(:ptables).call(:index, {:per_page => 999999})['results'].each do |ptable| ptable = @api.resource(:ptables).call(:show, {'id' => ptable['id']}) name = ptable['name'] count = 1 osfamily = ptable['os_family'] layout = ptable['layout'] - csv << [name, count, osfamily, layout] + operatingsystems = export_column(ptable, 'operatingsystems', 'title') + + organizations = CSV.generate do |column| + column << organizations_map[name] if organizations_map[name] + end + organizations.delete!("\n") + locations = CSV.generate do |column| + column << locations_map[name] if locations_map[name] + end + locations.delete!("\n") + + csv << [name, count, organizations, locations, osfamily, operatingsystems, layout] end end end @@ -61,32 +64,42 @@ def import end def create_ptables_from_csv(line) + params = { + 'ptable' => { + 'os_family' => line[OSFAMILY], + 'layout' => line[LAYOUT] + } + } + # Check for backwards compatibility + if apipie_check_param(:ptable, :create, 'ptable[operatingsystem_ids]') + operatingsystems = collect_column(line[OPERATINGSYSTEMS]) do |operatingsystem| + foreman_operatingsystem(:name => operatingsystem) + end + params['ptable']['operatingsystem_ids'] = operatingsystems + end + if apipie_check_param(:ptable, :create, 'ptable[organization_ids]') + organizations = collect_column(line[ORGANIZATIONS]) do |organization| + foreman_organization(:name => organization) + end + params['ptable']['organization_ids'] = organizations + end + if apipie_check_param(:ptable, :create, 'ptable[location_ids]') + locations = collect_column(line[LOCATIONS]) do |location| + foreman_location(:name => location) + end + params['ptable']['location_ids'] = locations + end + line[COUNT].to_i.times do |number| name = namify(line[NAME], number) - operatingsystem_ids = CSV.parse_line(line[OPERATINGSYSTEMS]).collect do |operatingsystem_name| - foreman_operatingsystem(:name => operatingsystem_name) - end if line[OPERATINGSYSTEMS] + params['ptable']['name'] = name if !@existing.include? name - print "Creating ptable '#{name}'... " if option_verbose? - @api.resource(:ptables).call(:create, { - 'ptable' => { - 'name' => name, - 'os_family' => line[OSFAMILY], - 'operatingsystem_ids' => operatingsystem_ids, - 'layout' => line[LAYOUT] - } - }) + print "Creating partition-table '#{name}'... " if option_verbose? + @api.resource(:ptables).call(:create, params) else - print "Updating ptable '#{name}'..." if option_verbose? - @api.resource(:ptables).call(:update, { - 'id' => @existing[name], - 'ptable' => { - 'name' => name, - 'os_family' => line[OSFAMILY], - 'operatingsystem_ids' => operatingsystem_ids, - 'layout' => line[LAYOUT] - } - }) + print "Updating partition-table '#{name}'..." if option_verbose? + params['id'] = @existing[name] + @api.resource(:ptables).call(:update, params) end print "done\n" if option_verbose? end diff --git a/lib/hammer_cli_csv/products.rb b/lib/hammer_cli_csv/products.rb index a5a8c12..44d8896 100644 --- a/lib/hammer_cli_csv/products.rb +++ b/lib/hammer_cli_csv/products.rb @@ -1,23 +1,10 @@ -# Copyright 2013-2014 Red Hat, Inc. -# -# This software is licensed to you under the GNU General Public -# License as published by the Free Software Foundation; either version -# 2 of the License (GPLv2) or (at your option) any later version. -# There is NO WARRANTY for this software, express or implied, -# including the implied warranties of MERCHANTABILITY, -# NON-INFRINGEMENT, or FITNESS FOR A PARTICULAR PURPOSE. You should -# have received a copy of GPLv2 along with this software; if not, see -# http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt. - module HammerCLICsv class CsvCommand class ProductsCommand < BaseCommand command_name 'products' desc _('import or export products') - option %w(--organization), 'ORGANIZATION', _('Only process organization matching this name') - option %w(--sync), 'true|false', _('Sync product repositories (default true)') - + option %w(--[no-]sync), :flag, _('Sync product repositories (default true)'), :default => true LABEL = 'Label' ORGANIZATION = 'Organization' REPOSITORY = 'Repository' @@ -26,7 +13,7 @@ class ProductsCommand < BaseCommand DESCRIPTION = 'Description' def export - CSV.open(option_csv_file || '/dev/stdout', 'wb', {:force_quotes => false}) do |csv| + CSV.open(option_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 @@ -41,6 +28,7 @@ def export 'product_id' => product['id'], 'organization_id' => organization['id'] })['results'].each do |repository| + repository = @api.resource(:repositories).call(:show, {:id => repository['id']}) repository_type = repository['product_type'] == 'custom' ? 'Custom' : 'Red Hat' repository_type += " #{repository['content_type'].capitalize}" csv << [product['name'], 1, product['label'], organization['name'], @@ -140,7 +128,7 @@ def create_products_from_csv(line) @existing_repositories[line[ORGANIZATION] + name][line[LABEL]] = repository end - sync_repository(line, repository) + sync_repository(line, name, repository) puts _('done') if option_verbose? end @@ -161,14 +149,14 @@ def content_type(repository_type) end end - def sync_repository(line, repository) - if option_sync =~ (/A(true|1|yes)$/i) || HammerCLI::Settings.get(:csv, :products_sync) || - (option_sync.nil? && HammerCLI::Settings.get(:csv, :products_sync).nil?) + def sync_repository(line, name, repository) + if (HammerCLI::Settings.get(:csv, :products_sync) == true || HammerCLI::Settings.get(:csv, :products_sync).nil?) && + option_sync? if option_verbose? print _("syncing repository '%{repository_name}' in product '%{name}'...") % - {:repository_name => repository_name, :name => name} + {:repository_name => repository['name'], :name => name} end - if repository['sync_state'] == 'finished' + if repository['last_sync'] print _("previously synced, skipping...") if option_verbose? else exec_sync_repository(line, repository) diff --git a/lib/hammer_cli_csv/provisioning_templates.rb b/lib/hammer_cli_csv/provisioning_templates.rb index 9be229c..2a460e6 100644 --- a/lib/hammer_cli_csv/provisioning_templates.rb +++ b/lib/hammer_cli_csv/provisioning_templates.rb @@ -1,22 +1,9 @@ -# Copyright 2014 Red Hat, Inc. -# -# This software is licensed to you under the GNU General Public -# License as published by the Free Software Foundation; either version -# 2 of the License (GPLv2) or (at your option) any later version. -# There is NO WARRANTY for this software, express or implied, -# including the implied warranties of MERCHANTABILITY, -# NON-INFRINGEMENT, or FITNESS FOR A PARTICULAR PURPOSE. You should -# have received a copy of GPLv2 along with this software; if not, see -# http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt. - module HammerCLICsv class CsvCommand class ProvisioningTemplatesCommand < BaseCommand command_name 'provisioning-templates' desc 'import or export provisioning templates' - option %w(--organization), 'ORGANIZATION', 'Only process organization matching this name' - ORGANIZATIONS = 'Organizations' LOCATIONS = 'Locations' OPERATINGSYSTEMS = 'Operating Systems' @@ -25,7 +12,7 @@ class ProvisioningTemplatesCommand < BaseCommand TEMPLATE = 'Template' def export - CSV.open(option_csv_file || '/dev/stdout', 'wb', {:force_quotes => true}) do |csv| + CSV.open(option_file || '/dev/stdout', 'wb', {:force_quotes => true}) do |csv| csv << [NAME, COUNT, ORGANIZATIONS, LOCATIONS, OPERATINGSYSTEMS, ASSOCIATIONS, KIND, TEMPLATE] @api.resource(:config_templates).call(:index, { :per_page => 999999 diff --git a/lib/hammer_cli_csv/puppet_environments.rb b/lib/hammer_cli_csv/puppet_environments.rb index 6a2a87a..10f55b7 100644 --- a/lib/hammer_cli_csv/puppet_environments.rb +++ b/lib/hammer_cli_csv/puppet_environments.rb @@ -1,30 +1,3 @@ -# Copyright 2013-2014 Red Hat, Inc. -# -# This software is licensed to you under the GNU General Public -# License as published by the Free Software Foundation; either version -# 2 of the License (GPLv2) or (at your option) any later version. -# There is NO WARRANTY for this software, express or implied, -# including the implied warranties of MERCHANTABILITY, -# NON-INFRINGEMENT, or FITNESS FOR A PARTICULAR PURPOSE. You should -# have received a copy of GPLv2 along with this software; if not, see -# http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt. - -# -# -= Environments CSV =- -# -# Columns -# Name -# - Environment name -# - May contain '%d' which will be replaced with current iteration number of Count -# - eg. "os%d" -> "os1" -# Count -# - Number of times to iterate on this line of the CSV file -# - -require 'hammer_cli' -require 'json' -require 'csv' - module HammerCLICsv class CsvCommand class PuppetEnvironmentsCommand < BaseCommand @@ -32,15 +5,18 @@ class PuppetEnvironmentsCommand < BaseCommand desc 'import or export puppet environments' ORGANIZATIONS = 'Organizations' + LOCATIONS = 'Locations' def export - CSV.open(option_csv_file || '/dev/stdout', 'wb', {:force_quotes => true}) do |csv| - csv << [NAME, COUNT, ORGANIZATIONS] + CSV.open(option_file || '/dev/stdout', 'wb', {:force_quotes => true}) do |csv| + csv << [NAME, COUNT, ORGANIZATIONS, LOCATIONS] @api.resource(:environments).call(:index, {:per_page => 999999})['results'].each do |environment| + environment = @api.resource(:environments).call(:show, {:id => environment['id']}) name = environment['name'] count = 1 - csv << [name, count] - raise 'TODO: organizations' + organizations = export_column(environment, 'organizations', 'name') + locations = export_column(environment, 'locations', 'name') + csv << [name, count, organizations, locations] end end end @@ -57,45 +33,47 @@ def import end def create_environments_from_csv(line) + organizations = collect_column(line[ORGANIZATIONS]) do |organization| + foreman_organization(:name => organization) + end + locations = collect_column(line[LOCATIONS]) do |location| + foreman_location(:name => location) + end + line[COUNT].to_i.times do |number| name = namify(line[NAME], number) if !@existing.include? name print "Creating environment '#{name}'..." if option_verbose? id = @api.resource(:environments).call(:create, { 'environment' => { - 'name' => name + 'name' => name, + 'organization_ids' => organizations } })['id'] else print "Updating environment '#{name}'..." if option_verbose? - id = @api.resource(:environments).call(:update, { - 'id' => @existing[name], - 'environment' => { - 'name' => name - } - })['environment']['id'] - end - # Update associated resources - # TODO: Bug #4738: organization json does not include puppet environments - # http://projects.theforeman.org/issues/4738#change-15319 - # Update below to match style of domains - organization_ids = CSV.parse_line(line[ORGANIZATIONS]).collect do |organization| - foreman_organization(:name => organization) - end - organization_ids += @api.resource(:environments).call(:show, {'id' => id})['organizations'].collect do |organization| - organization['id'] - end - organization_ids.uniq! + environment = @api.resource(:environments).call(:show, {'id' => @existing[name]}) + environment['organizations'].collect do |organization| + organizations << organization['id'] + end + organizations.uniq! + environment['locations'].collect do |location| + locations << location['id'] + end + locations.uniq! - @api.resource(:environments).call(:update, { - 'id' => id, - 'environment' => { - 'organization_ids' => organization_ids - } - }) + @api.resource(:environments).call(:update, { + 'id' => @existing[name], + 'environment' => { + 'name' => name, + 'organization_ids' => organizations, + 'location_ids' => locations + } + }) + end - print "done\n" if option_verbose? + puts "done" if option_verbose? end rescue RuntimeError => e raise "#{e}\n #{line}" diff --git a/lib/hammer_cli_csv/puppet_facts.rb b/lib/hammer_cli_csv/puppet_facts.rb index 2057ae1..204eae4 100644 --- a/lib/hammer_cli_csv/puppet_facts.rb +++ b/lib/hammer_cli_csv/puppet_facts.rb @@ -1,32 +1,3 @@ -# Copyright 2013-2014 Red Hat, Inc. -# -# This software is licensed to you under the GNU General Public -# License as published by the Free Software Foundation; either version -# 2 of the License (GPLv2) or (at your option) any later version. -# There is NO WARRANTY for this software, express or implied, -# including the implied warranties of MERCHANTABILITY, -# NON-INFRINGEMENT, or FITNESS FOR A PARTICULAR PURPOSE. You should -# have received a copy of GPLv2 along with this software; if not, see -# http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt. - -# -# -= Puppet Facts CSV =- -# -# Columns -# Name -# - Host name -# - May contain '%d' which will be replaced with current iteration number of Count -# - eg. "os%d" -> "os1" -# Count -# - Number of times to iterate on this line of the CSV file -# -# - May contain '%d' which will be replaced with current iteration number of Count -# - -require 'hammer_cli' -require 'json' -require 'csv' - module HammerCLICsv class CsvCommand class PuppetFactsCommand < BaseCommand @@ -34,7 +5,7 @@ class PuppetFactsCommand < BaseCommand desc 'import or export puppet facts' def export - CSV.open(option_csv_file || '/dev/stdout', 'wb', {:force_quotes => true}) do |csv| + CSV.open(option_file || '/dev/stdout', 'wb', {:force_quotes => true}) do |csv| headers = [NAME, COUNT] # Extracted facts are always based upon the first host found, otherwise this would be an intensive # method to gather all the possible column names diff --git a/lib/hammer_cli_csv/puppet_reports.rb b/lib/hammer_cli_csv/puppet_reports.rb index 00698b6..6b029ec 100644 --- a/lib/hammer_cli_csv/puppet_reports.rb +++ b/lib/hammer_cli_csv/puppet_reports.rb @@ -1,36 +1,3 @@ -# Copyright 2013-2014 Red Hat, Inc. -# -# This software is licensed to you under the GNU General Public -# License as published by the Free Software Foundation; either version -# 2 of the License (GPLv2) or (at your option) any later version. -# There is NO WARRANTY for this software, express or implied, -# including the implied warranties of MERCHANTABILITY, -# NON-INFRINGEMENT, or FITNESS FOR A PARTICULAR PURPOSE. You should -# have received a copy of GPLv2 along with this software; if not, see -# http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt. - -# -# -= Systems CSV =- -# -# Columns -# Name -# - System name -# - May contain '%d' which will be replaced with current iteration number of Count -# - eg. "os%d" -> "os1" -# Count -# - Number of times to iterate on this line of the CSV file -# MAC Address -# - MAC address -# - May contain '%d' which will be replaced with current iteration number of Count -# - eg. "FF:FF:FF:FF:FF:%02x" -> "FF:FF:FF:FF:FF:0A" -# - Warning: be sure to keep count below 255 or MAC hex will exceed limit -# - -require 'hammer_cli' -require 'json' -require 'csv' -require 'uri' - module HammerCLICsv class CsvCommand class PuppetReportsCommand < BaseCommand @@ -53,7 +20,7 @@ class PuppetReportsCommand < BaseCommand SUBSCRIPTIONS = 'Subscriptions' def export - CSV.open(option_csv_file || '/dev/stdout', 'wb', {:force_quotes => false}) do |csv| + CSV.open(option_file || '/dev/stdout', 'wb', {:force_quotes => false}) do |csv| csv << [NAME, COUNT, ORGANIZATION, ENVIRONMENT, CONTENTVIEW, SYSTEMGROUPS, VIRTUAL, HOST, OPERATINGSYSTEM, ARCHITECTURE, SOCKETS, RAM, CORES, SLA, PRODUCTS, SUBSCRIPTIONS] @api.resource(:organizations).call(:index, { diff --git a/lib/hammer_cli_csv/reports.rb b/lib/hammer_cli_csv/reports.rb index 4ba0be0..47078fa 100644 --- a/lib/hammer_cli_csv/reports.rb +++ b/lib/hammer_cli_csv/reports.rb @@ -1,14 +1,3 @@ -# Copyright 2013-2014 Red Hat, Inc. -# -# This software is licensed to you under the GNU General Public -# License as published by the Free Software Foundation; either version -# 2 of the License (GPLv2) or (at your option) any later version. -# There is NO WARRANTY for this software, express or implied, -# including the implied warranties of MERCHANTABILITY, -# NON-INFRINGEMENT, or FITNESS FOR A PARTICULAR PURPOSE. You should -# have received a copy of GPLv2 along with this software; if not, see -# http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt. - module HammerCLICsv class CsvCommand class ReportsCommand < BaseCommand @@ -25,7 +14,7 @@ class ReportsCommand < BaseCommand METRICS = 'Metrics' def export - CSV.open(option_csv_file || '/dev/stdout', 'wb', {:force_quotes => false}) do |csv| + CSV.open(option_file || '/dev/stdout', 'wb', {:force_quotes => false}) do |csv| csv << [NAME, COUNT] @api.resource(:reports).call(:index, { 'per_page' => 999999 diff --git a/lib/hammer_cli_csv/roles.rb b/lib/hammer_cli_csv/roles.rb index 3a73bef..d703658 100644 --- a/lib/hammer_cli_csv/roles.rb +++ b/lib/hammer_cli_csv/roles.rb @@ -1,14 +1,3 @@ -# Copyright 2013-2014 Red Hat, Inc. -# -# This software is licensed to you under the GNU General Public -# License as published by the Free Software Foundation; either version -# 2 of the License (GPLv2) or (at your option) any later version. -# There is NO WARRANTY for this software, express or implied, -# including the implied warranties of MERCHANTABILITY, -# NON-INFRINGEMENT, or FITNESS FOR A PARTICULAR PURPOSE. You should -# have received a copy of GPLv2 along with this software; if not, see -# http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt. - module HammerCLICsv class CsvCommand class RolesCommand < BaseCommand @@ -22,7 +11,7 @@ class RolesCommand < BaseCommand LOCATIONS = 'Locations' def export - CSV.open(option_csv_file || '/dev/stdout', 'wb', {:force_quotes => false}) do |csv| + CSV.open(option_file || '/dev/stdout', 'wb', {:force_quotes => false}) do |csv| csv << [NAME, COUNT, RESOURCE, SEARCH, PERMISSIONS, ORGANIZATIONS, LOCATIONS] @api.resource(:roles).call(:index, {'per_page' => 999999})['results'].each do |role| @api.resource(:filters).call(:index, { diff --git a/lib/hammer_cli_csv/settings.rb b/lib/hammer_cli_csv/settings.rb new file mode 100644 index 0000000..921c001 --- /dev/null +++ b/lib/hammer_cli_csv/settings.rb @@ -0,0 +1,49 @@ +module HammerCLICsv + class CsvCommand + class SettingsCommand < BaseCommand + command_name 'settings' + desc 'import or export settings' + + VALUE = 'Value' + + def export + CSV.open(option_file || '/dev/stdout', 'wb') do |csv| + csv << [NAME, COUNT, VALUE] + @api.resource(:settings).call(:index, {'per_page' => 999999})['results'].each do |setting| + csv << [setting['name'], 1, setting['value']] + end + end + end + + def import + @existing = {} + + thread_import do |line| + create_settings_from_csv(line) + end + end + + def create_settings_from_csv(line) + line[COUNT].to_i.times do |number| + name = namify(line[NAME], number) + params = { 'id' => get_setting_id(name), + 'setting' => { + 'value' => line[VALUE] + } + } + print "Updating setting '#{name}'..." if option_verbose? + @api.resource(:settings).call(:update, params) + end + print "done\n" if option_verbose? + end + + private + + def get_setting_id(name) + results = @api.resource(:settings).call(:index, { :search => "name=\"#{name}\"" })['results'] + raise "Setting '#{name}' not found" if !results || results.empty? + results[0]['id'] + end + end + end +end diff --git a/lib/hammer_cli_csv/smart_proxies.rb b/lib/hammer_cli_csv/smart_proxies.rb index 46f3f20..94f7bcd 100644 --- a/lib/hammer_cli_csv/smart_proxies.rb +++ b/lib/hammer_cli_csv/smart_proxies.rb @@ -1,18 +1,3 @@ -# Copyright 2013-2014 Red Hat, Inc. -# -# This software is licensed to you under the GNU General Public -# License as published by the Free Software Foundation; either version -# 2 of the License (GPLv2) or (at your option) any later version. -# There is NO WARRANTY for this software, express or implied, -# including the implied warranties of MERCHANTABILITY, -# NON-INFRINGEMENT, or FITNESS FOR A PARTICULAR PURPOSE. You should -# have received a copy of GPLv2 along with this software; if not, see -# http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt. - -require 'hammer_cli' -require 'json' -require 'csv' - module HammerCLICsv class CsvCommand class SmartProxiesCommand < BaseCommand @@ -25,7 +10,7 @@ class SmartProxiesCommand < BaseCommand LIFECYCLE_ENVIRONMENTS = 'Lifecycle Environments' def export - CSV.open(option_csv_file || '/dev/stdout', 'wb', {:force_quotes => true}) do |csv| + CSV.open(option_file || '/dev/stdout', 'wb', {:force_quotes => true}) do |csv| csv << [NAME, COUNT, ORGANIZATIONS, LOCATIONS, URL, LIFECYCLE_ENVIRONMENTS] @api.resource(:smart_proxies).call(:index, {:per_page => 999999})['results'].each do |smart_proxy| smart_proxy = @api.resource(:smart_proxies).call(:show, {'id' => smart_proxy['id']}) diff --git a/lib/hammer_cli_csv/splice.rb b/lib/hammer_cli_csv/splice.rb index a6d732a..9e24199 100644 --- a/lib/hammer_cli_csv/splice.rb +++ b/lib/hammer_cli_csv/splice.rb @@ -1,14 +1,3 @@ -# Copyright 2013-2014 Red Hat, Inc. -# -# This software is licensed to you under the GNU General Public -# License as published by the Free Software Foundation; either version -# 2 of the License (GPLv2) or (at your option) any later version. -# There is NO WARRANTY for this software, express or implied, -# including the implied warranties of MERCHANTABILITY, -# NON-INFRINGEMENT, or FITNESS FOR A PARTICULAR PURPOSE. You should -# have received a copy of GPLv2 along with this software; if not, see -# http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt. - require 'openssl' require 'date' @@ -18,7 +7,6 @@ class SpliceCommand < BaseCommand command_name 'splice' desc 'import Satellite-5 splice data' - option %w(--organization), 'ORGANIZATION', 'Only process organization matching this name' option %w(--dir), 'DIR', 'Directory of Splice exported CSV files (default pwd)' option %w(--mapping-dir), 'DIR', diff --git a/lib/hammer_cli_csv/subnets.rb b/lib/hammer_cli_csv/subnets.rb index 9dff41d..f718470 100644 --- a/lib/hammer_cli_csv/subnets.rb +++ b/lib/hammer_cli_csv/subnets.rb @@ -1,18 +1,3 @@ -# Copyright 2013-2014 Red Hat, Inc. -# -# This software is licensed to you under the GNU General Public -# License as published by the Free Software Foundation; either version -# 2 of the License (GPLv2) or (at your option) any later version. -# There is NO WARRANTY for this software, express or implied, -# including the implied warranties of MERCHANTABILITY, -# NON-INFRINGEMENT, or FITNESS FOR A PARTICULAR PURPOSE. You should -# have received a copy of GPLv2 along with this software; if not, see -# http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt. - -require 'hammer_cli' -require 'json' -require 'csv' - module HammerCLICsv class CsvCommand class SubnetsCommand < BaseCommand @@ -35,7 +20,7 @@ class SubnetsCommand < BaseCommand VLAN_ID = 'VLAN ID' def export - CSV.open(option_csv_file || '/dev/stdout', 'wb', {:force_quotes => true}) do |csv| + CSV.open(option_file || '/dev/stdout', 'wb', {:force_quotes => true}) do |csv| csv << [NAME, COUNT, ORGANIZATIONS, LOCATIONS, NETWORK, NETWORK_MASK, NETWORK_FROM, NETWORK_TO, DOMAINS, GATEWAY, DHCP_PROXY, TFTP_PROXY, DNS_PROXY, DNS_PRIMARY, DNS_SECONDARY, VLAN_ID] @@ -87,7 +72,12 @@ def create_subnets_from_csv(line) print "Creating subnet '#{name}'..." if option_verbose? id = @api.resource(:subnets).call(:create, { 'subnet' => { - 'name' => name + 'name' => name, + 'network' => line[NETWORK], + 'mask' => line[NETWORK_MASK], + #'from' => line[NETWORK_FROM], + #'to' => line[NETWORK_TO], + #'domain_ids' => line[DOMAINS] } })['id'] else diff --git a/lib/hammer_cli_csv/subscriptions.rb b/lib/hammer_cli_csv/subscriptions.rb index 0e6697b..232e66d 100644 --- a/lib/hammer_cli_csv/subscriptions.rb +++ b/lib/hammer_cli_csv/subscriptions.rb @@ -1,23 +1,9 @@ -# Copyright 2013-2014 Red Hat, Inc. -# -# This software is licensed to you under the GNU General Public -# License as published by the Free Software Foundation; either version -# 2 of the License (GPLv2) or (at your option) any later version. -# There is NO WARRANTY for this software, express or implied, -# including the implied warranties of MERCHANTABILITY, -# NON-INFRINGEMENT, or FITNESS FOR A PARTICULAR PURPOSE. You should -# have received a copy of GPLv2 along with this software; if not, see -# http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt. - - module HammerCLICsv class CsvCommand class SubscriptionsCommand < BaseCommand command_name 'subscriptions' desc 'import or export subscriptions' - option %w(--organization), 'ORGANIZATION', 'Only process organization matching this name' - ORGANIZATION = 'Organization' MANIFEST = 'Manifest File' CONTENT_SET = 'Content Set' @@ -25,7 +11,7 @@ class SubscriptionsCommand < BaseCommand RELEASE = 'Release' def export - CSV.open(option_csv_file || '/dev/stdout', 'wb', {:force_quotes => false}) do |csv| + CSV.open(option_file || '/dev/stdout', 'wb', {:force_quotes => false}) do |csv| csv << [NAME, COUNT, ORGANIZATION, MANIFEST, CONTENT_SET, ARCH, RELEASE] @api.resource(:organizations).call(:index, {:per_page => 999999})['results'].each do |organization| next if option_organization && organization['name'] != option_organization @@ -34,7 +20,7 @@ def export 'organization_id' => organization['id'], 'enabled' => true })['results'].each do |product| - if product['provider']['name'] == 'Red Hat' + if product['redhat'] name = product['name'] @api.resource(:repository_sets).call(:index, { 'per_page' => 999999, @@ -75,7 +61,7 @@ def enable_products_from_csv(line) })['results'] raise "No match for product '#{line[NAME]}'" if results.length == 0 raise "Multiple matches for product '#{line[NAME]}'" if results.length != 1 - product = results[0] + product = @api.resource(:products).call(:show, {'id' => results[0]['id']}) results = @api.resource(:repository_sets).call(:index, { 'per_page' => 999999, diff --git a/lib/hammer_cli_csv/sync_plans.rb b/lib/hammer_cli_csv/sync_plans.rb index 78569c9..d044753 100644 --- a/lib/hammer_cli_csv/sync_plans.rb +++ b/lib/hammer_cli_csv/sync_plans.rb @@ -1,23 +1,9 @@ -# Copyright 2013-2014 Red Hat, Inc. -# -# This software is licensed to you under the GNU General Public -# License as published by the Free Software Foundation; either version -# 2 of the License (GPLv2) or (at your option) any later version. -# There is NO WARRANTY for this software, express or implied, -# including the implied warranties of MERCHANTABILITY, -# NON-INFRINGEMENT, or FITNESS FOR A PARTICULAR PURPOSE. You should -# have received a copy of GPLv2 along with this software; if not, see -# http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt. - - module HammerCLICsv class CsvCommand class SyncPlansCommand < BaseCommand command_name 'sync-plans' desc 'import or export repository sync plans' - option %w(--organization), 'ORGANIZATION', 'Only process organization matching this name' - ORGANIZATION = 'Organization' DESCRIPTION = 'Description' ENABLED = 'Enabled' @@ -26,7 +12,7 @@ class SyncPlansCommand < BaseCommand PRODUCTS = 'Products' def export - CSV.open(option_csv_file || '/dev/stdout', 'wb', {:force_quotes => false}) do |csv| + CSV.open(option_file || '/dev/stdout', 'wb', {:force_quotes => false}) do |csv| csv << [NAME, COUNT, ORGANIZATION, DESCRIPTION, ENABLED, STARTDATE, INTERVAL, PRODUCTS] @api.resource(:organizations).call(:index, {:per_page => 999999})['results'].each do |organization| diff --git a/lib/hammer_cli_csv/users.rb b/lib/hammer_cli_csv/users.rb index 71f428c..f7d2622 100644 --- a/lib/hammer_cli_csv/users.rb +++ b/lib/hammer_cli_csv/users.rb @@ -1,14 +1,3 @@ -# Copyright 2013-2014 Red Hat, Inc. -# -# This software is licensed to you under the GNU General Public -# License as published by the Free Software Foundation; either version -# 2 of the License (GPLv2) or (at your option) any later version. -# There is NO WARRANTY for this software, express or implied, -# including the implied warranties of MERCHANTABILITY, -# NON-INFRINGEMENT, or FITNESS FOR A PARTICULAR PURPOSE. You should -# have received a copy of GPLv2 along with this software; if not, see -# http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt. - module HammerCLICsv class CsvCommand class UsersCommand < BaseCommand @@ -24,7 +13,7 @@ class UsersCommand < BaseCommand ROLES = 'Roles' def export - CSV.open(option_csv_file || '/dev/stdout', 'wb', {:force_quotes => true}) do |csv| + CSV.open(option_file || '/dev/stdout', 'wb', {:force_quotes => true}) do |csv| csv << [NAME, COUNT, FIRSTNAME, LASTNAME, EMAIL, ORGANIZATIONS, LOCATIONS, ADMIN, ROLES] @api.resource(:users).call(:index, {:per_page => 999999})['results'].each do |user| if user['organizations'] diff --git a/lib/hammer_cli_csv/version.rb b/lib/hammer_cli_csv/version.rb index 56fc0cf..0b697c2 100644 --- a/lib/hammer_cli_csv/version.rb +++ b/lib/hammer_cli_csv/version.rb @@ -1,14 +1,3 @@ -# Copyright 2013-2014 Red Hat, Inc. -# -# This software is licensed to you under the GNU General Public -# License as published by the Free Software Foundation; either version -# 2 of the License (GPLv2) or (at your option) any later version. -# There is NO WARRANTY for this software, express or implied, -# including the implied warranties of MERCHANTABILITY, -# NON-INFRINGEMENT, or FITNESS FOR A PARTICULAR PURPOSE. You should -# have received a copy of GPLv2 along with this software; if not, see -# http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt. - module HammerCLICsv def self.version @version ||= Gem::Version.new('1.0.1') diff --git a/test/data/content-views.csv b/test/data/content-views.csv index b66c43c..92be68c 100644 --- a/test/data/content-views.csv +++ b/test/data/content-views.csv @@ -1,6 +1,6 @@ -Name,Count,Organization,Description,Composite,Repositories -Katello,1,Mega Corporation,"Katello - The Sysadmin's Fortress",No,katello-1.4 -Katello,1,Mega Corporation,"Katello - The Sysadmin's Fortress",No,katello-1.4-client -RHEL 6Server (Minimal),1,Mega Corporation,"RHEL 6Server - minimal installation",No,Red Hat Enterprise Linux 6 Server RPMs x86_64 6Server -RHEL 6.1 Kickstart,1,Mega Corporation,"RHEL 6.1 Kickstart",No,Red Hat Enterprise Linux 6 Server Kickstart x86_64 6.1 +Name,Count,Organization,Description,Composite,Repositories,Lifecycle Environments +Katello,1,Mega Corporation,"Katello - The Sysadmin's Fortress",No,katello-1.4,Library +Katello,1,Mega Corporation,"Katello - The Sysadmin's Fortress",No,katello-1.4-client,Library +RHEL 6Server (Minimal),1,Mega Corporation,"RHEL 6Server - minimal installation",No,Red Hat Enterprise Linux 6 Server RPMs x86_64 6Server,Library +RHEL 6.1 Kickstart,1,Mega Corporation,"RHEL 6.1 Kickstart",No,Red Hat Enterprise Linux 6 Server Kickstart x86_64 6.1,Library diff --git a/test/data/hosts.csv b/test/data/hosts.csv index f34851b..2cd1a0d 100644 --- a/test/data/hosts.csv +++ b/test/data/hosts.csv @@ -1,5 +1,5 @@ -Name,Count,Organization,Environment,Operating System,Architecture,MAC Address,Domain,Partition Table -dhcp130-%03d.megacorp.com,255,Mega Corporation,Development,RHEL 6.4,x86_64,"02:FE:B5:E0:70:%02x",megacorp.com,RedHat default +Name,Count,Organization,Location,Environment,Operating System,Architecture,MAC Address,Domain,Partition Table +dhcp130-%03d.megacorp.com,255,Mega Corporation,Asia Pacific,Development,RHEL 6.4,x86_64,"02:FE:B5:E0:70:%02x",megacorp.com,RedHat default dhcp129-%03d.megacorp.com,255,Mega Corporation,Development,RHEL 6.4,x86_64,"01:FE:B5:E0:70:%02x",megacorp.com,RedHat default # dhcp129-1%02d.megacorp.com,255,Mega Corporation,Development,RHEL 6.4,x86_64,"01:FE:B5:E1:70:%02x",megacorp.com,RedHat default # dhcp129-2%02d.megacorp.com,255,Mega Corporation,Development,RHEL 6.4,x86_64,"01:FE:B5:E2:70:%02x",megacorp.com,RedHat default diff --git a/test/data/operating-systems.csv b/test/data/operating-systems.csv index dca6aa0..1e666c1 100644 --- a/test/data/operating-systems.csv +++ b/test/data/operating-systems.csv @@ -1,16 +1,16 @@ -"Name","Count","Family" -"CentOS 6.4","1","Redhat" -"Debian 6.0","1","Debian" -"Debian 7.0","1","Debian" -"Fedora 14","1","Redhat" -"Fedora 17","1","Redhat" -"Fedora 18","1","Redhat" -"Fedora 19","1","Redhat" -"OpenSuSE 11.4","1","Suse" -"RHEL 5.8","1","Redhat" -"RHEL 6.3","1","Redhat" -"RHEL 5.7","1","Redhat" -"RHEL 6.4","1","Redhat" -"Solaris 5.10","1","Solaris" -"Ubuntu 12.10","1","Debian" -"Windows 6.1","1","Windows" +Name,Count,Description,Family +CentOS 6.4,1,,Redhat +Debian 6.0,1,,Debian +Debian 7.0,1,,Debian +Fedora 14,1,,Redhat +Fedora 17,1,,Redhat +Fedora 18,1,,Redhat +Fedora 19,1,,Redhat +OpenSuSE 11.4,1,,Suse +RHEL 5.8,1,,Redhat +RHEL 6.3,1,,Redhat +RHEL 5.7,1,,Redhat +RHEL 6.4,1,,Redhat +Solaris 5.10,1,,Solaris +Ubuntu 12.10,1,,Debian +Windows 6.1,1,,Windows diff --git a/test/data/products.csv b/test/data/products.csv index edae697..fe20e23 100644 --- a/test/data/products.csv +++ b/test/data/products.csv @@ -14,6 +14,6 @@ Red Hat Enterprise Linux Server,1,Red_Hat_Enterprise_Linux_Server,Mega Corporati Red Hat Enterprise Linux Server,1,Red_Hat_Enterprise_Linux_Server,Mega Corporation,Red Hat Enterprise Linux 6 Server RPMs x86_64 6.5,Red Hat Yum,https://cdn.redhat.com/content/dist/rhel/server/6/6.5/x86_64/os Red Hat Enterprise Linux Server,1,Red_Hat_Enterprise_Linux_Server,Mega Corporation,Red Hat Enterprise Linux 6 Server RPMs x86_64 6Server,Red Hat Yum,https://cdn.redhat.com/content/dist/rhel/server/6/6Server/x86_64/os Red Hat Enterprise Linux Server,1,Red_Hat_Enterprise_Linux_Server,Mega Corporation,Red Hat Enterprise Linux 7 Server Kickstart x86_64 7.0,Red Hat Yum,https://cdn.redhat.com/content/dist/rhel/server/7/7.0/x86_64/kickstart -Red Hat Enterprise Linux Server,1,Red_Hat_Enterprise_Linux_Server,Mega Corporation,Red Hat Enterprise Linux 7 Server Kickstart x86_64 7Server,Red Hat Yum,https://cdn.redhat.com/content/dist/rhel/server/7/7Server/x86_64/kickstart +Red Hat Enterprise Linux Server,1,Red_Hat_Enterprise_Linux_Server,Mega Corporation,Red Hat Enterprise Linux 7 Server Kickstart) x86_64 7Server,Red Hat Yum,https://cdn.redhat.com/content/dist/rhel/server/7/7Server/x86_64/kickstart Red Hat Enterprise Linux Server,1,Red_Hat_Enterprise_Linux_Server,Mega Corporation,Red Hat Enterprise Linux 7 Server RPMs x86_64 7.0,Red Hat Yum,https://cdn.redhat.com/content/dist/rhel/server/7/7.0/x86_64/os Red Hat Enterprise Linux Server,1,Red_Hat_Enterprise_Linux_Server,Mega Corporation,Red Hat Enterprise Linux 7 Server RPMs x86_64 7Server,Red Hat Yum,https://cdn.redhat.com/content/dist/rhel/server/7/7Server/x86_64/os \ No newline at end of file diff --git a/test/data/settings.csv b/test/data/settings.csv new file mode 100644 index 0000000..88f97bb --- /dev/null +++ b/test/data/settings.csv @@ -0,0 +1,3 @@ +Name,Count,Value +administrator,1,root@example.com +idle_timeout,1,60000