Updated Branches:
  refs/heads/4.2 93090aa7c -> a52bf1b64

CLOUDSTACK-3152 addVmwareDc API call fails if called more than once to add same 
VMware DC to same cloudstack zone.

Check if the associated VMware DC matches the one specified in API params. This 
check would yield success as the association exists between same entities (zone 
and VMware DC)
This scenario would result in if the API addVmwareDc is called more than once 
with same parameters. For such scenario, return success response with 
VmwareDatacenter object read from database. Also returns success even if the 
zone has resources (clusters) already.

Signed-off-by: Sateesh Chodapuneedi <[email protected]>


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

Branch: refs/heads/4.2
Commit: a52bf1b641ae53d7b6f09ffe3b751dc01f5e8ab4
Parents: 93090aa
Author: Sateesh Chodapuneedi <[email protected]>
Authored: Tue Jul 2 18:25:41 2013 +0530
Committer: Sateesh Chodapuneedi <[email protected]>
Committed: Tue Jul 2 18:25:41 2013 +0530

----------------------------------------------------------------------
 .../vmware/manager/VmwareManagerImpl.java       | 48 +++++++++++++++-----
 1 file changed, 37 insertions(+), 11 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cloudstack/blob/a52bf1b6/plugins/hypervisors/vmware/src/com/cloud/hypervisor/vmware/manager/VmwareManagerImpl.java
----------------------------------------------------------------------
diff --git 
a/plugins/hypervisors/vmware/src/com/cloud/hypervisor/vmware/manager/VmwareManagerImpl.java
 
b/plugins/hypervisors/vmware/src/com/cloud/hypervisor/vmware/manager/VmwareManagerImpl.java
index fd99561..7b8697d 100755
--- 
a/plugins/hypervisors/vmware/src/com/cloud/hypervisor/vmware/manager/VmwareManagerImpl.java
+++ 
b/plugins/hypervisors/vmware/src/com/cloud/hypervisor/vmware/manager/VmwareManagerImpl.java
@@ -915,15 +915,6 @@ public class VmwareManagerImpl extends ManagerBase 
implements VmwareManager, Vmw
         String vCenterHost = cmd.getVcenter();
         String vmwareDcName = cmd.getName();
 
-        // Zone validation
-        validateZone(zoneId, "add VMware datacenter to zone");
-
-        VmwareDatacenterZoneMapVO vmwareDcZoneMap = 
_vmwareDcZoneMapDao.findByZoneId(zoneId);
-        // Check if zone is associated with VMware DC
-        if (vmwareDcZoneMap != null) {
-            throw new CloudRuntimeException("Zone " + zoneId + " is already 
associated with a VMware datacenter.");
-        }
-
         // Validate username, password, VMware DC name and vCenter
         if (userName == null) {
             throw new InvalidParameterValueException("Missing or invalid 
parameter username.");
@@ -942,6 +933,36 @@ public class VmwareManagerImpl extends ManagerBase 
implements VmwareManager, Vmw
                     "Please provide valid VMware vCenter server's IP address 
or fully qualified domain name.");
         }
 
+        if (zoneId == null) {
+            throw new InvalidParameterValueException("Missing or invalid 
parameter name. " +
+                    "Please provide valid zone id.");
+        }
+
+        // Zone validation
+        validateZone(zoneId);
+
+        VmwareDatacenterZoneMapVO vmwareDcZoneMap = 
_vmwareDcZoneMapDao.findByZoneId(zoneId);
+        // Check if zone is associated with VMware DC
+        if (vmwareDcZoneMap != null) {
+            // Check if the associated VMware DC matches the one specified in 
API params
+            // This check would yield success as the association exists 
between same entities (zone and VMware DC)
+            // This scenario would result in if the API addVmwareDc is called 
more than once with same parameters.
+            Long associatedVmwareDcId = vmwareDcZoneMap.getVmwareDcId();
+            VmwareDatacenterVO associatedVmwareDc = 
_vmwareDcDao.findById(associatedVmwareDcId);
+            if 
(associatedVmwareDc.getVcenterHost().equalsIgnoreCase(vCenterHost) &&
+                    
associatedVmwareDc.getVmwareDatacenterName().equalsIgnoreCase(vmwareDcName)) {
+                s_logger.info("Ignoring API call addVmwareDc, because VMware 
DC " + vCenterHost + "/" + vmwareDcName +
+                              " is already associated with specified zone with 
id " + zoneId);
+                return associatedVmwareDc;
+            } else {
+                throw new CloudRuntimeException("Zone " + zoneId + " is 
already associated with a VMware datacenter. " +
+                                                "Only 1 VMware DC can be 
associated with a zone.");
+            }
+        }
+        // Zone validation to check if the zone already has resources.
+        // Association of VMware DC to zone is not allowed if zone already has 
resources added.
+        validateZoneWithResources(zoneId, "add VMware datacenter to zone");
+
         // Check if DC is already part of zone
         // In that case vmware_data_center table should have the DC
         vmwareDc = _vmwareDcDao.getVmwareDatacenterByGuid(vmwareDcName + "@" + 
vCenterHost);
@@ -1037,7 +1058,10 @@ public class VmwareManagerImpl extends ManagerBase 
implements VmwareManager, Vmw
     public boolean removeVmwareDatacenter(RemoveVmwareDcCmd cmd) throws 
ResourceInUseException {
         Long zoneId = cmd.getZoneId();
         // Validate zone
-        validateZone(zoneId, "remove VMware datacenter from zone");
+        validateZone(zoneId);
+        // Zone validation to check if the zone already has resources.
+        // Association of VMware DC to zone is not allowed if zone already has 
resources added.
+        validateZoneWithResources(zoneId, "remove VMware datacenter to zone");
 
         // Get DC associated with this zone
         VmwareDatacenterZoneMapVO vmwareDcZoneMap;
@@ -1107,7 +1131,7 @@ public class VmwareManagerImpl extends ManagerBase 
implements VmwareManager, Vmw
         return true;
     }
 
-    private void validateZone(Long zoneId, String errStr) throws 
ResourceInUseException {
+    private void validateZone(Long zoneId) throws ResourceInUseException {
         // Check if zone with specified id exists
         DataCenterVO zone = _dcDao.findById(zoneId);
         if (zone == null) {
@@ -1115,7 +1139,9 @@ public class VmwareManagerImpl extends ManagerBase 
implements VmwareManager, Vmw
                     "Can't find zone by the id specified.");
             throw ex;
         }
+    }
 
+    private void validateZoneWithResources(Long zoneId, String errStr) throws 
ResourceInUseException {
         // Check if zone has resources? - For now look for clusters
         List<ClusterVO> clusters = _clusterDao.listByZoneId(zoneId);
         if (clusters != null && clusters.size() > 0) {

Reply via email to