Search in sources :

Example 51 with ActionEvent

use of com.cloud.event.ActionEvent in project cloudstack by apache.

the class TemplateManagerImpl method registerIso.

@Override
@ActionEvent(eventType = EventTypes.EVENT_ISO_CREATE, eventDescription = "creating iso")
public VirtualMachineTemplate registerIso(RegisterIsoCmd cmd) throws ResourceAllocationException {
    TemplateAdapter adapter = getAdapter(HypervisorType.None);
    TemplateProfile profile = adapter.prepare(cmd);
    VMTemplateVO template = adapter.create(profile);
    if (template != null) {
        return template;
    } else {
        throw new CloudRuntimeException("Failed to create ISO");
    }
}
Also used : CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) VMTemplateVO(com.cloud.storage.VMTemplateVO) TemplateProfile(com.cloud.storage.TemplateProfile) ActionEvent(com.cloud.event.ActionEvent)

Example 52 with ActionEvent

use of com.cloud.event.ActionEvent in project cloudstack by apache.

the class TemplateManagerImpl method registerTemplateForPostUpload.

@Override
@ActionEvent(eventType = EventTypes.EVENT_TEMPLATE_CREATE, eventDescription = "creating post upload template")
public GetUploadParamsResponse registerTemplateForPostUpload(GetUploadParamsForTemplateCmd cmd) throws ResourceAllocationException, MalformedURLException {
    TemplateAdapter adapter = getAdapter(HypervisorType.getType(cmd.getHypervisor()));
    TemplateProfile profile = adapter.prepare(cmd);
    List<TemplateOrVolumePostUploadCommand> payload = adapter.createTemplateForPostUpload(profile);
    if (CollectionUtils.isNotEmpty(payload)) {
        GetUploadParamsResponse response = new GetUploadParamsResponse();
        /*
             * There can be one or more commands depending on the number of secondary stores the template needs to go to. Taking the first one to do the url upload. The
             * template will be propagated to the rest through copy by management server commands.
             */
        TemplateOrVolumePostUploadCommand firstCommand = payload.get(0);
        String ssvmUrlDomain = _configDao.getValue(Config.SecStorageSecureCopyCert.key());
        String url = ImageStoreUtil.generatePostUploadUrl(ssvmUrlDomain, firstCommand.getRemoteEndPoint(), firstCommand.getEntityUUID());
        response.setPostURL(new URL(url));
        // set the post url, this is used in the monitoring thread to determine the SSVM
        TemplateDataStoreVO templateStore = _tmplStoreDao.findByTemplate(firstCommand.getEntityId(), DataStoreRole.getRole(firstCommand.getDataToRole()));
        if (templateStore != null) {
            templateStore.setExtractUrl(url);
            _tmplStoreDao.persist(templateStore);
        }
        response.setId(UUID.fromString(firstCommand.getEntityUUID()));
        int timeout = ImageStoreUploadMonitorImpl.getUploadOperationTimeout();
        DateTime currentDateTime = new DateTime(DateTimeZone.UTC);
        String expires = currentDateTime.plusMinutes(timeout).toString();
        response.setTimeout(expires);
        String key = _configDao.getValue(Config.SSVMPSK.key());
        /*
             * encoded metadata using the post upload config ssh key
             */
        Gson gson = new GsonBuilder().create();
        String metadata = EncryptionUtil.encodeData(gson.toJson(firstCommand), key);
        response.setMetadata(metadata);
        /*
             * signature calculated on the url, expiry, metadata.
             */
        response.setSignature(EncryptionUtil.generateSignature(metadata + url + expires, key));
        return response;
    } else {
        throw new CloudRuntimeException("Unable to register template.");
    }
}
Also used : GsonBuilder(com.google.gson.GsonBuilder) Gson(com.google.gson.Gson) TemplateDataStoreVO(org.apache.cloudstack.storage.datastore.db.TemplateDataStoreVO) GetUploadParamsResponse(org.apache.cloudstack.api.response.GetUploadParamsResponse) URL(java.net.URL) EndPoint(org.apache.cloudstack.engine.subsystem.api.storage.EndPoint) DateTime(org.joda.time.DateTime) TemplateOrVolumePostUploadCommand(org.apache.cloudstack.storage.command.TemplateOrVolumePostUploadCommand) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) TemplateProfile(com.cloud.storage.TemplateProfile) ActionEvent(com.cloud.event.ActionEvent)

Example 53 with ActionEvent

use of com.cloud.event.ActionEvent in project cloudstack by apache.

the class TemplateManagerImpl method registerTemplate.

@Override
@ActionEvent(eventType = EventTypes.EVENT_TEMPLATE_CREATE, eventDescription = "creating template")
public VirtualMachineTemplate registerTemplate(RegisterTemplateCmd cmd) throws URISyntaxException, ResourceAllocationException {
    Account account = CallContext.current().getCallingAccount();
    if (cmd.getTemplateTag() != null) {
        if (!_accountService.isRootAdmin(account.getId())) {
            throw new PermissionDeniedException("Parameter templatetag can only be specified by a Root Admin, permission denied");
        }
    }
    if (cmd.isRoutingType() != null) {
        if (!_accountService.isRootAdmin(account.getId())) {
            throw new PermissionDeniedException("Parameter isrouting can only be specified by a Root Admin, permission denied");
        }
    }
    TemplateAdapter adapter = getAdapter(HypervisorType.getType(cmd.getHypervisor()));
    TemplateProfile profile = adapter.prepare(cmd);
    VMTemplateVO template = adapter.create(profile);
    if (template != null) {
        return template;
    } else {
        throw new CloudRuntimeException("Failed to create a template");
    }
}
Also used : Account(com.cloud.user.Account) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) VMTemplateVO(com.cloud.storage.VMTemplateVO) PermissionDeniedException(com.cloud.exception.PermissionDeniedException) TemplateProfile(com.cloud.storage.TemplateProfile) ActionEvent(com.cloud.event.ActionEvent)

Example 54 with ActionEvent

use of com.cloud.event.ActionEvent in project cloudstack by apache.

the class TemplateManagerImpl method detachIso.

@Override
@ActionEvent(eventType = EventTypes.EVENT_ISO_DETACH, eventDescription = "detaching ISO", async = true)
public boolean detachIso(long vmId) {
    Account caller = CallContext.current().getCallingAccount();
    Long userId = CallContext.current().getCallingUserId();
    // Verify input parameters
    UserVmVO vmInstanceCheck = _userVmDao.findById(vmId);
    if (vmInstanceCheck == null) {
        throw new InvalidParameterValueException("Unable to find a virtual machine with id " + vmId);
    }
    UserVm userVM = _userVmDao.findById(vmId);
    if (userVM == null) {
        throw new InvalidParameterValueException("Please specify a valid VM.");
    }
    _accountMgr.checkAccess(caller, null, true, userVM);
    Long isoId = userVM.getIsoId();
    if (isoId == null) {
        throw new InvalidParameterValueException("The specified VM has no ISO attached to it.");
    }
    CallContext.current().setEventDetails("Vm Id: " + vmId + " ISO Id: " + isoId);
    State vmState = userVM.getState();
    if (vmState != State.Running && vmState != State.Stopped) {
        throw new InvalidParameterValueException("Please specify a VM that is either Stopped or Running.");
    }
    // attach=false
    boolean result = attachISOToVM(vmId, userId, isoId, false);
    // => detach
    if (result) {
        return result;
    } else {
        throw new CloudRuntimeException("Failed to detach iso");
    }
}
Also used : Account(com.cloud.user.Account) UserVmVO(com.cloud.vm.UserVmVO) UserVm(com.cloud.uservm.UserVm) InvalidParameterValueException(com.cloud.exception.InvalidParameterValueException) State(com.cloud.vm.VirtualMachine.State) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) ActionEvent(com.cloud.event.ActionEvent)

Example 55 with ActionEvent

use of com.cloud.event.ActionEvent in project cloudstack by apache.

the class AccountManagerImpl method deleteUserAccount.

@Override
@ActionEvent(eventType = EventTypes.EVENT_ACCOUNT_DELETE, eventDescription = "deleting account", async = true)
public // This method deletes the account
boolean deleteUserAccount(long accountId) {
    CallContext ctx = CallContext.current();
    long callerUserId = ctx.getCallingUserId();
    Account caller = ctx.getCallingAccount();
    // If the user is a System user, return an error. We do not allow this
    AccountVO account = _accountDao.findById(accountId);
    if (account == null || account.getRemoved() != null) {
        if (account != null) {
            s_logger.info("The account:" + account.getAccountName() + " is already removed");
        }
        return true;
    }
    // don't allow removing Project account
    if (account == null || account.getType() == Account.ACCOUNT_TYPE_PROJECT) {
        throw new InvalidParameterValueException("The specified account does not exist in the system");
    }
    checkAccess(caller, null, true, account);
    // don't allow to delete default account (system and admin)
    if (account.isDefault()) {
        throw new InvalidParameterValueException("The account is default and can't be removed");
    }
    // Account that manages project(s) can't be removed
    List<Long> managedProjectIds = _projectAccountDao.listAdministratedProjectIds(accountId);
    if (!managedProjectIds.isEmpty()) {
        StringBuilder projectIds = new StringBuilder();
        for (Long projectId : managedProjectIds) {
            projectIds.append(projectId + ", ");
        }
        throw new InvalidParameterValueException("The account id=" + accountId + " manages project(s) with ids " + projectIds + "and can't be removed");
    }
    CallContext.current().putContextParameter(Account.class, account.getUuid());
    return deleteAccount(account, callerUserId, caller);
}
Also used : InvalidParameterValueException(com.cloud.exception.InvalidParameterValueException) CallContext(org.apache.cloudstack.context.CallContext) ActionEvent(com.cloud.event.ActionEvent)

Aggregations

ActionEvent (com.cloud.event.ActionEvent)209 InvalidParameterValueException (com.cloud.exception.InvalidParameterValueException)174 Account (com.cloud.user.Account)114 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)80 DB (com.cloud.utils.db.DB)79 TransactionStatus (com.cloud.utils.db.TransactionStatus)40 PermissionDeniedException (com.cloud.exception.PermissionDeniedException)32 ResourceUnavailableException (com.cloud.exception.ResourceUnavailableException)32 ArrayList (java.util.ArrayList)31 CallContext (org.apache.cloudstack.context.CallContext)22 ConcurrentOperationException (com.cloud.exception.ConcurrentOperationException)20 TransactionCallbackNoReturn (com.cloud.utils.db.TransactionCallbackNoReturn)20 DataCenterVO (com.cloud.dc.DataCenterVO)18 Network (com.cloud.network.Network)18 LoadBalancerVO (com.cloud.network.dao.LoadBalancerVO)17 InvalidParameterException (java.security.InvalidParameterException)16 List (java.util.List)16 NetworkVO (com.cloud.network.dao.NetworkVO)15 ConfigurationException (javax.naming.ConfigurationException)15 ResourceAllocationException (com.cloud.exception.ResourceAllocationException)14