From 26ab2ac98c5e10ea0c0b498183a82c1fd7f9b8c5 Mon Sep 17 00:00:00 2001 From: Marek Aufart Date: Fri, 14 Dec 2018 15:19:33 +0100 Subject: [PATCH] Merge pull request #409 from mansam/exclude-attached-instances-from-volume-attachment-form Exclude already attached VMs from the volume attachment form (cherry picked from commit 66f9fd169cc73519fdfc5bd735cc1f05025706a3) Fixes https://bugzilla.redhat.com/show_bug.cgi?id=1672695 --- .../providers/openstack/cloud_manager/cloud_volume.rb | 2 +- .../providers/openstack/cloud_manager/cloud_volume_spec.rb | 6 ++++++ 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/app/models/manageiq/providers/openstack/cloud_manager/cloud_volume.rb b/app/models/manageiq/providers/openstack/cloud_manager/cloud_volume.rb index 95981a2e4..1dad2a84a 100644 --- a/app/models/manageiq/providers/openstack/cloud_manager/cloud_volume.rb +++ b/app/models/manageiq/providers/openstack/cloud_manager/cloud_volume.rb @@ -137,7 +137,7 @@ def create_volume_snapshot_queue(userid, options) end def available_vms - cloud_tenant.vms + cloud_tenant.vms.where.not(:id => vms.select(&:id)) end def provider_object(connection) diff --git a/spec/models/manageiq/providers/openstack/cloud_manager/cloud_volume_spec.rb b/spec/models/manageiq/providers/openstack/cloud_manager/cloud_volume_spec.rb index a5dedfc13..37362cc3b 100644 --- a/spec/models/manageiq/providers/openstack/cloud_manager/cloud_volume_spec.rb +++ b/spec/models/manageiq/providers/openstack/cloud_manager/cloud_volume_spec.rb @@ -144,5 +144,11 @@ it "supports attachment to only those instances that are in the same tenant" do expect(cloud_volume.available_vms).to contain_exactly(first_instance, second_instance) end + + it "should exclude instances that are already attached to the volume" do + attached_instance = FactoryGirl.create(:vm_openstack, :ext_management_system => ems, :ems_ref => "attached_instance", :cloud_tenant => tenant) + allow(cloud_volume).to receive(:vms).and_return([attached_instance]) + expect(cloud_volume.available_vms).to contain_exactly(first_instance, second_instance) + end end end