diff --git a/lib/hammer_cli_foreman.rb b/lib/hammer_cli_foreman.rb index b7c6ce2ad..22fa57811 100644 --- a/lib/hammer_cli_foreman.rb +++ b/lib/hammer_cli_foreman.rb @@ -45,6 +45,10 @@ def self.exception_handler_class 'HammerCLIForeman::Audit', 'hammer_cli_foreman/audit' ) + HammerCLI::MainCommand.lazy_subcommand('compute-profile', _("Manipulate compute profiles"), + 'HammerCLIForeman::ComputeProfile', 'hammer_cli_foreman/compute_profile' + ) + HammerCLI::MainCommand.lazy_subcommand('compute-resource', _("Manipulate compute resources"), 'HammerCLIForeman::ComputeResource', 'hammer_cli_foreman/compute_resource' ) diff --git a/lib/hammer_cli_foreman/compute_attribute.rb b/lib/hammer_cli_foreman/compute_attribute.rb new file mode 100644 index 000000000..065d75a59 --- /dev/null +++ b/lib/hammer_cli_foreman/compute_attribute.rb @@ -0,0 +1,319 @@ +require 'hammer_cli_foreman/image' +require 'hammer_cli_foreman/compute_resource/register_compute_resources' +require 'hammer_cli_foreman/compute_resource/help_utils' + +module HammerCLIForeman + class ComputeAttribute < HammerCLIForeman::Command + resource :compute_attributes + + def self.get_params(options) + params = {} + params['compute_attribute'] = {} + profile = HammerCLIForeman.record_to_common_format( + HammerCLIForeman.foreman_resource(:compute_profiles).call(:show, 'id' => options['option_compute_profile_id'] ) + ) + params['compute_attribute'] = profile['compute_attributes'].select { |hash| hash['compute_resource_id'] == options['option_compute_resource_id']}[0] || {} + params['compute_attribute'].delete('attributes') if params['compute_attribute']['attributes'] + params + end + + def self.attribute_hash(attribute_list) + attribute_list.size.times.map { |idx| idx.to_s }.zip(attribute_list).to_h + end + + class Create < HammerCLIForeman::CreateCommand + desc _('Create compute profile set of values') + + option '--compute-attributes', 'COMPUTE_ATTRS', _('Compute resource attributes'), + :format => HammerCLI::Options::Normalizers::KeyValueList.new + option '--interface', 'INTERFACE', _('Interface parameters, should be comma separated list of values'), + :format => HammerCLI::Options::Normalizers::KeyValueList.new, :multivalued => true + option '--volume', 'VOLUME', _('Volume parameters, should be comma separated list of values'), + :format => HammerCLI::Options::Normalizers::KeyValueList.new, :multivalued => true + + extend_help do |h| + ::HammerCLIForeman::ComputeResources.extend_help(h, :all) + end + + validate_options do + any(:option_compute_profile_id, :option_compute_profile_name ).required + any(:option_compute_resource_id, :option_compute_resource_name).required + end + + def request_params + params = super + compute_resource_name = HammerCLIForeman.record_to_common_format(HammerCLIForeman.foreman_resource(:compute_resources).call(:show, 'id' => options["option_compute_resource_id"] ))["provider_friendly_name"] + interfaces_attrs_name_list = ::HammerCLIForeman.get_interfaces_list_name + + params['compute_attribute']['vm_attrs'] = option_compute_attributes || {} + params['compute_attribute']['vm_attrs'][interfaces_attrs_name_list[compute_resource_name]]= HammerCLIForeman::ComputeAttribute.attribute_hash(option_interface_list) unless option_interface_list.empty? + params['compute_attribute']['vm_attrs']['volumes_attributes'] = HammerCLIForeman::ComputeAttribute.attribute_hash(option_volume_list) unless option_volume_list.empty? + params + end + + success_message _('Compute profile attributes are set.') + failure_message _('Could not set the compute profile attributes') + build_options + end + + class Update < HammerCLIForeman::UpdateCommand + desc _('Update compute profile values') + + option '--compute-attributes', 'COMPUTE_ATTRS', _('Compute resource attributes, should be comma separated list of values'), + :format => HammerCLI::Options::Normalizers::KeyValueList.new + option '--interface', 'INTERFACE', _('Interface parameters, should be comma separated list of values'), + :format => HammerCLI::Options::Normalizers::KeyValueList.new, :multivalued => true + option '--volume', 'VOLUME', _('Volume parameters, should be comma separated list of values'), + :format => HammerCLI::Options::Normalizers::KeyValueList.new, :multivalued => true + + extend_help do |h| + ::HammerCLIForeman::ComputeResources.extend_help(h, :all) + end + + validate_options do + any(:option_compute_profile_id, :option_compute_profile_name ).required + any(:option_compute_resource_id, :option_compute_resource_name).required + end + + + def request_params + + params = HammerCLIForeman::ComputeAttribute.get_params(options) + compute_resource_name = HammerCLIForeman.record_to_common_format(HammerCLIForeman.foreman_resource(:compute_resources).call(:show, 'id' => options["option_compute_resource_id"] ))["provider_friendly_name"] + interfaces_attrs_name_list = ::HammerCLIForeman.get_interfaces_list_name + + raise ArgumentError, "Compute profile value to update does not exist yet; it needs to be created first" if !params['compute_attribute'].key?('id') + params['id'] = params['compute_attribute']['id'] + vm_attrs = params['compute_attribute']['vm_attrs'] + original_volumes = vm_attrs['volumes_attributes'] || {} + original_interfaces = vm_attrs[interfaces_attrs_name_list[compute_resource_name]] || {} + + if options['option_compute_attributes'] + vm_attrs = options['option_compute_attributes'] + vm_attrs['volumes_attributes'] ||= original_volumes + vm_attrs[interfaces_attrs_name_list[compute_resource_name]] ||= original_interfaces + end + vm_attrs[interfaces_attrs_name_list[compute_resource_name]] = HammerCLIForeman::ComputeAttribute.attribute_hash(options['option_interface_list']) unless options['option_interface_list'].empty? + vm_attrs['volumes_attributes'] = HammerCLIForeman::ComputeAttribute.attribute_hash(options['option_volume_list']) unless options['option_volume_list'].empty? + params['compute_attribute']['vm_attrs'] = vm_attrs + params + + end + success_message _('Compute profile attributes updated.') + failure_message _('Could not update the compute profile attributes') + + build_options :without => :id + end + + # Using the Update command because adding a new interface is done by modifying existing compute_attribute + class AddInterface < HammerCLIForeman::UpdateCommand + command_name 'add-interface' + desc _('Add interface for Compute Profile') + option '--interface', 'SET_VALUES', _('Interface parameters, should be comma separated list of values'), + :format => HammerCLI::Options::Normalizers::KeyValueList.new, :required => true + + extend_help do |h| + ::HammerCLIForeman::ComputeResources.extend_help(h, :interface) + end + + def validate_options + super + validator.any(:option_compute_profile_id,:option_compute_profile_name).required + validator.any(:option_compute_resource_id,:option_compute_resource_name).required + end + + def request_params + params = HammerCLIForeman::ComputeAttribute.get_params(options) + + raise ArgumentError, "Compute profile value to update does not exist yet; it needs to be created first" if !params['compute_attribute'].key?('id') + compute_resource_name = HammerCLIForeman.record_to_common_format(HammerCLIForeman.foreman_resource(:compute_resources).call(:show, 'id' => options["option_compute_resource_id"] ))["provider_friendly_name"] + + interfaces_attrs_name_list = ::HammerCLIForeman.get_interfaces_list_name + + interface_attr = params['compute_attribute']['vm_attrs'][interfaces_attrs_name_list[compute_resource_name]] || {} + new_interface_id = (interface_attr.keys.max.to_i + 1 ).to_s if interface_attr.any? + new_interface_id ||= "0" + params['id'] = params['compute_attribute']['id'] + + params['compute_attribute']['vm_attrs'][interfaces_attrs_name_list[compute_resource_name]] ||= {} + params['compute_attribute']['vm_attrs'][interfaces_attrs_name_list[compute_resource_name]][new_interface_id] = option_interface + params + end + + success_message _('Interface was created.') + failure_message _('Could not create interface') + + build_options :without => :id + end + + class UpdateInterface < HammerCLIForeman::UpdateCommand + command_name 'update-interface' + + desc _('Update compute profile interface') + + option '--interface', 'SET_VALUES', _('Interface parameters, should be comma separated list of values'), + :required => true, + :format => HammerCLI::Options::Normalizers::KeyValueList.new + option '--interface-id', 'INTERFACE_ID', _('Interface id'), + :required => true, + :format => HammerCLI::Options::Normalizers::Number.new + + extend_help do |h| + ::HammerCLIForeman::ComputeResources.extend_help(h, :interface) + end + + def validate_options + super + validator.any(:option_compute_profile_id, :option_compute_profile_name).required + validator.any(:option_compute_resource_id, :option_compute_resource_name).required + end + + def request_params + params = HammerCLIForeman::ComputeAttribute.get_params(options) + raise ArgumentError, "Compute profile value to update does not exist yet; it needs to be created first" if !params['compute_attribute'].key?('id') + compute_resource_name = HammerCLIForeman.record_to_common_format(HammerCLIForeman.foreman_resource(:compute_resources).call(:show, 'id' => options["option_compute_resource_id"] ))["provider_friendly_name"] + interfaces_attrs_name_list = ::HammerCLIForeman.get_interfaces_list_name + params['id'] = params['compute_attribute']['id'] + params['compute_attribute']['vm_attrs'][interfaces_attrs_name_list[compute_resource_name]] ||= {} + params['compute_attribute']['vm_attrs'][interfaces_attrs_name_list[compute_resource_name]][option_interface_id] = option_interface + params + end + success_message _('Interface was updated.') + failure_message _('Could not update interface') + build_options :without => :id + end + + # Using the Update command because removing an interface is done by modifying existing compute_attribute + class RemoveInterface < HammerCLIForeman::UpdateCommand + command_name 'remove-interface' + desc _('Remove compute profile interface') + option '--interface-id', 'INTERFACE ID', _('Interface id'), + :format => HammerCLI::Options::Normalizers::Number.new , :required => true + + def validate_options + super + validator.any(:option_compute_profile_id, :option_compute_profile_name).required + validator.any(:option_compute_resource_id, :option_compute_resource_name).required + end + + def request_params + params = HammerCLIForeman::ComputeAttribute.get_params(options) + raise ArgumentError, "Compute profile value to update does not exist yet; it needs to be created first" if !params['compute_attribute'].key?('id') + compute_resource_name = HammerCLIForeman.record_to_common_format(HammerCLIForeman.foreman_resource(:compute_resources).call(:show, 'id' => options["option_compute_resource_id"] ))["provider_friendly_name"] + interfaces_attrs_name_list = ::HammerCLIForeman.get_interfaces_list_name + params['id'] = params['compute_attribute']['id'] + if params['compute_attribute']['vm_attrs'][interfaces_attrs_name_list[compute_resource_name]].has_key?(option_interface_id.to_s) + params['compute_attribute']['vm_attrs'][interfaces_attrs_name_list[compute_resource_name]].delete(option_interface_id.to_s) + else + signal_usage_error _('unknown interface id') + end + params + end + success_message _('Interface was removed.') + failure_message _('Could not remove interface') + build_options :without => :id + end + + # Using the Update command because adding a new volume is done by modifying existing compute_attribute + class AddVolume < HammerCLIForeman::UpdateCommand + command_name 'add-volume' + desc _('Add volume for Compute Profile') + + option '--volume', 'VOLUME', _('Volume parameters, should be comma separated list of values'), + :format => HammerCLI::Options::Normalizers::KeyValueList.new, :required => true + + extend_help do |h| + ::HammerCLIForeman::ComputeResources.extend_help(h, :volume) + end + + def validate_options + super + validator.any(:option_compute_profile_id, :option_compute_profile_name).required + validator.any(:option_compute_resource_id, :option_compute_resource_name).required + end + + def request_params + params = HammerCLIForeman::ComputeAttribute.get_params(options) + raise ArgumentError, "Compute profile value to update does not exist yet; it needs to be created first" if !params['compute_attribute'].key?('id') + volume_attr = params['compute_attribute']['vm_attrs']['volumes_attributes'] || {} + new_volume_id = (volume_attr.keys.max.to_i + 1 ).to_s if volume_attr.any? + new_volume_id ||= "0" + params['id'] = params['compute_attribute']['id'] + params['compute_attribute']['vm_attrs']['volumes_attributes'] ||= {} + params['compute_attribute']['vm_attrs']['volumes_attributes'][new_volume_id] = option_volume + params + end + + success_message _('Volume was created.') + failure_message _('Could not create volume') + build_options :without => :id + end + + class UpdateVolume < HammerCLIForeman::UpdateCommand + command_name 'update-volume' + desc _('Update compute profile volume') + + option '--volume', 'VOLUME', _('Volume parameters, should be comma separated list of values'), + :required => true, + :format => HammerCLI::Options::Normalizers::KeyValueList.new + option '--volume-id', 'VOLUME_ID', _('Volume id'), + :required => true, + :format => HammerCLI::Options::Normalizers::Number.new + + extend_help do |h| + ::HammerCLIForeman::ComputeResources.extend_help(h, :volume) + end + + + def validate_options + super + validator.any(:option_compute_profile_id, :option_compute_profile_name).required + validator.any(:option_compute_resource_id, :option_compute_resource_name).required + end + + def request_params + params = HammerCLIForeman::ComputeAttribute.get_params(options) + raise ArgumentError, "Compute profile value to update does not exist yet; it needs to be created first" if !params['compute_attribute'].key?('id') + params['id'] = params['compute_attribute']['id'] + params['compute_attribute']['vm_attrs']['volumes_attributes'] ||= {} + params['compute_attribute']['vm_attrs']['volumes_attributes'][option_volume_id] = option_volume + params + end + success_message _('Volume was updated.') + failure_message _('Could not update volume') + build_options :without => :id + end + + # Using the Update command because removing a volume is done by modifying existing compute_attribute + class RemoveVolume < HammerCLIForeman::UpdateCommand + command_name 'remove-volume' + resource :compute_attributes + desc _('Remove compute profile volume') + option '--volume-id', 'VOLUME_ID', _('Volume id'), + :format => HammerCLI::Options::Normalizers::Number.new, :required=> true + + def validate_options + super + validator.any(:option_compute_profile_id, :option_compute_profile_name).required + validator.any(:option_compute_resource_id, :option_compute_resource_name).required + end + + def request_params + params = HammerCLIForeman::ComputeAttribute.get_params(options) + raise ArgumentError, "Compute profile value to update does not exist yet; it needs to be created first" if !params['compute_attribute'].key?('id') + params['id'] = params['compute_attribute']['id'] + if params['compute_attribute']['vm_attrs']['volumes_attributes'].has_key?(option_volume_id.to_s) + params['compute_attribute']['vm_attrs']['volumes_attributes'].delete(option_volume_id.to_s) + else + signal_usage_error _('unknown volume id') + end + params + end + success_message _('Volume was removed.') + failure_message _('Could not remove volume') + build_options :without => :id + end + + autoload_subcommands + end +end diff --git a/lib/hammer_cli_foreman/compute_profile.rb b/lib/hammer_cli_foreman/compute_profile.rb new file mode 100644 index 000000000..00dd9c615 --- /dev/null +++ b/lib/hammer_cli_foreman/compute_profile.rb @@ -0,0 +1,61 @@ +require 'hammer_cli_foreman/image' + +module HammerCLIForeman + class ComputeProfile < HammerCLIForeman::Command + resource :compute_profiles + + class ListCommand < HammerCLIForeman::ListCommand + output do + field :id, _('Id') + field :name, _('Name') + end + build_options + end + + class InfoCommand < HammerCLIForeman::InfoCommand + output ListCommand.output_definition do + + HammerCLIForeman::References.taxonomies(self) + HammerCLIForeman::References.timestamps(self) + + collection :compute_attributes, _('Compute attributes') do + field :id, _('Id') + field :name, _('Name') + field nil, _('Compute Resource'), Fields::SingleReference, :key => :compute_resource + field :vm_attrs, _('VM attributes') + end + end + build_options + end + + class CreateCommand < HammerCLIForeman::CreateCommand + success_message _('Compute profile created.') + failure_message _('Could not create a compute profile') + build_options + end + + class UpdateCommand < HammerCLIForeman::UpdateCommand + success_message _('Compute profile updated.') + failure_message _('Could not update the compute profile') + validate_options do + any(:option_name,:option_id).required + end + build_options + end + + class DeleteCommand < HammerCLIForeman::DeleteCommand + success_message _('Compute profile deleted.') + failure_message _('Could not delete the Compute profile') + validate_options do + any(:option_name,:option_id).required + end + build_options + end + + lazy_subcommand('values', _("Create update and delete Compute profile values"), + 'HammerCLIForeman::ComputeAttribute', 'hammer_cli_foreman/compute_attribute' + ) + + autoload_subcommands + end +end diff --git a/lib/hammer_cli_foreman/compute_resource/base.rb b/lib/hammer_cli_foreman/compute_resource/base.rb new file mode 100644 index 000000000..3ed94576f --- /dev/null +++ b/lib/hammer_cli_foreman/compute_resource/base.rb @@ -0,0 +1,11 @@ +module HammerCLIForeman + module ComputeResources + class Base + attr_reader :name + def compute_attributes; []; end + def interface_attributes; []; end + def volume_attributes; []; end + def interfaces_attrs_name; "interfaces_attributes" ; end + end + end +end \ No newline at end of file diff --git a/lib/hammer_cli_foreman/compute_resource/ec2.rb b/lib/hammer_cli_foreman/compute_resource/ec2.rb new file mode 100644 index 000000000..4a510c8dc --- /dev/null +++ b/lib/hammer_cli_foreman/compute_resource/ec2.rb @@ -0,0 +1,20 @@ +module HammerCLIForeman + module ComputeResources + class EC2 < Base + def name + _('EC2') + end + + def compute_attributes + [ + 'flavor_id', + 'image_id', + 'availability_zone', + 'security_group_ids', + 'managed_ip' + ] + end + end + HammerCLIForeman.register_compute_resource('EC2', EC2.new ) + end +end \ No newline at end of file diff --git a/lib/hammer_cli_foreman/compute_resource/gce.rb b/lib/hammer_cli_foreman/compute_resource/gce.rb new file mode 100644 index 000000000..56eb629fc --- /dev/null +++ b/lib/hammer_cli_foreman/compute_resource/gce.rb @@ -0,0 +1,23 @@ +module HammerCLIForeman + module ComputeResources + class GCE < Base + def name + _('GCE') + end + + def compute_attributes + [ + 'machine_type', + 'image_id', + 'network', + 'external_ip' + ] + end + + def interfaces_attrs_name + "network_interfaces_nics_attributes" + end + end + HammerCLIForeman.register_compute_resource('GCE', GCE.new) + end +end \ No newline at end of file diff --git a/lib/hammer_cli_foreman/compute_resource/help_utils.rb b/lib/hammer_cli_foreman/compute_resource/help_utils.rb new file mode 100644 index 000000000..e91323763 --- /dev/null +++ b/lib/hammer_cli_foreman/compute_resource/help_utils.rb @@ -0,0 +1,34 @@ +module HammerCLIForeman + module ComputeResources + def self.extend_help(h, attributes, add_host_specific_attrs: false ) + h.section _('Provider specific options') do |h| + ::HammerCLIForeman.compute_resources.each do |name, provider| + h.section name do |h| + if add_host_specific_attrs and defined?(provider.host_attributes) and defined?(provider.interface_attributes) + host_interfaces = provider.interface_attributes + provider.host_attributes + end + if attributes == :all || attributes == :volume + h.section '--volume' do |h| + h.list(provider.volume_attributes) if defined?(provider.volume_attributes) + end + end + if attributes == :all || attributes == :interface + h.section '--interface' do |h| + if host_interfaces + h.list(host_interfaces) + else + h.list(provider.interface_attributes) if defined?(provider.interface_attributes) + end + end + end + if attributes == :all || attributes == :compute + h.section '--compute-attributes' do |h| + h.list(provider.compute_attributes) if defined?(provider.compute_attributes) + end + end + end + end + end + end + end +end \ No newline at end of file diff --git a/lib/hammer_cli_foreman/compute_resource/libvirt.rb b/lib/hammer_cli_foreman/compute_resource/libvirt.rb new file mode 100644 index 000000000..931d457d3 --- /dev/null +++ b/lib/hammer_cli_foreman/compute_resource/libvirt.rb @@ -0,0 +1,44 @@ +module HammerCLIForeman + module ComputeResources + class Libvirt < Base + def name + _('Libvirt') + end + + def compute_attributes + [ + ['cpus', _('Number of CPUs')], + ['memory', _('String, amount of memory, value in bytes')], + ['start', _('Boolean (expressed as 0 or 1), whether to start the machine or not')] + ] + end + + def host_attributes + [ + ['start', _('Boolean (expressed as 0 or 1), whether to start the machine or not')] + ] + end + + def interface_attributes + [ + ['type', _('Possible values: %s') % 'bridge, network'], + ['bridge', _('Name of interface according to type')], + ['model', _('Possible values: %s') % 'virtio, rtl8139, ne2k_pci, pcnet, e1000'] + ] + end + + def volume_attributes + [ + ['pool_name', _('One of available storage pools')], + ['capacity', _('String value, eg. 10G')], + ['format_type', _('Possible values: %s') % 'raw, qcow2'] + ] + end + + def interfaces_attrs_name + "nics_attributes" + end + end + HammerCLIForeman.register_compute_resource('Libvirt', Libvirt.new) + end +end \ No newline at end of file diff --git a/lib/hammer_cli_foreman/compute_resource/openstack.rb b/lib/hammer_cli_foreman/compute_resource/openstack.rb new file mode 100644 index 000000000..a053116af --- /dev/null +++ b/lib/hammer_cli_foreman/compute_resource/openstack.rb @@ -0,0 +1,20 @@ +module HammerCLIForeman + module ComputeResources + class OpenStack < Base + def name + _('OpenStack') + end + + def compute_attributes + [ + 'flavor_ref', + 'image_ref', + 'tenant_id', + 'security_groups', + 'network' + ] + end + end + HammerCLIForeman.register_compute_resource('OpenStack', OpenStack.new) + end +end \ No newline at end of file diff --git a/lib/hammer_cli_foreman/compute_resource/ovirt.rb b/lib/hammer_cli_foreman/compute_resource/ovirt.rb new file mode 100644 index 000000000..7a08196d0 --- /dev/null +++ b/lib/hammer_cli_foreman/compute_resource/ovirt.rb @@ -0,0 +1,42 @@ +module HammerCLIForeman + module ComputeResources + class Ovirt < Base + def name + _('oVirt') + end + + def compute_attributes + [ + ['cluster'], + ['template', _('Hardware profile to use')], + ['cores', _('Integer value, number of cores')], + ['memory', _('Amount of memory, integer value in bytes')], + ] + end + + def host_attributes + [ + ['start', _('Boolean (expressed as 0 or 1), whether to start the machine or not')] + + ] + end + + def interface_attributes + [ + ['name', _('compute name, Eg. eth0')], + ['network', _('Select one of available networks for a cluster')], + ['interface', ('interface type')] + ] + end + + def volume_attributes; + [ + ['size_gb', _('Volume size in GB, integer value')], + ['storage_domain', _('Select one of available storage domains')], + ['bootable', _('Boolean, only one volume can be bootable')] + ] + end + end + HammerCLIForeman.register_compute_resource('oVirt', Ovirt.new) + end +end \ No newline at end of file diff --git a/lib/hammer_cli_foreman/compute_resource/rackspace.rb b/lib/hammer_cli_foreman/compute_resource/rackspace.rb new file mode 100644 index 000000000..243c43d16 --- /dev/null +++ b/lib/hammer_cli_foreman/compute_resource/rackspace.rb @@ -0,0 +1,17 @@ +module HammerCLIForeman + module ComputeResources + class Rackspace < Base + def name + _('Rackspace') + end + + def compute_attributes + [ + 'flavor_id', + 'image_id' + ] + end + end + HammerCLIForeman.register_compute_resource('Rackspace', Rackspace.new) + end +end \ No newline at end of file diff --git a/lib/hammer_cli_foreman/compute_resource/register_compute_resources.rb b/lib/hammer_cli_foreman/compute_resource/register_compute_resources.rb new file mode 100644 index 000000000..cc02ec6be --- /dev/null +++ b/lib/hammer_cli_foreman/compute_resource/register_compute_resources.rb @@ -0,0 +1,24 @@ +module HammerCLIForeman + @compute_resources = {} + @interfaces_attrs_name_list ={} + def self.compute_resources + @compute_resources + end + + def self.register_compute_resource(name, compute_resource) + @compute_resources[name] = compute_resource + @interfaces_attrs_name_list[name] = compute_resource.interfaces_attrs_name + end + + def self.get_interfaces_list_name + @interfaces_attrs_name_list + end + require 'hammer_cli_foreman/compute_resource/base' + require 'hammer_cli_foreman/compute_resource/ec2.rb' + require 'hammer_cli_foreman/compute_resource/gce.rb' + require 'hammer_cli_foreman/compute_resource/libvirt.rb' + require 'hammer_cli_foreman/compute_resource/openstack.rb' + require 'hammer_cli_foreman/compute_resource/ovirt.rb' + require 'hammer_cli_foreman/compute_resource/rackspace.rb' + require 'hammer_cli_foreman/compute_resource/vmware.rb' +end \ No newline at end of file diff --git a/lib/hammer_cli_foreman/compute_resource/vmware.rb b/lib/hammer_cli_foreman/compute_resource/vmware.rb new file mode 100644 index 000000000..75bd90d19 --- /dev/null +++ b/lib/hammer_cli_foreman/compute_resource/vmware.rb @@ -0,0 +1,63 @@ +module HammerCLIForeman + module ComputeResources + class VMware < Base + INTERFACE_TYPES = %w( + VirtualVmxnet3, + VirtualE1000 + ) + + def name + _('VMware') + end + + def compute_attributes + [ + ['cpus', _('CPU count')], + ['corespersocket', _('Number of cores per socket (applicable to hardware versions < 10 only)')], + ['memory_mb', _('Integer number, amount of memory in MB')], + ['firmware', 'automatic/bios/efi'], + ['cluster', _('Cluster ID from VMware')], + ['resource_pool', _('Resource Pool ID from VMware')], + ['path', _('Path to folder')], + ['guest_id', _('Guest OS ID form VMware')], + ['scsi_controller_type', _('ID of the controller from VMware')], + ['hardware_version', _('Hardware version ID from VMware')], + ['add_cdrom', _('Must be a 1 or 0, Add a CD-ROM drive to the virtual machine')], + ['cpuHotAddEnabled', _('Must be a 1 or 0, lets you add memory resources while the machine is on')], + ['memoryHotAddEnabled', _('Must be a 1 or 0, lets you add CPU resources while the machine is on')], + ['annotation', _("Annotation Notes")] + ] + end + + def host_attributes + [ + ['start', _("Must be a 1 or 0, whether to start the machine or not")], + ] + end + + def interface_attributes + [ + ['compute_type', [ + _('Type of the network adapter, for example one of:'), + INTERFACE_TYPES.map { |it| ' ' + it }, + _('See documentation center for your version of vSphere to find more details about available adapter types:'), + ' https://www.vmware.com/support/pubs/'].flatten(1).join("\n") ] , + ['compute_network', _('Network ID from VMware')] + ] + end + + def volume_attributes; + [ + ['name'], + ['storage_pod', _('Storage Pod ID from VMware')], + ['datastore', _('Datastore ID from VMware')], + ['size_gb', _('Integer number, volume size in GB')], + ['thin', 'true/false'], + ['eager_zero', 'true/false'], + ['mode', 'persistent/independent_persistent/independent_nonpersistent'] + ] + end + end + HammerCLIForeman.register_compute_resource('VMware', VMware.new) + end +end \ No newline at end of file diff --git a/lib/hammer_cli_foreman/compute_resources/all.rb b/lib/hammer_cli_foreman/compute_resources/all.rb deleted file mode 100644 index ca51c379b..000000000 --- a/lib/hammer_cli_foreman/compute_resources/all.rb +++ /dev/null @@ -1,7 +0,0 @@ -require 'hammer_cli_foreman/compute_resources/ec2' -require 'hammer_cli_foreman/compute_resources/gce' -require 'hammer_cli_foreman/compute_resources/libvirt' -require 'hammer_cli_foreman/compute_resources/openstack' -require 'hammer_cli_foreman/compute_resources/ovirt' -require 'hammer_cli_foreman/compute_resources/rackspace' -require 'hammer_cli_foreman/compute_resources/vmware' diff --git a/lib/hammer_cli_foreman/compute_resources/ec2.rb b/lib/hammer_cli_foreman/compute_resources/ec2.rb deleted file mode 100644 index 0c1b61812..000000000 --- a/lib/hammer_cli_foreman/compute_resources/ec2.rb +++ /dev/null @@ -1,9 +0,0 @@ -require 'hammer_cli_foreman/compute_resources/ec2/host_help_extenstion' - -module HammerCLIForeman - module ComputeResources - module EC2 - HammerCLIForeman::Host.extend_cr_help(HostHelpExtenstion.new) - end - end -end diff --git a/lib/hammer_cli_foreman/compute_resources/ec2/host_help_extenstion.rb b/lib/hammer_cli_foreman/compute_resources/ec2/host_help_extenstion.rb deleted file mode 100644 index a72d0b503..000000000 --- a/lib/hammer_cli_foreman/compute_resources/ec2/host_help_extenstion.rb +++ /dev/null @@ -1,23 +0,0 @@ -module HammerCLIForeman - module ComputeResources - module EC2 - class HostHelpExtenstion - def name - _('EC2') - end - - def host_create_help(h) - h.section '--compute-attributes' do |h| - h.list([ - 'flavor_id', - 'image_id', - 'availability_zone', - 'security_group_ids', - 'managed_ip', - ]) - end - end - end - end - end -end diff --git a/lib/hammer_cli_foreman/compute_resources/gce.rb b/lib/hammer_cli_foreman/compute_resources/gce.rb deleted file mode 100644 index c5de0c6d3..000000000 --- a/lib/hammer_cli_foreman/compute_resources/gce.rb +++ /dev/null @@ -1,9 +0,0 @@ -require 'hammer_cli_foreman/compute_resources/gce/host_help_extenstion' - -module HammerCLIForeman - module ComputeResources - module GCE - HammerCLIForeman::Host.extend_cr_help(HostHelpExtenstion.new) - end - end -end diff --git a/lib/hammer_cli_foreman/compute_resources/gce/host_help_extenstion.rb b/lib/hammer_cli_foreman/compute_resources/gce/host_help_extenstion.rb deleted file mode 100644 index eb94a5534..000000000 --- a/lib/hammer_cli_foreman/compute_resources/gce/host_help_extenstion.rb +++ /dev/null @@ -1,22 +0,0 @@ -module HammerCLIForeman - module ComputeResources - module GCE - class HostHelpExtenstion - def name - _('GCE') - end - - def host_create_help(h) - h.section '--compute-attributes' do |h| - h.list([ - 'machine_type', - 'image_id', - 'network', - 'external_ip' - ]) - end - end - end - end - end -end diff --git a/lib/hammer_cli_foreman/compute_resources/libvirt.rb b/lib/hammer_cli_foreman/compute_resources/libvirt.rb deleted file mode 100644 index d2999330a..000000000 --- a/lib/hammer_cli_foreman/compute_resources/libvirt.rb +++ /dev/null @@ -1,9 +0,0 @@ -require 'hammer_cli_foreman/compute_resources/libvirt/host_help_extenstion' - -module HammerCLIForeman - module ComputeResources - module Libvirt - HammerCLIForeman::Host.extend_cr_help(HostHelpExtenstion.new) - end - end -end diff --git a/lib/hammer_cli_foreman/compute_resources/libvirt/host_help_extenstion.rb b/lib/hammer_cli_foreman/compute_resources/libvirt/host_help_extenstion.rb deleted file mode 100644 index de02c3ba7..000000000 --- a/lib/hammer_cli_foreman/compute_resources/libvirt/host_help_extenstion.rb +++ /dev/null @@ -1,35 +0,0 @@ -module HammerCLIForeman - module ComputeResources - module Libvirt - class HostHelpExtenstion - def name - _('Libvirt') - end - - def host_create_help(h) - h.section '--compute-attributes' do |h| - h.list([ - ['cpus', _('Number of CPUs')], - ['memory', _('String, amount of memory, value in bytes')], - ['start', _('Boolean (expressed as 0 or 1), whether to start the machine or not')] - ]) - end - h.section '--interface' do |h| - h.list([ - ['compute_type', _('Possible values: %s') % 'bridge, network'], - ['compute_network / compute_bridge', _('Name of interface according to type')], - ['compute_model', _('Possible values: %s') % 'virtio, rtl8139, ne2k_pci, pcnet, e1000'] - ]) - end - h.section '--volume' do |h| - h.list([ - ['pool_name', _('One of available storage pools')], - ['capacity', _('String value, eg. 10G')], - ['format_type', _('Possible values: %s') % 'raw, qcow2'] - ]) - end - end - end - end - end -end diff --git a/lib/hammer_cli_foreman/compute_resources/openstack.rb b/lib/hammer_cli_foreman/compute_resources/openstack.rb deleted file mode 100644 index 6eb6e53f2..000000000 --- a/lib/hammer_cli_foreman/compute_resources/openstack.rb +++ /dev/null @@ -1,9 +0,0 @@ -require 'hammer_cli_foreman/compute_resources/openstack/host_help_extenstion' - -module HammerCLIForeman - module ComputeResources - module OpenStack - HammerCLIForeman::Host.extend_cr_help(HostHelpExtenstion.new) - end - end -end diff --git a/lib/hammer_cli_foreman/compute_resources/openstack/host_help_extenstion.rb b/lib/hammer_cli_foreman/compute_resources/openstack/host_help_extenstion.rb deleted file mode 100644 index 0674c34a8..000000000 --- a/lib/hammer_cli_foreman/compute_resources/openstack/host_help_extenstion.rb +++ /dev/null @@ -1,23 +0,0 @@ -module HammerCLIForeman - module ComputeResources - module OpenStack - class HostHelpExtenstion - def name - _('OpenStack') - end - - def host_create_help(h) - h.section '--compute-attributes' do |h| - h.list([ - 'flavor_ref', - 'image_ref', - 'tenant_id', - 'security_groups', - 'network' - ]) - end - end - end - end - end -end diff --git a/lib/hammer_cli_foreman/compute_resources/ovirt.rb b/lib/hammer_cli_foreman/compute_resources/ovirt.rb deleted file mode 100644 index 837a34286..000000000 --- a/lib/hammer_cli_foreman/compute_resources/ovirt.rb +++ /dev/null @@ -1,9 +0,0 @@ -require 'hammer_cli_foreman/compute_resources/ovirt/host_help_extenstion' - -module HammerCLIForeman - module ComputeResources - module Ovirt - HammerCLIForeman::Host.extend_cr_help(HostHelpExtenstion.new) - end - end -end diff --git a/lib/hammer_cli_foreman/compute_resources/ovirt/host_help_extenstion.rb b/lib/hammer_cli_foreman/compute_resources/ovirt/host_help_extenstion.rb deleted file mode 100644 index 46b3b4f86..000000000 --- a/lib/hammer_cli_foreman/compute_resources/ovirt/host_help_extenstion.rb +++ /dev/null @@ -1,36 +0,0 @@ -module HammerCLIForeman - module ComputeResources - module Ovirt - class HostHelpExtenstion - def name - _('oVirt') - end - - def host_create_help(h) - h.section '--compute-attributes' do |h| - h.list([ - ['cluster'], - ['template', _('Hardware profile to use')], - ['cores', _('Integer value, number of cores')], - ['memory', _('Amount of memory, integer value in bytes')], - ['start', _('Boolean (expressed as 0 or 1), whether to start the machine or not')] - ]) - end - h.section '--interface' do |h| - h.list([ - ['compute_name', _('Eg. eth0')], - ['compute_network', _('Select one of available networks for a cluster')] - ]) - end - h.section '--volume' do |h| - h.list([ - ['size_gb', _('Volume size in GB, integer value')], - ['storage_domain', _('Select one of available storage domains')], - ['bootable', _('Boolean, only one volume can be bootable')] - ]) - end - end - end - end - end -end diff --git a/lib/hammer_cli_foreman/compute_resources/rackspace.rb b/lib/hammer_cli_foreman/compute_resources/rackspace.rb deleted file mode 100644 index ad49f1837..000000000 --- a/lib/hammer_cli_foreman/compute_resources/rackspace.rb +++ /dev/null @@ -1,9 +0,0 @@ -require 'hammer_cli_foreman/compute_resources/rackspace/host_help_extenstion' - -module HammerCLIForeman - module ComputeResources - module Rackspace - HammerCLIForeman::Host.extend_cr_help(HostHelpExtenstion.new) - end - end -end diff --git a/lib/hammer_cli_foreman/compute_resources/rackspace/host_help_extenstion.rb b/lib/hammer_cli_foreman/compute_resources/rackspace/host_help_extenstion.rb deleted file mode 100644 index 4b5ca30f1..000000000 --- a/lib/hammer_cli_foreman/compute_resources/rackspace/host_help_extenstion.rb +++ /dev/null @@ -1,20 +0,0 @@ -module HammerCLIForeman - module ComputeResources - module Rackspace - class HostHelpExtenstion - def name - _('Rackspace') - end - - def host_create_help(h) - h.section '--compute-attributes' do |h| - h.list([ - 'flavor_id', - 'image_id' - ]) - end - end - end - end - end -end diff --git a/lib/hammer_cli_foreman/compute_resources/vmware.rb b/lib/hammer_cli_foreman/compute_resources/vmware.rb deleted file mode 100644 index 09ba46ed2..000000000 --- a/lib/hammer_cli_foreman/compute_resources/vmware.rb +++ /dev/null @@ -1,9 +0,0 @@ -require 'hammer_cli_foreman/compute_resources/vmware/host_help_extenstion' - -module HammerCLIForeman - module ComputeResources - module VMware - HammerCLIForeman::Host.extend_cr_help(HostHelpExtenstion.new) - end - end -end diff --git a/lib/hammer_cli_foreman/compute_resources/vmware/host_help_extenstion.rb b/lib/hammer_cli_foreman/compute_resources/vmware/host_help_extenstion.rb deleted file mode 100644 index 6d781dea2..000000000 --- a/lib/hammer_cli_foreman/compute_resources/vmware/host_help_extenstion.rb +++ /dev/null @@ -1,66 +0,0 @@ -module HammerCLIForeman - module ComputeResources - module VMware - class HostHelpExtenstion - INTERFACE_TYPES = %w( - VirtualVmxnet3, - VirtualE1000 - ) - - def name - _('VMware') - end - - def host_create_help(h) - h.section '--compute-attributes' do |h| - h.list([ - ['cpus', _('CPU count')], - ['corespersocket', _('Number of cores per socket (applicable to hardware versions < 10 only)')], - ['memory_mb', _('Integer number, amount of memory in MB')], - ['firmware', 'automatic/bios/efi'], - ['cluster', _('Cluster ID from VMware')], - ['resource_pool', _('Resource Pool ID from VMware')], - ['path', _('Path to folder')], - ['guest_id', _('Guest OS ID form VMware')], - ['scsi_controller_type', _('ID of the controller from VMware')], - ['hardware_version', _('Hardware version ID from VMware')], - ['add_cdrom', _('Must be a 1 or 0, Add a CD-ROM drive to the virtual machine')], - ['cpuHotAddEnabled', _('Must be a 1 or 0, lets you add memory resources while the machine is on')], - ['memoryHotAddEnabled', _('Must be a 1 or 0, lets you add CPU resources while the machine is on')], - ['start', _("Must be a 1 or 0, whether to start the machine or not")], - ['annotation', _("Annotation Notes")] - ]) - end - h.section '--interface' do |h| - h.list([ - ['compute_type', interface_type_description(h)], - ['compute_network', _('Network ID from VMware')] - ]) - end - h.section '--volume' do |h| - h.list([ - ['name'], - ['storage_pod', _('Storage Pod ID from VMware')], - ['datastore', _('Datastore ID from VMware')], - ['size_gb', _('Integer number, volume size in GB')], - ['thin', 'true/false'], - ['eager_zero', 'true/false'], - ['mode', 'persistent/independent_persistent/independent_nonpersistent'] - ]) - end - end - - private - - def interface_type_description(h) - [ - _('Type of the network adapter, for example one of:'), - h.indent(INTERFACE_TYPES), - _("See documentation center for your version of vSphere to find more details about available adapter types:"), - h.indent('https://www.vmware.com/support/pubs/') - ].join("\n") - end - end - end - end -end diff --git a/lib/hammer_cli_foreman/host.rb b/lib/hammer_cli_foreman/host.rb index 4316c3ecd..70f60cb30 100644 --- a/lib/hammer_cli_foreman/host.rb +++ b/lib/hammer_cli_foreman/host.rb @@ -6,6 +6,8 @@ require 'hammer_cli_foreman/interface' require 'hammer_cli_foreman/hosts/common_update_options' require 'hammer_cli_foreman/hosts/common_update_help' +require 'hammer_cli_foreman/compute_resource/register_compute_resources' +require 'hammer_cli_foreman/compute_resource/help_utils' require 'highline/import' @@ -16,14 +18,6 @@ class Host < HammerCLIForeman::Command resource :hosts - def self.extend_cr_help(cr) - cr_help_extensions[cr.name] = cr.method(:host_create_help) - end - - def self.cr_help_extensions - @cr_help_extensions ||= {} - end - class ListCommand < HammerCLIForeman::ListCommand # FIXME: list compute resource (model) output do @@ -521,5 +515,3 @@ def execute subcommand 'interface', HammerCLIForeman::Interface.desc, HammerCLIForeman::Interface end end - -require 'hammer_cli_foreman/compute_resources/all' diff --git a/lib/hammer_cli_foreman/hosts/common_update_help.rb b/lib/hammer_cli_foreman/hosts/common_update_help.rb index fe1506c2e..9c38b4fb7 100644 --- a/lib/hammer_cli_foreman/hosts/common_update_help.rb +++ b/lib/hammer_cli_foreman/hosts/common_update_help.rb @@ -39,13 +39,7 @@ def self.included(base) end end - h.section _('Provider specific options') do |h| - HammerCLIForeman::Host.cr_help_extensions.each do |name, help| - h.section name do |h| - help.call(h) - end - end - end + ::HammerCLIForeman::ComputeResources.extend_help(h, :all, add_host_specific_attrs: true ) end end end diff --git a/lib/hammer_cli_foreman/id_resolver.rb b/lib/hammer_cli_foreman/id_resolver.rb index 97110f76c..d8b8929f9 100644 --- a/lib/hammer_cli_foreman/id_resolver.rb +++ b/lib/hammer_cli_foreman/id_resolver.rb @@ -36,6 +36,7 @@ def self.s_name(description, options={}) :architecture => [ s_name(_("Architecture name")) ], :audit => [], :compute_resource => [ s_name(_("Compute resource name")) ], + :compute_profile => [ s_name(_("Compute profile name")) ], :domain => [ s_name(_("Domain name")) ], :environment => [ s_name(_("Environment name")) ], :fact_value => [], @@ -62,7 +63,8 @@ def self.s_name(description, options={}) :common_parameter => [ s_name(_("Common parameter name")) ], :smart_class_parameter => [ s_name(_("Smart class parameter name"), :editable => false) ], :smart_variable => [ s("variable", _("Smart variable name")) ], - :template_combination => [] + :template_combination => [], + :compute_attribute => [] } DEFAULT_SEARCHABLES = [ s_name(_("Name to search by")) ] diff --git a/lib/hammer_cli_foreman/references.rb b/lib/hammer_cli_foreman/references.rb index c567db4b6..39ba0d35b 100644 --- a/lib/hammer_cli_foreman/references.rb +++ b/lib/hammer_cli_foreman/references.rb @@ -56,6 +56,14 @@ def self.smart_proxies(dsl) end end + def self.compute_profiles(dsl) + dsl.build do + collection :compute_profiles, _("Compute profiles"), :numbered => false do + custom_field Fields::Reference + end + end + end + def self.compute_resources(dsl) dsl.build do collection :compute_resources, _("Compute resources"), :numbered => false do diff --git a/test/functional/compute_attribute_test.rb b/test/functional/compute_attribute_test.rb new file mode 100644 index 000000000..efb22f757 --- /dev/null +++ b/test/functional/compute_attribute_test.rb @@ -0,0 +1,654 @@ +require File.join(File.dirname(__FILE__), 'test_helper') +describe "parameters" do + + describe "create values" do + before do + @cmd = ["compute-profile", "values", "create"] + @compute_profile = + { + "id" => 1, + "name" => "profile2", + } + end + + it "should print error on missing --compute-profile-id or --compute-profile" do + expected_result = common_error_result( + @cmd, + "Could not find compute_profile, please set one of options --compute-profile, --compute-profile-id.", + "Could not set the compute profile attributes" + ) + + api_expects_no_call + result = run_cmd(@cmd) + assert_cmd(expected_result, result) + end + + it "should print error on missing --compute-resource-id or --compute-resource" do + params = ['--compute-profile-id=1'] + expected_result = common_error_result( + @cmd, + "Could not find compute_resource, please set one of options --compute-resource, --compute-resource-id.", + "Could not set the compute profile attributes" + ) + + api_expects_no_call + result = run_cmd(@cmd + params) + assert_cmd(expected_result, result) + end + end + + describe "update values" do + before do + @cmd = ["compute-profile", "values", "update"] + @compute_profile = + { + "id" => 1, + "name" => "profile2", + "compute_attributes" => [{ + "id" => 2, + "compute_resource_id" => 1, + "compute_resource_name" => "bla", + "provider_friendly_name" => "oVirt", + "compute_profile_id" => 1, + "compute_profile_name" => "profile2", + "vm_attrs" => {} + }] + } + @compute_attribute = + { + "compute_attribute" => + { + "id" => 2, "compute_resource_id" => 1, "compute_resource_name" => "bla", + "provider_friendly_name" => "oVirt", "compute_profile_id" => 1, + "compute_profile_name" => "profile2", "vm_attrs" => {"cores" => "1"} + } + } + + @compute_resource = { + "id" => 1, + "name" => "ovirt_compute_resource", + "provider" => "Ovirt", + "provider_friendly_name" => "oVirt", + } + end + + it "values update - should print error on missing --compute-profile-id or --compute-profile" do + expected_result = usage_error_result( + @cmd, + "At least one of options --compute-profile-id, --compute-profile is required.", + "Could not update the compute profile attributes" + ) + + api_expects_no_call + result = run_cmd(@cmd) + assert_cmd(expected_result, result) + end + + it "values update - should print error on missing --compute-resource-id or --compute-resource" do + params = ['--compute-profile-id=1'] + expected_result = usage_error_result( + @cmd, + "At least one of options --compute-resource-id, --compute-resource is required.", + "Could not update the compute profile attributes" + ) + + api_expects_no_call + result = run_cmd(@cmd + params) + + assert_cmd(expected_result, result) + end + + + it "values update - should update attributes" do + params = ['--compute-profile-id=1', '--compute-resource-id=1', '--compute-attributes', 'cores=1'] + + api_expects(:compute_profiles, :show) do |par| + par['id'] == 1 + end.returns(@compute_profile) + + api_expects(:compute_resources, :show) do |par| + par['id'] == 1 + end.returns(@compute_resource) + + api_expects(:compute_attributes, :update) do |par| + par['id'] == 2 + end.returns(@compute_attribute) + result = run_cmd(@cmd + params) + assert_cmd(success_result("Compute profile attributes updated.\n"), result) + end + end + + describe "add-volume" do + before do + @cmd = ["compute-profile", "values", "add-volume"] + @compute_profile = + { + "id" => 1, + "name" => "profile2", + "compute_attributes" => [{ + "id" => 2, + "compute_resource_id" => 1, + "compute_resource_name" => "bla", + "provider_friendly_name" => "oVirt", + "compute_profile_id" => 1, + "compute_profile_name" => "profile2", + "vm_attrs" => {} + }] + } + @compute_attribute = { + "id" => 2, "compute_resource_id" => 1, "compute_resource_name" => "bla", + "provider_friendly_name" => "oVirt", "compute_profile_id" => 1, "compute_profile_name" => "profile2", + "vm_attrs" => {"volumes_attributes" => {"1525004465" => {"size_gb" => "1"}}} + } + + end + + it "should print error on missing --compute-profile-id or --compute-profile" do + params= ['--volume', 'size_gb=1'] + expected_result = usage_error_result( + @cmd, + "At least one of options --compute-profile-id, --compute-profile is required.", + "Could not create volume" + ) + + api_expects_no_call + result = run_cmd(@cmd + params) + assert_cmd(expected_result, result) + end + + it "should print error on missing --compute-resource-id or --compute-resource" do + params = ['--compute-profile-id=1', '--volume', 'size_gb=1'] + expected_result = usage_error_result( + @cmd, + "At least one of options --compute-resource-id, --compute-resource is required.", + "Could not create volume" + ) + + api_expects_no_call + result = run_cmd(@cmd + params) + assert_cmd(expected_result, result) + end + + + it "should print error on compute profile value does not exist" do + params = ['--compute-profile-id=1', '--compute-resource-id=15', '--volume', 'size_gb=1'] + expected_result = CommandExpectation.new + expected_result.expected_err = "Could not create volume:\n Compute profile value to update does not exist yet; it needs to be created first\n" + expected_result.expected_exit_code = HammerCLI::EX_USAGE + + api_expects(:compute_profiles, :show) do |par| + par['id'] == 1 + end.returns(@compute_profile) + + result = run_cmd(@cmd + params) + assert_cmd(expected_result, result) + end + + + it "should add volume" do + params = ['--compute-profile-id=1', '--compute-resource-id=1', '--volume', 'size_gb=1'] + + api_expects(:compute_profiles, :show) do |par| + par['id'] == 1 + end.returns(@compute_profile) + + api_expects(:compute_attributes, :update) do |par| + par['id'] == 2 + end.returns(@compute_attribute) + result = run_cmd(@cmd + params) + assert_cmd(success_result("Volume was created.\n"), result) + end + end + + describe "update-volume" do + before do + @cmd = ["compute-profile", "values", "update-volume"] + @compute_profile = + { + "id" => 1, + "name" => "profile2", + "compute_attributes" => [{ + "id" => 2, + "compute_resource_id" => 1, + "compute_resource_name" => "bla", + "provider_friendly_name" => "oVirt", + "compute_profile_id" => 1, + "compute_profile_name" => "profile2", + "vm_attrs" => {} + }] + } + end + + it "should print error on missing --volume" do + expected_result = usage_error_result( + @cmd, + "Option '--volume' is required.", + "Could not update volume" + ) + + api_expects_no_call + result = run_cmd(@cmd) + assert_cmd(expected_result, result) + end + + it "should print error on missing --volume-id" do + params = ['--volume', 'compute_name="compute"'] + expected_result = usage_error_result( + @cmd, + "Option '--volume-id' is required.", + "Could not update volume" + ) + + api_expects_no_call + result = run_cmd(@cmd + params) + assert_cmd(expected_result, result) + end + + it "should print error on missing --compute-resource-id or --compute-resource." do + params = ['--volume', 'compute_name="eth1"', '--volume-id=1'] + expected_result = usage_error_result( + @cmd, + "At least one of options --compute-profile-id, --compute-profile is required.", + "Could not update volume" + ) + + api_expects_no_call + result = run_cmd(@cmd + params) + assert_cmd(expected_result, result) + end + + it "should update volume" do + params = ['--compute-profile-id=1', '--compute-resource-id=1', '--volume-id=1', '--volume', 'size_gb=1'] + + api_expects(:compute_profiles, :show) do |par| + par['id'] == 1 + end.returns(@compute_profile) + + api_expects(:compute_attributes, :update) do |par| + par['id'] == 2 + end.returns(@compute_attribute) + result = run_cmd(@cmd + params) + assert_cmd(success_result("Volume was updated.\n"), result) + end + end + + describe "remove-volume" do + before do + @cmd = ["compute-profile", "values", "remove-volume"] + @compute_profile = + { + "id" => 1, + "name" => "profile2", + "compute_attributes" => [{ + "id" => 2, + "compute_resource_id" => 1, + "compute_resource_name" => "bla", + "provider_friendly_name" => "oVirt", + "compute_profile_id" => 1, + "compute_profile_name" => "profile2", + "vm_attrs" => {"volumes_attributes" => { "1" => {"size_gb"=>"1"}}} + }] + } + @compute_attribute = { + "id" => 2, "compute_resource_id" => 1, "compute_resource_name" => "bla", + "provider_friendly_name" => "oVirt", "compute_profile_id" => 1, + "compute_profile_name" => "profile2", "vm_attrs" => {"volumes_attributes" => {}} + } + end + + it "should print error on missing --compute-profile-id or --compute-profile" do + params = ['--volume-id=1'] + expected_result = usage_error_result( + @cmd, + "At least one of options --compute-profile-id, --compute-profile is required.", + "Could not remove volume" + ) + + api_expects_no_call + result = run_cmd(@cmd + params) + assert_cmd(expected_result, result) + end + + it "should print error on missing --compute-resource-id or --compute-resource" do + params = ['--compute-profile-id=1', '--volume-id=1'] + expected_result = usage_error_result( + @cmd, + "At least one of options --compute-resource-id, --compute-resource is required.", + "Could not remove volume" + ) + + api_expects_no_call + result = run_cmd(@cmd + params) + assert_cmd(expected_result, result) + end + + it "should print error on missing --volume-id" do + params = ['--compute-profile-id=1', '--compute-resource-id=1'] + expected_result = usage_error_result( + @cmd, + "Option '--volume-id' is required.", + "Could not remove volume" + ) + + api_expects_no_call + result = run_cmd(@cmd + params) + assert_cmd(expected_result, result) + end + + it "should remove volume" do + params = ['--compute-profile-id=1', '--compute-resource-id=1', '--volume-id=1'] + + api_expects(:compute_profiles, :show) do |par| + par['id'] == 1 + end.returns(@compute_profile) + + api_expects(:compute_attributes, :update) do |par| + par['id'] == 2 + end.returns(@compute_attribute) + + result = run_cmd(@cmd + params) + assert_cmd(success_result("Volume was removed.\n"), result) + end + end + + describe "add-interface" do + before do + @cmd = ["compute-profile", "values", "add-interface"] + @compute_profile = + { + "id" => 1, + "name" => "profile2", + "compute_attributes" => [{ + "id" => 2, + "compute_resource_id" => 1, + "compute_resource_name" => "bla", + "provider_friendly_name" => "oVirt", + "compute_profile_id" => 1, + "compute_profile_name" => "profile2", + "vm_attrs" => {} + }] + } + @compute_profile2 = + { + "id" => 200, + "name" => "profile2", + "compute_attributes" => [{ + "id" => 2, + "compute_resource_id" => 1, + "compute_resource_name" => "bla", + "provider_friendly_name" => "oVirt", + "compute_profile_id" => 1, + "compute_profile_name" => "profile2", + "vm_attrs" => {} + }] + } + + @compute_resource = { + "id" => 1, + "name" => "ovirt_compute_resource", + "provider" => "Ovirt", + "provider_friendly_name" => "oVirt", + } + + end + + it "should print error on missing --compute-profile-id or --compute-profile" do + param = ['--interface', 'name=eth0'] + expected_result = usage_error_result( + @cmd, + "At least one of options --compute-profile-id, --compute-profile is required.", + "Could not create interface" + ) + + api_expects_no_call + result = run_cmd(@cmd+ param) + assert_cmd(expected_result, result) + end + + it "should print error on missing --compute-resource-id or --compute-resource." do + params = ['--compute-profile-id=1', '--interface', 'name=eth0'] + expected_result = usage_error_result( + @cmd, + "At least one of options --compute-resource-id, --compute-resource is required.", + "Could not create interface" + ) + + api_expects_no_call + result = run_cmd(@cmd + params) + assert_cmd(expected_result, result) + end + + it "should print error on missing --interface" do + params = ['--compute-profile=A', '--compute-resource=B'] + expected_result = usage_error_result( + @cmd, + "Option '--interface' is required.", + "Could not create interface" + ) + + api_expects_no_call + result = run_cmd(@cmd + params) + assert_cmd(expected_result, result) + end + + it "should print error on compute profile value does not exist" do + params = ['--compute-profile-id=1', '--compute-resource-id=15', '--interface', 'name=eth0'] + expected_result = CommandExpectation.new + expected_result.expected_err = "Could not create interface:\n Compute profile value to update does not exist yet; it needs to be created first\n" + expected_result.expected_exit_code = HammerCLI::EX_USAGE + + api_expects(:compute_profiles, :show) do |par| + par['id'] == 1 + end.returns(@compute_profile) + result = run_cmd(@cmd + params) + assert_cmd(expected_result, result) + end + + it "should print error on unknown compute profile" do + params = ['--compute-profile-id=200', '--compute-resource-id=1', '--interface', 'name=eth0'] + expected_result = not_found_error_result( + @cmd, + "Resource compute_profile not found by id '200'", + "Could not create interface" + ) + expected_message = "{ \"error\": {\"message\": \"Error: Resource compute_profile not found by id '200'\" }}" + response = HammerCLIForeman.foreman_api.api.send(:create_fake_response, 404, + expected_message, "GET", "http://example.com/", {}) + api_expects(:compute_profiles, :show) do |par| + par['id'] == 200 + end.raises(RestClient::NotFound, response) + + result = run_cmd(@cmd + params) + assert_cmd(expected_result, result) + end + + it "should add interface" do + params = [ '--interface', 'name=eth0', '--compute-profile-id=1', '--compute-resource-id=1'] + api_expects(:compute_profiles, :show) do |par| + par['id'] == 1 + end.returns(@compute_profile) + + api_expects(:compute_resources, :show) do |par| + par['id'] == 1 + end.returns(@compute_resource) + + api_expects(:compute_attributes, :update) do |par| + par['id'] == 2 + end.returns(@compute_attribute) + result = run_cmd(@cmd + params) + assert_cmd(success_result("Interface was created.\n"), result) + end + end + + describe "update-interface" do + before do + @cmd = ["compute-profile", "values", "update-interface"] + @compute_profile = + { + "id" => 1, + "name" => "profile2", + "compute_attributes" => [{ + "id" => 2, + "compute_resource_id" => 1, + "compute_resource_name" => "bla", + "provider_friendly_name" => "oVirt", + "compute_profile_id" => 1, + "compute_profile_name" => "profile2", + "vm_attrs" => {} + }] + } + + @compute_resource = { + "id" => 1, + "name" => "ovirt_compute_resource", + "provider" => "Ovirt", + "provider_friendly_name" => "oVirt", + } + end + + it "should print error on missing --interface" do + expected_result = usage_error_result( + @cmd, + "Option '--interface' is required.", + "Could not update interface" + ) + + api_expects_no_call + result = run_cmd(@cmd) + assert_cmd(expected_result, result) + end + + it "should print error on missing --interface-id" do + params = ['--interface', 'compute_name="compute"'] + expected_result = usage_error_result( + @cmd, + "Option '--interface-id' is required.", + "Could not update interface" + ) + + api_expects_no_call + result = run_cmd(@cmd + params) + assert_cmd(expected_result, result) + end + + it "should print error on missing --compute-resource-id or --compute-resource." do + params = ['--interface', 'compute_name="eth1"', '--interface-id=1'] + expected_result = usage_error_result( + @cmd, + "At least one of options --compute-profile-id, --compute-profile is required.", + "Could not update interface" + ) + + api_expects_no_call + result = run_cmd(@cmd + params) + assert_cmd(expected_result, result) + end + + it "should update interface" do + params = ['--compute-profile-id=1', '--compute-resource-id=1', '--interface-id=1', '--interface', 'compute_name=eth0'] + + api_expects(:compute_profiles, :show) do |par| + par['id'] == 1 + end.returns(@compute_profile) + + api_expects(:compute_resources, :show) do |par| + par['id'] == 1 + end.returns(@compute_resource) + + api_expects(:compute_attributes, :update) do |par| + par['id'] == 2 + end.returns(@compute_attribute) + result = run_cmd(@cmd + params) + assert_cmd(success_result("Interface was updated.\n"), result) + end + end + + describe "remove-interface" do + before do + @cmd = ["compute-profile", "values", "remove-interface"] + @compute_profile = + { + "id" => 1, + "name" => "profile2", + "compute_attributes" => [{ + "id" => 2, + "compute_resource_id" => 1, + "compute_resource_name" => "bla", + "provider_friendly_name" => "oVirt", + "compute_profile_id" => 1, + "compute_profile_name" => "profile2", + "vm_attrs" => {"interfaces_attributes" => { "1" => {"compute_name"=>"eth0"}}} + }] + } + @compute_attribute = { + "id" => 2, "compute_resource_id" => 1, "compute_resource_name" => "bla", + "provider_friendly_name" => "oVirt", "compute_profile_id" => 1, + "compute_profile_name" => "profile2", "vm_attrs" => {"interfaces_attributes" => {}} + } + + @compute_resource = { + "id" => 1, + "name" => "ovirt_compute_resource", + "provider" => "Ovirt", + "provider_friendly_name" => "oVirt", + } + + end + + it "should print error on missing --compute-profile-id or --compute-profile" do + param = ['--interface-id=1'] + expected_result = usage_error_result( + @cmd, + "At least one of options --compute-profile-id, --compute-profile is required.", + "Could not remove interface" + ) + + api_expects_no_call + result = run_cmd(@cmd + param) + assert_cmd(expected_result, result) + end + + it "should print error on missing --compute-resource-id or --compute-resource" do + params = ['--compute-profile-id=1', '--interface-id=1'] + expected_result = usage_error_result( + @cmd, + "At least one of options --compute-resource-id, --compute-resource is required.", + "Could not remove interface" + ) + + api_expects_no_call + result = run_cmd(@cmd + params) + assert_cmd(expected_result, result) + end + + it "should print error on missing --interface-id" do + params = ['--compute-profile-id=1', '--compute-resource-id=1'] + expected_result = usage_error_result( + @cmd, + "Option '--interface-id' is required.", + "Could not remove interface" + ) + api_expects_no_call + result = run_cmd(@cmd + params) + assert_cmd(expected_result, result) + end + + it "should remove interface" do + params = ['--compute-profile-id=1', '--compute-resource-id=1', '--interface-id=1'] + + api_expects(:compute_profiles, :show) do |par| + par['id'] == 1 + end.returns(@compute_profile) + + api_expects(:compute_resources, :show) do |par| + par['id'] == 1 + end.returns(@compute_resource) + + api_expects(:compute_attributes, :update) do |par| + par['id'] == 2 + end.returns(@compute_attribute) + result = run_cmd(@cmd + params) + assert_cmd(success_result("Interface was removed.\n"), result) + end + end +end diff --git a/test/functional/compute_profile_test.rb b/test/functional/compute_profile_test.rb new file mode 100644 index 000000000..db0ea35b4 --- /dev/null +++ b/test/functional/compute_profile_test.rb @@ -0,0 +1,99 @@ +require File.join(File.dirname(__FILE__), 'test_helper') +describe "parameters" do + + describe "create compute profile" do + before do + @cmd = ["compute-profile", "create"] + end + it 'should create a compute-profile' do + params = ['--name=newProfile'] + api_expects(:compute_profiles, :create, 'Create Compute Profile') do |params| + (params['compute_profile']['name'] == 'newProfile') + end + result = run_cmd(@cmd + params) + assert_cmd(success_result("Compute profile created.\n"), result) + end + end + + describe "update compute profile" do + before do + @cmd = ["compute-profile","update"] + @compute_profile = + { + "id": 1, + "name": "profile2", + "compute_attributes": [{ + "id": 2, + "compute_resource_id": 3, + "compute_resource_name": "bla", + "provider_friendly_name": "oVirt", + "compute_profile_id": 1, + "compute_profile_name": "profile2", + }] + } + end + it 'update compute profile name' do + params = ['--id=1' ,'--new-name=profile2'] + api_expects(:compute_profiles, :update, 'Update the compute profile').with_params({"name" =>"profile2"}).returns(@compute_profile) + result = run_cmd(@cmd + params) + assert_cmd(success_result("Compute profile updated.\n"), result) + end + end + + describe "delete compute profile" do + before do + @cmd = ["compute-profile","delete"] + @compute_profile = + { + "id": 3, + "name": "profile3", + "compute_attributes": [{ + "id": 4, + "compute_resource_id": 3, + "compute_resource_name": "bla", + "provider_friendly_name": "oVirt", + "compute_profile_id": 3, + "compute_profile_name": "profile3", + }] + } + end + it 'delete compute profile' do + params = ['--id=3'] + api_expects(:compute_profiles, :destroy, :id => 3).returns({}) + result = run_cmd(@cmd + params) + assert_cmd(success_result("Compute profile deleted.\n"), result) + end + + + it "should print error on missing --name or --id" do + params = [] + + expected_result = usage_error_result( + @cmd, + "At least one of options --name, --id is required.", + "Could not delete the Compute profile" + ) + + api_expects_no_call + result = run_cmd(@cmd + params) + assert_cmd(expected_result, result) + end + + it "should print error when the compute profile doesn't exist" do + params = ['--id=200'] + + expected_result = not_found_error_result( + @cmd, + "Resource compute_profile not found by id '200'", + "Could not delete the Compute profile" + ) + expected_message = "{ \"error\": {\"message\": \"Error: Resource compute_profile not found by id '200'\" }}" + response = HammerCLIForeman.foreman_api.api.send(:create_fake_response, 404, + expected_message, "GET", "http://example.com/", {}) + api_expects(:compute_profiles, :destroy, :id => 3).raises(RestClient::NotFound, response) + result = run_cmd(@cmd + params) + assert_cmd(expected_result, result) + end + end + +end