From 162577f44fa1b81076f83ae4c6e708cb0538f6c6 Mon Sep 17 00:00:00 2001 From: Brandon Dunne Date: Mon, 1 May 2017 10:19:16 -0400 Subject: [PATCH] Merge pull request #14946 from lfu/hosts_with_selected_vlan_1435814 Filter out the hosts with the selected network. (cherry picked from commit acf768135451b7dc174fe2586b594beedc293a47) https://bugzilla.redhat.com/show_bug.cgi?id=1458363 --- app/models/miq_provision_virt_workflow.rb | 9 ++++++ .../miq_provision_virt_workflow_spec.rb | 31 +++++++++++++++++++ 2 files changed, 40 insertions(+) diff --git a/app/models/miq_provision_virt_workflow.rb b/app/models/miq_provision_virt_workflow.rb index 8f614eb2318..cf12a426685 100644 --- a/app/models/miq_provision_virt_workflow.rb +++ b/app/models/miq_provision_virt_workflow.rb @@ -185,6 +185,15 @@ def update_field_read_only(options = {}) fields(:hardware) { |fn, f, _dn, _d| f[:read_only] = true if options[:read_only_fields].include?(fn) } end + def allowed_hosts_obj(options = {}) + all_hosts = super + vlan_name = get_value(@values[:vlan]) + return all_hosts unless vlan_name + + _log.info "Filtering hosts with the following network: <#{vlan_name}>" + all_hosts.reject { |h| !h.lans.pluck(:name).include?(vlan_name) } + end + # # Methods for populating lists of allowed values for a field # => Input - A hash containing options specific to the called method diff --git a/spec/models/miq_provision_virt_workflow_spec.rb b/spec/models/miq_provision_virt_workflow_spec.rb index b4297ccb5c1..853d80ab8c4 100644 --- a/spec/models/miq_provision_virt_workflow_spec.rb +++ b/spec/models/miq_provision_virt_workflow_spec.rb @@ -100,6 +100,37 @@ end end + context '#allowed_hosts_obj' do + before do + @ems = FactoryGirl.create(:ems_vmware) + @host1 = FactoryGirl.create(:host_vmware, :ems_id => @ems.id) + @host2 = FactoryGirl.create(:host_vmware, :ems_id => @ems.id) + @src_vm = FactoryGirl.create(:vm_vmware, :ems_id => @ems.id) + allow(workflow).to receive(:find_all_ems_of_type).and_return([@host1, @host2]) + allow(Rbac).to receive(:search) do |hash| + [Array.wrap(hash[:targets])] + end + workflow.instance_variable_set(:@target_resource, nil) + + s1 = FactoryGirl.create(:switch, :name => "A") + s2 = FactoryGirl.create(:switch, :name => "B") + @host1.switches = [s1] + @host2.switches = [s2] + @lan1 = FactoryGirl.create(:lan, :name => "lan_A", :switch_id => s1.id) + @lan2 = FactoryGirl.create(:lan, :name => "lan_B", :switch_id => s2.id) + end + + it 'finds all hosts with no selected network' do + workflow.instance_variable_set(:@values, :src_vm_id => @src_vm.id) + expect(workflow.allowed_hosts_obj).to match_array([@host1, @host2]) + end + + it 'finds only the hosts that can access the selected network' do + workflow.instance_variable_set(:@values, :src_vm_id => @src_vm.id, :vlan => [@lan1.name, @lan1.name]) + expect(workflow.allowed_hosts_obj).to match_array([@host1]) + end + end + context "#update_requester_from_parameters" do let(:user_new) { FactoryGirl.create(:user_with_email) } let(:data_new_user) { {:user_name => user_new.name} }