Search in sources :

Example 36 with DataObject

use of org.apache.cloudstack.engine.subsystem.api.storage.DataObject in project cloudstack by apache.

the class VolumeApiServiceImpl method uploadVolume.

@Override
@ActionEvent(eventType = EventTypes.EVENT_VOLUME_UPLOAD, eventDescription = "uploading volume for post upload", async = true)
public GetUploadParamsResponse uploadVolume(final GetUploadParamsForVolumeCmd cmd) throws ResourceAllocationException, MalformedURLException {
    Account caller = CallContext.current().getCallingAccount();
    long ownerId = cmd.getEntityOwnerId();
    final Account owner = _entityMgr.findById(Account.class, ownerId);
    final Long zoneId = cmd.getZoneId();
    final String volumeName = cmd.getName();
    String format = cmd.getFormat();
    final Long diskOfferingId = cmd.getDiskOfferingId();
    String imageStoreUuid = cmd.getImageStoreUuid();
    final DataStore store = _tmpltMgr.getImageStore(imageStoreUuid, zoneId);
    validateVolume(caller, ownerId, zoneId, volumeName, null, format, diskOfferingId);
    return Transaction.execute(new TransactionCallbackWithException<GetUploadParamsResponse, MalformedURLException>() {

        @Override
        public GetUploadParamsResponse doInTransaction(TransactionStatus status) throws MalformedURLException {
            VolumeVO volume = persistVolume(owner, zoneId, volumeName, null, cmd.getFormat(), diskOfferingId, Volume.State.NotUploaded);
            VolumeInfo vol = volFactory.getVolume(volume.getId());
            RegisterVolumePayload payload = new RegisterVolumePayload(null, cmd.getChecksum(), cmd.getFormat());
            vol.addPayload(payload);
            Pair<EndPoint, DataObject> pair = volService.registerVolumeForPostUpload(vol, store);
            EndPoint ep = pair.first();
            DataObject dataObject = pair.second();
            GetUploadParamsResponse response = new GetUploadParamsResponse();
            String ssvmUrlDomain = _configDao.getValue(Config.SecStorageSecureCopyCert.key());
            String url = ImageStoreUtil.generatePostUploadUrl(ssvmUrlDomain, ep.getPublicAddr(), vol.getUuid());
            response.setPostURL(new URL(url));
            // set the post url, this is used in the monitoring thread to determine the SSVM
            VolumeDataStoreVO volumeStore = _volumeStoreDao.findByVolume(vol.getId());
            assert (volumeStore != null) : "sincle volume is registered, volumestore cannot be null at this stage";
            volumeStore.setExtractUrl(url);
            _volumeStoreDao.persist(volumeStore);
            response.setId(UUID.fromString(vol.getUuid()));
            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 key
                  */
            TemplateOrVolumePostUploadCommand command = new TemplateOrVolumePostUploadCommand(vol.getId(), vol.getUuid(), volumeStore.getInstallPath(), cmd.getChecksum(), vol.getType().toString(), vol.getName(), vol.getFormat().toString(), dataObject.getDataStore().getUri(), dataObject.getDataStore().getRole().toString());
            command.setLocalPath(volumeStore.getLocalDownloadPath());
            //using the existing max upload size configuration
            command.setMaxUploadSize(_configDao.getValue(Config.MaxUploadVolumeSize.key()));
            command.setDefaultMaxAccountSecondaryStorage(_configDao.getValue(Config.DefaultMaxAccountSecondaryStorage.key()));
            command.setAccountId(vol.getAccountId());
            Gson gson = new GsonBuilder().create();
            String metadata = EncryptionUtil.encodeData(gson.toJson(command), key);
            response.setMetadata(metadata);
            /*
                 * signature calculated on the url, expiry, metadata.
                 */
            response.setSignature(EncryptionUtil.generateSignature(metadata + url + expires, key));
            return response;
        }
    });
}
Also used : Account(com.cloud.user.Account) MalformedURLException(java.net.MalformedURLException) GsonBuilder(com.google.gson.GsonBuilder) TransactionStatus(com.cloud.utils.db.TransactionStatus) Gson(com.google.gson.Gson) VolumeInfo(org.apache.cloudstack.engine.subsystem.api.storage.VolumeInfo) EndPoint(org.apache.cloudstack.engine.subsystem.api.storage.EndPoint) GetUploadParamsResponse(org.apache.cloudstack.api.response.GetUploadParamsResponse) URL(java.net.URL) DateTime(org.joda.time.DateTime) DataObject(org.apache.cloudstack.engine.subsystem.api.storage.DataObject) TemplateOrVolumePostUploadCommand(org.apache.cloudstack.storage.command.TemplateOrVolumePostUploadCommand) DataStore(org.apache.cloudstack.engine.subsystem.api.storage.DataStore) VolumeDataStoreVO(org.apache.cloudstack.storage.datastore.db.VolumeDataStoreVO) Pair(com.cloud.utils.Pair) ActionEvent(com.cloud.event.ActionEvent)

Aggregations

DataObject (org.apache.cloudstack.engine.subsystem.api.storage.DataObject)36 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)16 DataStore (org.apache.cloudstack.engine.subsystem.api.storage.DataStore)13 CopyCommandResult (org.apache.cloudstack.engine.subsystem.api.storage.CopyCommandResult)12 CreateCmdResult (org.apache.cloudstack.engine.subsystem.api.storage.CreateCmdResult)11 EndPoint (org.apache.cloudstack.engine.subsystem.api.storage.EndPoint)11 ZoneScope (org.apache.cloudstack.engine.subsystem.api.storage.ZoneScope)8 CopyCommand (org.apache.cloudstack.storage.command.CopyCommand)7 Answer (com.cloud.agent.api.Answer)6 ConcurrentOperationException (com.cloud.exception.ConcurrentOperationException)6 AsyncCallFuture (org.apache.cloudstack.framework.async.AsyncCallFuture)6 RemoteHostEndPoint (org.apache.cloudstack.storage.RemoteHostEndPoint)6 CopyCmdAnswer (org.apache.cloudstack.storage.command.CopyCmdAnswer)6 NoTransitionException (com.cloud.utils.fsm.NoTransitionException)5 MigrateVolumeAnswer (com.cloud.agent.api.storage.MigrateVolumeAnswer)4 HostVO (com.cloud.host.HostVO)4 ClusterScope (org.apache.cloudstack.engine.subsystem.api.storage.ClusterScope)4 TemplateInfo (org.apache.cloudstack.engine.subsystem.api.storage.TemplateInfo)4 VMTemplateVO (com.cloud.storage.VMTemplateVO)3 DB (com.cloud.utils.db.DB)3