Skip to content

Commit b1bb0af

Browse files
author
Vijayendra Bhamidipati
committed
Bug 13127: API error text refer to database ids instead of uuids
Description: Modifying the API functions' exception handling to call addProxyObject() wherever applicable, and removing some wrong calls to addProxyObject() that were put in in an earlier commit for this bug. With this commit, we cover many API functions to use the new exception handling code, but some pieces may still be left out. These will be covered as work in progress, when making changes to the CS API code.
1 parent 2b3b3e2 commit b1bb0af

8 files changed

Lines changed: 553 additions & 91 deletions

File tree

api/src/com/cloud/api/BaseCmd.java

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@
6060
import com.cloud.user.ResourceLimitService;
6161
import com.cloud.utils.Pair;
6262
import com.cloud.utils.component.ComponentLocator;
63+
import com.cloud.utils.AnnotationHelper;
6364
import com.cloud.vm.BareMetalVmService;
6465
import com.cloud.vm.UserVmService;
6566

@@ -485,7 +486,7 @@ public Long finalyzeAccountId(String accountName, Long domainId, Long projectId,
485486
if (!enabledOnly || account.getState() == Account.State.enabled) {
486487
return account.getId();
487488
} else {
488-
throw new PermissionDeniedException("Can't add resources to the account id=" + account.getId() + " in state=" + account.getState() + " as it's no longer active");
489+
throw new PermissionDeniedException("Can't add resources to the account id=" + account.getId() + " in state=" + account.getState() + " as it's no longer active");
489490
}
490491
} else {
491492
throw new InvalidParameterValueException("Unable to find account by name " + accountName + " in domain id=" + domainId);
@@ -498,10 +499,26 @@ public Long finalyzeAccountId(String accountName, Long domainId, Long projectId,
498499
if (!enabledOnly || project.getState() == Project.State.Active) {
499500
return project.getProjectAccountId();
500501
} else {
501-
throw new PermissionDeniedException("Can't add resources to the project id=" + projectId + " in state=" + project.getState() + " as it's no longer active");
502+
PermissionDeniedException ex = new PermissionDeniedException("Can't add resources to the project with specified projectId in state=" + project.getState() + " as it's no longer active");
503+
// Get the VO object's table name.
504+
String tablename = AnnotationHelper.getTableName(project);
505+
if (tablename != null) {
506+
ex.addProxyObject(tablename, projectId, "projectId");
507+
} else {
508+
s_logger.info("\nCould not retrieve table name (annotation) from " + tablename + " VO proxy object\n");
509+
}
510+
throw ex;
502511
}
503512
} else {
504-
throw new InvalidParameterValueException("Unable to find project by id " + projectId);
513+
InvalidParameterValueException ex = new InvalidParameterValueException("Unable to find project with specified projectId");
514+
// Get the VO object's table name.
515+
String tablename = AnnotationHelper.getTableName(project);
516+
if (tablename != null) {
517+
ex.addProxyObject(tablename, projectId, "projectId");
518+
} else {
519+
s_logger.info("\nCould not retrieve table name (annotation) from " + tablename + " VO proxy object\n");
520+
}
521+
throw ex;
505522
}
506523
}
507524
return null;

api/src/com/cloud/api/commands/AddAccountToProjectCmd.java

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@
3232
import com.cloud.exception.InvalidParameterValueException;
3333
import com.cloud.projects.Project;
3434
import com.cloud.user.UserContext;
35+
import com.cloud.utils.AnnotationHelper;
36+
3537

3638
@Implementation(description="Adds acoount to a project", responseObject=SuccessResponse.class, since="3.0.0")
3739
public class AddAccountToProjectCmd extends BaseAsyncCmd {
@@ -101,7 +103,16 @@ public long getEntityOwnerId() {
101103
Project project= _projectService.getProject(projectId);
102104
//verify input parameters
103105
if (project == null) {
104-
throw new InvalidParameterValueException("Unable to find project by id " + projectId);
106+
InvalidParameterValueException ex = new InvalidParameterValueException("Unable to find project with specified id");
107+
// getProject() returns an object of type ProjectVO.
108+
// Get the VO object's table name.
109+
String tablename = AnnotationHelper.getTableName(project);
110+
if (tablename != null) {
111+
ex.addProxyObject(tablename, projectId, "projectId");
112+
} else {
113+
s_logger.info("\nCould not retrieve table name (annotation) from " + tablename + " VO proxy object\n");
114+
}
115+
throw ex;
105116
}
106117

107118
return _projectService.getProjectOwner(projectId).getId();

server/src/com/cloud/network/NetworkManagerImpl.java

Lines changed: 49 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -2037,9 +2037,7 @@ public boolean disassociateIpAddress(long ipAddressId) throws InsufficientAddres
20372037
// Verify input parameters
20382038
IPAddressVO ipVO = _ipAddressDao.findById(ipAddressId);
20392039
if (ipVO == null) {
2040-
InvalidParameterValueException ex = new InvalidParameterValueException("Unable to find ip address by id");
2041-
ex.addProxyObject("user_ip_address", ipAddressId, "ipAddressId");
2042-
throw ex;
2040+
throw new InvalidParameterValueException("Unable to find ip address by id");
20432041
}
20442042

20452043
if (ipVO.getAllocatedTime() == null) {
@@ -2124,9 +2122,7 @@ public List<NetworkVO> getNetworksforOffering(long offeringId, long dataCenterId
21242122
public String getNextAvailableMacAddressInNetwork(long networkId) throws InsufficientAddressCapacityException {
21252123
String mac = _networksDao.getNextAvailableMacAddress(networkId);
21262124
if (mac == null) {
2127-
InsufficientAddressCapacityException ex = new InsufficientAddressCapacityException("Unable to create another mac address", Network.class, networkId);
2128-
ex.addProxyObject("networks", networkId, "networkId");
2129-
throw ex;
2125+
throw new InsufficientAddressCapacityException("Unable to create another mac address", Network.class, networkId);
21302126
}
21312127
return mac;
21322128
}
@@ -2255,7 +2251,16 @@ public Network createNetwork(CreateNetworkCmd cmd) throws InsufficientCapacityEx
22552251
NetworkOfferingVO networkOffering = _networkOfferingDao.findById(networkOfferingId);
22562252
if (networkOffering == null || networkOffering.isSystemOnly()) {
22572253
InvalidParameterValueException ex = new InvalidParameterValueException("Unable to find network offering by specified id");
2258-
ex.addProxyObject("network_offerings", networkOfferingId, "networkOfferingId");
2254+
if (networkOffering != null) {
2255+
// Get the VO object's table name.
2256+
String tablename = AnnotationHelper.getTableName(networkOffering);
2257+
if (tablename != null) {
2258+
ex.addProxyObject(tablename, networkOfferingId, "networkOfferingId");
2259+
} else {
2260+
s_logger.info("\nCould not retrieve table name (annotation) from " + tablename + " VO proxy object\n");
2261+
}
2262+
throw ex;
2263+
}
22592264
throw ex;
22602265
}
22612266
// validate physical network and zone
@@ -2264,9 +2269,7 @@ public Network createNetwork(CreateNetworkCmd cmd) throws InsufficientCapacityEx
22642269
if (physicalNetworkId != null) {
22652270
pNtwk = _physicalNetworkDao.findById(physicalNetworkId);
22662271
if (pNtwk == null) {
2267-
InvalidParameterValueException ex = new InvalidParameterValueException("Unable to find a physical network having the given id");
2268-
ex.addProxyObject("physical_network", physicalNetworkId, "physicalNetworkId");
2269-
throw ex;
2272+
throw new InvalidParameterValueException("Unable to find a physical network having the specified physical network id");
22702273
}
22712274
}
22722275

@@ -2275,11 +2278,21 @@ public Network createNetwork(CreateNetworkCmd cmd) throws InsufficientCapacityEx
22752278
}
22762279

22772280
DataCenter zone = _dcDao.findById(zoneId);
2281+
if (zone == null) {
2282+
throw new InvalidParameterValueException("Specified zone id was not found");
2283+
}
2284+
22782285
if (Grouping.AllocationState.Disabled == zone.getAllocationState() && !_accountMgr.isRootAdmin(caller.getType())) {
22792286
// See DataCenterVO.java
22802287
PermissionDeniedException ex = new PermissionDeniedException("Cannot perform this operation since specified Zone is currently disabled");
2281-
ex.addProxyObject("data_center", zone.getId(), "zoneId");
2282-
throw ex;
2288+
// Get the VO object's table name.
2289+
String tablename = AnnotationHelper.getTableName(zone);
2290+
if (tablename != null) {
2291+
ex.addProxyObject(tablename, zoneId, "zoneId");
2292+
} else {
2293+
s_logger.info("\nCould not retrieve table name (annotation) from " + tablename + " VO proxy object\n");
2294+
}
2295+
throw ex;
22832296
}
22842297

22852298
// Only domain and account ACL types are supported in Acton.
@@ -2333,11 +2346,8 @@ public Network createNetwork(CreateNetworkCmd cmd) throws InsufficientCapacityEx
23332346
}
23342347

23352348
DomainVO domain = _domainDao.findById(domainId);
2336-
if (domain == null) {
2337-
// see DomainVO.java
2338-
InvalidParameterValueException ex = new InvalidParameterValueException("Unable to find domain by specified if");
2339-
ex.addProxyObject("domain", domainId, "domainId");
2340-
throw ex;
2349+
if (domain == null) {
2350+
throw new InvalidParameterValueException("Unable to find domain by specified id");
23412351
}
23422352
_accountMgr.checkAccess(caller, domain);
23432353
}
@@ -2488,8 +2498,14 @@ public Network createGuestNetwork(long networkOfferingId, String name, String di
24882498
if (ntwkOff.getState() != NetworkOffering.State.Enabled) {
24892499
// see NetworkOfferingVO
24902500
InvalidParameterValueException ex = new InvalidParameterValueException("Can't use specified network offering id as its stat is not " + NetworkOffering.State.Enabled);
2491-
ex.addProxyObject("network_offerings", networkOfferingId, "networkOfferingId");
2492-
throw ex;
2501+
// Get the VO object's table name.
2502+
String tablename = AnnotationHelper.getTableName(networkOfferingId);
2503+
if (tablename != null) {
2504+
ex.addProxyObject(tablename, networkOfferingId, "networkOfferingId");
2505+
} else {
2506+
s_logger.info("\nCould not retrieve table name (annotation) from " + tablename + " VO proxy object\n");
2507+
}
2508+
throw ex;
24932509
}
24942510

24952511
// Validate physical network
@@ -2716,19 +2732,15 @@ public List<? extends Network> searchForNetworks(ListNetworksCmd cmd) {
27162732
DomainVO domain = _domainDao.findById(domainId);
27172733
if (domain == null) {
27182734
// see DomainVO.java
2719-
InvalidParameterValueException ex = new InvalidParameterValueException("Specified domain id doesn't exist in the system");
2720-
ex.addProxyObject("domain", domainId, "domainId");
2721-
throw ex;
2735+
throw new InvalidParameterValueException("Specified domain id doesn't exist in the system");
27222736
}
27232737

27242738
_accountMgr.checkAccess(caller, domain);
27252739
if (accountName != null) {
27262740
Account owner = _accountMgr.getActiveAccountByName(accountName, domainId);
27272741
if (owner == null) {
27282742
// see DomainVO.java
2729-
InvalidParameterValueException ex = new InvalidParameterValueException("Unable to find account " + accountName + " in specified domain");
2730-
ex.addProxyObject("domain", domainId, "domainId");
2731-
throw ex;
2743+
throw new InvalidParameterValueException("Unable to find account " + accountName + " in specified domain");
27322744
}
27332745

27342746
_accountMgr.checkAccess(caller, null, true, owner);
@@ -2753,17 +2765,20 @@ public List<? extends Network> searchForNetworks(ListNetworksCmd cmd) {
27532765
} else {
27542766
permittedAccounts.clear();
27552767
Project project = _projectMgr.getProject(projectId);
2756-
if (project == null) {
2757-
// see ProjectVO.java
2758-
InvalidParameterValueException ex = new InvalidParameterValueException("Unable to find project by specified id");
2759-
ex.addProxyObject("projects", projectId, "projectId");
2760-
throw ex;
2768+
if (project == null) {
2769+
throw new InvalidParameterValueException("Unable to find project by specified id");
27612770
}
27622771
if (!_projectMgr.canAccessProjectAccount(caller, project.getProjectAccountId())) {
2763-
// see ProjectVO.java
2772+
// getProject() returns type ProjectVO.
27642773
InvalidParameterValueException ex = new InvalidParameterValueException("Account " + caller + " cannot access specified project id");
2765-
ex.addProxyObject("projects", projectId, "projectId");
2766-
throw ex;
2774+
// Get the VO object's table name.
2775+
String tablename = AnnotationHelper.getTableName(project);
2776+
if (tablename != null) {
2777+
ex.addProxyObject(tablename, projectId, "projectId");
2778+
} else {
2779+
s_logger.info("\nCould not retrieve table name (annotation) from " + tablename + " VO proxy object\n");
2780+
}
2781+
throw ex;
27672782
}
27682783
permittedAccounts.add(project.getProjectAccountId());
27692784
}
@@ -3571,7 +3586,7 @@ public Map<Capability, String> getNetworkServiceCapabilities(long networkId, Ser
35713586

35723587
if (!areServicesSupportedInNetwork(networkId, service)) {
35733588
// TBD: networkId to uuid. No VO object being passed. So we will need to call
3574-
// addProxyObject with hardcoded tablename.
3589+
// addProxyObject with hardcoded tablename. Or we should probably look up the correct dao proxy object.
35753590
throw new UnsupportedServiceException("Service " + service.getName() + " is not supported in the network id=" + networkId);
35763591
}
35773592

0 commit comments

Comments
 (0)