use of com.cloud.exception.InvalidParameterValueException in project cloudstack by apache.
the class DomainManagerImpl method updateDomain.
@Override
@ActionEvent(eventType = EventTypes.EVENT_DOMAIN_UPDATE, eventDescription = "updating Domain")
@DB
public DomainVO updateDomain(UpdateDomainCmd cmd) {
final Long domainId = cmd.getId();
final String domainName = cmd.getDomainName();
final String networkDomain = cmd.getNetworkDomain();
// check if domain exists in the system
final DomainVO domain = _domainDao.findById(domainId);
if (domain == null) {
InvalidParameterValueException ex = new InvalidParameterValueException("Unable to find domain with specified domain id");
ex.addProxyObject(domainId.toString(), "domainId");
throw ex;
} else if (domain.getParent() == null && domainName != null) {
// check if domain is ROOT domain - and deny to edit it with the new name
throw new InvalidParameterValueException("ROOT domain can not be edited with a new name");
}
// check permissions
Account caller = getCaller();
_accountMgr.checkAccess(caller, domain);
// domain name is unique in the cloud
if (domainName != null) {
SearchCriteria<DomainVO> sc = _domainDao.createSearchCriteria();
sc.addAnd("name", SearchCriteria.Op.EQ, domainName);
List<DomainVO> domains = _domainDao.search(sc, null);
boolean sameDomain = (domains.size() == 1 && domains.get(0).getId() == domainId);
if (!domains.isEmpty() && !sameDomain) {
InvalidParameterValueException ex = new InvalidParameterValueException("Failed to update specified domain id with name '" + domainName + "' since it already exists in the system");
ex.addProxyObject(domain.getUuid(), "domainId");
throw ex;
}
}
// validate network domain
if (networkDomain != null && !networkDomain.isEmpty()) {
if (!NetUtils.verifyDomainName(networkDomain)) {
throw new InvalidParameterValueException("Invalid network domain. Total length shouldn't exceed 190 chars. Each domain label must be between 1 and 63 characters long, can contain ASCII letters 'a' through 'z', the digits '0' through '9', " + "and the hyphen ('-'); can't start or end with \"-\"");
}
}
Transaction.execute(new TransactionCallbackNoReturn() {
@Override
public void doInTransactionWithoutResult(TransactionStatus status) {
if (domainName != null) {
String updatedDomainPath = getUpdatedDomainPath(domain.getPath(), domainName);
updateDomainChildren(domain, updatedDomainPath);
domain.setName(domainName);
domain.setPath(updatedDomainPath);
}
if (networkDomain != null) {
if (networkDomain.isEmpty()) {
domain.setNetworkDomain(null);
} else {
domain.setNetworkDomain(networkDomain);
}
}
_domainDao.update(domainId, domain);
CallContext.current().putContextParameter(Domain.class, domain.getUuid());
}
});
return _domainDao.findById(domainId);
}
use of com.cloud.exception.InvalidParameterValueException in project cloudstack by apache.
the class RegionManagerTest method testUniqueName.
@Test
public void testUniqueName() {
RegionManagerImpl regionMgr = new RegionManagerImpl();
RegionDao regionDao = Mockito.mock(RegionDao.class);
RegionVO region = new RegionVO(2, "APAC", "");
Mockito.when(regionDao.findByName(Matchers.anyString())).thenReturn(region);
regionMgr._regionDao = regionDao;
try {
regionMgr.addRegion(2, "APAC", "");
} catch (InvalidParameterValueException e) {
Assert.assertEquals("Region with name: APAC already exists", e.getMessage());
}
}
use of com.cloud.exception.InvalidParameterValueException in project cloudstack by apache.
the class GlobalLoadBalancingRulesServiceImplTest method runCreateGlobalLoadBalancerRuleInvalidServiceType.
void runCreateGlobalLoadBalancerRuleInvalidServiceType() throws Exception {
TransactionLegacy txn = TransactionLegacy.open("runCreateGlobalLoadBalancerRulePostiveTest");
GlobalLoadBalancingRulesServiceImpl gslbServiceImpl = new GlobalLoadBalancingRulesServiceImpl();
gslbServiceImpl._accountMgr = Mockito.mock(AccountManager.class);
Account account = new AccountVO("testaccount", 1, "networkdomain", (short) 0, UUID.randomUUID().toString());
when(gslbServiceImpl._accountMgr.getAccount(anyLong())).thenReturn(account);
gslbServiceImpl._gslbRuleDao = Mockito.mock(GlobalLoadBalancerRuleDao.class);
when(gslbServiceImpl._gslbRuleDao.persist(any(GlobalLoadBalancerRuleVO.class))).thenReturn(new GlobalLoadBalancerRuleVO());
gslbServiceImpl._gslbLbMapDao = Mockito.mock(GlobalLoadBalancerLbRuleMapDao.class);
gslbServiceImpl._regionDao = Mockito.mock(RegionDao.class);
RegionVO region = new RegionVO();
region.setGslbEnabled(true);
when(gslbServiceImpl._regionDao.findById(anyInt())).thenReturn(region);
gslbServiceImpl._rulesMgr = Mockito.mock(RulesManager.class);
gslbServiceImpl._lbDao = Mockito.mock(LoadBalancerDao.class);
gslbServiceImpl._networkDao = Mockito.mock(NetworkDao.class);
gslbServiceImpl._globalConfigDao = Mockito.mock(ConfigurationDao.class);
gslbServiceImpl._ipAddressDao = Mockito.mock(IPAddressDao.class);
gslbServiceImpl._agentMgr = Mockito.mock(AgentManager.class);
CreateGlobalLoadBalancerRuleCmd createCmd = new CreateGlobalLoadBalancerRuleCmdExtn();
Class<?> _class = createCmd.getClass().getSuperclass();
Field regionIdField = _class.getDeclaredField("regionId");
regionIdField.setAccessible(true);
regionIdField.set(createCmd, new Integer(1));
Field algoField = _class.getDeclaredField("algorithm");
algoField.setAccessible(true);
algoField.set(createCmd, "roundrobin");
Field stickyField = _class.getDeclaredField("stickyMethod");
stickyField.setAccessible(true);
stickyField.set(createCmd, "sourceip");
Field nameField = _class.getDeclaredField("globalLoadBalancerRuleName");
nameField.setAccessible(true);
nameField.set(createCmd, "gslb-rule");
Field descriptionField = _class.getDeclaredField("description");
descriptionField.setAccessible(true);
descriptionField.set(createCmd, "testing create gslb-rule");
Field serviceDomainField = _class.getDeclaredField("serviceDomainName");
serviceDomainField.setAccessible(true);
serviceDomainField.set(createCmd, "gslb-rule-domain");
Field serviceTypeField = _class.getDeclaredField("serviceType");
serviceTypeField.setAccessible(true);
serviceTypeField.set(createCmd, "invalidtcp");
try {
gslbServiceImpl.createGlobalLoadBalancerRule(createCmd);
} catch (InvalidParameterValueException e) {
Assert.assertTrue(e.getMessage().contains("Invalid service type"));
}
}
use of com.cloud.exception.InvalidParameterValueException in project cloudstack by apache.
the class GlobalLoadBalancingRulesServiceImplTest method runAssignToGlobalLoadBalancerRuleTestSameZoneLb.
void runAssignToGlobalLoadBalancerRuleTestSameZoneLb() throws Exception {
TransactionLegacy txn = TransactionLegacy.open("runAssignToGlobalLoadBalancerRuleTestSameZoneLb");
GlobalLoadBalancingRulesServiceImpl gslbServiceImpl = new GlobalLoadBalancingRulesServiceImpl();
gslbServiceImpl._accountMgr = Mockito.mock(AccountManager.class);
gslbServiceImpl._gslbRuleDao = Mockito.mock(GlobalLoadBalancerRuleDao.class);
gslbServiceImpl._gslbLbMapDao = Mockito.mock(GlobalLoadBalancerLbRuleMapDao.class);
gslbServiceImpl._regionDao = Mockito.mock(RegionDao.class);
gslbServiceImpl._rulesMgr = Mockito.mock(RulesManager.class);
gslbServiceImpl._lbDao = Mockito.mock(LoadBalancerDao.class);
gslbServiceImpl._networkDao = Mockito.mock(NetworkDao.class);
gslbServiceImpl._globalConfigDao = Mockito.mock(ConfigurationDao.class);
gslbServiceImpl._ipAddressDao = Mockito.mock(IPAddressDao.class);
gslbServiceImpl._agentMgr = Mockito.mock(AgentManager.class);
AssignToGlobalLoadBalancerRuleCmd assignCmd = new AssignToGlobalLoadBalancerRuleCmdExtn();
Class<?> _class = assignCmd.getClass().getSuperclass();
Account account = new AccountVO("testaccount", 3, "networkdomain", (short) 0, UUID.randomUUID().toString());
when(gslbServiceImpl._accountMgr.getAccount(anyLong())).thenReturn(account);
Field gslbRuleId = _class.getDeclaredField("id");
gslbRuleId.setAccessible(true);
gslbRuleId.set(assignCmd, new Long(1));
GlobalLoadBalancerRuleVO gslbRule = new GlobalLoadBalancerRuleVO("test-gslb-rule", "test-gslb-rule", "test-domain", "roundrobin", "sourceip", "tcp", 1, 3, 1, GlobalLoadBalancerRule.State.Active);
when(gslbServiceImpl._gslbRuleDao.findById(new Long(1))).thenReturn(gslbRule);
LoadBalancerVO lbRule1 = new LoadBalancerVO();
lbRule1.setState(FirewallRule.State.Active);
Field networkIdField1 = LoadBalancerVO.class.getSuperclass().getDeclaredField("networkId");
Field accountIdField1 = LoadBalancerVO.class.getSuperclass().getDeclaredField("accountId");
Field domainIdField1 = LoadBalancerVO.class.getSuperclass().getDeclaredField("domainId");
networkIdField1.setAccessible(true);
accountIdField1.setAccessible(true);
domainIdField1.setAccessible(true);
networkIdField1.set(lbRule1, new Long(1));
accountIdField1.set(lbRule1, new Long(3));
domainIdField1.set(lbRule1, new Long(1));
Field idField1 = LoadBalancerVO.class.getSuperclass().getDeclaredField("id");
idField1.setAccessible(true);
idField1.set(lbRule1, new Long(1));
LoadBalancerVO lbRule2 = new LoadBalancerVO();
lbRule2.setState(FirewallRule.State.Active);
Field networkIdField2 = LoadBalancerVO.class.getSuperclass().getDeclaredField("networkId");
Field accountIdField2 = LoadBalancerVO.class.getSuperclass().getDeclaredField("accountId");
Field domainIdField2 = LoadBalancerVO.class.getSuperclass().getDeclaredField("domainId");
networkIdField2.setAccessible(true);
accountIdField2.setAccessible(true);
domainIdField2.setAccessible(true);
networkIdField2.set(lbRule2, new Long(1));
accountIdField2.set(lbRule2, new Long(3));
domainIdField2.set(lbRule2, new Long(1));
Field idField2 = LoadBalancerVO.class.getSuperclass().getDeclaredField("id");
idField2.setAccessible(true);
idField2.set(lbRule2, new Long(2));
when(gslbServiceImpl._lbDao.findById(new Long(1))).thenReturn(lbRule1);
when(gslbServiceImpl._lbDao.findById(new Long(2))).thenReturn(lbRule2);
Field lbRules = _class.getDeclaredField("loadBalancerRulesIds");
lbRules.setAccessible(true);
List<Long> lbRuleIds = new ArrayList<Long>();
lbRuleIds.add(new Long(1));
lbRuleIds.add(new Long(2));
lbRules.set(assignCmd, lbRuleIds);
NetworkVO networkVo = new NetworkVO();
Field dcID = NetworkVO.class.getDeclaredField("dataCenterId");
dcID.setAccessible(true);
dcID.set(networkVo, new Long(1));
when(gslbServiceImpl._networkDao.findById(new Long(1))).thenReturn(networkVo);
try {
gslbServiceImpl.assignToGlobalLoadBalancerRule(assignCmd);
} catch (InvalidParameterValueException e) {
s_logger.info(e.getMessage());
Assert.assertTrue(e.getMessage().contains("Load balancer rule specified should be in unique zone"));
}
}
use of com.cloud.exception.InvalidParameterValueException in project cloudstack by apache.
the class HttpUploadServerHandler method channelRead0.
@Override
public void channelRead0(ChannelHandlerContext ctx, HttpObject msg) throws Exception {
if (msg instanceof HttpRequest) {
HttpRequest request = this.request = (HttpRequest) msg;
responseContent.setLength(0);
if (request.getMethod().equals(HttpMethod.POST)) {
URI uri = new URI(request.getUri());
String signature = null;
String expires = null;
String metadata = null;
String hostname = null;
long contentLength = 0;
for (Entry<String, String> entry : request.headers()) {
switch(entry.getKey()) {
case HEADER_SIGNATURE:
signature = entry.getValue();
break;
case HEADER_METADATA:
metadata = entry.getValue();
break;
case HEADER_EXPIRES:
expires = entry.getValue();
break;
case HEADER_HOST:
hostname = entry.getValue();
break;
case HttpHeaders.Names.CONTENT_LENGTH:
contentLength = Long.parseLong(entry.getValue());
break;
}
}
logger.info("HEADER: signature=" + signature);
logger.info("HEADER: metadata=" + metadata);
logger.info("HEADER: expires=" + expires);
logger.info("HEADER: hostname=" + hostname);
logger.info("HEADER: Content-Length=" + contentLength);
QueryStringDecoder decoderQuery = new QueryStringDecoder(uri);
Map<String, List<String>> uriAttributes = decoderQuery.parameters();
uuid = uriAttributes.get("uuid").get(0);
logger.info("URI: uuid=" + uuid);
UploadEntity uploadEntity = null;
try {
// Validate the request here
storageResource.validatePostUploadRequest(signature, metadata, expires, hostname, contentLength, uuid);
//create an upload entity. This will fail if entity already exists.
uploadEntity = storageResource.createUploadEntity(uuid, metadata, contentLength);
} catch (InvalidParameterValueException ex) {
logger.error("post request validation failed", ex);
responseContent.append(ex.getMessage());
writeResponse(ctx.channel(), HttpResponseStatus.BAD_REQUEST);
requestProcessed = true;
return;
}
if (uploadEntity == null) {
logger.error("Unable to create upload entity. An exception occurred.");
responseContent.append("Internal Server Error");
writeResponse(ctx.channel(), HttpResponseStatus.INTERNAL_SERVER_ERROR);
requestProcessed = true;
return;
}
//set the base directory to download the file
DiskFileUpload.baseDirectory = uploadEntity.getInstallPathPrefix();
logger.info("base directory: " + DiskFileUpload.baseDirectory);
try {
//initialize the decoder
decoder = new HttpPostRequestDecoder(factory, request);
} catch (ErrorDataDecoderException | IncompatibleDataDecoderException e) {
logger.error("exception while initialising the decoder", e);
responseContent.append(e.getMessage());
writeResponse(ctx.channel(), HttpResponseStatus.INTERNAL_SERVER_ERROR);
requestProcessed = true;
return;
}
} else {
logger.warn("received a get request");
responseContent.append("only post requests are allowed");
writeResponse(ctx.channel(), HttpResponseStatus.BAD_REQUEST);
requestProcessed = true;
return;
}
}
// check if the decoder was constructed before
if (decoder != null) {
if (msg instanceof HttpContent) {
// New chunk is received
HttpContent chunk = (HttpContent) msg;
try {
decoder.offer(chunk);
} catch (ErrorDataDecoderException e) {
logger.error("data decoding exception", e);
responseContent.append(e.getMessage());
writeResponse(ctx.channel(), HttpResponseStatus.INTERNAL_SERVER_ERROR);
requestProcessed = true;
return;
}
if (chunk instanceof LastHttpContent) {
writeResponse(ctx.channel(), readFileUploadData());
reset();
}
}
}
}
Aggregations