Search in sources :

Example 11 with CloudException

use of com.cloud.exception.CloudException in project cloudstack by apache.

the class UsageIPAddressDaoImpl method update.

@Override
public void update(UsageIPAddressVO usage) {
    TransactionLegacy txn = TransactionLegacy.open(TransactionLegacy.USAGE_DB);
    try {
        txn.start();
        if (usage.getReleased() != null) {
            try (PreparedStatement pstmt = txn.prepareStatement(UPDATE_RELEASED)) {
                if (pstmt != null) {
                    pstmt.setString(1, DateUtil.getDateDisplayString(TimeZone.getTimeZone("GMT"), usage.getReleased()));
                    pstmt.setLong(2, usage.getAccountId());
                    pstmt.setString(3, usage.getAddress());
                    pstmt.executeUpdate();
                }
            } catch (SQLException e) {
                throw new CloudException("update:Exception:" + e.getMessage(), e);
            }
        }
        txn.commit();
    } catch (Exception e) {
        txn.rollback();
        s_logger.error("Error updating usageIPAddressVO:" + e.getMessage(), e);
    } finally {
        txn.close();
    }
}
Also used : TransactionLegacy(com.cloud.utils.db.TransactionLegacy) SQLException(java.sql.SQLException) PreparedStatement(java.sql.PreparedStatement) CloudException(com.cloud.exception.CloudException) SQLException(java.sql.SQLException) CloudException(com.cloud.exception.CloudException)

Example 12 with CloudException

use of com.cloud.exception.CloudException in project cloudstack by apache.

the class DatastoreMO method searchFileInSubFolders.

public String searchFileInSubFolders(String fileName, boolean caseInsensitive) throws Exception {
    String datastorePath = "[" + getName() + "]";
    String rootDirectoryFilePath = String.format("%s %s", datastorePath, fileName);
    if (fileExists(rootDirectoryFilePath)) {
        return rootDirectoryFilePath;
    }
    String parentFolderPath = null;
    String absoluteFileName = null;
    s_logger.info("Searching file " + fileName + " in " + datastorePath);
    HostDatastoreBrowserMO browserMo = getHostDatastoreBrowserMO();
    ArrayList<HostDatastoreBrowserSearchResults> results = browserMo.searchDatastoreSubFolders("[" + getName() + "]", fileName, caseInsensitive);
    if (results != null && results.size() > 1) {
        s_logger.warn("Multiple files with name " + fileName + " exists in datastore " + datastorePath + ". Trying to choose first file found in search attempt.");
    } else if (results == null) {
        String msg = "No file found with name " + fileName + " found in datastore " + datastorePath;
        s_logger.error(msg);
        throw new CloudException(msg);
    }
    for (HostDatastoreBrowserSearchResults result : results) {
        List<FileInfo> info = result.getFile();
        if (info != null && info.size() > 0) {
            for (FileInfo fi : info) {
                absoluteFileName = parentFolderPath = result.getFolderPath();
                s_logger.info("Found file " + fileName + " in datastore at " + absoluteFileName);
                if (parentFolderPath.endsWith("]"))
                    absoluteFileName += " ";
                absoluteFileName += fi.getPath();
                break;
            }
        }
    }
    return absoluteFileName;
}
Also used : HostDatastoreBrowserSearchResults(com.vmware.vim25.HostDatastoreBrowserSearchResults) FileInfo(com.vmware.vim25.FileInfo) CloudException(com.cloud.exception.CloudException)

Example 13 with CloudException

use of com.cloud.exception.CloudException in project cloudstack by apache.

the class UserVmManagerImpl method stopVirtualMachine.

@Override
public boolean stopVirtualMachine(long userId, long vmId) {
    boolean status = false;
    if (s_logger.isDebugEnabled()) {
        s_logger.debug("Stopping vm=" + vmId);
    }
    UserVmVO vm = _vmDao.findById(vmId);
    if (vm == null || vm.getRemoved() != null) {
        if (s_logger.isDebugEnabled()) {
            s_logger.debug("VM is either removed or deleted.");
        }
        return true;
    }
    _userDao.findById(userId);
    try {
        VirtualMachineEntity vmEntity = _orchSrvc.getVirtualMachine(vm.getUuid());
        status = vmEntity.stop(Long.toString(userId));
    } catch (ResourceUnavailableException e) {
        s_logger.debug("Unable to stop due to ", e);
        status = false;
    } catch (CloudException e) {
        throw new CloudRuntimeException("Unable to contact the agent to stop the virtual machine " + vm, e);
    }
    return status;
}
Also used : CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) VirtualMachineEntity(org.apache.cloudstack.engine.cloud.entity.api.VirtualMachineEntity) ResourceUnavailableException(com.cloud.exception.ResourceUnavailableException) CloudException(com.cloud.exception.CloudException)

Example 14 with CloudException

use of com.cloud.exception.CloudException in project cloudstack by apache.

the class UserVmManagerImpl method stopVirtualMachine.

@Override
@ActionEvent(eventType = EventTypes.EVENT_VM_STOP, eventDescription = "stopping Vm", async = true)
public UserVm stopVirtualMachine(long vmId, boolean forced) throws ConcurrentOperationException {
    // Input validation
    Account caller = CallContext.current().getCallingAccount();
    Long userId = CallContext.current().getCallingUserId();
    // if account is removed, return error
    if (caller != null && caller.getRemoved() != null) {
        throw new PermissionDeniedException("The account " + caller.getUuid() + " is removed");
    }
    UserVmVO vm = _vmDao.findById(vmId);
    if (vm == null) {
        throw new InvalidParameterValueException("unable to find a virtual machine with id " + vmId);
    }
    _userDao.findById(userId);
    boolean status = false;
    try {
        VirtualMachineEntity vmEntity = _orchSrvc.getVirtualMachine(vm.getUuid());
        if (forced) {
            status = vmEntity.stopForced(Long.toString(userId));
        } else {
            status = vmEntity.stop(Long.toString(userId));
        }
        if (status) {
            return _vmDao.findById(vmId);
        } else {
            return null;
        }
    } catch (ResourceUnavailableException e) {
        throw new CloudRuntimeException("Unable to contact the agent to stop the virtual machine " + vm, e);
    } catch (CloudException e) {
        throw new CloudRuntimeException("Unable to contact the agent to stop the virtual machine " + vm, e);
    }
}
Also used : Account(com.cloud.user.Account) InvalidParameterValueException(com.cloud.exception.InvalidParameterValueException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) VirtualMachineEntity(org.apache.cloudstack.engine.cloud.entity.api.VirtualMachineEntity) ResourceUnavailableException(com.cloud.exception.ResourceUnavailableException) PermissionDeniedException(com.cloud.exception.PermissionDeniedException) CloudException(com.cloud.exception.CloudException) ActionEvent(com.cloud.event.ActionEvent)

Example 15 with CloudException

use of com.cloud.exception.CloudException in project cloudstack by apache.

the class NetworkProviderTest method createTestNetwork.

private Network createTestNetwork(String name) {
    CreateNetworkCmd cmd = new CreateNetworkCmd();
    ComponentContext.inject(cmd);
    Account system = _accountMgr.getSystemAccount();
    DataCenter zone = _server.getZone();
    ManagementServerMock.setParameter(cmd, "accountName", BaseCmd.CommandType.STRING, system.getAccountName());
    ManagementServerMock.setParameter(cmd, ApiConstants.NAME, BaseCmd.CommandType.STRING, name);
    ManagementServerMock.setParameter(cmd, "displayText", BaseCmd.CommandType.STRING, "test network");
    ManagementServerMock.setParameter(cmd, "networkOfferingId", BaseCmd.CommandType.LONG, _contrailMgr.getRouterOffering().getId());
    ManagementServerMock.setParameter(cmd, "zoneId", BaseCmd.CommandType.LONG, zone.getId());
    ManagementServerMock.setParameter(cmd, ApiConstants.GATEWAY, BaseCmd.CommandType.STRING, "10.0.1.254");
    ManagementServerMock.setParameter(cmd, ApiConstants.NETMASK, BaseCmd.CommandType.STRING, "255.255.255.0");
    // Physical network id can't be specified for Guest traffic type.
    // SetParameter(cmd, "physicalNetworkId", BaseCmd.CommandType.LONG, _znet.getId());
    Network result = null;
    try {
        result = _networkService.createGuestNetwork(cmd);
    } catch (CloudException e) {
        e.printStackTrace();
        return null;
    }
    return result;
}
Also used : Account(com.cloud.user.Account) DataCenter(com.cloud.dc.DataCenter) VirtualNetwork(net.juniper.contrail.api.types.VirtualNetwork) Network(com.cloud.network.Network) CreateNetworkCmd(org.apache.cloudstack.api.command.user.network.CreateNetworkCmd) CloudException(com.cloud.exception.CloudException)

Aggregations

CloudException (com.cloud.exception.CloudException)18 TransactionLegacy (com.cloud.utils.db.TransactionLegacy)11 PreparedStatement (java.sql.PreparedStatement)11 SQLException (java.sql.SQLException)11 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)4 VirtualMachineEntity (org.apache.cloudstack.engine.cloud.entity.api.VirtualMachineEntity)3 InvalidParameterValueException (com.cloud.exception.InvalidParameterValueException)2 ResourceUnavailableException (com.cloud.exception.ResourceUnavailableException)2 Account (com.cloud.user.Account)2 ResultSet (java.sql.ResultSet)2 DataCenter (com.cloud.dc.DataCenter)1 ActionEvent (com.cloud.event.ActionEvent)1 PermissionDeniedException (com.cloud.exception.PermissionDeniedException)1 VirtualSwitchType (com.cloud.hypervisor.vmware.mo.VirtualSwitchType)1 VmwareContext (com.cloud.hypervisor.vmware.util.VmwareContext)1 Network (com.cloud.network.Network)1 TrafficType (com.cloud.network.Networks.TrafficType)1 VmwareTrafficLabel (com.cloud.network.VmwareTrafficLabel)1 ResourceState (com.cloud.resource.ResourceState)1 ServiceOfferingVO (com.cloud.service.ServiceOfferingVO)1