From b31e64d49df4f9cf1e16179354bf7916f19b0b3b Mon Sep 17 00:00:00 2001 From: Boris Schrijver Date: Wed, 16 Sep 2015 22:19:14 +0200 Subject: [PATCH] Refactored checkIfZoneIsDeletable(). It now makes use of various DAO DB function instead of running a self build query. --- .../ConfigurationManagerImpl.java | 137 ++++++------------ 1 file changed, 41 insertions(+), 96 deletions(-) diff --git a/server/src/com/cloud/configuration/ConfigurationManagerImpl.java b/server/src/com/cloud/configuration/ConfigurationManagerImpl.java index 462d6e9e959..69e70e6cd9e 100644 --- a/server/src/com/cloud/configuration/ConfigurationManagerImpl.java +++ b/server/src/com/cloud/configuration/ConfigurationManagerImpl.java @@ -19,8 +19,6 @@ package com.cloud.configuration; import java.net.URI; import java.sql.Date; import java.sql.PreparedStatement; -import java.sql.ResultSet; -import java.sql.SQLException; import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; @@ -236,6 +234,8 @@ public class ConfigurationManagerImpl extends ManagerBase implements Configurati VolumeDao _volumeDao; @Inject VMInstanceDao _vmInstanceDao; + //@Inject + //VmwareDatacenterZoneMapDao _vmwareDatacenterZoneMapDao; @Inject AccountVlanMapDao _accountVlanMapDao; @Inject @@ -894,7 +894,7 @@ public class ConfigurationManagerImpl extends ManagerBase implements Configurati // Check if there are allocated private IP addresses in the pod if (_privateIpAddressDao.countIPs(podId, pod.getDataCenterId(), true) != 0) { - throw new CloudRuntimeException(errorMsg + "there are private IP addresses allocated in this pod"); + throw new CloudRuntimeException(errorMsg + "there are private IP addresses allocated in this pod."); } // Check if there are any non-removed volumes in the pod. @@ -1296,104 +1296,49 @@ public class ConfigurationManagerImpl extends ManagerBase implements Configurati @DB protected void checkIfZoneIsDeletable(final long zoneId) { - final List> tablesToCheck = new ArrayList>(); + final String errorMsg = "The zone cannot be deleted because "; - final List host = new ArrayList(); - host.add(0, "host"); - host.add(1, "data_center_id"); - host.add(2, "there are servers running in this zone"); - tablesToCheck.add(host); - final List hostPodRef = new ArrayList(); - hostPodRef.add(0, "host_pod_ref"); - hostPodRef.add(1, "data_center_id"); - hostPodRef.add(2, "there are pods in this zone"); - tablesToCheck.add(hostPodRef); - final List privateIP = new ArrayList(); - privateIP.add(0, "op_dc_ip_address_alloc"); - privateIP.add(1, "data_center_id"); - privateIP.add(2, "there are private IP addresses allocated for this zone"); - tablesToCheck.add(privateIP); - - final List publicIP = new ArrayList(); - publicIP.add(0, "user_ip_address"); - publicIP.add(1, "data_center_id"); - publicIP.add(2, "there are public IP addresses allocated for this zone"); - tablesToCheck.add(publicIP); - - final List vmInstance = new ArrayList(); - vmInstance.add(0, "vm_instance"); - vmInstance.add(1, "data_center_id"); - vmInstance.add(2, "there are virtual machines running in this zone"); - tablesToCheck.add(vmInstance); - - final List volumes = new ArrayList(); - volumes.add(0, "volumes"); - volumes.add(1, "data_center_id"); - volumes.add(2, "there are storage volumes for this zone"); - tablesToCheck.add(volumes); - - final List physicalNetworks = new ArrayList(); - physicalNetworks.add(0, "physical_network"); - physicalNetworks.add(1, "data_center_id"); - physicalNetworks.add(2, "there are physical networks in this zone"); - tablesToCheck.add(physicalNetworks); - - final List vmwareDcs = new ArrayList(); - vmwareDcs.add(0, "vmware_data_center_zone_map"); - vmwareDcs.add(1, "zone_id"); - vmwareDcs.add(2, "there are VMware datacenters associated with this zone. Remove VMware DC from this zone."); - tablesToCheck.add(vmwareDcs); - - for (final List table : tablesToCheck) { - final String tableName = table.get(0); - final String column = table.get(1); - final String errorMsg = table.get(2); - - final String dbName = "cloud"; - - String selectSql = "SELECT * FROM `?`.`?` WHERE ? = ?"; - - if (tableName.equals("op_dc_vnet_alloc")) { - selectSql += " AND taken IS NOT NULL"; - } - - if (tableName.equals("user_ip_address")) { - selectSql += " AND state!='Free'"; - } - - if (tableName.equals("op_dc_ip_address_alloc")) { - selectSql += " AND taken IS NOT NULL"; - } - - if (tableName.equals("host_pod_ref") || tableName.equals("host") || tableName.equals("volumes") || tableName.equals("physical_network")) { - selectSql += " AND removed is NULL"; - } - - if (tableName.equals("vm_instance")) { - selectSql += " AND state != ? AND removed IS NULL"; - } - - final TransactionLegacy txn = TransactionLegacy.currentTxn(); - try { - final PreparedStatement stmt = txn.prepareAutoCloseStatement(selectSql); - stmt.setString(1,dbName); - stmt.setString(2,tableName); - stmt.setString(3,column); - stmt.setLong(4, zoneId); - if (tableName.equals("vm_instance")) { - stmt.setString(5, VirtualMachine.State.Expunging.toString()); - } - final ResultSet rs = stmt.executeQuery(); - if (rs != null && rs.next()) { - throw new CloudRuntimeException("The zone is not deletable because " + errorMsg); - } - } catch (final SQLException ex) { - throw new CloudRuntimeException("The Management Server failed to detect if zone is deletable. Please contact Cloud Support."); - } + // Check if there are any non-removed hosts in the zone. + if (!_hostDao.listByDataCenterId(zoneId).isEmpty()) { + throw new CloudRuntimeException(errorMsg + "there are servers in this zone."); } + // Check if there are any non-removed pods in the zone. + if (!_podDao.listByDataCenterId(zoneId).isEmpty()) { + throw new CloudRuntimeException(errorMsg + "there are pods in this zone."); + } + + // Check if there are allocated private IP addresses in the zone. + if (_privateIpAddressDao.countIPs(zoneId, true) != 0) { + throw new CloudRuntimeException(errorMsg + "there are private IP addresses allocated in this zone."); + } + + // Check if there are allocated public IP addresses in the zone. + if (_publicIpAddressDao.countIPs(zoneId, true) != 0) { + throw new CloudRuntimeException(errorMsg + "there are public IP addresses allocated in this zone."); + } + + // Check if there are any non-removed vms in the zone. + if (!_vmInstanceDao.listByZoneId(zoneId).isEmpty()) { + throw new CloudRuntimeException(errorMsg + "there are virtual machines in this zone."); + } + + // Check if there are any non-removed volumes in the zone. + if (!_volumeDao.findByDc(zoneId).isEmpty()) { + throw new CloudRuntimeException(errorMsg + "there are storage volumes in this zone."); + } + + // Check if there are any non-removed physical networks in the zone. + if (!_physicalNetworkDao.listByZone(zoneId).isEmpty()) { + throw new CloudRuntimeException(errorMsg + "there are physical networks in this zone."); + } + + // Check if there are any non-removed VMware datacenters in the zone. + //if (_vmwareDatacenterZoneMapDao.findByZoneId(zoneId) != null) { + // throw new CloudRuntimeException(errorMsg + "there are VMware datacenters in this zone."); + //} } private void checkZoneParameters(final String zoneName, final String dns1, final String dns2, final String internalDns1, final String internalDns2, final boolean checkForDuplicates, final Long domainId,