Skip to content

Commit

Permalink
Fixes #24072 - Fix free IP auto suggest for hostgroup with compute r…
Browse files Browse the repository at this point in the history
…esource

* Refs #24072 - fix selecting hostgroup with compute resource

Without this fix, the 'Virtual Machine' tab was giving:

   'mybox' not found on 'libvirt (Libvirt)' 'mybox' could
   be deleted or 'libvirt (Libvirt)' is not responding.

(cherry picked from commit 641ef42)

* Refs #24072 - fix compute profile with CR set via HG

When compute resource is set via compute profile, we're not sending it
via the form (as it's disabled). Therefore, we need to search for in via
hostgroup.

(cherry picked from commit cbe40e3)

* Fixes #24072 - freeip with compute resource via hostgroup

In #5790, we added an option to set the compute resource via hostgroup.
There was one thing missed though: the freeip not getting assigned
automagically, when the compute resource was selected this way.

After this change, we make sure to check for the ip addresses (and other
potential subnet related refreshes) after the hostgroup is selected, in
case the interfaces were populated during the hostgroup change.

(cherry picked from commit dc31392)
  • Loading branch information
xprazak2 authored and dLobatog committed Jul 3, 2018
1 parent 7756ca5 commit dfb0a3f
Show file tree
Hide file tree
Showing 6 changed files with 78 additions and 20 deletions.
14 changes: 10 additions & 4 deletions app/assets/javascripts/host_edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,14 @@ function update_nics(success_callback) {
}

var nic_update_handler = function() {
update_nics(function() {
interface_subnet_selected(primary_nic_form().find('select.interface_subnet'), 'ip');
interface_subnet_selected(primary_nic_form().find('select.interface_subnet6'), 'ip6');
});
update_nics(updatePrimarySubnetIPs);
};

function updatePrimarySubnetIPs() {
interface_subnet_selected(primary_nic_form().find('select.interface_subnet'), 'ip');
interface_subnet_selected(primary_nic_form().find('select.interface_subnet6'), 'ip6');
}

function computeResourceSelected(item){
providerSpecificNICInfo = null;
var compute = $(item).val();
Expand Down Expand Up @@ -316,6 +318,10 @@ function update_form(element, options) {
// to handle case if def process_taxonomy changed compute_resource_id to nil
if (!host_compute_resource_id.val()) {
host_compute_resource_id.change();
} else {
// in case the compute resource was selected, we still want to check for
// free ip if applicable
updatePrimarySubnetIPs();
}
update_capabilities(host_compute_resource_id.val() ? $('#capabilities').val() : $('#bare_metal_capabilities').val());
}
Expand Down
10 changes: 7 additions & 3 deletions app/controllers/hosts_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -146,10 +146,13 @@ def random_name
end

def compute_resource_selected
return not_found unless (params[:host] && (id=params[:host][:compute_resource_id]))
return not_found unless params[:host]
Taxonomy.as_taxonomy @organization, @location do
compute_profile_id = params[:host][:compute_profile_id] || Hostgroup.find_by_id(params[:host][:hostgroup_id]).try(:inherited_compute_profile_id)
compute_resource = ComputeResource.authorized(:view_compute_resources).find_by_id(id)
hostgroup = Hostgroup.find_by_id(params[:host][:hostgroup_id])
compute_resource_id = params[:host][:compute_resource_id] || hostgroup.try(:inherited_compute_resource_id)
return not_found if compute_resource_id.blank?
compute_profile_id = params[:host][:compute_profile_id] || hostgroup.try(:inherited_compute_profile_id)
compute_resource = ComputeResource.authorized(:view_compute_resources).find_by_id(compute_resource_id)
render :partial => "compute", :locals => { :compute_resource => compute_resource,
:vm_attrs => compute_resource.compute_profile_attributes_for(compute_profile_id) }
end
Expand Down Expand Up @@ -605,6 +608,7 @@ def process_hostgroup

@host.set_hostgroup_defaults true
@host.set_compute_attributes unless params[:host][:compute_profile_id]
@host.apply_compute_profile(InterfaceMerge.new) if @host.compute_profile_id
set_class_variables(@host)
render :partial => "form"
end
Expand Down
7 changes: 5 additions & 2 deletions app/models/host/managed.rb
Original file line number Diff line number Diff line change
Expand Up @@ -579,8 +579,11 @@ def inherited_attributes
end

def set_compute_attributes
return unless compute_profile_present?
self.compute_attributes = compute_resource.compute_profile_attributes_for(compute_profile_id)
if compute_profile_present?
self.compute_attributes = compute_resource.compute_profile_attributes_for(compute_profile_id)
elsif compute_resource
self.compute_attributes ||= {}
end
end

def set_ip_address
Expand Down
4 changes: 4 additions & 0 deletions test/factories/host_related.rb
Original file line number Diff line number Diff line change
Expand Up @@ -467,6 +467,10 @@ def set_nic_attributes(host, attributes, evaluator)
puppetclasses { [ FactoryBot.create(:puppetclass, :environments => [environment]) ] }
end

trait :with_compute_resource do
compute_resource { FactoryBot.create(:compute_resource, :libvirt) }
end

trait :with_config_group do
environment
config_groups { [ FactoryBot.create(:config_group, :with_puppetclass, :class_environments => [environment]) ] }
Expand Down
55 changes: 44 additions & 11 deletions test/integration/host_js_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,13 @@ class HostJSTest < IntegrationTestWithJavascript
SETTINGS[:locations_enabled] = false
SETTINGS[:organizations_enabled] = false
as_admin { @host = FactoryBot.create(:host, :with_puppet, :managed) }
Fog.mock!
end

after do
SETTINGS[:locations_enabled] = true
SETTINGS[:organizations_enabled] = true
Fog.unmock!
end

describe 'multiple hosts selection' do
Expand Down Expand Up @@ -178,6 +180,46 @@ class HostJSTest < IntegrationTestWithJavascript
assert_equal overridden_hostgroup.environment.name, environment
end

test 'choosing a hostgroup with compute resource works' do
hostgroup = FactoryBot.create(:hostgroup, :with_environment, :with_subnet, :with_domain, :with_compute_resource)
hostgroup.subnet.update!(ipam: IPAM::MODES[:db])
compute_profile = FactoryBot.create(:compute_profile, :with_compute_attribute, :compute_resource => hostgroup.compute_resource)
compute_attributes = compute_profile.compute_attributes.where(:compute_resource_id => hostgroup.compute_resource.id).first
compute_attributes.vm_attrs['nics_attributes'] = {'0' => {'type' => 'bridge', 'bridge' => 'test'}}
compute_attributes.vm_attrs['cpus'] = '2'
compute_attributes.save

require 'fog/libvirt/models/compute/node'
Foreman::Model::Libvirt.any_instance.stubs(:hypervisor).returns(Fog::Compute::Libvirt::Node.new(:cpus => 4))

visit new_host_path
select2(hostgroup.name, :from => 'host_hostgroup_id')
wait_for_ajax
click_link('Virtual Machine')
cpus_field = page.find_field('host_compute_attributes_cpus')
assert_equal '1', cpus_field.value

click_link('Interfaces')
click_button('Edit')
ipv4_field = page.find_field('host_interfaces_attributes_0_ip')
refute_empty ipv4_field.value
close_interfaces_modal

find(:css, '#host_tab').click
click_on_inherit('compute_profile')
select2(compute_profile.name, :from => 'host_compute_profile_id')
wait_for_ajax

click_link('Virtual Machine')
cpus_field = page.find_field('host_compute_attributes_cpus')
assert_equal '2', cpus_field.value

click_link('Interfaces')
click_button('Edit')
bridge_field = page.find_field('host_interfaces_attributes_0_compute_attributes_bridge')
assert_equal 'test', bridge_field.value
end

test 'saves correct values for inherited fields without hostgroup' do
env = FactoryBot.create(:environment)
os = FactoryBot.create(:ubuntu14_10, :with_associations)
Expand All @@ -204,11 +246,7 @@ class HostJSTest < IntegrationTestWithJavascript
fill_in 'host_interfaces_attributes_0_mac', :with => '00:11:11:11:11:11'
wait_for_ajax
fill_in 'host_interfaces_attributes_0_ip', :with => '1.1.1.1'
click_button 'Ok' #close interfaces
#wait for the dialog to close
Timeout.timeout(Capybara.default_max_wait_time) do
loop while find(:css, '#interfaceModal', :visible => false).visible?
end
close_interfaces_modal
click_on_submit
find('#host-show') #wait for host details page

Expand Down Expand Up @@ -248,12 +286,7 @@ class HostJSTest < IntegrationTestWithJavascript
wait_for_ajax
fill_in 'host_interfaces_attributes_0_ip', :with => '2.3.4.44'
wait_for_ajax
click_button 'Ok'

#wait for the dialog to close
Timeout.timeout(Capybara.default_max_wait_time) do
loop while find(:css, '#interfaceModal', :visible => false).visible?
end
close_interfaces_modal

wait_for_ajax
click_on_submit
Expand Down
8 changes: 8 additions & 0 deletions test/integration_test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,14 @@ def wait_for_ajax
end
end

def close_interfaces_modal
click_button 'Ok' # close interfaces
# wait for the dialog to close
Timeout.timeout(Capybara.default_max_wait_time) do
loop while find(:css, '#interfaceModal', :visible => false).visible?
end
end

def login_user(username, password)
logout_admin
visit "/"
Expand Down

0 comments on commit dfb0a3f

Please sign in to comment.