3434
3535import javax .inject .Inject ;
3636
37- import com .cloud .projects .Project ;
38- import com .cloud .projects .ProjectManager ;
39- import org .apache .cloudstack .api .command .user .volume .AssignVolumeCmd ;
37+ import org .apache .cloudstack .api .ApiConstants .IoDriverPolicy ;
4038import org .apache .cloudstack .api .ApiErrorCode ;
4139import org .apache .cloudstack .api .ServerApiException ;
42- import org .apache .cloudstack .api .ApiConstants . IoDriverPolicy ;
40+ import org .apache .cloudstack .api .command . user . volume . AssignVolumeCmd ;
4341import org .apache .cloudstack .api .command .user .volume .AttachVolumeCmd ;
4442import org .apache .cloudstack .api .command .user .volume .ChangeOfferingForVolumeCmd ;
4543import org .apache .cloudstack .api .command .user .volume .CreateVolumeCmd ;
104102import org .apache .cloudstack .utils .volume .VirtualMachineDiskInfo ;
105103import org .apache .commons .collections .CollectionUtils ;
106104import org .apache .commons .collections .MapUtils ;
107- import org .apache .commons .lang3 .ObjectUtils ;
108105import org .apache .commons .lang3 .BooleanUtils ;
106+ import org .apache .commons .lang3 .ObjectUtils ;
109107import org .apache .commons .lang3 .StringUtils ;
110108import org .apache .log4j .Logger ;
111109import org .jetbrains .annotations .NotNull ;
148146import com .cloud .hypervisor .dao .HypervisorCapabilitiesDao ;
149147import com .cloud .offering .DiskOffering ;
150148import com .cloud .org .Grouping ;
149+ import com .cloud .projects .Project ;
150+ import com .cloud .projects .ProjectManager ;
151151import com .cloud .resource .ResourceState ;
152152import com .cloud .serializer .GsonHelper ;
153153import com .cloud .server .ManagementService ;
@@ -2778,31 +2778,10 @@ private Volume orchestrateDetachVolumeFromVM(long vmId, long volumeId) {
27782778 String errorMsg = "Failed to detach volume " + volume .getName () + " from VM " + vm .getHostName ();
27792779 boolean sendCommand = vm .getState () == State .Running ;
27802780
2781- Long hostId = vm .getHostId ();
2782-
2783- if (hostId == null ) {
2784- hostId = vm .getLastHostId ();
2785- HostVO host = _hostDao .findById (hostId );
2786-
2787- if (host != null && host .getHypervisorType () == HypervisorType .VMware ) {
2788- sendCommand = true ;
2789- }
2790- }
2791-
2792- HostVO host = null ;
27932781 StoragePoolVO volumePool = _storagePoolDao .findByIdIncludingRemoved (volume .getPoolId ());
2794-
2795- if (hostId != null ) {
2796- host = _hostDao .findById (hostId );
2797-
2798- if (host != null && host .getHypervisorType () == HypervisorType .XenServer && volumePool != null && volumePool .isManaged ()) {
2799- sendCommand = true ;
2800- }
2801- }
2802-
2803- if (volumePool == null ) {
2804- sendCommand = false ;
2805- }
2782+ HostVO host = getHostForVmVolumeAttachDetach (vm , volumePool );
2783+ Long hostId = host != null ? host .getId () : null ;
2784+ sendCommand = sendCommand || isSendCommandForVmVolumeAttachDetach (host , volumePool );
28062785
28072786 Answer answer = null ;
28082787
@@ -4080,15 +4059,15 @@ private String getNameOfClusteredFileSystem(HostVO hostVO) {
40804059 return "clustered file systems" ;
40814060 }
40824061
4083- private HostVO getHostForVmVolumeAttach ( UserVmVO vm , StoragePoolVO volumeToAttachStoragePool ) {
4062+ private HostVO getHostForVmVolumeAttachDetach ( VirtualMachine vm , StoragePoolVO volumeStoragePool ) {
40844063 HostVO host = null ;
40854064 Pair <Long , Long > clusterAndHostId = virtualMachineManager .findClusterAndHostIdForVm (vm .getId ());
40864065 Long hostId = clusterAndHostId .second ();
40874066 Long clusterId = clusterAndHostId .first ();
40884067 if (hostId == null && clusterId != null &&
40894068 State .Stopped .equals (vm .getState ()) &&
4090- volumeToAttachStoragePool != null &&
4091- !ScopeType .HOST .equals (volumeToAttachStoragePool .getScope ())) {
4069+ volumeStoragePool != null &&
4070+ !ScopeType .HOST .equals (volumeStoragePool .getScope ())) {
40924071 List <HostVO > hosts = _hostDao .findHypervisorHostInCluster (clusterId );
40934072 if (!hosts .isEmpty ()) {
40944073 host = hosts .get (0 );
@@ -4100,6 +4079,18 @@ private HostVO getHostForVmVolumeAttach(UserVmVO vm, StoragePoolVO volumeToAttac
41004079 return host ;
41014080 }
41024081
4082+ protected boolean isSendCommandForVmVolumeAttachDetach (HostVO host , StoragePoolVO volumeStoragePool ) {
4083+ if (host == null || volumeStoragePool == null ) {
4084+ return false ;
4085+ }
4086+ boolean sendCommand = HypervisorType .VMware .equals (host .getHypervisorType ());
4087+ if (HypervisorType .XenServer .equals (host .getHypervisorType ()) &&
4088+ volumeStoragePool .isManaged ()) {
4089+ sendCommand = true ;
4090+ }
4091+ return sendCommand ;
4092+ }
4093+
41034094 private VolumeVO sendAttachVolumeCommand (UserVmVO vm , VolumeVO volumeToAttach , Long deviceId ) {
41044095 String errorMsg = "Failed to attach volume " + volumeToAttach .getName () + " to VM " + vm .getHostName ();
41054096 boolean sendCommand = vm .getState () == State .Running ;
@@ -4108,16 +4099,9 @@ private VolumeVO sendAttachVolumeCommand(UserVmVO vm, VolumeVO volumeToAttach, L
41084099 if (s_logger .isTraceEnabled () && volumeToAttachStoragePool != null ) {
41094100 s_logger .trace (String .format ("storage is gotten from volume to attach: %s/%s" ,volumeToAttachStoragePool .getName (),volumeToAttachStoragePool .getUuid ()));
41104101 }
4111- HostVO host = getHostForVmVolumeAttach (vm , volumeToAttachStoragePool );
4112- Long hostId = host == null ? null : host .getId ();
4113- if (host != null && host .getHypervisorType () == HypervisorType .VMware ) {
4114- sendCommand = true ;
4115- }
4116-
4117- if (host != null && host .getHypervisorType () == HypervisorType .XenServer &&
4118- volumeToAttachStoragePool != null && volumeToAttachStoragePool .isManaged ()) {
4119- sendCommand = true ;
4120- }
4102+ HostVO host = getHostForVmVolumeAttachDetach (vm , volumeToAttachStoragePool );
4103+ Long hostId = host != null ? host .getId () : null ;
4104+ sendCommand = sendCommand || isSendCommandForVmVolumeAttachDetach (host , volumeToAttachStoragePool );
41214105
41224106 if (host != null ) {
41234107 _hostDao .loadDetails (host );
0 commit comments