|
23 | 23 | import java.util.Map; |
24 | 24 | import java.util.UUID; |
25 | 25 |
|
26 | | -import javax.annotation.PostConstruct; |
27 | 26 | import javax.ejb.Local; |
28 | 27 | import javax.inject.Inject; |
29 | 28 | import javax.naming.ConfigurationException; |
30 | 29 |
|
31 | 30 | import org.apache.log4j.Logger; |
| 31 | + |
| 32 | +import com.vmware.vim25.ClusterDasConfigInfo; |
| 33 | +import com.vmware.vim25.ManagedObjectReference; |
| 34 | + |
32 | 35 | import org.apache.cloudstack.api.ApiConstants; |
33 | | -import org.springframework.beans.NullValueInNestedPathException; |
34 | 36 |
|
35 | 37 | import com.cloud.agent.api.StartupCommand; |
36 | 38 | import com.cloud.agent.api.StartupRoutingCommand; |
37 | 39 | import com.cloud.alert.AlertManager; |
38 | 40 | import com.cloud.configuration.Config; |
39 | | -import com.cloud.configuration.dao.ConfigurationDao; |
40 | 41 | import com.cloud.dc.ClusterDetailsDao; |
41 | 42 | import com.cloud.dc.ClusterVO; |
42 | 43 | import com.cloud.dc.DataCenter.NetworkType; |
|
67 | 68 | import com.cloud.network.VmwareTrafficLabel; |
68 | 69 | import com.cloud.network.dao.CiscoNexusVSMDeviceDao; |
69 | 70 | import com.cloud.network.element.CiscoNexusVSMElement; |
70 | | -import com.cloud.network.element.CiscoNexusVSMElementService; |
71 | 71 | import com.cloud.resource.Discoverer; |
72 | 72 | import com.cloud.resource.DiscovererBase; |
73 | 73 | import com.cloud.resource.ResourceManager; |
|
81 | 81 | import com.cloud.user.Account; |
82 | 82 | import com.cloud.utils.UriUtils; |
83 | 83 |
|
84 | | -import com.vmware.vim25.ClusterDasConfigInfo; |
85 | | -import com.vmware.vim25.ManagedObjectReference; |
86 | | - |
87 | 84 |
|
88 | 85 | @Local(value = Discoverer.class) |
89 | 86 | public class VmwareServerDiscoverer extends DiscovererBase implements |
@@ -150,14 +147,41 @@ public Map<? extends ServerResource, Map<String, String>> find(long dcId, Long p |
150 | 147 |
|
151 | 148 | Map<String, String> clusterDetails = _clusterDetailsDao.findDetails(clusterId); |
152 | 149 | boolean legacyZone = _vmwareMgr.isLegacyZone(dcId); |
| 150 | + boolean usernameNotProvided = (username == null || username.isEmpty()); |
| 151 | + boolean passwordNotProvided = (password == null || password.isEmpty()); |
153 | 152 | //Check if NOT a legacy zone. |
154 | 153 | if (!legacyZone) { |
155 | | - String updatedInventoryPath = validateCluster(dcId, url, username, password); |
| 154 | + // Retrieve VMware DC associated with specified zone |
| 155 | + VmwareDatacenterVO vmwareDc = fetchVmwareDatacenterByZone(dcId); |
| 156 | + // Ensure username & password provided. |
| 157 | + // If either or both not provided, try to retrieve & use the credentials from database, which are provided earlier while adding VMware DC to zone. |
| 158 | + if (usernameNotProvided || passwordNotProvided) { |
| 159 | + // Retrieve credentials associated with VMware DC |
| 160 | + s_logger.info("Username and/or Password not provided while adding cluster to cloudstack zone. " + |
| 161 | + "Hence using both username & password provided while adding VMware DC to CloudStack zone."); |
| 162 | + username = vmwareDc.getUser(); |
| 163 | + password = vmwareDc.getPassword(); |
| 164 | + clusterDetails.put("username", username); |
| 165 | + clusterDetails.put("password", password); |
| 166 | + _clusterDetailsDao.persist(clusterId, clusterDetails); |
| 167 | + } |
| 168 | + String updatedInventoryPath = validateCluster(url, vmwareDc); |
156 | 169 | if (url.getPath() != updatedInventoryPath) { |
157 | | - // If url from API doesn't specifiy DC then update url in database with DC assocaited with this zone. |
| 170 | + // If url from API doesn't specify DC then update url in database with DC associated with this zone. |
158 | 171 | clusterDetails.put("url", url.getScheme() + "://" + url.getHost() + updatedInventoryPath); |
159 | 172 | _clusterDetailsDao.persist(clusterId, clusterDetails); |
160 | 173 | } |
| 174 | + } else { |
| 175 | + // For legacy zones insist on the old model of asking for credentials for each cluster being added. |
| 176 | + if (usernameNotProvided) { |
| 177 | + if (passwordNotProvided) { |
| 178 | + throw new InvalidParameterValueException("Please provide username & password to add this cluster to zone"); |
| 179 | + } else { |
| 180 | + throw new InvalidParameterValueException("Please provide username to add this cluster to zone"); |
| 181 | + } |
| 182 | + } else if (passwordNotProvided) { |
| 183 | + throw new InvalidParameterValueException("Please provide password to add this cluster to zone"); |
| 184 | + } |
161 | 185 | } |
162 | 186 |
|
163 | 187 | List<HostVO> hosts = _resourceMgr.listAllHostsInCluster(clusterId); |
@@ -414,28 +438,36 @@ public Map<? extends ServerResource, Map<String, String>> find(long dcId, Long p |
414 | 438 | } |
415 | 439 | } |
416 | 440 |
|
417 | | - private String validateCluster(Long dcId, URI url, String username, String password) throws DiscoveryException { |
418 | | - String msg; |
419 | | - long vmwareDcId; |
| 441 | + private VmwareDatacenterVO fetchVmwareDatacenterByZone(Long dcId) throws DiscoveryException { |
420 | 442 | VmwareDatacenterVO vmwareDc; |
421 | | - String vmwareDcNameFromDb; |
422 | | - String vmwareDcNameFromApi; |
423 | | - String vCenterHost; |
424 | | - String updatedInventoryPath = url.getPath(); |
425 | | - String clusterName = null; |
| 443 | + VmwareDatacenterZoneMapVO vmwareDcZone; |
| 444 | + long vmwareDcId; |
| 445 | + String msg; |
426 | 446 |
|
427 | 447 | // Check if zone is associated with DC |
428 | | - VmwareDatacenterZoneMapVO vmwareDcZone = _vmwareDcZoneMapDao.findByZoneId(dcId); |
| 448 | + vmwareDcZone = _vmwareDcZoneMapDao.findByZoneId(dcId); |
429 | 449 | if (vmwareDcZone == null) { |
430 | 450 | 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."; |
| 451 | + + "Please add VMware DC to this zone first and then try to add clusters."; |
432 | 452 | s_logger.error(msg); |
433 | 453 | throw new DiscoveryException(msg); |
434 | 454 | } |
435 | 455 |
|
436 | 456 | // Retrieve DC added to this zone from database |
437 | 457 | vmwareDcId = vmwareDcZone.getVmwareDcId(); |
438 | 458 | vmwareDc = _vmwareDcDao.findById(vmwareDcId); |
| 459 | + |
| 460 | + return vmwareDc; |
| 461 | + } |
| 462 | + |
| 463 | + private String validateCluster(URI url, VmwareDatacenterVO vmwareDc) throws DiscoveryException { |
| 464 | + String msg; |
| 465 | + String vmwareDcNameFromDb; |
| 466 | + String vmwareDcNameFromApi; |
| 467 | + String vCenterHost; |
| 468 | + String updatedInventoryPath = url.getPath(); |
| 469 | + String clusterName = null; |
| 470 | + |
439 | 471 | vmwareDcNameFromApi = vmwareDcNameFromDb = vmwareDc.getVmwareDatacenterName(); |
440 | 472 | vCenterHost = vmwareDc.getVcenterHost(); |
441 | 473 | String inventoryPath = url.getPath(); |
|
0 commit comments