@Override public Boolean doInTransaction(TransactionStatus status) throws ResourceInUseException { SecurityGroupVO group = _securityGroupDao.lockRow(groupId, true); if (group == null) { throw new InvalidParameterValueException("Unable to find security group by id " + groupId); } if (group.getName().equalsIgnoreCase(SecurityGroupManager.DEFAULT_GROUP_NAME)) { throw new InvalidParameterValueException("The network group default is reserved"); } List<SecurityGroupRuleVO> allowingRules = _securityGroupRuleDao.listByAllowedSecurityGroupId(groupId); List<SecurityGroupVMMapVO> securityGroupVmMap = _securityGroupVMMapDao.listBySecurityGroup(groupId); if (!allowingRules.isEmpty()) { throw new ResourceInUseException("Cannot delete group when there are security rules that allow this group"); } else if (!securityGroupVmMap.isEmpty()) { throw new ResourceInUseException("Cannot delete group when it's in use by virtual machines"); } _securityGroupDao.expunge(groupId); s_logger.debug("Deleted security group id=" + groupId); return true; } });
} catch (ResourceInUseException ex) { s_logger.warn("Exception: ", ex); ServerApiException e = new ServerApiException(ApiErrorCode.INTERNAL_ERROR, ex.getMessage()); for (String proxyObj : ex.getIdProxyList()) { e.addProxyObject(proxyObj);
@Override public void execute() { try { boolean result = _securityGroupService.deleteSecurityGroup(this); if (result) { SuccessResponse response = new SuccessResponse(getCommandName()); setResponseObject(response); } else { throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, "Failed to delete security group"); } } catch (ResourceInUseException ex) { s_logger.warn("Exception: ", ex); throw new ServerApiException(ApiErrorCode.RESOURCE_IN_USE_ERROR, ex.getMessage()); } } }
@Test public void testExecuteForNullResult() { ResourceService resourceService = Mockito.mock(ResourceService.class); try { Mockito.when(resourceService.discoverCluster(addClusterCmd)).thenReturn(null); } catch (ResourceInUseException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IllegalArgumentException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (DiscoveryException e) { // TODO Auto-generated catch block e.printStackTrace(); } addClusterCmd._resourceService = resourceService; try { addClusterCmd.execute(); } catch (ServerApiException exception) { Assert.assertEquals("Failed to add cluster", exception.getDescription()); } }
@Override public void execute() { try { StoragePool result = _storageService.createPool(this); if (result != null) { StoragePoolResponse response = _responseGenerator.createStoragePoolResponse(result); response.setResponseName(getCommandName()); this.setResponseObject(response); } else { throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, "Failed to add storage pool"); } } catch (ResourceUnavailableException ex1) { s_logger.warn("Exception: ", ex1); throw new ServerApiException(ApiErrorCode.RESOURCE_UNAVAILABLE_ERROR, ex1.getMessage()); } catch (ResourceInUseException ex2) { s_logger.warn("Exception: ", ex2); throw new ServerApiException(ApiErrorCode.RESOURCE_IN_USE_ERROR, ex2.getMessage()); } catch (UnknownHostException ex3) { s_logger.warn("Exception: ", ex3); throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, ex3.getMessage()); } catch (Exception ex4) { throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, ex4.getMessage()); } } }
@Override public void execute() { boolean result = false; try { result = _autoScaleService.deleteCounter(getId()); } catch (ResourceInUseException ex) { s_logger.warn("Exception: ", ex); throw new ServerApiException(ApiErrorCode.RESOURCE_IN_USE_ERROR, ex.getMessage()); } if (result) { SuccessResponse response = new SuccessResponse(getCommandName()); this.setResponseObject(response); } else { s_logger.warn("Failed to delete counter with Id: " + getId()); throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, "Failed to delete counter."); } }
@Override @ActionEvent(eventType = EventTypes.EVENT_COUNTER_DELETE, eventDescription = "counter") public boolean deleteCounter(long counterId) throws ResourceInUseException { // Verify Counter id CounterVO counter = _counterDao.findById(counterId); if (counter == null) { throw new InvalidParameterValueException("Unable to find Counter"); } // Verify if it is used in any Condition ConditionVO condition = _conditionDao.findByCounterId(counterId); if (condition != null) { s_logger.info("Cannot delete counter " + counter.getName() + " as it is being used in a condition."); throw new ResourceInUseException("Counter is in use."); } boolean success = _counterDao.remove(counterId); if (success) { s_logger.info("Successfully deleted counter with Id: " + counterId); } return success; }
@Override public void execute() { boolean result = false; try { result = _autoScaleService.deleteCondition(getId()); } catch (ResourceInUseException ex) { s_logger.warn("Exception: ", ex); throw new ServerApiException(ApiErrorCode.RESOURCE_IN_USE_ERROR, ex.getMessage()); } if (result) { SuccessResponse response = new SuccessResponse(getCommandName()); setResponseObject(response); } else { s_logger.warn("Failed to delete condition " + getId()); throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, "Failed to delete condition."); } }
@Override @ActionEvent(eventType = EventTypes.EVENT_CONDITION_DELETE, eventDescription = "condition") public boolean deleteCondition(long conditionId) throws ResourceInUseException { /* Check if entity is in database */ ConditionVO condition = getEntityInDatabase(CallContext.current().getCallingAccount(), "Condition", conditionId, _conditionDao); if (condition == null) { throw new InvalidParameterValueException("Unable to find Condition"); } // Verify if condition is used in any autoscale policy if (_autoScalePolicyConditionMapDao.isConditionInUse(conditionId)) { s_logger.info("Cannot delete condition " + conditionId + " as it is being used in a condition."); throw new ResourceInUseException("Cannot delete Condition when it is in use by one or more AutoScale Policies."); } boolean success = _conditionDao.remove(conditionId); if (success) { s_logger.info("Successfully deleted condition " + condition.getId()); } return success; }