Project

General

Profile

« Previous | Next » 

Revision b2e88b7c

Added by Thomas McKay about 8 years ago

fixes #14162 - hide unsupported sub-commands and options

+ updated for ruby 2.0.0
+ updated README
+ fixed rubocop
+ updated tests
+ based on feedback switched to default being unsupported for new commands

View differences:

Gemfile
gemspec
group :test do
gem 'rake'
gem 'rake', '~> 10.1.0'
gem 'thor'
gem 'minitest', '4.7.4'
gem 'minitest-spec-context'
gem 'simplecov'
gem 'mocha'
gem 'ci_reporter'
gem 'ci_reporter', '>= 1.6.3', "< 2.0.0", :require => false
gem 'rubocop-checkstyle_formatter'
end
README.md
[Hammer](https://github.com/theforeman/hammer-cli/blob/master/README.md) is a command line interface (CLI) framework which provides a core to which modules may be added. This module, hammer-cli-csv, adds commands to interact with the following products: [Foreman](https://theforeman.org) standalone, [Katello](http://www.katello.org/), Red Hat's Satellite-6, and Red Hat's Subscription Asset Manager (SAM).
The purpose of this module's commands are to allow a convenient mechanism to both export to and import from CSV files (comma separated values). Each of the server's supported resource types, such as organizations and users, are handled.
The purpose of this module's commands are to allow a convenient mechanism to both export to and import from CSV files (comma separated values). Each of the server's supported resource types, such as organizations and users, is handled.
Some possible uses include
* Import demo or development data easily and consistently
* Export another server's data and then import into elsewhere for testing and debug
* Export for backup and auditing
* Export from SAM-1.4 to import into Satellite-6
The following sections will cover installation, usage, and examples. All of the resource types are follow in the order which generally is required for dependency resolution (eg. roles must exist to assign to users so the role section comes first).
......
```
## Usage
## General Usage
Supported commands and options are available by using the --help option. Additional subcommands and options may be listed here but are considered "tech preview" to indicate the lack of testing and official support.
| Option | Description |
| ---------------:| :--------------|
| --csv-export | If not specified will run import. |
| --csv-file FILE_NAME | File to import from or export to. If not specified reads or writes to/from stdin and stdout. Note: On ruby-1.8.7 this option is required. |
| --export | If not specified will run import. |
| --file FILE_NAME | File to import from or export to. If not specified reads or writes to/from stdin and stdout. Note: On ruby-1.8.7 this option is required. |
| --prefix PREFIX | Convenience method to add a prefix to all Count substituted values. See examples below. |
| --server SERVER | The server to run against. Overrides any config file value. |
| --username USERNAME | Username for server. Overrides any config file value. |
......
*Note: % column indicates Count substituion*
| Column Title | Column Description | % | Foreman | Katello | Satellite | SAM |
| :----------- | :----------------- | :-: | :-: | :-: | :-: | :-: |
| Name | Name of the organization to update or create | x | x | x | x | x |
| Count | Number of times to iterate this CSV row, incrementing value for substitution | | x | x | x | x |
| Label | Unique organization label | x | | x | x | x |
| Description | Organization description | | x | x | x | x |
| Column Title | Column Description | % |
| :----------- | :----------------- | :-: |
| Name | Name of the organization to update or create | x |
| Count | Number of times to iterate this CSV row, incrementing value for substitution | |
| Label | Unique organization label | x | |
| Description | Organization description | |
**Examples**
......
*Note: % column indicates Count substituion*
| Column Title | Column Description | % | Foreman | Katello | Satellite | SAM |
| :----------- | :----------------- | :-: | :-: | :-: | :-: | :-: |
| Name | Name of the location to update or create | x | x | x | x | |
| Count | Number of times to iterate this CSV row, incrementing value for substitution | | x | x | x | |
| Parent Location | Parent location | | x | x | x | |
| Column Title | Column Description | % |
| :----------- | :----------------- | :-: |
| Name | Name of the location to update or create | x |
| Count | Number of times to iterate this CSV row, incrementing value for substitution | |
| Parent Location | Parent location | |
## Puppet Environments
......
*Note: % column indicates Count substituion*
| Column Title | Column Description | % | Foreman | Katello | Satellite | SAM |
| :----------- | :----------------- | :-: | :-: | :-: | :-: | :-: |
| Name | Name of the puppet environments to update or create | x | x | x | x | |
| Count | Number of times to iterate this CSV row, incrementing value for substitution | | x | x | x | |
| Organizations | Comma separated list of organizations | | x | x | x | |
| Column Title | Column Description | % |
| :----------- | :----------------- | :-: |
| Name | Name of the puppet environments to update or create | x |
| Count | Number of times to iterate this CSV row, incrementing value for substitution | |
| Organizations | Comma separated list of organizations | |
## Operating Systems
......
*Note: % column indicates Count substituion*
| Column Title | Column Description | % | Foreman | Katello | Satellite | SAM |
| :----------- | :----------------- | :-: | :-: | :-: | :-: | :-: |
| Name | Name of the operating systems to update or create | x | x | x | x | |
| Count | Number of times to iterate this CSV row, incrementing value for substitution | | x | x | x | |
| Family | Operating system family | | x | x | x | |
| Description | Operating system description | | x | x | x | |
| Password Hash | MD5, SHA256, SHA512, or Base64 | | x | x | x | |
| Partition Tables | List of partition table names | | x | x | x | |
| Architectures | List of architectures names | | x | x | x | |
| Media | List of media names | | x | x | x | |
| Provisioning Templates | List of provisioning template names | | x | x | x | |
| Parameters | List of parameters | | x | x | x | |
| Column Title | Column Description | % |
| :----------- | :----------------- | :-: |
| Name | Name of the operating systems to update or create | x |
| Count | Number of times to iterate this CSV row, incrementing value for substitution | |
| Family | Operating system family | |
| Description | Operating system description | |
| Password Hash | MD5, SHA256, SHA512, or Base64 | |
| Partition Tables | List of partition table names | |
| Architectures | List of architectures names | |
| Media | List of media names | |
| Provisioning Templates | List of provisioning template names | |
| Parameters | List of parameters | |
## Domains
......
*Note: % column indicates Count substituion*
| Column Title | Column Description | % | Foreman | Katello | Satellite | SAM |
| :----------- | :----------------- | :-: | :-: | :-: | :-: | :-: |
| Name | Name of the domains to update or create | x | x | x | x | |
| Count | Number of times to iterate this CSV row, incrementing value for substitution | | x | x | x | |
| Full Name | Full name of the domain | | x | x | x | |
| Organizations | Comma separated list of organizations | | x | x | x | |
| Column Title | Column Description | % |
| :----------- | :----------------- | :-: |
| Name | Name of the domains to update or create | x |
| Count | Number of times to iterate this CSV row, incrementing value for substitution | |
| Full Name | Full name of the domain | |
| Organizations | Comma separated list of organizations | |
## Architectures
......
*Note: % column indicates Count substituion*
| Column Title | Column Description | % | Foreman | Katello | Satellite | SAM |
| :----------- | :----------------- | :-: | :-: | :-: | :-: | :-: |
| Name | Name of the architectures to update or create | x | x | x | x | |
| Count | Number of times to iterate this CSV row, incrementing value for substitution | | x | x | x | |
| Operating Systems | Comma separated list of operating system names | | x | x | x | |
| Column Title | Column Description | % |
| :----------- | :----------------- | :-: |
| Name | Name of the architectures to update or create | x |
| Count | Number of times to iterate this CSV row, incrementing value for substitution | |
| Operating Systems | Comma separated list of operating system names | |
## Partition Tables
......
*Note: % column indicates Count substituion*
| Column Title | Column Description | % | Foreman | Katello | Satellite | SAM |
| :----------- | :----------------- | :-: | :-: | :-: | :-: | :-: |
| Name | Name of the partition tables to update or create | x | x | x | x | |
| Count | Number of times to iterate this CSV row, incrementing value for substitution | | x | x | x | |
| OS Family | Operating system family | | x | x | x | |
| Operating Systems | Comma separated list of operating system names | | x | x | x | |
| Layout | Disk layout | | x | x | x | |
| Column Title | Column Description | % |
| :----------- | :----------------- | :-: |
| Name | Name of the partition tables to update or create | x |
| Count | Number of times to iterate this CSV row, incrementing value for substitution | |
| OS Family | Operating system family | |
| Operating Systems | Comma separated list of operating system names | |
| Layout | Disk layout | |
## Lifecycle Environments
......
*Note: % column indicates Count substituion*
| Column Title | Column Description | % | Foreman | Katello | Satellite | SAM |
| :----------- | :----------------- | :-: | :-: | :-: | :-: | :-: |
| Name | Name of the lifecycle environments to update or create | x | x | x | x | |
| Count | Number of times to iterate this CSV row, incrementing value for substitution | | x | x | x | |
| Organization | Organization name | | x | x | x | |
| Prior Environment | Previous organization name in path | | x | x | x | |
| Description | Lifecycle environment description | | x | x | x | |
| Column Title | Column Description | % |
| :----------- | :----------------- | :-: |
| Name | Name of the lifecycle environments to update or create | x |
| Count | Number of times to iterate this CSV row, incrementing value for substitution | |
| Organization | Organization name | |
| Prior Environment | Previous organization name in path | |
| Description | Lifecycle environment description | |
## Host Collections
......
*Note: % column indicates Count substituion*
| Column Title | Column Description | % | Foreman | Katello | Satellite | SAM |
| :----------- | :----------------- | :-: | :-: | :-: | :-: | :-: |
| Name | Name of the host collections to update or create | x | x | x | x | |
| Count | Number of times to iterate this CSV row, incrementing value for substitution | | x | x | x | |
| Organization | Organization name | | x | x | x | |
| Limit | Usage limit | | x | x | x | |
| Description | Host collection description | | x | x | x | |
| Column Title | Column Description | % |
| :----------- | :----------------- | :-: |
| Name | Name of the host collections to update or create | x |
| Count | Number of times to iterate this CSV row, incrementing value for substitution | |
| Organization | Organization name | |
| Limit | Usage limit | |
| Description | Host collection description | |
## Products
......
*Note: % column indicates Count substituion*
| Column Title | Column Description | % | Foreman | Katello | Satellite | SAM |
| :----------- | :----------------- | :-: | :-: | :-: | :-: | :-: |
| Name | Name of the host collections to update or create | x | x | x | x | |
| Count | Number of times to iterate this CSV row, incrementing value for substitution | | x | x | x | |
| Label | Unique label | | x | x | x | |
| Organization | Organization name | | x | x | x | |
| Repository | Repository name | | x | x | x | |
| Repository Url | Repository Url | | x | x | x | |
| Description | Repository description | | x | x | x | |
| Column Title | Column Description | % |
| :----------- | :----------------- | :-: |
| Name | Name of the host collections to update or create | x |
| Count | Number of times to iterate this CSV row, incrementing value for substitution | |
| Label | Unique label | |
| Organization | Organization name | |
| Repository | Repository name | |
| Repository Url | Repository Url | |
| Description | Repository description | |
## Provisioning Templates
......
*Note: % column indicates Count substituion*
| Column Title | Column Description | % | Foreman | Katello | Satellite | SAM |
| :----------- | :----------------- | :-: | :-: | :-: | :-: | :-: |
| Name | Name of the provisioning templates to update or create | x | x | x | x | |
| Count | Number of times to iterate this CSV row, incrementing value for substitution | | x | x | x | |
| Organizations | Comma separated list of organizations | | x | x | x | |
| Locations | Comma separated list of locations | | x | x | x | |
| Operating Systems | Comma separated list of associated operating systems | | x | x | x | |
| Host Group / Puppet Environment Combinations | Comma separated list of host group and puppet environment associations | | x | x | x | |
| Kind | Kind of template (eg. snippet) | | x | x | x | |
| Template | Full text of template | | x | x | x | |
| Column Title | Column Description | % |
| :----------- | :----------------- | :-: |
| Name | Name of the provisioning templates to update or create | x |
| Count | Number of times to iterate this CSV row, incrementing value for substitution | |
| Organizations | Comma separated list of organizations | |
| Locations | Comma separated list of locations | |
| Operating Systems | Comma separated list of associated operating systems | |
| Host Group / Puppet Environment Combinations | Comma separated list of host group and puppet environment associations | |
| Kind | Kind of template (eg. snippet) | |
| Template | Full text of template | |
## Subscriptions
......
*Note: % column indicates Count substituion*
| Column Title | Column Description | % | Foreman | Katello | Satellite | SAM |
| :----------- | :----------------- | :-: | :-: | :-: | :-: | :-: |
| Name | Name of the subscriptions to update or create | x | x | x | x | |
| Count | Number of times to iterate this CSV row, incrementing value for substitution | | x | x | x | |
| Organization | Organization name | | x | x | x | |
| Manifest File | Path to manifest file | | x | x | x | |
| Content Set | Repository content set to enable | | x | x | x | |
| Arch | Architecture | | x | x | x | |
| Release | Release version | | x | x | x | |
| Column Title | Column Description | % |
| :----------- | :----------------- | :-: |
| Name | Name of the subscriptions to update or create | x |
| Count | Number of times to iterate this CSV row, incrementing value for substitution | |
| Organization | Organization name | |
| Manifest File | Path to manifest file | |
| Content Set | Repository content set to enable | |
| Arch | Architecture | |
| Release | Release version | |
## Activation Keys
......
*Note: % column indicates Count substituion*
| Column Title | Column Description | % | Foreman | Katello | Satellite | SAM |
| :----------- | :----------------- | :-: | :-: | :-: | :-: | :-: |
| Name | Name of the activation keys to update or create | x | x | x | x | |
| Count | Number of times to iterate this CSV row, incrementing value for substitution | | x | x | x | |
| Organization | Parent organization name | | x | x | x | |
| Description | Activation key description | | x | x | x | |
| Limit | Usage limit | | x | x | x | |
| Environment | Lifecycle environment name | | x | x | x | |
| Content View | Content view name | | x | x | x | |
| Host Collections | Comma separated list of host collections | | x | x | x | |
| Subscriptions | Comma separated list of subscriptions | | x | x | x | |
| Column Title | Column Description | % |
| :----------- | :----------------- | :-: |
| Name | Name of the activation keys to update or create | x |
| Count | Number of times to iterate this CSV row, incrementing value for substitution | |
| Organization | Parent organization name | |
| Description | Activation key description | |
| Limit | Usage limit | |
| Environment | Lifecycle environment name | |
| Content View | Content view name | |
| Host Collections | Comma separated list of host collections | |
| Subscriptions | Comma separated list of subscriptions | |
## Hosts
......
*Note: % column indicates Count substituion*
| Column Title | Column Description | % | Foreman | Katello | Satellite | SAM |
| :----------- | :----------------- | :-: | :-: | :-: | :-: | :-: |
| Name | Name of the hosts to update or create | x | x | x | x | |
| Count | Number of times to iterate this CSV row, incrementing value for substitution | | x | x | x | |
| Organization | Organization name | | x | x | x | |
| Environment | Puppet environment name | | x | x | x | |
| Operating System | Operating system name | | x | x | x | |
| Architecture | Architecture name | | x | x | x | |
| MAC Address | Unique MAC address | x | x | x | x | |
| Domain | Domain name | | x | x | x | |
| Partition Table | Partition table name | | x | x | x | |
| Column Title | Column Description | % |
| :----------- | :----------------- | :-: |
| Name | Name of the hosts to update or create | x |
| Count | Number of times to iterate this CSV row, incrementing value for substitution | |
| Organization | Organization name | |
| Environment | Puppet environment name | |
| Operating System | Operating system name | |
| Architecture | Architecture name | |
| MAC Address | Unique MAC address | x |
| Domain | Domain name | |
| Partition Table | Partition table name | |
## Content Hosts
......
*Note: % column indicates Count substituion*
| Column Title | Column Description | % | Foreman | Katello | Satellite | SAM |
| :----------- | :----------------- | :-: | :-: | :-: | :-: | :-: |
| Name | Name of the content hosts to update or create | x | x | x | x | |
| Count | Number of times to iterate this CSV row, incrementing value for substitution | | x | x | x | |
| Organization | Organization name | | x | x | x | |
| Environment | Puppet environment name | | x | x | x | |
| Content View | Content view name | | x | x | x | |
| Host Collections | Comma separate list of host collectoins | | x | x | x | |
| Virtual | Virtual (Yes or No) | | x | x | x | |
| Host | Virtual content host name | | x | x | x | |
| OS | Operating system name | | x | x | x | |
| Arch | Architecture name | | x | x | x | |
| Sockets | Number of sockets | | x | x | x | |
| RAM | Amount of RAM with units | | x | x | x | |
| Cores | Number of cores | | x | x | x | |
| SLA | Service Level Agreement | | x | x | x | |
| Products | Comma separated list of subscriptions | | x | x | x | |
| Subscriptions | Comma separated list of subscriptions | | x | x | x | |
| Column Title | Column Description | % |
| :----------- | :----------------- | :-: |
| Name | Name of the content hosts to update or create | x |
| Count | Number of times to iterate this CSV row, incrementing value for substitution | |
| Organization | Organization name | |
| Environment | Puppet environment name | |
| Content View | Content view name | |
| Host Collections | Comma separate list of host collectoins | |
| Virtual | Virtual (Yes or No) | |
| Host | Virtual content host name | |
| OS | Operating system name | |
| Arch | Architecture name | |
| Sockets | Number of sockets | |
| RAM | Amount of RAM with units | |
| Cores | Number of cores | |
| SLA | Service Level Agreement | |
| Products | Comma separated list of subscriptions | |
| Subscriptions | Comma separated list of subscriptions | |
## Reports
......
*Note: % column indicates Count substituion*
| Column Title | Column Description | % | Foreman | Katello | Satellite | SAM |
| :----------- | :----------------- | :-: | :-: | :-: | :-: | :-: |
| Name | Name of the reports to update or create | x | x | x | x | |
| Count | Number of times to iterate this CSV row, incrementing value for substitution | | x | x | x | |
| Time | Time of report | | x | x | x | |
| Applied | | | x | x | x | |
| Restarted | | | x | x | x | |
| Failed | | | x | x | x | |
| Failed Restarts | | | x | x | x | |
| Skipped | | | x | x | x | |
| Pending | | | x | x | x | |
| Metrics | | | x | x | x | |
| Column Title | Column Description | % |
| :----------- | :----------------- | :-: |
| Name | Name of the reports to update or create | x |
| Count | Number of times to iterate this CSV row, incrementing value for substitution | |
| Time | Time of report | |
| Applied | | |
| Restarted | | |
| Failed | | |
| Failed Restarts | | |
| Skipped | | |
| Pending | | |
| Metrics | | |
## Roles
......
*Note: % column indicates Count substituion*
| Column Title | Column Description | % | Foreman | Katello | Satellite | SAM |
| :----------- | :----------------- | :-: | :-: | :-: | :-: | :-: |
| Name | Name of the roles to update or create | x | x | x | x | |
| Count | Number of times to iterate this CSV row, incrementing value for substitution | | x | x | x | |
| Resource | Resource to apply role to | | x | x | x | |
| Search | Search string | | x | x | x | |
| Permissions | Role permission | | x | x | x | |
| Organizations | Comma separated list of organizations | | x | x | x | |
| Locations | Comma separated list of locations | | x | x | x | |
| Column Title | Column Description | % |
| :----------- | :----------------- | :-: |
| Name | Name of the roles to update or create | x |
| Count | Number of times to iterate this CSV row, incrementing value for substitution | |
| Resource | Resource to apply role to | |
| Search | Search string | |
| Permissions | Role permission | |
| Organizations | Comma separated list of organizations | |
| Locations | Comma separated list of locations | |
## Users
......
*Note: % column indicates Count substituion*
| Column Title | Column Description | % | Foreman | Katello | Satellite | SAM |
| :----------- | :----------------- | :-: | :-: | :-: | :-: | :-: |
| Name | Name of the users to update or create | x | x | x | x | |
| Count | Number of times to iterate this CSV row, incrementing value for substitution | | x | x | x | |
| First Name | First name of user | | x | x | x | |
| Last Name | Last name of user | | x | x | x | |
| email | Email address | | x | x | x | |
| Organizations | Comma separated list of organizations | | x | x | x | |
| Locations | Comma separated list of locations | | x | x | x | |
| Roles | Comma separated list of role names for user | | x | x | x | |
| Column Title | Column Description | % |
| :----------- | :----------------- | :-: |
| Name | Name of the users to update or create | x |
| Count | Number of times to iterate this CSV row, incrementing value for substitution | |
| First Name | First name of user | |
| Last Name | Last name of user | |
| email | Email address | |
| Organizations | Comma separated list of organizations | |
| Locations | Comma separated list of locations | |
| Roles | Comma separated list of role names for user | |
## Settings
......
*Note: % column indicates Count substituion*
| Column Title | Column Description | % | Foreman | Katello | Satellite | SAM |
| :----------- | :----------------- | :-: | :-: | :-: | :-: | :-: |
| Name | Setting name to update or create | x | x | x | x | |
| Count | Number of times to iterate this CSV row, incrementing value for substitution | | x | x | x | |
| Value | Setting value | | x | x | x | |
| Column Title | Column Description | % |
| :----------- | :----------------- | :-: |
| Name | Setting name to update or create | x |
| Count | Number of times to iterate this CSV row, incrementing value for substitution | |
| Value | Setting value | |
**Examples**
```
......
**Examples**
```
% hammer csv import -v --organizations test/data/organizations.csv --locations test/data/locations.csv
Creating organization 'Mega Corporation'... done
Creating organization 'Mega Subsidiary'... done
Creating location 'Asia Pacific'... done
Creating location 'Asia Pacific (Tokyo) Region'... done
Creating location 'Asia Pacific (Singapore) Region'... done
Creating location 'Asia Pacific (Sydney) Region'... done
Creating location 'EU (Ireland) Region'... done
Creating location 'South America (Sao Paulo) Region'... done
Creating location 'US East (Northern Virginia) Region'... done
Creating location 'US West (Northern California) Region'... done
Creating location 'US West (Oregon) Region'... done
% hammer csv import -v --dir test/data --settings
Importing settings from 'test/data/settings.csv'
Updating setting 'idle_timeout'...done
```
# Development
......
The tests are meant to run against a live server.
```rake test TEST=test/settings_test.rb```
Test server configuration is taken from test/config.yml
```
% cat test/config.yml
:csv:
:enable_module: true
:foreman:
:enable_module: true
:host: 'http://katello:3000'
:username: 'admin'
:password: 'changeme'
:katello:
:enable_module: true
```
lib/hammer_cli_csv.rb
require 'hammer_cli_foreman_tasks'
require 'hammer_cli_csv/base'
require 'hammer_cli_csv/exception_handler'
require 'hammer_cli_csv/csv'
require 'hammer_cli_csv/activation_keys'
lib/hammer_cli_csv/base.rb
:default => 1, :hidden => true
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(--prefix), 'PREFIX', 'Prefix for all name columns',
:hidden => true
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 --export", :hidden => true,
:attribute_name => :option_export
NAME = 'Name'
COUNT = 'Count'
def self.supported?
false
end
def supported?
self.class.supported?
end
def help
print_message _('**** This command is unsupported and is provided as tech preview. ****') unless supported?
super
end
def execute
@server = (HammerCLI::Settings.settings[:_params] &&
HammerCLI::Settings.settings[:_params][:host]) ||
lib/hammer_cli_csv/csv.rb
module HammerCLICsv
class CsvCommand < HammerCLI::AbstractCommand
def help
self.class.help(invocation_path, CsvSortedBuilder.new)
end
class CsvSortedBuilder < SortedBuilder
def add_list(heading, items)
items.delete_if do |item|
if item.class == Clamp::Subcommand::Definition
!item.subcommand_class.supported?
else
false
end
end
super(heading, items)
end
end
end
HammerCLI::MainCommand.subcommand('csv',
_('import to, or export from a running foretello server'),
_('import to or export from a running foreman server'),
HammerCLICsv::CsvCommand)
end
lib/hammer_cli_csv/exception_handler.rb
require 'hammer_cli/exception_handler'
module HammerCLICsv
class ExceptionHandler < HammerCLI::ExceptionHandler
def mappings
super + [
[Exception, :handle_csv_exception],
[RestClient::Forbidden, :handle_forbidden],
[RestClient::UnprocessableEntity, :handle_unprocessable_entity],
[ArgumentError, :handle_argument_error]
]
end
protected
def handle_csv_exception(e)
$stderr.puts e.message
log_full_error e
HammerCLI::EX_DATAERR
end
def handle_unprocessable_entity(e)
response = JSON.parse(e.response)
response = response[response.keys[0]]
print_error response['full_messages']
HammerCLI::EX_DATAERR
end
def handle_argument_error(e)
print_error e.message
log_full_error e
HammerCLI::EX_USAGE
end
def handle_forbidden(e)
print_error 'Forbidden - server refused to process the request'
log_full_error e
HammerCLI::EX_NOPERM
end
end
end
lib/hammer_cli_csv/export.rb
command_name 'export'
desc 'export into directory'
def self.supported?
true
end
option %w(-v --verbose), :flag, _('be verbose')
option %w(--threads), 'THREAD_COUNT', _('Number of threads to hammer with'),
:default => 1, :hidden => true
option '--dir', 'DIRECTORY', _('directory to import from')
option '--dir', 'DIRECTORY', _('directory to export to')
option %w(--organization), 'ORGANIZATION', _('Only process organization matching this name')
RESOURCES = %w(
organizations locations puppet_environments operating_systems
settings organizations locations puppet_environments operating_systems
domains architectures partition_tables lifecycle_environments host_collections
provisioning_templates
subscriptions activation_keys hosts content_hosts reports roles users
)
SUPPORTED_RESOURCES = %w(
settings
)
RESOURCES.each do |resource|
dashed = resource.sub('_', '-')
option "--#{dashed}", 'FILE', "csv file for #{dashed}"
option "--#{dashed}", 'FILE', "csv file for #{dashed}",
:hidden => !SUPPORTED_RESOURCES.include?(resource)
end
def execute
......
:username => @username,
:password => @password
})
skipped_resources = %w( locations puppet_environments operating_systems
skipped_resources = %w( settings locations puppet_environments operating_systems
domains architectures partition_tables lifecycle_environments
provisioning_templates
hosts reports )
......
:password => @password,
:api_version => 2
})
skipped_resources = []
skipped_resources = (RESOURCES - SUPPORTED_RESOURCES)
end
# Swing the hammers
lib/hammer_cli_csv/import.rb
command_name 'import'
desc 'import by directory'
def self.supported?
true
end
option %w(-v --verbose), :flag, _('be verbose')
option %w(--threads), 'THREAD_COUNT', _('Number of threads to hammer with'),
:default => 1, :hidden => true
option '--dir', 'DIRECTORY', _('directory to import from')
option %w(--organization), 'ORGANIZATION', _('Only process organization matching this name')
option %w(--prefix), 'PREFIX', _('Prefix for all name columns')
option %w(--prefix), 'PREFIX', _('Prefix for all name columns'),
:hidden => true
RESOURCES = %w( settings organizations locations puppet_environments operating_systems
domains architectures partition_tables lifecycle_environments host_collections
provisioning_templates
subscriptions products content_views content_view_filters activation_keys
hosts content_hosts smart_proxies compute_resources reports roles users )
RESOURCES = %w(
settings organizations locations puppet_environments operating_systems
domains architectures partition_tables lifecycle_environments host_collections
provisioning_templates
subscriptions products content_views content_view_filters activation_keys
hosts content_hosts smart_proxies compute_resources reports roles users
)
SUPPORTED_RESOURCES = %w(
settings
)
RESOURCES.each do |resource|
dashed = resource.gsub('_', '-')
option "--#{dashed}", 'FILE', "csv file for #{dashed}"
option "--#{dashed}", 'FILE', "csv file for #{dashed}",
:hidden => !SUPPORTED_RESOURCES.include?(resource)
end
def execute
......
def hammer_resource(resource)
return if !self.send("option_#{resource}") && !option_dir
options_file = "#{option_dir}/#{resource.gsub('_', '-')}.csv" || self.send("option_#{resource}")
options_file = option_dir ? "#{option_dir}/#{resource.gsub('_', '-')}.csv" : self.send("option_#{resource}")
unless options_file_exists? options_file
if option_dir
return unless SUPPORTED_RESOURCES.include?(resource)
puts _("Skipping #{resource} because '#{options_file}' does not exist") if option_verbose?
return
end
lib/hammer_cli_csv/settings.rb
VALUE = 'Value'
def self.supported?
true
end
def export
CSV.open(option_file || '/dev/stdout', 'wb') do |csv|
csv << [NAME, VALUE]
test/csv_test.rb
require File.join(File.dirname(__FILE__), 'csv_test_helper')
describe 'csv' do
extend CommandTestHelper
context "help" do
it "displays supported options" do
set_user 'admin'
stdout,stderr = capture {
hammer.run(%W{csv --help})
}
stderr.must_equal ''
stdout.must_equal <<-HELP
Usage:
csv [OPTIONS] SUBCOMMAND [ARG] ...
Parameters:
SUBCOMMAND subcommand
[ARG] ... subcommand arguments
Subcommands:
export export into directory
import import by directory
settings import or export settings
Options:
-h, --help print help
HELP
end
end
end
test/export_test.rb
require File.join(File.dirname(__FILE__), 'csv_test_helper')
describe 'export' do
extend CommandTestHelper
context "help" do
it "displays supported options" do
set_user 'admin'
stdout,stderr = capture {
hammer.run(%W{csv export --help})
}
stderr.must_equal ''
stdout.must_equal <<-HELP
Usage:
csv export [OPTIONS]
Options:
--dir DIRECTORY directory to export to
--organization ORGANIZATION Only process organization matching this name
--settings FILE csv file for settings
-h, --help print help
-v, --verbose be verbose
HELP
end
end
end
test/import_test.rb
require File.join(File.dirname(__FILE__), 'csv_test_helper')
require 'stringio'
require 'tempfile'
describe 'import' do
extend CommandTestHelper
before :each do
HammerCLI::Settings.load_from_file 'test/config.yml'
context "help" do
it "displays supported options" do
set_user 'admin'
stdout,stderr = capture {
hammer.run(%W{csv import --help})
}
stderr.must_equal ''
stdout.must_equal <<-HELP
Usage:
csv import [OPTIONS]
Options:
--dir DIRECTORY directory to import from
--organization ORGANIZATION Only process organization matching this name
--settings FILE csv file for settings
-h, --help print help
-v, --verbose be verbose
HELP
end
end
context "--dir" do
......
stdout.must_equal(
<<-eos
Skipping settings because 'does-not-exist/settings.csv' does not exist
Skipping organizations because 'does-not-exist/organizations.csv' does not exist
Skipping locations because 'does-not-exist/locations.csv' does not exist
Skipping puppet_environments because 'does-not-exist/puppet-environments.csv' does not exist
Skipping operating_systems because 'does-not-exist/operating-systems.csv' does not exist
Skipping domains because 'does-not-exist/domains.csv' does not exist
Skipping architectures because 'does-not-exist/architectures.csv' does not exist
Skipping partition_tables because 'does-not-exist/partition-tables.csv' does not exist
Skipping lifecycle_environments because 'does-not-exist/lifecycle-environments.csv' does not exist
Skipping host_collections because 'does-not-exist/host-collections.csv' does not exist
Skipping provisioning_templates because 'does-not-exist/provisioning-templates.csv' does not exist
Skipping subscriptions because 'does-not-exist/subscriptions.csv' does not exist
Skipping products because 'does-not-exist/products.csv' does not exist
Skipping content_views because 'does-not-exist/content-views.csv' does not exist
Skipping content_view_filters because 'does-not-exist/content-view_filters.csv' does not exist
Skipping activation_keys because 'does-not-exist/activation-keys.csv' does not exist
Skipping hosts because 'does-not-exist/hosts.csv' does not exist
Skipping content_hosts because 'does-not-exist/content-hosts.csv' does not exist
Skipping reports because 'does-not-exist/reports.csv' does not exist
Skipping roles because 'does-not-exist/roles.csv' does not exist
Skipping users because 'does-not-exist/users.csv' does not exist
eos
)
end
test/settings_test.rb
require File.join(File.dirname(__FILE__), 'csv_test_helper')
describe 'job-templates' do
describe 'settings' do
extend CommandTestHelper
context "help" do
it "displays supported options" do
set_user 'admin'
stdout,stderr = capture {
hammer.run(%W{csv settings --help})
}
stderr.must_equal ''
stdout.must_equal <<-HELP
Usage:
csv settings [OPTIONS]
Options:
--export Export current data instead of importing
--file FILE_NAME CSV file (default to /dev/stdout with --csv-export, otherwise required)
--organization ORGANIZATION Only process organization matching this name
-h, --help print help
-v, --verbose be verbose
HELP
end
end
context "import" do
it "update settings w/ Count column" do
set_user 'admin'

Also available in: Unified diff