Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BZ #20538 - add compute profile commands #398

Merged
merged 6 commits into from Feb 26, 2019
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions lib/hammer_cli_foreman.rb
Expand Up @@ -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'
)
Expand Down
303 changes: 303 additions & 0 deletions lib/hammer_cli_foreman/attribute.rb
@@ -0,0 +1,303 @@
require 'hammer_cli_foreman/image'
require '../hammer-cli-foreman/lib/hammer_cli_foreman/compute_resource/register_compute_resources'
require '../hammer-cli-foreman/lib/hammer_cli_foreman/compute_resource/help_utils'
mbacovsky marked this conversation as resolved.
Show resolved Hide resolved

module HammerCLIForeman
class Attribute < HammerCLIForeman::Command
mbacovsky marked this conversation as resolved.
Show resolved Hide resolved
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
mbacovsky marked this conversation as resolved.
Show resolved Hide resolved
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.')
mbacovsky marked this conversation as resolved.
Show resolved Hide resolved

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'),
mbacovsky marked this conversation as resolved.
Show resolved Hide resolved
:format => HammerCLI::Options::Normalizers::KeyValueList.new, :multivalued => true
option '--volume', 'VOLUME', _('Volume parameters, should be comma separated list of values'),
mbacovsky marked this conversation as resolved.
Show resolved Hide resolved
: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
params['compute_attribute']['vm_attrs'] = option_compute_attributes || {}
params['compute_attribute']['vm_attrs']['interfaces_attributes']= HammerCLIForeman::Attribute.attribute_hash(option_interface_list) unless option_interface_list.empty?
params['compute_attribute']['vm_attrs']['volumes_attributes'] = HammerCLIForeman::Attribute.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::Attribute.get_params(options)
mbacovsky marked this conversation as resolved.
Show resolved Hide resolved
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_attributes'] || {}

if options['option_compute_attributes']
vm_attrs = options['option_compute_attributes']
vm_attrs['volumes_attributes'] ||= original_volumes
vm_attrs['interfaces_attributes'] ||= original_interfaces
end
vm_attrs['interfaces_attributes'] = HammerCLIForeman::Attribute.attribute_hash(options['option_interface_list']) unless options['option_interface_list'].empty?
vm_attrs['volumes_attributes'] = HammerCLIForeman::Attribute.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'
mbacovsky marked this conversation as resolved.
Show resolved Hide resolved
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::Attribute.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')
interface_attr = params['compute_attribute']['vm_attrs']['interfaces_attributes'] || {}
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_attributes'] ||= {}
params['compute_attribute']['vm_attrs']['interfaces_attributes'][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::Attribute.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']['interfaces_attributes'] ||= {}
params['compute_attribute']['vm_attrs']['interfaces_attributes'][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::Attribute.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']['interfaces_attributes'].has_key?(option_interface_id.to_s)
params['compute_attribute']['vm_attrs']['interfaces_attributes'].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::Attribute.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::Attribute.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::Attribute.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
61 changes: 61 additions & 0 deletions 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::Attribute', 'hammer_cli_foreman/attribute'
)

autoload_subcommands
end
end
10 changes: 10 additions & 0 deletions lib/hammer_cli_foreman/compute_resource/base.rb
@@ -0,0 +1,10 @@
module HammerCLIForeman
module ComputeResources
class Base
attr_reader :name
def compute_attributes; []; end
def interface_attributes; []; end
def volume_attributes; []; end
end
end
end