Refactored checkIfZoneIsDeletable().

It now makes use of various DAO DB function instead of running a self build 
query.


Project: http://git-wip-us.apache.org/repos/asf/cloudstack/repo
Commit: http://git-wip-us.apache.org/repos/asf/cloudstack/commit/b31e64d4
Tree: http://git-wip-us.apache.org/repos/asf/cloudstack/tree/b31e64d4
Diff: http://git-wip-us.apache.org/repos/asf/cloudstack/diff/b31e64d4

Branch: refs/heads/master
Commit: b31e64d49df4f9cf1e16179354bf7916f19b0b3b
Parents: 0df3357
Author: Boris Schrijver <[email protected]>
Authored: Wed Sep 16 22:19:14 2015 +0200
Committer: Boris Schrijver <[email protected]>
Committed: Wed Sep 16 22:19:14 2015 +0200

----------------------------------------------------------------------
 .../configuration/ConfigurationManagerImpl.java | 125 ++++++-------------
 1 file changed, 35 insertions(+), 90 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cloudstack/blob/b31e64d4/server/src/com/cloud/configuration/ConfigurationManagerImpl.java
----------------------------------------------------------------------
diff --git a/server/src/com/cloud/configuration/ConfigurationManagerImpl.java 
b/server/src/com/cloud/configuration/ConfigurationManagerImpl.java
index 462d6e9..69e70e6 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<List<String>> tablesToCheck = new ArrayList<List<String>>();
+        final String errorMsg = "The zone cannot be deleted because ";
 
-        final List<String> host = new ArrayList<String>();
-        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<String> hostPodRef = new ArrayList<String>();
-        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<String> privateIP = new ArrayList<String>();
-        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<String> publicIP = new ArrayList<String>();
-        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<String> vmInstance = new ArrayList<String>();
-        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<String> volumes = new ArrayList<String>();
-        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<String> physicalNetworks = new ArrayList<String>();
-        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<String> vmwareDcs = new ArrayList<String>();
-        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<String> 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 ? = ?";
+        // 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.");
+        }
 
-            if (tableName.equals("op_dc_vnet_alloc")) {
-                selectSql += " AND taken IS NOT NULL";
-            }
+        // 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.");
+        }
 
-            if (tableName.equals("user_ip_address")) {
-                selectSql += " AND state!='Free'";
-            }
+        // 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.");
+        }
 
-            if (tableName.equals("op_dc_ip_address_alloc")) {
-                selectSql += " AND taken IS NOT NULL";
-            }
+        // 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.");
+        }
 
-            if (tableName.equals("host_pod_ref") || tableName.equals("host") 
|| tableName.equals("volumes") || tableName.equals("physical_network")) {
-                selectSql += " AND removed is NULL";
-            }
+        // 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.");
+        }
 
-            if (tableName.equals("vm_instance")) {
-                selectSql += " AND state != ? AND removed IS NULL";
-            }
+        // 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.");
+        }
 
-            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 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,

Reply via email to