Skip to content

Commit a989180

Browse files
committed
api,server,client: fix issues around missing placeholders
Signed-off-by: Abhishek Kumar <abhishek.mrt22@gmail.com>
1 parent aa3dd38 commit a989180

6 files changed

Lines changed: 119 additions & 88 deletions

File tree

‎api/src/main/java/org/apache/cloudstack/api/command/user/vm/BaseDeployVMCmd.java‎

Lines changed: 48 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,6 @@
5656

5757
import com.cloud.agent.api.LogLevel;
5858
import com.cloud.event.EventTypes;
59-
import com.cloud.exception.InvalidParameterValueException;
6059
import com.cloud.hypervisor.Hypervisor.HypervisorType;
6160
import com.cloud.network.Network;
6261
import com.cloud.network.Network.IpAddresses;
@@ -323,10 +322,10 @@ public ApiConstants.BootType getBootType() {
323322
String type = bootType.trim().toUpperCase();
324323
return ApiConstants.BootType.valueOf(type);
325324
} catch (IllegalArgumentException e) {
326-
String errMesg = "Invalid bootType " + bootType + "Specified for vm " + getName()
327-
+ " Valid values are: " + Arrays.toString(ApiConstants.BootType.values());
328-
logger.warn(errMesg);
329-
throw new InvalidParameterValueException(errMesg);
325+
logger.warn("Invalid bootType {} specified for vm {}", bootType, getName());
326+
throw Exceptions.invalidParameterValueException("vm.deploy.boot.type.invalid", Map.of(
327+
"bootType", bootType,
328+
"validValues", Arrays.toString(ApiConstants.BootType.values())));
330329
}
331330
}
332331
return null;
@@ -376,17 +375,17 @@ public ApiConstants.BootMode getBootMode() {
376375
String mode = bootMode.trim().toUpperCase();
377376
return ApiConstants.BootMode.valueOf(mode);
378377
} catch (IllegalArgumentException e) {
379-
String msg = String.format("Invalid %s: %s specified for VM: %s. Valid values are: %s",
380-
ApiConstants.BOOT_MODE, bootMode, getName(), Arrays.toString(ApiConstants.BootMode.values()));
381-
logger.error(msg);
382-
throw new InvalidParameterValueException(msg);
378+
logger.error("Invalid {}: {} specified for VM: {}", ApiConstants.BOOT_MODE, bootMode, getName());
379+
throw Exceptions.invalidParameterValueException("vm.deploy.boot.mode.invalid", Map.of(
380+
"bootMode", bootMode,
381+
"validValues", Arrays.toString(ApiConstants.BootMode.values())));
383382
}
384383
}
385384
if (ApiConstants.BootType.UEFI.equals(getBootType())) {
386-
String msg = String.format("%s must be specified for the VM with boot type: %s. Valid values are: %s",
387-
ApiConstants.BOOT_MODE, getBootType(), Arrays.toString(ApiConstants.BootMode.values()));
388-
logger.error(msg);
389-
throw new InvalidParameterValueException(msg);
385+
logger.error("{} must be specified for the VM with boot type: {}", ApiConstants.BOOT_MODE, getBootType());
386+
throw Exceptions.invalidParameterValueException("vm.deploy.boot.mode.required.for.uefi", Map.of(
387+
"bootType", getBootType(),
388+
"validValues", Arrays.toString(ApiConstants.BootMode.values())));
390389
}
391390
return null;
392391
}
@@ -422,7 +421,9 @@ public Map<Integer, Long> getVmNetworkMap() {
422421
logger.trace(String.format("nic, '%s', goes on net, '%s'", nic, networkUuid));
423422
}
424423
if (nic == null || StringUtils.isEmpty(networkUuid) || _entityMgr.findByUuid(Network.class, networkUuid) == null) {
425-
throw new InvalidParameterValueException(String.format("Network ID: %s for NIC ID: %s is invalid", networkUuid, nic));
424+
throw Exceptions.invalidParameterValueException("vm.deploy.vapp.nic.network.invalid", Map.of(
425+
"networkId", String.valueOf(networkUuid),
426+
"nicId", String.valueOf(nic)));
426427
}
427428
map.put(nic, _entityMgr.findByUuid(Network.class, networkUuid).getId());
428429
}
@@ -492,23 +493,28 @@ public VMLeaseManager.ExpiryAction getLeaseExpiryAction() {
492493
}
493494
VMLeaseManager.ExpiryAction action = EnumUtils.getEnumIgnoreCase(VMLeaseManager.ExpiryAction.class, leaseExpiryAction);
494495
if (action == null) {
495-
throw new InvalidParameterValueException("Invalid value configured for leaseexpiryaction, valid values are: " +
496-
com.cloud.utils.EnumUtils.listValues(VMLeaseManager.ExpiryAction.values()));
496+
throw Exceptions.invalidParameterValueException("vm.deploy.lease.expiry.action.invalid", Map.of(
497+
"value", leaseExpiryAction,
498+
"validValues", com.cloud.utils.EnumUtils.listValues(VMLeaseManager.ExpiryAction.values())));
497499
}
498500
return action;
499501
}
500502

501503
public List<Long> getNetworkIds() {
502504
if (MapUtils.isNotEmpty(vAppNetworks)) {
503505
if (CollectionUtils.isNotEmpty(networkIds) || ipAddress != null || getIp6Address() != null || MapUtils.isNotEmpty(ipToNetworkList)) {
504-
throw new InvalidParameterValueException(String.format("%s can't be specified along with %s, %s, %s", ApiConstants.NIC_NETWORK_LIST, ApiConstants.NETWORK_IDS, ApiConstants.IP_ADDRESS, ApiConstants.IP_NETWORK_LIST));
506+
throw Exceptions.invalidParameterValueException("vm.deploy.nic.network.list.mutually.exclusive", Map.of(
507+
"nicNetworkList", ApiConstants.NIC_NETWORK_LIST,
508+
"networkIds", ApiConstants.NETWORK_IDS,
509+
"ipAddress", ApiConstants.IP_ADDRESS,
510+
"ipNetworkList", ApiConstants.IP_NETWORK_LIST));
505511
} else {
506512
return new ArrayList<>();
507513
}
508514
}
509515
if (ipToNetworkList != null && !ipToNetworkList.isEmpty()) {
510516
if ((networkIds != null && !networkIds.isEmpty()) || ipAddress != null || getIp6Address() != null) {
511-
throw new InvalidParameterValueException("ipToNetworkMap can't be specified along with networkIds or ipAddress");
517+
throw Exceptions.invalidParameterValueException("vm.deploy.ip.to.network.map.mutually.exclusive");
512518
} else {
513519
List<Long> networks = new ArrayList<Long>();
514520
networks.addAll(getIpToNetworkMap().keySet());
@@ -541,7 +547,7 @@ public List<VmDiskInfo> getDataDiskInfoList() {
541547
return null;
542548
}
543549
if (dataDiskTemplateToDiskOfferingList != null) {
544-
throw new InvalidParameterValueException("datadisktemplatetodiskofferinglist parameter can't be specified along with datadisksdetails parameter");
550+
throw Exceptions.invalidParameterValueException("vm.deploy.datadiskdetails.with.datadisktemplatetodiskofferinglist");
545551
}
546552
List<VmDiskInfo> vmDiskInfoList = new ArrayList<>();
547553
Collection dataDisksCollection = dataDisksDetails.values();
@@ -550,37 +556,37 @@ public List<VmDiskInfo> getDataDiskInfoList() {
550556
HashMap<String, String> dataDisk = (HashMap<String, String>)iter.next();
551557
String diskOfferingUuid = dataDisk.get(ApiConstants.DISK_OFFERING_ID);
552558
if (diskOfferingUuid == null) {
553-
throw new InvalidParameterValueException("diskofferingid parameter is required for datadiskdetails");
559+
throw Exceptions.invalidParameterValueException("vm.deploy.datadiskdetails.diskofferingid.required");
554560
}
555561
DiskOffering diskOffering = _entityMgr.findByUuid(DiskOffering.class, diskOfferingUuid);
556562
if (diskOffering == null) {
557-
throw new InvalidParameterValueException("Unable to find disk offering " + diskOfferingUuid);
563+
throw Exceptions.invalidParameterValueException("vm.deploy.datadiskdetails.diskoffering.not.found", Map.of("diskOfferingId", diskOfferingUuid));
558564
}
559565
if (diskOffering.isComputeOnly()) {
560-
throw new InvalidParameterValueException(String.format("The disk offering id %d provided is directly mapped to a service offering, please provide an individual disk offering", diskOffering.getUuid()));
566+
throw Exceptions.invalidParameterValueException("vm.deploy.datadiskdetails.diskoffering.compute.only", Map.of("diskOffering", diskOffering));
561567
}
562568

563569
Long size = null;
564570
Long minIops = null;
565571
Long maxIops = null;
566572
if (dataDisk.get(ApiConstants.DEVICE_ID) == null) {
567-
throw new InvalidParameterValueException("deviceid parameter is required for datadiskdetails");
573+
throw Exceptions.invalidParameterValueException("vm.deploy.datadiskdetails.deviceid.required");
568574
}
569575
Long deviceId = Long.parseLong(dataDisk.get(ApiConstants.DEVICE_ID));
570576
if (diskOffering.isCustomized()) {
571577
if (dataDisk.get(ApiConstants.SIZE) == null) {
572-
throw new InvalidParameterValueException("Size is required for custom disk offering");
578+
throw Exceptions.invalidParameterValueException("vm.deploy.datadiskdetails.size.required", Map.of("diskOffering", diskOffering));
573579
}
574580
size = Long.parseLong(dataDisk.get(ApiConstants.SIZE));
575581
} else {
576582
size = diskOffering.getDiskSize() / (1024 * 1024 * 1024);
577583
}
578584
if (diskOffering.isCustomizedIops() != null && diskOffering.isCustomizedIops()) {
579585
if (dataDisk.get(ApiConstants.MIN_IOPS) == null) {
580-
throw new InvalidParameterValueException("Min IOPS is required for custom disk offering");
586+
throw Exceptions.invalidParameterValueException("vm.deploy.datadiskdetails.miniops.required", Map.of("diskOffering", diskOffering));
581587
}
582588
if (dataDisk.get(ApiConstants.MAX_IOPS) == null) {
583-
throw new InvalidParameterValueException("Max IOPS is required for custom disk offering");
589+
throw Exceptions.invalidParameterValueException("vm.deploy.datadiskdetails.maxiops.required", Map.of("diskOffering", diskOffering));
584590
}
585591
minIops = Long.parseLong(dataDisk.get(ApiConstants.MIN_IOPS));
586592
maxIops = Long.parseLong(dataDisk.get(ApiConstants.MAX_IOPS));
@@ -602,7 +608,7 @@ public boolean getStartVm() {
602608

603609
public Map<Long, IpAddresses> getIpToNetworkMap() {
604610
if ((networkIds != null || ipAddress != null || getIp6Address() != null) && ipToNetworkList != null) {
605-
throw new InvalidParameterValueException("NetworkIds and ipAddress can't be specified along with ipToNetworkMap parameter");
611+
throw Exceptions.invalidParameterValueException("vm.deploy.network.ids.ip.address.with.ip.to.network.map");
606612
}
607613
LinkedHashMap<Long, IpAddresses> ipToNetworkMap = null;
608614
if (ipToNetworkList != null && !ipToNetworkList.isEmpty()) {
@@ -630,9 +636,9 @@ private IpAddresses getIpAddressesFromIpMap(HashMap<String, String> ips) {
630636
}
631637
if (requestedMac != null) {
632638
if(!NetUtils.isValidMac(requestedMac)) {
633-
throw new InvalidParameterValueException("Mac address is not valid: " + requestedMac);
639+
throw Exceptions.invalidParameterValueException("vm.deploy.mac.address.invalid", Map.of("macAddress", requestedMac));
634640
} else if(!NetUtils.isUnicastMac(requestedMac)) {
635-
throw new InvalidParameterValueException("Mac address is not unicast: " + requestedMac);
641+
throw Exceptions.invalidParameterValueException("vm.deploy.mac.address.not.unicast", Map.of("macAddress", requestedMac));
636642
}
637643
requestedMac = NetUtils.standardizeMacAddress(requestedMac);
638644
}
@@ -674,16 +680,16 @@ public String getMacAddress() {
674680
return null;
675681
}
676682
if(!NetUtils.isValidMac(macAddress)) {
677-
throw new InvalidParameterValueException("Mac address is not valid: " + macAddress);
683+
throw Exceptions.invalidParameterValueException("vm.deploy.mac.address.invalid", Map.of("macAddress", macAddress));
678684
} else if(!NetUtils.isUnicastMac(macAddress)) {
679-
throw new InvalidParameterValueException("Mac address is not unicast: " + macAddress);
685+
throw Exceptions.invalidParameterValueException("vm.deploy.mac.address.not.unicast", Map.of("macAddress", macAddress));
680686
}
681687
return NetUtils.standardizeMacAddress(macAddress);
682688
}
683689

684690
public List<Long> getAffinityGroupIdList() {
685691
if (affinityGroupNameList != null && affinityGroupIdList != null) {
686-
throw new InvalidParameterValueException("affinitygroupids parameter is mutually exclusive with affinitygroupnames parameter");
692+
throw Exceptions.invalidParameterValueException("vm.deploy.affinity.group.ids.names.mutually.exclusive");
687693
}
688694

689695
// transform group names to ids here
@@ -692,7 +698,7 @@ public List<Long> getAffinityGroupIdList() {
692698
for (String groupName : affinityGroupNameList) {
693699
Long groupId = _responseGenerator.getAffinityGroupId(groupName, getEntityOwnerId());
694700
if (groupId == null) {
695-
throw new InvalidParameterValueException("Unable to find affinity group by name " + groupName);
701+
throw Exceptions.invalidParameterValueException("vm.deploy.affinity.group.not.found.by.name", Map.of("groupName", groupName));
696702
} else {
697703
affinityGroupIds.add(groupId);
698704
}
@@ -741,7 +747,7 @@ public Map<String, Map<Integer, String>> getDhcpOptionsMap() {
741747

742748
public Map<Long, DiskOffering> getDataDiskTemplateToDiskOfferingMap() {
743749
if (diskOfferingId != null && dataDiskTemplateToDiskOfferingList != null) {
744-
throw new InvalidParameterValueException("diskofferingid parameter can't be specified along with datadisktemplatetodiskofferinglist parameter");
750+
throw Exceptions.invalidParameterValueException("vm.deploy.diskofferingid.with.datadisktemplatetodiskofferinglist");
745751
}
746752
if (MapUtils.isEmpty(dataDiskTemplateToDiskOfferingList)) {
747753
return new HashMap<Long, DiskOffering>();
@@ -756,14 +762,16 @@ public Map<Long, DiskOffering> getDataDiskTemplateToDiskOfferingMap() {
756762
if (dataDiskTemplate == null) {
757763
dataDiskTemplate = _entityMgr.findById(VirtualMachineTemplate.class, dataDiskTemplates.get("datadisktemplateid"));
758764
if (dataDiskTemplate == null)
759-
throw new InvalidParameterValueException("Unable to translate and find entity with datadisktemplateid " + dataDiskTemplates.get("datadisktemplateid"));
765+
throw Exceptions.invalidParameterValueException("vm.deploy.datadisktemplateid.not.found",
766+
Map.of("templateId", String.valueOf(dataDiskTemplates.get("datadisktemplateid"))));
760767
}
761768
dataDiskTemplateId = dataDiskTemplate.getId();
762769
dataDiskOffering = _entityMgr.findByUuid(DiskOffering.class, dataDiskTemplates.get("diskofferingid"));
763770
if (dataDiskOffering == null) {
764771
dataDiskOffering = _entityMgr.findById(DiskOffering.class, dataDiskTemplates.get("diskofferingid"));
765772
if (dataDiskOffering == null)
766-
throw new InvalidParameterValueException("Unable to translate and find entity with diskofferingId " + dataDiskTemplates.get("diskofferingid"));
773+
throw Exceptions.invalidParameterValueException("vm.deploy.datadisktemplate.diskofferingid.not.found",
774+
Map.of("diskOfferingId", String.valueOf(dataDiskTemplates.get("diskofferingid"))));
767775
}
768776
dataDiskTemplateToDiskOfferingMap.put(dataDiskTemplateId, dataDiskOffering);
769777
}
@@ -796,9 +804,10 @@ public IoDriverPolicy getIoDriverPolicy() {
796804
String policyType = ioDriverPolicy.trim().toUpperCase();
797805
return IoDriverPolicy.valueOf(policyType);
798806
} catch (IllegalArgumentException e) {
799-
String errMesg = String.format("Invalid io policy %s specified for vm %s. Valid values are: %s", ioDriverPolicy, getName(), Arrays.toString(IoDriverPolicy.values()));
800-
logger.warn(errMesg);
801-
throw new InvalidParameterValueException(errMesg);
807+
logger.warn("Invalid io policy {} specified for vm {}", ioDriverPolicy, getName());
808+
throw Exceptions.invalidParameterValueException("vm.deploy.io.driver.policy.invalid", Map.of(
809+
"ioDriverPolicy", ioDriverPolicy,
810+
"validValues", Arrays.toString(IoDriverPolicy.values())));
802811
}
803812
}
804813
return null;

‎api/src/main/java/org/apache/cloudstack/api/command/user/vm/UpdateVMCmd.java‎

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,10 @@
1717
package org.apache.cloudstack.api.command.user.vm;
1818

1919
import com.cloud.exception.InsufficientCapacityException;
20-
import com.cloud.exception.InvalidParameterValueException;
2120
import com.cloud.exception.ResourceUnavailableException;
2221
import com.cloud.user.Account;
2322
import com.cloud.uservm.UserVm;
2423
import com.cloud.utils.StringUtils;
25-
import com.cloud.utils.exception.CloudRuntimeException;
2624
import com.cloud.utils.net.Dhcp;
2725
import com.cloud.vm.VirtualMachine;
2826
import org.apache.cloudstack.acl.RoleType;
@@ -43,6 +41,7 @@
4341
import org.apache.cloudstack.api.response.UserDataResponse;
4442
import org.apache.cloudstack.api.response.UserVmResponse;
4543
import org.apache.cloudstack.context.CallContext;
44+
import org.apache.cloudstack.error.Exceptions;
4645
import org.apache.cloudstack.vm.lease.VMLeaseManager;
4746
import org.apache.commons.lang3.EnumUtils;
4847

@@ -305,12 +304,7 @@ public long getEntityOwnerId() {
305304
@Override
306305
public void execute() throws ResourceUnavailableException, InsufficientCapacityException, ServerApiException {
307306
CallContext.current().setEventDetails("Instance Id: " + this._uuidMgr.getUuid(VirtualMachine.class, getId()));
308-
UserVm result = null;
309-
try {
310-
result = _userVmService.updateVirtualMachine(this);
311-
} catch (CloudRuntimeException e) {
312-
throw new CloudRuntimeException(String.format("Failed to update Instance, due to: %s", e.getLocalizedMessage()), e);
313-
}
307+
UserVm result = _userVmService.updateVirtualMachine(this);
314308
if (result != null) {
315309
UserVmResponse response = _responseGenerator.createUserVmResponse(getResponseView(), "virtualmachine", result).get(0);
316310
response.setResponseName(getCommandName());
@@ -348,8 +342,9 @@ public VMLeaseManager.ExpiryAction getLeaseExpiryAction() {
348342
}
349343
VMLeaseManager.ExpiryAction action = EnumUtils.getEnumIgnoreCase(VMLeaseManager.ExpiryAction.class, leaseExpiryAction);
350344
if (action == null) {
351-
throw new InvalidParameterValueException("Invalid value configured for leaseexpiryaction, valid values are: " +
352-
com.cloud.utils.EnumUtils.listValues(VMLeaseManager.ExpiryAction.values()));
345+
throw Exceptions.invalidParameterValueException("vm.update.lease.expiry.action.invalid", Map.of(
346+
"value", leaseExpiryAction,
347+
"validValues", com.cloud.utils.EnumUtils.listValues(VMLeaseManager.ExpiryAction.values())));
353348
}
354349
return action;
355350
}

0 commit comments

Comments
 (0)