Search in sources :

Example 16 with SnapshotPolicyVO

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

the class SnapshotManagerImpl method findRecurringSnapshotSchedule.

@Override
public List<SnapshotScheduleVO> findRecurringSnapshotSchedule(ListRecurringSnapshotScheduleCmd cmd) {
    Long volumeId = cmd.getVolumeId();
    Long policyId = cmd.getSnapshotPolicyId();
    Account account = CallContext.current().getCallingAccount();
    // Verify parameters
    VolumeVO volume = _volsDao.findById(volumeId);
    if (volume == null) {
        throw new InvalidParameterValueException("Failed to list snapshot schedule, unable to find a volume with id " + volumeId);
    }
    if (account != null) {
        long volAcctId = volume.getAccountId();
        if (_accountMgr.isAdmin(account.getId())) {
            Account userAccount = _accountDao.findById(Long.valueOf(volAcctId));
            if (!_domainDao.isChildDomain(account.getDomainId(), userAccount.getDomainId())) {
                throw new PermissionDeniedException("Unable to list snapshot schedule for volume " + volumeId + ", permission denied.");
            }
        } else if (account.getId() != volAcctId) {
            throw new PermissionDeniedException("Unable to list snapshot schedule, account " + account.getAccountName() + " does not own volume id " + volAcctId);
        }
    }
    // List only future schedules, not past ones.
    List<SnapshotScheduleVO> snapshotSchedules = new ArrayList<SnapshotScheduleVO>();
    if (policyId == null) {
        List<SnapshotPolicyVO> policyInstances = listPoliciesforVolume(volumeId);
        for (SnapshotPolicyVO policyInstance : policyInstances) {
            SnapshotScheduleVO snapshotSchedule = _snapshotScheduleDao.getCurrentSchedule(volumeId, policyInstance.getId(), false);
            snapshotSchedules.add(snapshotSchedule);
        }
    } else {
        snapshotSchedules.add(_snapshotScheduleDao.getCurrentSchedule(volumeId, policyId, false));
    }
    return snapshotSchedules;
}
Also used : Account(com.cloud.user.Account) VolumeVO(com.cloud.storage.VolumeVO) InvalidParameterValueException(com.cloud.exception.InvalidParameterValueException) ArrayList(java.util.ArrayList) PermissionDeniedException(com.cloud.exception.PermissionDeniedException) SnapshotScheduleVO(com.cloud.storage.SnapshotScheduleVO) SnapshotPolicyVO(com.cloud.storage.SnapshotPolicyVO)

Example 17 with SnapshotPolicyVO

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

the class SnapshotManagerImpl method deleteSnapshotPolicies.

@Override
public boolean deleteSnapshotPolicies(DeleteSnapshotPoliciesCmd cmd) {
    Long policyId = cmd.getId();
    List<Long> policyIds = cmd.getIds();
    if ((policyId == null) && (policyIds == null)) {
        throw new InvalidParameterValueException("No policy id (or list of ids) specified.");
    }
    if (policyIds == null) {
        policyIds = new ArrayList<Long>();
        policyIds.add(policyId);
    } else if (policyIds.size() <= 0) {
        // Not even sure how this is even possible
        throw new InvalidParameterValueException("There are no policy ids");
    }
    if (policyIds.contains(Snapshot.MANUAL_POLICY_ID)) {
        throw new InvalidParameterValueException("Invalid Policy id given: " + Snapshot.MANUAL_POLICY_ID);
    }
    for (Long policy : policyIds) {
        SnapshotPolicyVO snapshotPolicyVO = _snapshotPolicyDao.findById(policy);
        if (snapshotPolicyVO == null) {
            throw new InvalidParameterValueException("Policy id given: " + policy + " does not exist");
        }
        VolumeVO volume = _volsDao.findById(snapshotPolicyVO.getVolumeId());
        if (volume == null) {
            throw new InvalidParameterValueException("Policy id given: " + policy + " does not belong to a valid volume");
        }
        _accountMgr.checkAccess(CallContext.current().getCallingAccount(), null, true, volume);
    }
    for (Long pId : policyIds) {
        if (!deletePolicy(pId)) {
            s_logger.warn("Failed to delete snapshot policy with Id: " + policyId);
            return false;
        }
    }
    return true;
}
Also used : VolumeVO(com.cloud.storage.VolumeVO) InvalidParameterValueException(com.cloud.exception.InvalidParameterValueException) SnapshotPolicyVO(com.cloud.storage.SnapshotPolicyVO)

Example 18 with SnapshotPolicyVO

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

the class SnapshotManagerImpl method persistSnapshotPolicy.

protected SnapshotPolicyVO persistSnapshotPolicy(VolumeVO volume, String schedule, String timezone, IntervalType intervalType, int maxSnaps, boolean display, boolean active, Map<String, String> tags) {
    long volumeId = volume.getId();
    String volumeDescription = volume.getVolumeDescription();
    GlobalLock createSnapshotPolicyLock = GlobalLock.getInternLock("createSnapshotPolicy_" + volumeId);
    boolean isLockAcquired = createSnapshotPolicyLock.lock(5);
    if (!isLockAcquired) {
        throw new CloudRuntimeException(String.format("Unable to aquire lock for creating snapshot policy [%s] for %s.", intervalType, volumeDescription));
    }
    s_logger.debug(String.format("Acquired lock for creating snapshot policy [%s] for volume %s.", intervalType, volumeDescription));
    try {
        SnapshotPolicyVO policy = _snapshotPolicyDao.findOneByVolumeInterval(volumeId, intervalType);
        if (policy == null) {
            policy = createSnapshotPolicy(volumeId, schedule, timezone, intervalType, maxSnaps, display);
        } else {
            updateSnapshotPolicy(policy, schedule, timezone, intervalType, maxSnaps, active, display);
        }
        createTagsForSnapshotPolicy(tags, policy);
        CallContext.current().putContextParameter(SnapshotPolicy.class, policy.getUuid());
        return policy;
    } finally {
        createSnapshotPolicyLock.unlock();
    }
}
Also used : GlobalLock(com.cloud.utils.db.GlobalLock) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) SnapshotPolicyVO(com.cloud.storage.SnapshotPolicyVO)

Example 19 with SnapshotPolicyVO

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

the class SnapshotManagerImpl method deletePolicy.

protected boolean deletePolicy(Long policyId) {
    SnapshotPolicyVO snapshotPolicy = _snapshotPolicyDao.findById(policyId);
    _snapSchedMgr.removeSchedule(snapshotPolicy.getVolumeId(), snapshotPolicy.getId());
    taggedResourceService.deleteTags(Collections.singletonList(snapshotPolicy.getUuid()), ResourceObjectType.SnapshotPolicy, null);
    return _snapshotPolicyDao.remove(policyId);
}
Also used : SnapshotPolicyVO(com.cloud.storage.SnapshotPolicyVO)

Example 20 with SnapshotPolicyVO

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

the class SnapshotTestWithFakeData method createSnapshotPolicy.

protected SnapshotPolicyVO createSnapshotPolicy(Long volId) {
    SnapshotPolicyVO policyVO = new SnapshotPolicyVO(volId, "jfkd", "fdfd", DateUtil.IntervalType.DAILY, 8, true);
    policyVO = snapshotPolicyDao.persist(policyVO);
    return policyVO;
}
Also used : SnapshotPolicyVO(com.cloud.storage.SnapshotPolicyVO)

Aggregations

SnapshotPolicyVO (com.cloud.storage.SnapshotPolicyVO)20 IntervalType (com.cloud.utils.DateUtil.IntervalType)5 InvalidParameterValueException (com.cloud.exception.InvalidParameterValueException)4 Test (org.junit.Test)4 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)4 VolumeVO (com.cloud.storage.VolumeVO)3 ResourceType (com.cloud.configuration.Resource.ResourceType)2 HypervisorType (com.cloud.hypervisor.Hypervisor.HypervisorType)2 SnapshotScheduleVO (com.cloud.storage.SnapshotScheduleVO)2 SnapshotVO (com.cloud.storage.SnapshotVO)2 Account (com.cloud.user.Account)2 VMSnapshotVO (com.cloud.vm.snapshot.VMSnapshotVO)2 ArrayList (java.util.ArrayList)2 Date (java.util.Date)2 VerificationMode (org.mockito.verification.VerificationMode)2 ActionEvent (com.cloud.event.ActionEvent)1 PermissionDeniedException (com.cloud.exception.PermissionDeniedException)1 ResourceObjectType (com.cloud.server.ResourceTag.ResourceObjectType)1 DataStoreRole (com.cloud.storage.DataStoreRole)1 ScopeType (com.cloud.storage.ScopeType)1