Search in sources :

Example 1 with TemplateProfile

use of com.cloud.storage.TemplateProfile in project cloudstack by apache.

the class TemplateManagerImpl method deleteIso.

@Override
@ActionEvent(eventType = EventTypes.EVENT_ISO_DELETE, eventDescription = "deleting iso", async = true)
public boolean deleteIso(DeleteIsoCmd cmd) {
    Long templateId = cmd.getId();
    Account caller = CallContext.current().getCallingAccount();
    Long zoneId = cmd.getZoneId();
    VMTemplateVO template = _tmpltDao.findById(templateId);
    if (template == null) {
        throw new InvalidParameterValueException("unable to find iso with id " + templateId);
    }
    _accountMgr.checkAccess(caller, AccessType.OperateEntry, true, template);
    if (template.getFormat() != ImageFormat.ISO) {
        throw new InvalidParameterValueException("Please specify a valid iso.");
    }
    // check if there is any VM using this ISO.
    if (!templateIsDeleteable(templateId)) {
        throw new InvalidParameterValueException("Unable to delete iso, as it's used by other vms");
    }
    if (zoneId != null && (_dataStoreMgr.getImageStore(zoneId) == null)) {
        throw new InvalidParameterValueException("Failed to find a secondary storage store in the specified zone.");
    }
    TemplateAdapter adapter = getAdapter(template.getHypervisorType());
    TemplateProfile profile = adapter.prepareDelete(cmd);
    boolean result = adapter.delete(profile);
    if (result) {
        return true;
    } else {
        throw new CloudRuntimeException("Failed to delete ISO");
    }
}
Also used : Account(com.cloud.user.Account) InvalidParameterValueException(com.cloud.exception.InvalidParameterValueException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) VMTemplateVO(com.cloud.storage.VMTemplateVO) TemplateProfile(com.cloud.storage.TemplateProfile) ActionEvent(com.cloud.event.ActionEvent)

Example 2 with TemplateProfile

use of com.cloud.storage.TemplateProfile 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 3 with TemplateProfile

use of com.cloud.storage.TemplateProfile 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 4 with TemplateProfile

use of com.cloud.storage.TemplateProfile 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 5 with TemplateProfile

use of com.cloud.storage.TemplateProfile in project cloudstack by apache.

the class TemplateManagerImpl method delete.

@Override
public boolean delete(long userId, long templateId, Long zoneId) {
    VMTemplateVO template = _tmpltDao.findById(templateId);
    if (template == null || template.getRemoved() != null) {
        throw new InvalidParameterValueException("Please specify a valid template.");
    }
    TemplateAdapter adapter = getAdapter(template.getHypervisorType());
    return adapter.delete(new TemplateProfile(userId, template, zoneId));
}
Also used : InvalidParameterValueException(com.cloud.exception.InvalidParameterValueException) VMTemplateVO(com.cloud.storage.VMTemplateVO) TemplateProfile(com.cloud.storage.TemplateProfile)

Aggregations

TemplateProfile (com.cloud.storage.TemplateProfile)16 VMTemplateVO (com.cloud.storage.VMTemplateVO)11 InvalidParameterValueException (com.cloud.exception.InvalidParameterValueException)9 Account (com.cloud.user.Account)6 ActionEvent (com.cloud.event.ActionEvent)5 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)4 UserVO (com.cloud.user.UserVO)3 PermissionDeniedException (com.cloud.exception.PermissionDeniedException)2 TemplateDataStoreVO (org.apache.cloudstack.storage.datastore.db.TemplateDataStoreVO)2 DataCenterVO (com.cloud.dc.DataCenterVO)1 GuestOS (com.cloud.storage.GuestOS)1 ImageFormat (com.cloud.storage.Storage.ImageFormat)1 AccountVO (com.cloud.user.AccountVO)1 Gson (com.google.gson.Gson)1 GsonBuilder (com.google.gson.GsonBuilder)1 URL (java.net.URL)1 GetUploadParamsResponse (org.apache.cloudstack.api.response.GetUploadParamsResponse)1 DataStore (org.apache.cloudstack.engine.subsystem.api.storage.DataStore)1 EndPoint (org.apache.cloudstack.engine.subsystem.api.storage.EndPoint)1 TemplateInfo (org.apache.cloudstack.engine.subsystem.api.storage.TemplateInfo)1