@@ -915,15 +915,6 @@ public VmwareDatacenterVO addVmwareDatacenter(AddVmwareDcCmd cmd) throws Resourc
915915 String vCenterHost = cmd .getVcenter ();
916916 String vmwareDcName = cmd .getName ();
917917
918- // Zone validation
919- validateZone (zoneId , "add VMware datacenter to zone" );
920-
921- VmwareDatacenterZoneMapVO vmwareDcZoneMap = _vmwareDcZoneMapDao .findByZoneId (zoneId );
922- // Check if zone is associated with VMware DC
923- if (vmwareDcZoneMap != null ) {
924- throw new CloudRuntimeException ("Zone " + zoneId + " is already associated with a VMware datacenter." );
925- }
926-
927918 // Validate username, password, VMware DC name and vCenter
928919 if (userName == null ) {
929920 throw new InvalidParameterValueException ("Missing or invalid parameter username." );
@@ -942,6 +933,36 @@ public VmwareDatacenterVO addVmwareDatacenter(AddVmwareDcCmd cmd) throws Resourc
942933 "Please provide valid VMware vCenter server's IP address or fully qualified domain name." );
943934 }
944935
936+ if (zoneId == null ) {
937+ throw new InvalidParameterValueException ("Missing or invalid parameter name. " +
938+ "Please provide valid zone id." );
939+ }
940+
941+ // Zone validation
942+ validateZone (zoneId );
943+
944+ VmwareDatacenterZoneMapVO vmwareDcZoneMap = _vmwareDcZoneMapDao .findByZoneId (zoneId );
945+ // Check if zone is associated with VMware DC
946+ if (vmwareDcZoneMap != null ) {
947+ // Check if the associated VMware DC matches the one specified in API params
948+ // This check would yield success as the association exists between same entities (zone and VMware DC)
949+ // This scenario would result in if the API addVmwareDc is called more than once with same parameters.
950+ Long associatedVmwareDcId = vmwareDcZoneMap .getVmwareDcId ();
951+ VmwareDatacenterVO associatedVmwareDc = _vmwareDcDao .findById (associatedVmwareDcId );
952+ if (associatedVmwareDc .getVcenterHost ().equalsIgnoreCase (vCenterHost ) &&
953+ associatedVmwareDc .getVmwareDatacenterName ().equalsIgnoreCase (vmwareDcName )) {
954+ s_logger .info ("Ignoring API call addVmwareDc, because VMware DC " + vCenterHost + "/" + vmwareDcName +
955+ " is already associated with specified zone with id " + zoneId );
956+ return associatedVmwareDc ;
957+ } else {
958+ throw new CloudRuntimeException ("Zone " + zoneId + " is already associated with a VMware datacenter. " +
959+ "Only 1 VMware DC can be associated with a zone." );
960+ }
961+ }
962+ // Zone validation to check if the zone already has resources.
963+ // Association of VMware DC to zone is not allowed if zone already has resources added.
964+ validateZoneWithResources (zoneId , "add VMware datacenter to zone" );
965+
945966 // Check if DC is already part of zone
946967 // In that case vmware_data_center table should have the DC
947968 vmwareDc = _vmwareDcDao .getVmwareDatacenterByGuid (vmwareDcName + "@" + vCenterHost );
@@ -1037,7 +1058,10 @@ public VmwareDatacenterVO addVmwareDatacenter(AddVmwareDcCmd cmd) throws Resourc
10371058 public boolean removeVmwareDatacenter (RemoveVmwareDcCmd cmd ) throws ResourceInUseException {
10381059 Long zoneId = cmd .getZoneId ();
10391060 // Validate zone
1040- validateZone (zoneId , "remove VMware datacenter from zone" );
1061+ validateZone (zoneId );
1062+ // Zone validation to check if the zone already has resources.
1063+ // Association of VMware DC to zone is not allowed if zone already has resources added.
1064+ validateZoneWithResources (zoneId , "remove VMware datacenter to zone" );
10411065
10421066 // Get DC associated with this zone
10431067 VmwareDatacenterZoneMapVO vmwareDcZoneMap ;
@@ -1107,15 +1131,17 @@ public boolean removeVmwareDatacenter(RemoveVmwareDcCmd cmd) throws ResourceInUs
11071131 return true ;
11081132 }
11091133
1110- private void validateZone (Long zoneId , String errStr ) throws ResourceInUseException {
1134+ private void validateZone (Long zoneId ) throws ResourceInUseException {
11111135 // Check if zone with specified id exists
11121136 DataCenterVO zone = _dcDao .findById (zoneId );
11131137 if (zone == null ) {
11141138 InvalidParameterValueException ex = new InvalidParameterValueException (
11151139 "Can't find zone by the id specified." );
11161140 throw ex ;
11171141 }
1142+ }
11181143
1144+ private void validateZoneWithResources (Long zoneId , String errStr ) throws ResourceInUseException {
11191145 // Check if zone has resources? - For now look for clusters
11201146 List <ClusterVO > clusters = _clusterDao .listByZoneId (zoneId );
11211147 if (clusters != null && clusters .size () > 0 ) {
0 commit comments