Skip to content

Commit 58b57ca

Browse files
author
Sateesh Chodapuneedi
committed
CLOUDSTACK-2805 [VMware] addVmwareDc fails with NPE when non-existent DC name is passed to the API
Made vcenter as required parameter to addVmwareDc API. Removed stale parameter url from addVmwareDc API. Improved Exception handling, logging remote exception details.
1 parent 89fa121 commit 58b57ca

5 files changed

Lines changed: 42 additions & 77 deletions

File tree

‎api/src/org/apache/cloudstack/api/ApiConstants.java‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -405,6 +405,7 @@ public class ApiConstants {
405405
public static final String VSM_CONFIG_MODE = "vsmconfigmode";
406406
public static final String VSM_CONFIG_STATE = "vsmconfigstate";
407407
public static final String VSM_DEVICE_STATE = "vsmdevicestate";
408+
public static final String VCENTER = "vcenter";
408409
public static final String ADD_VSM_FLAG = "addvsmflag";
409410
public static final String END_POINT = "endpoint";
410411
public static final String REGION_ID = "regionid";

‎plugins/hypervisors/vmware/src/com/cloud/hypervisor/vmware/dao/VmwareDatacenterDaoImpl.java‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,15 +75,15 @@ public VmwareDatacenterVO getVmwareDatacenterByGuid(String guid) {
7575

7676
@Override
7777
public List<VmwareDatacenterVO> getVmwareDatacenterByNameAndVcenter(String name, String vCenterHost) {
78-
SearchCriteria<VmwareDatacenterVO> sc = guidSearch.create();
78+
SearchCriteria<VmwareDatacenterVO> sc = nameVcSearch.create();
7979
sc.setParameters("name", name);
8080
sc.setParameters("vCenterHost", vCenterHost);
8181
return search(sc, null);
8282
}
8383

8484
@Override
8585
public List<VmwareDatacenterVO> listVmwareDatacenterByName(String name) {
86-
SearchCriteria<VmwareDatacenterVO> sc = guidSearch.create();
86+
SearchCriteria<VmwareDatacenterVO> sc = nameSearch.create();
8787
sc.setParameters("name", name);
8888
return search(sc, null);
8989
}

‎plugins/hypervisors/vmware/src/com/cloud/hypervisor/vmware/manager/VmwareManagerImpl.java‎

Lines changed: 33 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
import java.net.URI;
2222
import java.net.URISyntaxException;
2323
import java.net.URL;
24-
import java.net.URLDecoder;
24+
import java.rmi.RemoteException;
2525
import java.util.ArrayList;
2626
import java.util.HashMap;
2727
import java.util.List;
@@ -38,8 +38,6 @@
3838

3939
import org.apache.cloudstack.api.command.admin.zone.AddVmwareDcCmd;
4040
import org.apache.cloudstack.api.command.admin.zone.RemoveVmwareDcCmd;
41-
import org.apache.cloudstack.engine.subsystem.api.storage.DataStoreManager;
42-
import org.apache.cloudstack.engine.subsystem.api.storage.DataStoreRole;
4341
import org.apache.log4j.Logger;
4442

4543
import com.cloud.agent.AgentManager;
@@ -50,8 +48,6 @@
5048
import com.cloud.agent.api.Command;
5149
import com.cloud.agent.api.StartupCommand;
5250
import com.cloud.agent.api.StartupRoutingCommand;
53-
import com.cloud.agent.api.storage.MigrateVolumeAnswer;
54-
import com.cloud.agent.api.to.VolumeTO;
5551
import com.cloud.cluster.ClusterManager;
5652
import com.cloud.configuration.Config;
5753
import com.cloud.configuration.dao.ConfigurationDao;
@@ -80,7 +76,6 @@
8076
import com.cloud.hypervisor.vmware.dao.VmwareDatacenterDao;
8177
import com.cloud.hypervisor.vmware.dao.VmwareDatacenterZoneMapDao;
8278
import com.cloud.hypervisor.vmware.mo.CustomFieldConstants;
83-
import com.cloud.hypervisor.vmware.mo.CustomFieldsManagerMO;
8479
import com.cloud.hypervisor.vmware.mo.DatacenterMO;
8580
import com.cloud.hypervisor.vmware.mo.DiskControllerType;
8681
import com.cloud.hypervisor.vmware.mo.HostFirewallSystemMO;
@@ -89,7 +84,6 @@
8984
import com.cloud.hypervisor.vmware.mo.TaskMO;
9085
import com.cloud.hypervisor.vmware.mo.VirtualEthernetCardType;
9186
import com.cloud.hypervisor.vmware.mo.VmwareHostType;
92-
import com.cloud.utils.ssh.SshHelper;
9387
import com.cloud.hypervisor.vmware.resource.VmwareContextFactory;
9488
import com.cloud.hypervisor.vmware.util.VmwareClient;
9589
import com.cloud.hypervisor.vmware.util.VmwareContext;
@@ -103,15 +97,10 @@
10397
import com.cloud.server.ConfigurationServer;
10498
import com.cloud.storage.JavaStorageLayer;
10599
import com.cloud.storage.StorageLayer;
106-
import com.cloud.storage.StoragePool;
107-
import com.cloud.storage.VolumeVO;
108-
import com.cloud.storage.dao.VolumeDao;
109100
import com.cloud.storage.secondary.SecondaryStorageVmManager;
110101
import com.cloud.utils.FileUtil;
111102
import com.cloud.utils.NumbersUtil;
112103
import com.cloud.utils.Pair;
113-
import com.cloud.utils.UriUtils;
114-
import com.cloud.utils.component.Manager;
115104
import com.cloud.utils.component.ManagerBase;
116105
import com.cloud.utils.concurrency.NamedThreadFactory;
117106
import com.cloud.utils.db.DB;
@@ -121,8 +110,6 @@
121110
import com.cloud.utils.script.Script;
122111
import com.cloud.utils.ssh.SshHelper;
123112
import com.cloud.vm.DomainRouterVO;
124-
import com.cloud.vm.VMInstanceVO;
125-
import com.cloud.vm.dao.VMInstanceDao;
126113
import com.google.gson.Gson;
127114
import com.vmware.vim25.AboutInfo;
128115
import com.vmware.vim25.HostConnectSpec;
@@ -530,7 +517,7 @@ public void prepareSecondaryStorageStore(String storageUrl) {
530517
_configServer.updateKeyPairs();
531518

532519
s_logger.info("Copy System VM patch ISO file to secondary storage. source ISO: " + srcIso.getAbsolutePath() +
533-
", destination: " + destIso.getAbsolutePath());
520+
", destination: " + destIso.getAbsolutePath());
534521
try {
535522
FileUtil.copyfile(srcIso, destIso);
536523
} catch(IOException e) {
@@ -579,7 +566,7 @@ private File getSystemVMPatchIsoFile() {
579566

580567
assert(isoFile != null);
581568
if(!isoFile.exists()) {
582-
s_logger.error("Unable to locate systemvm.iso in your setup at " + isoFile.toString());
569+
s_logger.error("Unable to locate systemvm.iso in your setup at " + isoFile.toString());
583570
}
584571
return isoFile;
585572
}
@@ -596,7 +583,7 @@ public File getSystemVMKeyFile() {
596583
}
597584
assert(keyFile != null);
598585
if(!keyFile.exists()) {
599-
s_logger.error("Unable to locate id_rsa.cloud in your setup at " + keyFile.toString());
586+
s_logger.error("Unable to locate id_rsa.cloud in your setup at " + keyFile.toString());
600587
}
601588
return keyFile;
602589
}
@@ -914,9 +901,9 @@ public List<Class<?>> getCommands() {
914901
public VmwareDatacenterVO addVmwareDatacenter(AddVmwareDcCmd cmd) throws ResourceInUseException {
915902
VmwareDatacenterVO vmwareDc = null;
916903
Long zoneId = cmd.getZoneId();
917-
String url = cmd.getUrl();
918904
String userName = cmd.getUsername();
919905
String password = cmd.getPassword();
906+
String vCenterHost = cmd.getVcenter();
920907
String vmwareDcName = cmd.getName();
921908

922909
// Zone validation
@@ -928,25 +915,26 @@ public VmwareDatacenterVO addVmwareDatacenter(AddVmwareDcCmd cmd) throws Resourc
928915
throw new CloudRuntimeException("Zone " + zoneId + " is already associated with a VMware datacenter.");
929916
}
930917

931-
// Validate url and get uri
932-
URI uri = getUri(url);
933-
934-
// Validate username and password and DC name
918+
// Validate username, password, VMware DC name and vCenter
935919
if (userName == null) {
936-
throw new InvalidParameterValueException("Invalid parameter username.");
920+
throw new InvalidParameterValueException("Missing or invalid parameter username.");
937921
}
938922

939923
if (password == null) {
940-
throw new InvalidParameterValueException("Invalid parameter password.");
924+
throw new InvalidParameterValueException("Missing or invalid parameter username.");
941925
}
942926

943927
if (vmwareDcName == null) {
944-
throw new InvalidParameterValueException("Invalid parameter name. Please provide valid VMware datacenter name.");
928+
throw new InvalidParameterValueException("Missing or invalid parameter name. Please provide valid VMware datacenter name.");
929+
}
930+
931+
if (vCenterHost == null) {
932+
throw new InvalidParameterValueException("Missing or invalid parameter name. " +
933+
"Please provide valid VMware vCenter server's IP address or fully qualified domain name.");
945934
}
946935

947936
// Check if DC is already part of zone
948937
// In that case vmware_data_center table should have the DC
949-
String vCenterHost = uri.getHost();
950938
vmwareDc = _vmwareDcDao.getVmwareDatacenterByGuid(vmwareDcName + "@" + vCenterHost);
951939
if (vmwareDc != null) {
952940
throw new ResourceInUseException("This DC is already part of other CloudStack zone(s). Cannot add this DC to more zones.");
@@ -963,14 +951,13 @@ public VmwareDatacenterVO addVmwareDatacenter(AddVmwareDcCmd cmd) throws Resourc
963951
context = VmwareContextFactory.create(vCenterHost, userName, password);
964952

965953
// Check if DC exists on vCenter
966-
try {
967-
dcMo = new DatacenterMO(context, vmwareDcName);
968-
} catch(Throwable t) {
969-
String msg = "Unable to find DC " + vmwareDcName + " in vCenter " + vCenterHost;
954+
dcMo = new DatacenterMO(context, vmwareDcName);
955+
dcMor = dcMo.getMor();
956+
if (dcMor == null) {
957+
String msg = "Unable to find VMware DC " + vmwareDcName + " in vCenter " + vCenterHost + ". ";
970958
s_logger.error(msg);
971-
throw new DiscoveryException(msg);
959+
throw new InvalidParameterValueException(msg);
972960
}
973-
assert (dcMo != null);
974961

975962
// Check if DC is already associated with another cloudstack deployment
976963
// Get custom field property cloud.zone over this DC
@@ -1020,9 +1007,13 @@ public VmwareDatacenterVO addVmwareDatacenter(AddVmwareDcCmd cmd) throws Resourc
10201007
}
10211008
dcMo.setCustomFieldValue(CustomFieldConstants.CLOUD_ZONE, "true");
10221009

1023-
} catch (Exception e) {
1024-
String msg = "VMware DC discovery failed due to : " + VmwareHelper.getExceptionMessage(e);
1025-
s_logger.error(msg);
1010+
} catch (Throwable e) {
1011+
String msg = "Failed to add VMware DC to zone ";
1012+
if (e instanceof RemoteException) {
1013+
msg = "Encountered remote exception at vCenter. " + VmwareHelper.getExceptionMessage(e);
1014+
} else {
1015+
msg += "due to : " + e.getMessage();
1016+
}
10261017
throw new CloudRuntimeException(msg);
10271018
} finally {
10281019
if (context != null)
@@ -1032,6 +1023,7 @@ public VmwareDatacenterVO addVmwareDatacenter(AddVmwareDcCmd cmd) throws Resourc
10321023
return vmwareDc;
10331024
}
10341025

1026+
10351027
@Override
10361028
public boolean removeVmwareDatacenter(RemoveVmwareDcCmd cmd) throws ResourceInUseException {
10371029
Long zoneId = cmd.getZoneId();
@@ -1070,8 +1062,8 @@ public boolean removeVmwareDatacenter(RemoveVmwareDcCmd cmd) throws ResourceInUs
10701062
_vmwareDcZoneMapDao.remove(vmwareDcZoneMap.getId());
10711063
txn.commit();
10721064
} catch (Exception e) {
1073-
s_logger.info("Caught exception when trying to delete VMware datacenter record.." + e.getMessage());
1074-
throw new CloudRuntimeException("Failed to delete VMware datacenter ");
1065+
s_logger.info("Caught exception when trying to delete VMware datacenter record." + e.getMessage());
1066+
throw new CloudRuntimeException("Failed to delete VMware datacenter.");
10751067
}
10761068

10771069
// Construct context
@@ -1083,7 +1075,7 @@ public boolean removeVmwareDatacenter(RemoveVmwareDcCmd cmd) throws ResourceInUs
10831075
try {
10841076
dcMo = new DatacenterMO(context, vmwareDcName);
10851077
} catch(Throwable t) {
1086-
String msg = "able to find DC " + vmwareDcName + " in vCenter " + vCenterHost;
1078+
String msg = "Unable to find DC " + vmwareDcName + " in vCenter " + vCenterHost;
10871079
s_logger.error(msg);
10881080
throw new DiscoveryException(msg);
10891081
}
@@ -1095,7 +1087,7 @@ public boolean removeVmwareDatacenter(RemoveVmwareDcCmd cmd) throws ResourceInUs
10951087
s_logger.info("Sucessfully reset custom field property cloud.zone over DC " + vmwareDcName);
10961088
} catch (Exception e) {
10971089
String msg = "Unable to reset custom field property cloud.zone over DC " + vmwareDcName
1098-
+ " due to : " + VmwareHelper.getExceptionMessage(e);
1090+
+ " due to : " + VmwareHelper.getExceptionMessage(e);
10991091
s_logger.error(msg);
11001092
throw new CloudRuntimeException(msg);
11011093
} finally {
@@ -1111,7 +1103,7 @@ private void validateZone(Long zoneId, String errStr) throws ResourceInUseExcept
11111103
DataCenterVO zone = _dcDao.findById(zoneId);
11121104
if (zone == null) {
11131105
InvalidParameterValueException ex = new InvalidParameterValueException(
1114-
"Can't find zone by the id specified");
1106+
"Can't find zone by the id specified.");
11151107
throw ex;
11161108
}
11171109

@@ -1122,38 +1114,10 @@ private void validateZone(Long zoneId, String errStr) throws ResourceInUseExcept
11221114
for (ClusterVO cluster : clusters) {
11231115
if (cluster.getHypervisorType().equals(HypervisorType.VMware)) {
11241116
throw new ResourceInUseException("Zone has one or more clusters."
1125-
+ " Can't " + errStr + " which already has clusters.");
1126-
}
1127-
}
1128-
}
1129-
}
1130-
1131-
private URI getUri(String url) throws InvalidParameterValueException {
1132-
if (url == null) {
1133-
throw new InvalidParameterValueException("Invalid url.");
1134-
}
1135-
1136-
URI uri = null;
1137-
try {
1138-
uri = new URI(UriUtils.encodeURIComponent(url));
1139-
if (uri.getScheme() == null) {
1140-
throw new InvalidParameterValueException(
1141-
"uri.scheme is null " + url
1142-
+ ", add http:// as a prefix");
1143-
} else if (uri.getScheme().equalsIgnoreCase("http")) {
1144-
if (uri.getHost() == null
1145-
|| uri.getHost().equalsIgnoreCase("")
1146-
|| uri.getPath() == null
1147-
|| uri.getPath().equalsIgnoreCase("")) {
1148-
throw new InvalidParameterValueException(
1149-
"Your host and/or path is wrong. Make sure it's of the format http://hostname/path");
1117+
+ " Can't " + errStr + " which already has clusters.");
11501118
}
11511119
}
1152-
} catch (URISyntaxException e) {
1153-
throw new InvalidParameterValueException(url
1154-
+ " is not a valid uri");
11551120
}
1156-
return uri;
11571121
}
11581122

11591123
@Override

‎plugins/hypervisors/vmware/src/org/apache/cloudstack/api/command/admin/zone/AddVmwareDcCmd.java‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,8 @@ public class AddVmwareDcCmd extends BaseCmd {
4848
@Parameter(name=ApiConstants.NAME, type=CommandType.STRING, required=true, description="Name of VMware datacenter to be added to specified zone.")
4949
private String name;
5050

51-
@Parameter(name=ApiConstants.URL, type=CommandType.STRING, required=false, description="The URL of vCenter.")
52-
private String url;
51+
@Parameter(name=ApiConstants.VCENTER, type=CommandType.STRING, required=true, description="The name/ip of vCenter. Make sure it is IP address or full qualified domain name for host running vCenter server.")
52+
private String vCenter;
5353

5454
@Parameter(name=ApiConstants.USERNAME, type=CommandType.STRING, required=false, description="The Username required to connect to resource.")
5555
private String username;
@@ -64,8 +64,8 @@ public String getName() {
6464
return name;
6565
}
6666

67-
public String getUrl() {
68-
return url;
67+
public String getVcenter() {
68+
return vCenter;
6969
}
7070

7171
public String getUsername() {

‎plugins/hypervisors/vmware/test/com/cloud/hypervisor/vmware/VmwareDatacenterApiUnitTest.java‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,7 @@ public void testSetUp() {
214214
Mockito.when(_vmwareDcZoneMapDao.findByZoneId(1L)).thenReturn(null);
215215
Mockito.when(_vmwareDcZoneMapDao.expunge(1L)).thenReturn(true);
216216
Mockito.when(addCmd.getZoneId()).thenReturn(1L);
217-
Mockito.when(addCmd.getUrl()).thenReturn(url);
217+
Mockito.when(addCmd.getVcenter()).thenReturn(vCenterHost);
218218
Mockito.when(addCmd.getUsername()).thenReturn(user);
219219
Mockito.when(addCmd.getPassword()).thenReturn(password);
220220
Mockito.when(addCmd.getName()).thenReturn(vmwareDcName);
@@ -265,7 +265,7 @@ public void testAddVmwareDcWithNullPassword() throws ResourceInUseException, Ill
265265

266266
//@Test(expected = InvalidParameterValueException.class)
267267
public void testAddVmwareDcWithNullUrl() throws ResourceInUseException, IllegalArgumentException, DiscoveryException, Exception {
268-
Mockito.when(addCmd.getUrl()).thenReturn(null);
268+
Mockito.when(addCmd.getVcenter()).thenReturn(null);
269269
_vmwareDatacenterService.addVmwareDatacenter(addCmd);
270270
}
271271

0 commit comments

Comments
 (0)