Search in sources :

Example 16 with Snapshot

use of com.cloud.storage.Snapshot in project cosmic by MissionCriticalCloud.

the class ApiDBUtils method findJobInstanceUuid.

public static String findJobInstanceUuid(final AsyncJob job) {
    if (job == null) {
        return null;
    }
    String jobInstanceId = null;
    final ApiCommandJobType jobInstanceType = EnumUtils.fromString(ApiCommandJobType.class, job.getInstanceType(), ApiCommandJobType.None);
    if (job.getInstanceId() == null) {
        // when assert is hit, implement 'getInstanceId' of BaseAsyncCmd and return appropriate instance id
        assert (false);
        return null;
    }
    if (jobInstanceType == ApiCommandJobType.Volume) {
        final VolumeVO volume = ApiDBUtils.findVolumeById(job.getInstanceId());
        if (volume != null) {
            jobInstanceId = volume.getUuid();
        }
    } else if (jobInstanceType == ApiCommandJobType.Template || jobInstanceType == ApiCommandJobType.Iso) {
        final VMTemplateVO template = ApiDBUtils.findTemplateById(job.getInstanceId());
        if (template != null) {
            jobInstanceId = template.getUuid();
        }
    } else if (jobInstanceType == ApiCommandJobType.VirtualMachine || jobInstanceType == ApiCommandJobType.ConsoleProxy || jobInstanceType == ApiCommandJobType.SystemVm || jobInstanceType == ApiCommandJobType.DomainRouter) {
        final VMInstanceVO vm = ApiDBUtils.findVMInstanceById(job.getInstanceId());
        if (vm != null) {
            jobInstanceId = vm.getUuid();
        }
    } else if (jobInstanceType == ApiCommandJobType.Snapshot) {
        final Snapshot snapshot = ApiDBUtils.findSnapshotById(job.getInstanceId());
        if (snapshot != null) {
            jobInstanceId = snapshot.getUuid();
        }
    } else if (jobInstanceType == ApiCommandJobType.Host) {
        final Host host = ApiDBUtils.findHostById(job.getInstanceId());
        if (host != null) {
            jobInstanceId = host.getUuid();
        }
    } else if (jobInstanceType == ApiCommandJobType.StoragePool) {
        final StoragePoolVO spool = ApiDBUtils.findStoragePoolById(job.getInstanceId());
        if (spool != null) {
            jobInstanceId = spool.getUuid();
        }
    } else if (jobInstanceType == ApiCommandJobType.IpAddress) {
        final IPAddressVO ip = ApiDBUtils.findIpAddressById(job.getInstanceId());
        if (ip != null) {
            jobInstanceId = ip.getUuid();
        }
    } else if (jobInstanceType == ApiCommandJobType.PhysicalNetwork) {
        final PhysicalNetworkVO pnet = ApiDBUtils.findPhysicalNetworkById(job.getInstanceId());
        if (pnet != null) {
            jobInstanceId = pnet.getUuid();
        }
    } else if (jobInstanceType == ApiCommandJobType.TrafficType) {
        final PhysicalNetworkTrafficTypeVO trafficType = ApiDBUtils.findPhysicalNetworkTrafficTypeById(job.getInstanceId());
        if (trafficType != null) {
            jobInstanceId = trafficType.getUuid();
        }
    } else if (jobInstanceType == ApiCommandJobType.PhysicalNetworkServiceProvider) {
        final PhysicalNetworkServiceProvider sp = ApiDBUtils.findPhysicalNetworkServiceProviderById(job.getInstanceId());
        if (sp != null) {
            jobInstanceId = sp.getUuid();
        }
    } else if (jobInstanceType == ApiCommandJobType.FirewallRule) {
        final FirewallRuleVO fw = ApiDBUtils.findFirewallRuleById(job.getInstanceId());
        if (fw != null) {
            jobInstanceId = fw.getUuid();
        }
    } else if (jobInstanceType == ApiCommandJobType.Account) {
        final Account acct = ApiDBUtils.findAccountById(job.getInstanceId());
        if (acct != null) {
            jobInstanceId = acct.getUuid();
        }
    } else if (jobInstanceType == ApiCommandJobType.User) {
        final User usr = ApiDBUtils.findUserById(job.getInstanceId());
        if (usr != null) {
            jobInstanceId = usr.getUuid();
        }
    } else if (jobInstanceType == ApiCommandJobType.StaticRoute) {
        final StaticRouteVO route = ApiDBUtils.findStaticRouteById(job.getInstanceId());
        if (route != null) {
            jobInstanceId = route.getUuid();
        }
    } else if (jobInstanceType == ApiCommandJobType.PrivateGateway) {
        final VpcGatewayVO gateway = ApiDBUtils.findVpcGatewayById(job.getInstanceId());
        if (gateway != null) {
            jobInstanceId = gateway.getUuid();
        }
    } else if (jobInstanceType == ApiCommandJobType.Network) {
        final NetworkVO networkVO = ApiDBUtils.findNetworkById(job.getInstanceId());
        if (networkVO != null) {
            jobInstanceId = networkVO.getUuid();
        }
    } else if (jobInstanceType != ApiCommandJobType.None) {
        // entity table mapping
        assert (false);
    }
    return jobInstanceId;
}
Also used : ProjectAccount(com.cloud.projects.ProjectAccount) UserAccount(com.cloud.user.UserAccount) Account(com.cloud.user.Account) VpcGatewayVO(com.cloud.network.vpc.VpcGatewayVO) PhysicalNetworkVO(com.cloud.network.dao.PhysicalNetworkVO) NetworkVO(com.cloud.network.dao.NetworkVO) User(com.cloud.user.User) VMTemplateVO(com.cloud.storage.VMTemplateVO) VMInstanceVO(com.cloud.vm.VMInstanceVO) Host(com.cloud.host.Host) PhysicalNetworkTrafficTypeVO(com.cloud.network.dao.PhysicalNetworkTrafficTypeVO) PhysicalNetworkServiceProvider(com.cloud.network.PhysicalNetworkServiceProvider) StaticRouteVO(com.cloud.network.vpc.StaticRouteVO) FirewallRuleVO(com.cloud.network.rules.FirewallRuleVO) VMSnapshot(com.cloud.vm.snapshot.VMSnapshot) Snapshot(com.cloud.storage.Snapshot) VolumeVO(com.cloud.storage.VolumeVO) StoragePoolVO(com.cloud.storage.datastore.db.StoragePoolVO) PhysicalNetworkVO(com.cloud.network.dao.PhysicalNetworkVO) IPAddressVO(com.cloud.network.dao.IPAddressVO)

Example 17 with Snapshot

use of com.cloud.storage.Snapshot in project cosmic by MissionCriticalCloud.

the class ApiResponseHelper method createTemplateResponses.

@Override
public List<TemplateResponse> createTemplateResponses(final ResponseView view, final long templateId, final Long snapshotId, final Long volumeId, final boolean readyOnly) {
    Long zoneId = null;
    if (snapshotId != null) {
        final Snapshot snapshot = ApiDBUtils.findSnapshotById(snapshotId);
        final VolumeVO volume = findVolumeById(snapshot.getVolumeId());
        // if volume comes back null, use another technique to try to discover the zone
        if (volume == null) {
            final SnapshotDataStoreVO snapshotStore = _snapshotStoreDao.findBySnapshot(snapshot.getId(), DataStoreRole.Primary);
            if (snapshotStore != null) {
                final long storagePoolId = snapshotStore.getDataStoreId();
                final StoragePoolVO storagePool = _storagePoolDao.findById(storagePoolId);
                if (storagePool != null) {
                    zoneId = storagePool.getDataCenterId();
                }
            }
        } else {
            zoneId = volume.getDataCenterId();
        }
    } else {
        final VolumeVO volume = findVolumeById(volumeId);
        zoneId = volume.getDataCenterId();
    }
    if (zoneId == null) {
        throw new CloudRuntimeException("Unable to determine the zone ID");
    }
    return createTemplateResponses(view, templateId, zoneId, readyOnly);
}
Also used : VMSnapshot(com.cloud.vm.snapshot.VMSnapshot) Snapshot(com.cloud.storage.Snapshot) VolumeVO(com.cloud.storage.VolumeVO) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) SnapshotDataStoreVO(com.cloud.storage.datastore.db.SnapshotDataStoreVO) StoragePoolVO(com.cloud.storage.datastore.db.StoragePoolVO)

Example 18 with Snapshot

use of com.cloud.storage.Snapshot in project CloudStack-archive by CloudStack-extras.

the class ListSnapshotsCmd method execute.

@Override
public void execute() {
    List<? extends Snapshot> result = _snapshotService.listSnapshots(this);
    ListResponse<SnapshotResponse> response = new ListResponse<SnapshotResponse>();
    List<SnapshotResponse> snapshotResponses = new ArrayList<SnapshotResponse>();
    for (Snapshot snapshot : result) {
        SnapshotResponse snapshotResponse = _responseGenerator.createSnapshotResponse(snapshot);
        snapshotResponse.setObjectName("snapshot");
        snapshotResponses.add(snapshotResponse);
    }
    response.setResponses(snapshotResponses);
    response.setResponseName(getCommandName());
    this.setResponseObject(response);
}
Also used : Snapshot(com.cloud.storage.Snapshot) ListResponse(com.cloud.api.response.ListResponse) SnapshotResponse(com.cloud.api.response.SnapshotResponse) ArrayList(java.util.ArrayList)

Example 19 with Snapshot

use of com.cloud.storage.Snapshot in project CloudStack-archive by CloudStack-extras.

the class CreateTemplateCmd method getEntityOwnerId.

@Override
public long getEntityOwnerId() {
    Long volumeId = getVolumeId();
    Long snapshotId = getSnapshotId();
    Long accountId = null;
    if (volumeId != null) {
        Volume volume = _entityMgr.findById(Volume.class, volumeId);
        if (volume != null) {
            accountId = volume.getAccountId();
        } else {
            throw new InvalidParameterValueException("Unable to find volume by id=" + volumeId);
        }
    } else {
        Snapshot snapshot = _entityMgr.findById(Snapshot.class, snapshotId);
        if (snapshot != null) {
            accountId = snapshot.getAccountId();
        } else {
            throw new InvalidParameterValueException("Unable to find snapshot by id=" + snapshotId);
        }
    }
    Account account = _accountService.getAccount(accountId);
    //Can create templates for enabled projects/accounts only
    if (account.getType() == Account.ACCOUNT_TYPE_PROJECT) {
        Project project = _projectService.findByProjectAccountId(accountId);
        if (project.getState() != Project.State.Active) {
            PermissionDeniedException ex = new PermissionDeniedException("Can't add resources to the specified project id in state=" + project.getState() + " as it's no longer active");
            ex.addProxyObject(project, project.getId(), "projectId");
        }
    } else if (account.getState() == Account.State.disabled) {
        throw new PermissionDeniedException("The owner of template is disabled: " + account);
    }
    return accountId;
}
Also used : Snapshot(com.cloud.storage.Snapshot) Account(com.cloud.user.Account) Project(com.cloud.projects.Project) Volume(com.cloud.storage.Volume) InvalidParameterValueException(com.cloud.exception.InvalidParameterValueException) PermissionDeniedException(com.cloud.exception.PermissionDeniedException)

Example 20 with Snapshot

use of com.cloud.storage.Snapshot in project cosmic by MissionCriticalCloud.

the class CreateVolumeCmd method execute.

@Override
public void execute() {
    CallContext.current().setEventDetails("Volume Id: " + getEntityId() + ((getSnapshotId() == null) ? "" : " from snapshot: " + getSnapshotId()));
    final Volume volume = _volumeService.createVolume(this);
    if (volume != null) {
        final VolumeResponse response = _responseGenerator.createVolumeResponse(ResponseView.Restricted, volume);
        // FIXME - have to be moved to ApiResponseHelper
        if (getSnapshotId() != null) {
            final Snapshot snap = _entityMgr.findById(Snapshot.class, getSnapshotId());
            if (snap != null) {
                // if the volume was
                response.setSnapshotId(snap.getUuid());
            // created from a
            // snapshot,
            // snapshotId will
            // be set so we pass
            // it back in the
            // response
            }
        }
        response.setResponseName(getCommandName());
        setResponseObject(response);
    } else {
        throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, "Failed to create a volume");
    }
}
Also used : Snapshot(com.cloud.storage.Snapshot) VolumeResponse(com.cloud.api.response.VolumeResponse) ServerApiException(com.cloud.api.ServerApiException) Volume(com.cloud.storage.Volume)

Aggregations

Snapshot (com.cloud.storage.Snapshot)43 VMSnapshot (com.cloud.vm.snapshot.VMSnapshot)19 Test (org.junit.Test)11 ServerApiException (org.apache.cloudstack.api.ServerApiException)10 ServerApiException (com.cloud.api.ServerApiException)8 Volume (com.cloud.storage.Volume)8 VolumeVO (com.cloud.storage.VolumeVO)8 PermissionDeniedException (com.cloud.exception.PermissionDeniedException)7 SnapshotResponse (com.cloud.api.response.SnapshotResponse)6 Account (com.cloud.user.Account)6 SnapshotResponse (org.apache.cloudstack.api.response.SnapshotResponse)5 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)5 InvalidParameterValueException (com.cloud.exception.InvalidParameterValueException)4 ResourceAllocationException (com.cloud.exception.ResourceAllocationException)4 DataStoreRole (com.cloud.storage.DataStoreRole)4 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)4 InvalidParameterValueException (com.cloud.utils.exception.InvalidParameterValueException)3 ArrayList (java.util.ArrayList)3 HashMap (java.util.HashMap)3 SnapshotInfo (org.apache.cloudstack.engine.subsystem.api.storage.SnapshotInfo)3