Skip to content

Commit

Permalink
Updated invalid parameter/value to com.cloud.exception.InvalidParamet…
Browse files Browse the repository at this point in the history
…erValueException (#9455)
  • Loading branch information
sureshanaparti committed Aug 28, 2024
1 parent 70131be commit c05edc4
Show file tree
Hide file tree
Showing 15 changed files with 72 additions and 85 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,6 @@
// under the License.
package org.apache.cloudstack.api.command.user.vm;

import java.security.InvalidParameterException;


import org.apache.cloudstack.acl.SecurityChecker.AccessType;
import org.apache.cloudstack.api.ACL;
import org.apache.cloudstack.api.APICommand;
Expand All @@ -28,6 +25,7 @@
import org.apache.cloudstack.api.response.GetVMPasswordResponse;
import org.apache.cloudstack.api.response.UserVmResponse;

import com.cloud.exception.InvalidParameterValueException;
import com.cloud.user.Account;
import com.cloud.uservm.UserVm;
import com.cloud.vm.VirtualMachine;
Expand Down Expand Up @@ -61,7 +59,7 @@ public Long getId() {
public void execute() {
String passwd = _mgr.getVMPassword(this);
if (passwd == null || passwd.equals(""))
throw new InvalidParameterException("No password for VM with id '" + getId() + "' found.");
throw new InvalidParameterValueException("No password for VM with id '" + getId() + "' found.");

setResponseObject(new GetVMPasswordResponse(getCommandName(), passwd));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,6 @@
import org.mockito.Mockito;
import org.mockito.MockitoAnnotations;

import java.security.InvalidParameterException;

public class CreateVMScheduleCmdTest {
@Mock
public VMScheduleManager vmScheduleManager;
Expand Down Expand Up @@ -70,11 +68,11 @@ public void testSuccessfulExecution() {
/**
* given: "We have a VMScheduleManager and CreateVMScheduleCmd"
* when: "CreateVMScheduleCmd is executed with an invalid parameter"
* then: "an InvalidParameterException is thrown"
* then: "an InvalidParameterValueException is thrown"
*/
@Test(expected = InvalidParameterException.class)
public void testInvalidParameterException() {
Mockito.when(vmScheduleManager.createSchedule(createVMScheduleCmd)).thenThrow(InvalidParameterException.class);
@Test(expected = InvalidParameterValueException.class)
public void testInvalidParameterValueException() {
Mockito.when(vmScheduleManager.createSchedule(createVMScheduleCmd)).thenThrow(InvalidParameterValueException.class);
createVMScheduleCmd.execute();
}

Expand All @@ -94,7 +92,7 @@ public void testSuccessfulGetEntityOwnerId() {
/**
* given: "We have an EntityManager and CreateVMScheduleCmd"
* when: "CreateVMScheduleCmd.getEntityOwnerId is executed for a VM which doesn't exist"
* then: "an InvalidParameterException is thrown"
* then: "an InvalidParameterValueException is thrown"
*/
@Test(expected = InvalidParameterValueException.class)
public void testFailureGetEntityOwnerId() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,6 @@
import org.mockito.Mockito;
import org.mockito.MockitoAnnotations;

import java.security.InvalidParameterException;

public class DeleteVMScheduleCmdTest {
@Mock
public VMScheduleManager vmScheduleManager;
Expand Down Expand Up @@ -89,11 +87,11 @@ public void testServerApiException() {
/**
* given: "We have a VMScheduleManager and DeleteVMScheduleCmd"
* when: "DeleteVMScheduleCmd is executed with an invalid parameter"
* then: "an InvalidParameterException is thrown"
* then: "an InvalidParameterValueException is thrown"
*/
@Test(expected = InvalidParameterException.class)
public void testInvalidParameterException() {
Mockito.when(vmScheduleManager.removeSchedule(deleteVMScheduleCmd)).thenThrow(InvalidParameterException.class);
@Test(expected = InvalidParameterValueException.class)
public void testInvalidParameterValueException() {
Mockito.when(vmScheduleManager.removeSchedule(deleteVMScheduleCmd)).thenThrow(InvalidParameterValueException.class);
deleteVMScheduleCmd.execute();
}

Expand All @@ -113,7 +111,7 @@ public void testSuccessfulGetEntityOwnerId() {
/**
* given: "We have an EntityManager and DeleteVMScheduleCmd"
* when: "DeleteVMScheduleCmd.getEntityOwnerId is executed for a VM which doesn't exist"
* then: "an InvalidParameterException is thrown"
* then: "an InvalidParameterValueException is thrown"
*/
@Test(expected = InvalidParameterValueException.class)
public void testFailureGetEntityOwnerId() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
*/
package org.apache.cloudstack.api.command.user.vm;

import com.cloud.exception.InvalidParameterValueException;
import org.apache.cloudstack.api.response.ListResponse;
import org.apache.cloudstack.api.response.VMScheduleResponse;
import org.apache.cloudstack.vm.schedule.VMScheduleManager;
Expand All @@ -30,7 +31,6 @@
import org.mockito.Mockito;
import org.mockito.MockitoAnnotations;

import java.security.InvalidParameterException;
import java.util.ArrayList;
import java.util.Collections;

Expand Down Expand Up @@ -88,11 +88,11 @@ public void testNonEmptyResponse() {
/**
* given: "We have a VMScheduleManager and ListVMScheduleCmd"
* when: "ListVMScheduleCmd is executed with an invalid parameter"
* then: "an InvalidParameterException is thrown"
* then: "an InvalidParameterValueException is thrown"
*/
@Test(expected = InvalidParameterException.class)
public void testInvalidParameterException() {
Mockito.when(vmScheduleManager.listSchedule(listVMScheduleCmd)).thenThrow(InvalidParameterException.class);
@Test(expected = InvalidParameterValueException.class)
public void testInvalidParameterValueException() {
Mockito.when(vmScheduleManager.listSchedule(listVMScheduleCmd)).thenThrow(InvalidParameterValueException.class);
listVMScheduleCmd.execute();
ListResponse<VMScheduleResponse> actualResponseObject = (ListResponse<VMScheduleResponse>) listVMScheduleCmd.getResponseObject();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,6 @@
import org.mockito.Mockito;
import org.mockito.MockitoAnnotations;

import java.security.InvalidParameterException;

public class UpdateVMScheduleCmdTest {
@Mock
public VMScheduleManager vmScheduleManager;
Expand Down Expand Up @@ -71,11 +69,11 @@ public void testSuccessfulExecution() {
/**
* given: "We have a VMScheduleManager and UpdateVMScheduleCmd"
* when: "UpdateVMScheduleCmd is executed with an invalid parameter"
* then: "an InvalidParameterException is thrown"
* then: "an InvalidParameterValueException is thrown"
*/
@Test(expected = InvalidParameterException.class)
public void testInvalidParameterException() {
Mockito.when(vmScheduleManager.updateSchedule(updateVMScheduleCmd)).thenThrow(InvalidParameterException.class);
@Test(expected = InvalidParameterValueException.class)
public void testInvalidParameterValueException() {
Mockito.when(vmScheduleManager.updateSchedule(updateVMScheduleCmd)).thenThrow(InvalidParameterValueException.class);
updateVMScheduleCmd.execute();
}

Expand All @@ -99,7 +97,7 @@ public void testSuccessfulGetEntityOwnerId() {
/**
* given: "We have an EntityManager and UpdateVMScheduleCmd"
* when: "UpdateVMScheduleCmd.getEntityOwnerId is executed for a VM Schedule which doesn't exist"
* then: "an InvalidParameterException is thrown"
* then: "an InvalidParameterValueException is thrown"
*/
@Test(expected = InvalidParameterValueException.class)
public void testFailureGetEntityOwnerId() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

package org.apache.cloudstack.kvm.ha;

import com.cloud.exception.InvalidParameterValueException;
import com.cloud.host.Host;
import com.cloud.hypervisor.Hypervisor;

Expand All @@ -36,7 +37,6 @@
import org.joda.time.DateTime;

import javax.inject.Inject;
import java.security.InvalidParameterException;

public final class KVMHAProvider extends HAAbstractHostProvider implements HAProvider<Host>, Configurable {

Expand Down Expand Up @@ -129,7 +129,7 @@ public Object getConfigValue(final HAProviderConfig name, final Host host) {
case MaxDegradedWaitTimeout:
return KVMHAConfig.KvmHADegradedMaxPeriod.valueIn(clusterId);
default:
throw new InvalidParameterException("Unknown HAProviderConfig " + name.toString());
throw new InvalidParameterValueException("Unknown HAProviderConfig " + name.toString());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
package org.apache.cloudstack.ha;

import com.cloud.api.response.SimulatorHAStateResponse;
import com.cloud.exception.InvalidParameterValueException;
import com.cloud.host.Host;
import com.cloud.hypervisor.Hypervisor;
import com.cloud.utils.fsm.StateListener;
Expand All @@ -30,7 +31,6 @@
import org.joda.time.DateTime;

import javax.inject.Inject;
import java.security.InvalidParameterException;
import java.util.Collections;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -129,7 +129,7 @@ public Object getConfigValue(final HAProvider.HAProviderConfig name, final Host
case RecoveryWaitTimeout:
return 1L;
default:
throw new InvalidParameterException("Unknown HAProviderConfig " + name.toString());
throw new InvalidParameterValueException("Unknown HAProviderConfig " + name.toString());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@
// under the License.
package org.apache.cloudstack.api.command.user.kubernetes.cluster;

import java.security.InvalidParameterException;

import javax.inject.Inject;

import com.cloud.exception.InvalidParameterValueException;
Expand Down Expand Up @@ -227,7 +225,7 @@ public String getDockerRegistryUrl() {
public Long getNodeRootDiskSize() {
if (nodeRootDiskSize != null) {
if (nodeRootDiskSize < DEFAULT_NODE_ROOT_DISK_SIZE) {
throw new InvalidParameterException("Provided node root disk size is lesser than default size of " + DEFAULT_NODE_ROOT_DISK_SIZE +"GB");
throw new InvalidParameterValueException("Provided node root disk size is lesser than default size of " + DEFAULT_NODE_ROOT_DISK_SIZE +"GB");
}
return nodeRootDiskSize;
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@

import java.net.MalformedURLException;
import java.net.URL;
import java.security.InvalidParameterException;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
Expand Down Expand Up @@ -69,6 +68,7 @@
import com.cloud.agent.api.StartupCommand;
import com.cloud.host.Host;
import com.cloud.host.Host.Type;
import com.cloud.exception.InvalidParameterValueException;
import com.cloud.resource.ServerResource;

public class OpenDaylightControllerResource implements ServerResource {
Expand Down Expand Up @@ -332,11 +332,11 @@ private Answer executeRequest(AddHypervisorCommand cmd) {

private void updateConfiguration() {
if (!configuration.containsKey("url") || !configuration.containsKey("username") || !configuration.containsKey("password"))
throw new InvalidParameterException("OpenDaylightControllerResource needs a url, username and password.");
throw new InvalidParameterValueException("OpenDaylightControllerResource needs a url, username and password.");
try {
controllerUrl = new URL((String)configuration.get("url"));
} catch (MalformedURLException e) {
throw new InvalidParameterException("OpenDaylightControllerResource found an invalid controller url");
throw new InvalidParameterValueException("OpenDaylightControllerResource found an invalid controller url");
}
controllerUsername = (String)configuration.get("username");
controllerPassword = (String)configuration.get("password");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import com.cloud.domain.Domain;
import com.cloud.domain.DomainVO;
import com.cloud.domain.dao.DomainDao;
import com.cloud.exception.InvalidParameterValueException;
import com.cloud.exception.ResourceUnavailableException;
import com.cloud.host.Host;
import com.cloud.host.HostVO;
Expand Down Expand Up @@ -219,7 +220,6 @@
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.security.InvalidParameterException;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
Expand Down Expand Up @@ -2466,7 +2466,7 @@ public boolean applyLBHealthMonitor(final long lbId) {
boolean success = true;
LoadBalancerVO loadBalancer = loadBalancerDao.findById(lbId);
if (loadBalancer == null) {
throw new InvalidParameterException("Invalid Load balancer Id:" + lbId);
throw new InvalidParameterValueException("Invalid Load balancer Id:" + lbId);
}

if (loadBalancer.getState() == FirewallRule.State.Active) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
package org.apache.cloudstack.storage.datastore.util;

import com.cloud.agent.api.Answer;
import com.cloud.exception.InvalidParameterValueException;
import com.cloud.utils.Pair;
import com.cloud.utils.exception.CloudRuntimeException;
import com.google.gson.Gson;
Expand Down Expand Up @@ -47,7 +48,6 @@
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.UriBuilder;
import java.net.ConnectException;
import java.security.InvalidParameterException;
import java.security.SecureRandom;
import java.security.cert.X509Certificate;
import java.util.ArrayList;
Expand Down Expand Up @@ -196,8 +196,8 @@ public static ElastiCenterClient getElastistorRestClient() {

} catch (InvalidCredentialsException e) {
throw new CloudRuntimeException("InvalidCredentialsException:" + e.getMessage(), e);
} catch (InvalidParameterException e) {
throw new CloudRuntimeException("InvalidParameterException:" + e.getMessage(), e);
} catch (InvalidParameterValueException e) {
throw new CloudRuntimeException("InvalidParameterValueException:" + e.getMessage(), e);
} catch (SSLHandshakeException e) {
throw new CloudRuntimeException("SSLHandshakeException:" + e.getMessage(), e);
} catch (ServiceUnavailableException e) {
Expand Down Expand Up @@ -1050,22 +1050,22 @@ private static final class ElastiCenterClient {
private String queryparamapikey = "apikey";
private String queryparamresponse = "response";

public ElastiCenterClient(String address, String key) throws InvalidCredentialsException, InvalidParameterException, SSLHandshakeException, ServiceUnavailableException {
public ElastiCenterClient(String address, String key) throws InvalidCredentialsException, InvalidParameterValueException, SSLHandshakeException, ServiceUnavailableException {
elastiCenterAddress = address;
apiKey = key;
initialize();
}

public void initialize() throws InvalidParameterException, SSLHandshakeException, InvalidCredentialsException, ServiceUnavailableException {
public void initialize() throws InvalidParameterValueException, SSLHandshakeException, InvalidCredentialsException, ServiceUnavailableException {

if (apiKey == null || apiKey.trim().isEmpty()) {
throw new InvalidParameterException("Unable to initialize. Please specify a valid API Key.");
throw new InvalidParameterValueException("Unable to initialize. Please specify a valid API Key.");
}

if (elastiCenterAddress == null || elastiCenterAddress.trim().isEmpty()) {
// TODO : Validate the format, like valid IP address or
// hostname.
throw new InvalidParameterException("Unable to initialize. Please specify a valid ElastiCenter IP Address or Hostname.");
throw new InvalidParameterValueException("Unable to initialize. Please specify a valid ElastiCenter IP Address or Hostname.");
}

if (ignoreSSLCertificate) {
Expand Down Expand Up @@ -1143,7 +1143,7 @@ public Object executeCommand(String command, List<Pair<String, String>> params,
}

if (command == null || command.trim().isEmpty()) {
throw new InvalidParameterException("No command to execute.");
throw new InvalidParameterValueException("No command to execute.");
}

try {
Expand Down Expand Up @@ -1175,9 +1175,9 @@ public Object executeCommand(String command, List<Pair<String, String>> params,
if (401 == response.getStatus()) {
throw new InvalidCredentialsException("Please specify a valid API Key.");
} else if (431 == response.getStatus()) {
throw new InvalidParameterException(response.getHeaders().getFirst("X-Description"));
throw new InvalidParameterValueException(response.getHeaders().getFirst("X-Description"));
} else if (432 == response.getStatus()) {
throw new InvalidParameterException(command + " does not exist on the ElastiCenter server. Please specify a valid command or contact your ElastiCenter Administrator.");
throw new InvalidParameterValueException(command + " does not exist on the ElastiCenter server. Please specify a valid command or contact your ElastiCenter Administrator.");
} else {
throw new ServiceUnavailableException("Internal Error. Please contact your ElastiCenter Administrator.");
}
Expand Down
9 changes: 4 additions & 5 deletions server/src/main/java/com/cloud/network/NetworkModelImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
package com.cloud.network;

import java.math.BigInteger;
import java.security.InvalidParameterException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
Expand Down Expand Up @@ -417,10 +416,10 @@ public boolean canIpUsedForNonConserveService(PublicIp ip, Service service) {
}
// Since it's non-conserve mode, only one service should used for IP
if (services.size() != 1) {
throw new InvalidParameterException("There are multiple services used ip " + ip.getAddress() + ".");
throw new InvalidParameterValueException("There are multiple services used ip " + ip.getAddress() + ".");
}
if (service != null && !((Service)services.toArray()[0] == service || service.equals(Service.Firewall))) {
throw new InvalidParameterException("The IP " + ip.getAddress() + " is already used as " + ((Service)services.toArray()[0]).getName() + " rather than " +
throw new InvalidParameterValueException("The IP " + ip.getAddress() + " is already used as " + ((Service)services.toArray()[0]).getName() + " rather than " +
service.getName());
}
return true;
Expand Down Expand Up @@ -458,7 +457,7 @@ public boolean canIpUsedForService(PublicIp publicIp, Service service, Long netw
// Since IP already has service to bind with, the oldProvider can't be null
Set<Provider> newProviders = serviceToProviders.get(service);
if (newProviders == null || newProviders.isEmpty()) {
throw new InvalidParameterException("There is no new provider for IP " + publicIp.getAddress() + " of service " + service.getName() + "!");
throw new InvalidParameterValueException("There is no new provider for IP " + publicIp.getAddress() + " of service " + service.getName() + "!");
}
Provider newProvider = (Provider)newProviders.toArray()[0];
Set<Provider> oldProviders = serviceToProviders.get(services.toArray()[0]);
Expand All @@ -471,7 +470,7 @@ public boolean canIpUsedForService(PublicIp publicIp, Service service, Long netw
IpDeployer newIpDeployer = ((IpDeployingRequester)newElement).getIpDeployer(network);
// FIXME: I ignored this check
} else {
throw new InvalidParameterException("Ip cannot be applied for new provider!");
throw new InvalidParameterValueException("Ip cannot be applied for new provider!");
}
return true;
}
Expand Down
Loading

0 comments on commit c05edc4

Please sign in to comment.