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 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 35 additions & 25 deletions lib/hammer_cli_foreman/compute_attribute.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,15 +42,12 @@ class Create < HammerCLIForeman::CreateCommand

def request_params
params = super
compute_resource_name = HammerCLIForeman.record_to_common_format(
HammerCLIForeman.foreman_resource(:compute_resources).call(:show, 'id' => option_compute_resource_id )
)["name"]
interfaces_attrs_name_list = {"libvirt" => "nics_attributes","gce" => "network_interfaces_nics_attributes"}
interfaces_attrs_name = interfaces_attrs_name_list.key?(compute_resource_name) ? interfaces_attrs_name_list[compute_resource_name] : "interfaces_attributes"
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]= 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['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

Expand Down Expand Up @@ -81,20 +78,23 @@ class Update < HammerCLIForeman::UpdateCommand

def request_params

params = HammerCLIForeman::Attribute.get_params(options)
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_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_attributes'] ||= original_interfaces
vm_attrs[interfaces_attrs_name_list[compute_resource_name]] ||= 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?
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

Expand Down Expand Up @@ -123,14 +123,20 @@ def validate_options
end

def request_params
params = HammerCLIForeman::Attribute.get_params(options)
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')
interface_attr = params['compute_attribute']['vm_attrs']['interfaces_attributes'] || {}
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_attributes'] ||= {}
params['compute_attribute']['vm_attrs']['interfaces_attributes'][new_interface_id] = option_interface

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

Expand Down Expand Up @@ -163,11 +169,13 @@ def validate_options
end

def request_params
params = HammerCLIForeman::Attribute.get_params(options)
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_attributes'] ||= {}
params['compute_attribute']['vm_attrs']['interfaces_attributes'][option_interface_id] = option_interface
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.')
Expand All @@ -189,11 +197,13 @@ def validate_options
end

def request_params
params = HammerCLIForeman::Attribute.get_params(options)
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_attributes'].has_key?(option_interface_id.to_s)
params['compute_attribute']['vm_attrs']['interfaces_attributes'].delete(option_interface_id.to_s)
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
Expand Down Expand Up @@ -223,7 +233,7 @@ def validate_options
end

def request_params
params = HammerCLIForeman::Attribute.get_params(options)
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?
Expand Down Expand Up @@ -262,7 +272,7 @@ def validate_options
end

def request_params
params = HammerCLIForeman::Attribute.get_params(options)
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'] ||= {}
Expand All @@ -289,7 +299,7 @@ def validate_options
end

def request_params
params = HammerCLIForeman::Attribute.get_params(options)
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)
Expand Down
1 change: 1 addition & 0 deletions lib/hammer_cli_foreman/compute_resource/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ class Base
def compute_attributes; []; end
def interface_attributes; []; end
def volume_attributes; []; end
def interfaces_attrs_name; "interfaces_attributes" ; end
end
end
end
2 changes: 1 addition & 1 deletion lib/hammer_cli_foreman/compute_resource/ec2.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,6 @@ def compute_attributes
]
end
end
HammerCLIForeman.register_compute_resource('ec2', EC2.new)
HammerCLIForeman.register_compute_resource('EC2', EC2.new )
end
end
6 changes: 5 additions & 1 deletion lib/hammer_cli_foreman/compute_resource/gce.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,11 @@ def compute_attributes
'external_ip'
]
end

def interfaces_attrs_name
"network_interfaces_nics_attributes"
end
end
HammerCLIForeman.register_compute_resource('gce', GCE.new)
HammerCLIForeman.register_compute_resource('GCE', GCE.new)
end
end
2 changes: 1 addition & 1 deletion lib/hammer_cli_foreman/compute_resource/help_utils.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module HammerCLIForeman
module ComputeResources
def self.extend_help(h, attributes, add_host_specific_attrs = false )
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|
Expand Down
8 changes: 6 additions & 2 deletions lib/hammer_cli_foreman/compute_resource/libvirt.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,18 @@ def interface_attributes
]
end

def volume_attributes;
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)
HammerCLIForeman.register_compute_resource('Libvirt', Libvirt.new)
end
end
2 changes: 1 addition & 1 deletion lib/hammer_cli_foreman/compute_resource/openstack.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,6 @@ def compute_attributes
]
end
end
HammerCLIForeman.register_compute_resource('openstack', OpenStack.new)
HammerCLIForeman.register_compute_resource('OpenStack', OpenStack.new)
end
end
2 changes: 1 addition & 1 deletion lib/hammer_cli_foreman/compute_resource/ovirt.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,6 @@ def volume_attributes;
]
end
end
HammerCLIForeman.register_compute_resource('ovirt', Ovirt.new)
HammerCLIForeman.register_compute_resource('oVirt', Ovirt.new)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As it is not sometimes very clear how the compute resources are named we should use the names in lowercase as it was originally.

end
end
2 changes: 1 addition & 1 deletion lib/hammer_cli_foreman/compute_resource/rackspace.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,6 @@ def compute_attributes
]
end
end
HammerCLIForeman.register_compute_resource('rackspace', Rackspace.new)
HammerCLIForeman.register_compute_resource('Rackspace', Rackspace.new)
end
end
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
module HammerCLIForeman
@compute_resources = {}
@interfaces_attrs_name_list ={}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What benefit it has to store this duplicate information in here? I'd prefer to not add to top lavel namespace more things then it is necessary.

HammerCLIForeman.compute_resources['ovirt'].interfaces_attrs_name

feels readable enough.

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'
Expand Down
2 changes: 1 addition & 1 deletion lib/hammer_cli_foreman/compute_resource/vmware.rb
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,6 @@ def volume_attributes;
]
end
end
HammerCLIForeman.register_compute_resource('vmware', VMware.new)
HammerCLIForeman.register_compute_resource('VMware', VMware.new)
end
end
45 changes: 45 additions & 0 deletions test/functional/attribute_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,13 @@
"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
Expand Down Expand Up @@ -99,6 +106,10 @@
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)
Expand Down Expand Up @@ -371,6 +382,14 @@
"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
Expand Down Expand Up @@ -449,6 +468,10 @@
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)
Expand All @@ -474,6 +497,13 @@
"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
Expand Down Expand Up @@ -521,6 +551,10 @@
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)
Expand Down Expand Up @@ -552,6 +586,13 @@
"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
Expand Down Expand Up @@ -599,6 +640,10 @@
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)
Expand Down