Skip to content

Commit e9dfb79

Browse files
author
Sateesh Chodapuneedi
committed
CLOUDSTACK-3469 [VMware] Support to make username/password optional while adding VMware cluster
For legacy zones insist on the old model of asking for credentials for each cluster being added. For non-legacy zones check if username & password are provided. If either or both not provided, try to retrieve & use the credentials from database, which are provided earlier while adding VMware DC to zone. This lets user to not specify credentials while adding VMware cluster to zone, if the credentials are same as that of provided while adding VMware DC to zone. Signed-off-by: Sateesh Chodapuneedi <sateesh@apache.org>
1 parent f99d599 commit e9dfb79

1 file changed

Lines changed: 51 additions & 19 deletions

File tree

‎plugins/hypervisors/vmware/src/com/cloud/hypervisor/vmware/VmwareServerDiscoverer.java‎

Lines changed: 51 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -23,20 +23,21 @@
2323
import java.util.Map;
2424
import java.util.UUID;
2525

26-
import javax.annotation.PostConstruct;
2726
import javax.ejb.Local;
2827
import javax.inject.Inject;
2928
import javax.naming.ConfigurationException;
3029

3130
import org.apache.log4j.Logger;
31+
32+
import com.vmware.vim25.ClusterDasConfigInfo;
33+
import com.vmware.vim25.ManagedObjectReference;
34+
3235
import org.apache.cloudstack.api.ApiConstants;
33-
import org.springframework.beans.NullValueInNestedPathException;
3436

3537
import com.cloud.agent.api.StartupCommand;
3638
import com.cloud.agent.api.StartupRoutingCommand;
3739
import com.cloud.alert.AlertManager;
3840
import com.cloud.configuration.Config;
39-
import com.cloud.configuration.dao.ConfigurationDao;
4041
import com.cloud.dc.ClusterDetailsDao;
4142
import com.cloud.dc.ClusterVO;
4243
import com.cloud.dc.DataCenter.NetworkType;
@@ -67,7 +68,6 @@
6768
import com.cloud.network.VmwareTrafficLabel;
6869
import com.cloud.network.dao.CiscoNexusVSMDeviceDao;
6970
import com.cloud.network.element.CiscoNexusVSMElement;
70-
import com.cloud.network.element.CiscoNexusVSMElementService;
7171
import com.cloud.resource.Discoverer;
7272
import com.cloud.resource.DiscovererBase;
7373
import com.cloud.resource.ResourceManager;
@@ -81,9 +81,6 @@
8181
import com.cloud.user.Account;
8282
import com.cloud.utils.UriUtils;
8383

84-
import com.vmware.vim25.ClusterDasConfigInfo;
85-
import com.vmware.vim25.ManagedObjectReference;
86-
8784

8885
@Local(value = Discoverer.class)
8986
public class VmwareServerDiscoverer extends DiscovererBase implements
@@ -150,14 +147,41 @@ public Map<? extends ServerResource, Map<String, String>> find(long dcId, Long p
150147

151148
Map<String, String> clusterDetails = _clusterDetailsDao.findDetails(clusterId);
152149
boolean legacyZone = _vmwareMgr.isLegacyZone(dcId);
150+
boolean usernameNotProvided = (username == null || username.isEmpty());
151+
boolean passwordNotProvided = (password == null || password.isEmpty());
153152
//Check if NOT a legacy zone.
154153
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);
156169
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.
158171
clusterDetails.put("url", url.getScheme() + "://" + url.getHost() + updatedInventoryPath);
159172
_clusterDetailsDao.persist(clusterId, clusterDetails);
160173
}
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+
}
161185
}
162186

163187
List<HostVO> hosts = _resourceMgr.listAllHostsInCluster(clusterId);
@@ -414,28 +438,36 @@ public Map<? extends ServerResource, Map<String, String>> find(long dcId, Long p
414438
}
415439
}
416440

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 {
420442
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;
426446

427447
// Check if zone is associated with DC
428-
VmwareDatacenterZoneMapVO vmwareDcZone = _vmwareDcZoneMapDao.findByZoneId(dcId);
448+
vmwareDcZone = _vmwareDcZoneMapDao.findByZoneId(dcId);
429449
if (vmwareDcZone == null) {
430450
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.";
432452
s_logger.error(msg);
433453
throw new DiscoveryException(msg);
434454
}
435455

436456
// Retrieve DC added to this zone from database
437457
vmwareDcId = vmwareDcZone.getVmwareDcId();
438458
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+
439471
vmwareDcNameFromApi = vmwareDcNameFromDb = vmwareDc.getVmwareDatacenterName();
440472
vCenterHost = vmwareDc.getVcenterHost();
441473
String inventoryPath = url.getPath();

0 commit comments

Comments
 (0)