use of com.cloud.utils.DateUtil.IntervalType in project cloudstack by apache.
the class SnapshotManagerImpl method getSnapshotType.
private Type getSnapshotType(Long policyId) {
if (policyId.equals(Snapshot.MANUAL_POLICY_ID)) {
return Type.MANUAL;
} else {
SnapshotPolicyVO spstPolicyVO = _snapshotPolicyDao.findById(policyId);
IntervalType intvType = DateUtil.getIntervalType(spstPolicyVO.getInterval());
return getSnapshotType(intvType);
}
}
use of com.cloud.utils.DateUtil.IntervalType in project cloudstack by apache.
the class SnapshotSchedulerImpl method getNextScheduledTime.
private Date getNextScheduledTime(final long policyId, final Date currentTimestamp) {
final SnapshotPolicyVO policy = _snapshotPolicyDao.findById(policyId);
Date nextTimestamp = null;
if (policy != null) {
final short intervalType = policy.getInterval();
final IntervalType type = DateUtil.getIntervalType(intervalType);
final String schedule = policy.getSchedule();
final String timezone = policy.getTimezone();
nextTimestamp = DateUtil.getNextRunTime(type, schedule, timezone, currentTimestamp);
final String currentTime = DateUtil.displayDateInTimezone(DateUtil.GMT_TIMEZONE, currentTimestamp);
final String nextScheduledTime = DateUtil.displayDateInTimezone(DateUtil.GMT_TIMEZONE, nextTimestamp);
s_logger.debug("Current time is " + currentTime + ". NextScheduledTime of policyId " + policyId + " is " + nextScheduledTime);
}
return nextTimestamp;
}
use of com.cloud.utils.DateUtil.IntervalType in project cloudstack by apache.
the class SnapshotManagerTest method validatePersistSnapshotPolicyLockAquiredUpdateSnapshotPolicy.
@Test
public void validatePersistSnapshotPolicyLockAquiredUpdateSnapshotPolicy() {
PowerMockito.mockStatic(GlobalLock.class);
BDDMockito.given(GlobalLock.getInternLock(Mockito.anyString())).willReturn(globalLockMock);
Mockito.doReturn(true).when(globalLockMock).lock(Mockito.anyInt());
for (IntervalType intervalType : listIntervalTypes) {
Mockito.doReturn(snapshotPolicyVoInstance).when(snapshotPolicyDaoMock).findOneByVolumeInterval(Mockito.anyLong(), Mockito.eq(intervalType));
Mockito.doNothing().when(_snapshotMgr).updateSnapshotPolicy(Mockito.any(SnapshotPolicyVO.class), Mockito.anyString(), Mockito.anyString(), Mockito.any(DateUtil.IntervalType.class), Mockito.anyInt(), Mockito.anyBoolean(), Mockito.anyBoolean());
Mockito.doNothing().when(_snapshotMgr).createTagsForSnapshotPolicy(mapStringStringMock, snapshotPolicyVoMock);
SnapshotPolicyVO result = _snapshotMgr.persistSnapshotPolicy(volumeMock, TEST_SNAPSHOT_POLICY_SCHEDULE, TEST_SNAPSHOT_POLICY_TIMEZONE, intervalType, TEST_SNAPSHOT_POLICY_MAX_SNAPS, TEST_SNAPSHOT_POLICY_DISPLAY, TEST_SNAPSHOT_POLICY_ACTIVE, null);
assertSnapshotPolicyResultAgainstPreBuiltInstance(result);
}
VerificationMode timesVerification = Mockito.times(listIntervalTypes.size());
Mockito.verify(_snapshotMgr, Mockito.never()).createSnapshotPolicy(Mockito.anyLong(), Mockito.anyString(), Mockito.anyString(), Mockito.any(DateUtil.IntervalType.class), Mockito.anyInt(), Mockito.anyBoolean());
Mockito.verify(_snapshotMgr, timesVerification).updateSnapshotPolicy(Mockito.any(SnapshotPolicyVO.class), Mockito.anyString(), Mockito.anyString(), Mockito.any(DateUtil.IntervalType.class), Mockito.anyInt(), Mockito.anyBoolean(), Mockito.anyBoolean());
Mockito.verify(_snapshotMgr, timesVerification).createTagsForSnapshotPolicy(Mockito.any(), Mockito.any());
}
use of com.cloud.utils.DateUtil.IntervalType in project cloudstack by apache.
the class SnapshotManagerTest method validatePersistSnapshotPolicyLockAquiredCreateSnapshotPolicy.
@Test
public void validatePersistSnapshotPolicyLockAquiredCreateSnapshotPolicy() {
PowerMockito.mockStatic(GlobalLock.class);
BDDMockito.given(GlobalLock.getInternLock(Mockito.anyString())).willReturn(globalLockMock);
Mockito.doReturn(true).when(globalLockMock).lock(Mockito.anyInt());
for (IntervalType intervalType : listIntervalTypes) {
Mockito.doReturn(null).when(snapshotPolicyDaoMock).findOneByVolumeInterval(Mockito.anyLong(), Mockito.eq(intervalType));
Mockito.doReturn(snapshotPolicyVoInstance).when(_snapshotMgr).createSnapshotPolicy(Mockito.anyLong(), Mockito.anyString(), Mockito.anyString(), Mockito.eq(intervalType), Mockito.anyInt(), Mockito.anyBoolean());
Mockito.doNothing().when(_snapshotMgr).createTagsForSnapshotPolicy(mapStringStringMock, snapshotPolicyVoMock);
SnapshotPolicyVO result = _snapshotMgr.persistSnapshotPolicy(volumeMock, TEST_SNAPSHOT_POLICY_SCHEDULE, TEST_SNAPSHOT_POLICY_TIMEZONE, intervalType, TEST_SNAPSHOT_POLICY_MAX_SNAPS, TEST_SNAPSHOT_POLICY_DISPLAY, TEST_SNAPSHOT_POLICY_ACTIVE, null);
assertSnapshotPolicyResultAgainstPreBuiltInstance(result);
}
VerificationMode timesVerification = Mockito.times(listIntervalTypes.size());
Mockito.verify(_snapshotMgr, timesVerification).createSnapshotPolicy(Mockito.anyLong(), Mockito.anyString(), Mockito.anyString(), Mockito.any(DateUtil.IntervalType.class), Mockito.anyInt(), Mockito.anyBoolean());
Mockito.verify(_snapshotMgr, Mockito.never()).updateSnapshotPolicy(Mockito.any(SnapshotPolicyVO.class), Mockito.anyString(), Mockito.anyString(), Mockito.any(DateUtil.IntervalType.class), Mockito.anyInt(), Mockito.anyBoolean(), Mockito.anyBoolean());
Mockito.verify(_snapshotMgr, timesVerification).createTagsForSnapshotPolicy(Mockito.any(), Mockito.any());
}
use of com.cloud.utils.DateUtil.IntervalType in project cloudstack by apache.
the class SnapshotManagerImpl method createPolicy.
@Override
@DB
@ActionEvent(eventType = EventTypes.EVENT_SNAPSHOT_POLICY_CREATE, eventDescription = "creating snapshot policy")
public SnapshotPolicyVO createPolicy(CreateSnapshotPolicyCmd cmd, Account policyOwner) {
Long volumeId = cmd.getVolumeId();
boolean display = cmd.isDisplay();
VolumeVO volume = _volsDao.findById(cmd.getVolumeId());
if (volume == null) {
throw new InvalidParameterValueException("Failed to create snapshot policy, unable to find a volume with id " + volumeId);
}
String volumeDescription = volume.getVolumeDescription();
_accountMgr.checkAccess(CallContext.current().getCallingAccount(), null, true, volume);
// If display is false we don't actually schedule snapshots.
if (volume.getState() != Volume.State.Ready && display) {
throw new InvalidParameterValueException("VolumeId: " + volumeId + " is not in " + Volume.State.Ready + " state but " + volume.getState() + ". Cannot take snapshot.");
}
if (volume.getTemplateId() != null) {
VMTemplateVO template = _templateDao.findById(volume.getTemplateId());
Long instanceId = volume.getInstanceId();
UserVmVO userVmVO = null;
if (instanceId != null) {
userVmVO = _vmDao.findById(instanceId);
}
if (template != null && template.getTemplateType() == Storage.TemplateType.SYSTEM && (userVmVO == null || !UserVmManager.CKS_NODE.equals(userVmVO.getUserVmType()))) {
throw new InvalidParameterValueException("VolumeId: " + volumeId + " is for System VM , Creating snapshot against System VM volumes is not supported");
}
}
AccountVO owner = _accountDao.findById(volume.getAccountId());
Long instanceId = volume.getInstanceId();
String intervalType = cmd.getIntervalType();
if (instanceId != null) {
// It is not detached, but attached to a VM
if (_vmDao.findById(instanceId) == null) {
// It is not a UserVM but a SystemVM or DomR
throw new InvalidParameterValueException(String.format("Failed to create snapshot policy [%s] for volume %s; Snapshots of volumes attached to System or router VM are not allowed.", intervalType, volumeDescription));
}
}
IntervalType intvType = DateUtil.IntervalType.getIntervalType(intervalType);
if (intvType == null) {
throw new InvalidParameterValueException("Unsupported interval type " + intervalType);
}
Type type = getSnapshotType(intvType);
String cmdTimezone = cmd.getTimezone();
TimeZone timeZone = TimeZone.getTimeZone(cmdTimezone);
String timezoneId = timeZone.getID();
if (!timezoneId.equals(cmdTimezone)) {
s_logger.warn(String.format("Using timezone [%s] for running the snapshot policy [%s] for volume %s, as an equivalent of [%s].", timezoneId, intervalType, volumeDescription, cmdTimezone));
}
String schedule = cmd.getSchedule();
try {
DateUtil.getNextRunTime(intvType, schedule, timezoneId, null);
} catch (Exception e) {
throw new InvalidParameterValueException(String.format("%s has an invalid schedule [%s] for interval type [%s].", volumeDescription, schedule, intervalType));
}
int maxSnaps = cmd.getMaxSnaps();
if (maxSnaps <= 0) {
throw new InvalidParameterValueException(String.format("maxSnaps [%s] for volume %s should be greater than 0.", maxSnaps, volumeDescription));
}
int intervalMaxSnaps = type.getMax();
if (maxSnaps > intervalMaxSnaps) {
throw new InvalidParameterValueException(String.format("maxSnaps [%s] for volume %s exceeds limit [%s] for interval type [%s].", maxSnaps, volumeDescription, intervalMaxSnaps, intervalType));
}
// Verify that max doesn't exceed domain and account snapshot limits in case display is on
if (display) {
long accountLimit = _resourceLimitMgr.findCorrectResourceLimitForAccount(owner, ResourceType.snapshot);
long domainLimit = _resourceLimitMgr.findCorrectResourceLimitForDomain(_domainMgr.getDomain(owner.getDomainId()), ResourceType.snapshot);
if (!_accountMgr.isRootAdmin(owner.getId()) && ((accountLimit != -1 && maxSnaps > accountLimit) || (domainLimit != -1 && maxSnaps > domainLimit))) {
String message = "domain/account";
if (owner.getType() == Account.ACCOUNT_TYPE_PROJECT) {
message = "domain/project";
}
throw new InvalidParameterValueException("Max number of snapshots shouldn't exceed the " + message + " level snapshot limit");
}
}
Map<String, String> tags = cmd.getTags();
boolean active = true;
return persistSnapshotPolicy(volume, schedule, timezoneId, intvType, maxSnaps, display, active, tags);
}
Aggregations