3030
3131import org .apache .log4j .Logger ;
3232import org .apache .cloudstack .api .ApiConstants ;
33+ import org .springframework .beans .NullValueInNestedPathException ;
3334
3435import com .cloud .agent .api .StartupCommand ;
3536import com .cloud .agent .api .StartupRoutingCommand ;
5152import com .cloud .hypervisor .Hypervisor ;
5253import com .cloud .hypervisor .Hypervisor .HypervisorType ;
5354import com .cloud .hypervisor .dao .HypervisorCapabilitiesDao ;
55+ import com .cloud .hypervisor .vmware .dao .VmwareDatacenterDao ;
56+ import com .cloud .hypervisor .vmware .dao .VmwareDatacenterZoneMapDao ;
5457import com .cloud .hypervisor .vmware .manager .VmwareManager ;
5558import com .cloud .hypervisor .vmware .mo .ClusterMO ;
5659import com .cloud .hypervisor .vmware .mo .HostMO ;
@@ -107,11 +110,16 @@ public class VmwareServerDiscoverer extends DiscovererBase implements
107110 @ Inject
108111 CiscoNexusVSMDeviceDao _nexusDao ;
109112 @ Inject
110- CiscoNexusVSMElementService _nexusElement ;
113+ CiscoNexusVSMElement _nexusElement ;
111114 @ Inject
112115 NetworkModel _netmgr ;
113116 @ Inject
114117 HypervisorCapabilitiesDao _hvCapabilitiesDao ;
118+ @ Inject
119+ VmwareDatacenterZoneMapDao _vmwareDcZoneMapDao ;
120+ @ Inject
121+ VmwareDatacenterDao _vmwareDcDao ;
122+
115123 protected Map <String , String > _urlParams ;
116124 protected boolean useDVS = false ;
117125 protected boolean nexusDVS = false ;
@@ -140,6 +148,18 @@ public Map<? extends ServerResource, Map<String, String>> find(long dcId, Long p
140148 return null ;
141149 }
142150
151+ Map <String , String > clusterDetails = _clusterDetailsDao .findDetails (clusterId );
152+ boolean legacyZone = false ;
153+ //Check if NOT a legacy zone.
154+ if (!legacyZone ) {
155+ String updatedInventoryPath = validateCluster (dcId , url , username , password );
156+ if (url .getPath () != updatedInventoryPath ) {
157+ // If url from API doesn't specifiy DC then update url in database with DC assocaited with this zone.
158+ clusterDetails .put ("url" , url .getScheme () + "://" + url .getHost () + updatedInventoryPath );
159+ _clusterDetailsDao .persist (clusterId , clusterDetails );
160+ }
161+ }
162+
143163 List <HostVO > hosts = _resourceMgr .listAllHostsInCluster (clusterId );
144164 if (hosts != null && hosts .size () > 0 ) {
145165 int maxHostsPerCluster = _hvCapabilitiesDao .getMaxHostsPerCluster (hosts .get (0 ).getHypervisorType (), hosts .get (0 ).getHypervisorVersion ());
@@ -164,7 +184,6 @@ public Map<? extends ServerResource, Map<String, String>> find(long dcId, Long p
164184
165185 VmwareTrafficLabel guestTrafficLabelObj = new VmwareTrafficLabel (TrafficType .Guest );
166186 VmwareTrafficLabel publicTrafficLabelObj = new VmwareTrafficLabel (TrafficType .Public );
167- Map <String , String > clusterDetails = _clusterDetailsDao .findDetails (clusterId );
168187 DataCenterVO zone = _dcDao .findById (dcId );
169188 NetworkType zoneType = zone .getNetworkType ();
170189 _readGlobalConfigParameters ();
@@ -395,6 +414,63 @@ public Map<? extends ServerResource, Map<String, String>> find(long dcId, Long p
395414 }
396415 }
397416
417+ private String validateCluster (Long dcId , URI url , String username , String password ) throws DiscoveryException {
418+ String msg ;
419+ long vmwareDcId ;
420+ VmwareDatacenterVO vmwareDc ;
421+ String vmwareDcNameFromDb ;
422+ String vmwareDcNameFromApi ;
423+ String vCenterHost ;
424+ String updatedInventoryPath = url .getPath ();
425+ String clusterName = null ;
426+
427+ // Check if zone is associated with DC
428+ VmwareDatacenterZoneMapVO vmwareDcZone = _vmwareDcZoneMapDao .findByZoneId (dcId );
429+ if (vmwareDcZone == null ) {
430+ msg = "Zone " + dcId + " is not associated with any VMware DC yet. "
431+ + "Please add VMware DC to this zone first and then try to add clusters." ;
432+ s_logger .error (msg );
433+ throw new DiscoveryException (msg );
434+ }
435+
436+ // Retrieve DC added to this zone from database
437+ vmwareDcId = vmwareDcZone .getVmwareDcId ();
438+ vmwareDc = _vmwareDcDao .findById (vmwareDcId );
439+ vmwareDcNameFromApi = vmwareDcNameFromDb = vmwareDc .getVmwareDatacenterName ();
440+ vCenterHost = vmwareDc .getVcenterHost ();
441+ String inventoryPath = url .getPath ();
442+
443+ assert (inventoryPath != null );
444+
445+ String [] pathTokens = inventoryPath .split ("/" );
446+ if (pathTokens .length == 2 ) {
447+ // DC name is not present in url.
448+ // Using DC name read from database.
449+ clusterName = pathTokens [1 ];
450+ updatedInventoryPath = "/" + vmwareDcNameFromDb + "/" + clusterName ;
451+ } else if (pathTokens .length == 3 ) {
452+ vmwareDcNameFromApi = pathTokens [1 ];
453+ clusterName = pathTokens [2 ];
454+ }
455+
456+ if (!vCenterHost .equalsIgnoreCase (url .getHost ())) {
457+ msg = "This cluster " + clusterName + " belongs to vCenter " + url .getHost ()
458+ + " .But this zone is associated with VMware DC from vCenter " + vCenterHost
459+ + ". Make sure the cluster being added belongs to vCenter " + vCenterHost
460+ + " and DC " + vmwareDcNameFromDb ;
461+ s_logger .error (msg );
462+ throw new DiscoveryException (msg );
463+ } else if (!vmwareDcNameFromDb .equalsIgnoreCase (vmwareDcNameFromApi )) {
464+ msg = "This cluster " + clusterName + " belongs to VMware DC " + vmwareDcNameFromApi
465+ + " .But this zone is associated with VMware DC " + vmwareDcNameFromDb
466+ + ". Make sure the cluster being added belongs to DC " + vmwareDcNameFromDb
467+ + " in vCenter " + vCenterHost ;
468+ s_logger .error (msg );
469+ throw new DiscoveryException (msg );
470+ }
471+ return updatedInventoryPath ;
472+ }
473+
398474 private boolean validateDiscoveredHosts (VmwareContext context ,
399475 ManagedObjectReference morCluster ,
400476 List <ManagedObjectReference > morHosts ) throws Exception {
0 commit comments