diff --git a/api/src/com/cloud/api/commands/CreateSnapshotCmd.java b/api/src/com/cloud/api/commands/CreateSnapshotCmd.java index fdae2afbdaf..34817ab32ca 100755 --- a/api/src/com/cloud/api/commands/CreateSnapshotCmd.java +++ b/api/src/com/cloud/api/commands/CreateSnapshotCmd.java @@ -33,7 +33,6 @@ import com.cloud.exception.ResourceAllocationException; import com.cloud.storage.Snapshot; import com.cloud.storage.Volume; import com.cloud.user.Account; -import com.cloud.user.UserContext; @Implementation(description="Creates an instant snapshot of a volume.", responseObject=SnapshotResponse.class) public class CreateSnapshotCmd extends BaseAsyncCreateCmd { diff --git a/server/src/com/cloud/storage/dao/SnapshotDao.java b/server/src/com/cloud/storage/dao/SnapshotDao.java index 061d3adbd30..9e4f8aef124 100644 --- a/server/src/com/cloud/storage/dao/SnapshotDao.java +++ b/server/src/com/cloud/storage/dao/SnapshotDao.java @@ -18,9 +18,8 @@ package com.cloud.storage.dao; -import java.util.List; - -import com.cloud.storage.Snapshot; +import java.util.List; + import com.cloud.storage.SnapshotVO; import com.cloud.utils.db.Filter; import com.cloud.utils.db.GenericDao; diff --git a/server/src/com/cloud/storage/snapshot/SnapshotManagerImpl.java b/server/src/com/cloud/storage/snapshot/SnapshotManagerImpl.java index deefa91ec11..d35a59e147f 100755 --- a/server/src/com/cloud/storage/snapshot/SnapshotManagerImpl.java +++ b/server/src/com/cloud/storage/snapshot/SnapshotManagerImpl.java @@ -526,6 +526,10 @@ public class SnapshotManagerImpl implements SnapshotManager, SnapshotService, Ma _snapshotDao.update(snapshotId, snapshot); UsageEventVO usageEvent = new UsageEventVO(EventTypes.EVENT_SNAPSHOT_CREATE, snapshot.getAccountId(), volume.getDataCenterId(), snapshotId, snapshot.getName(), null, null, volume.getSize()); _usageEventDao.persist(usageEvent); + + if (snapshot.getSnapshotType() == Type.RECURRING.ordinal()) { + _accountMgr.incrementResourceCount(snapshot.getAccountId(), ResourceType.snapshot); + } } else { @@ -536,6 +540,12 @@ public class SnapshotManagerImpl implements SnapshotManager, SnapshotService, Ma // 2) Create the next Snapshot pretending this is a valid snapshot. // 3) backupSnapshotToSecondaryStorage of the next snapshot // will take care of cleaning up the state of this snapshot + + if (snapshot.getSnapshotType() == Type.RECURRING.ordinal()) { + _accountMgr.decrementResourceCount(snapshot.getAccountId(), ResourceType.snapshot); + UsageEventVO usageEvent = new UsageEventVO(EventTypes.EVENT_SNAPSHOT_DELETE, snapshot.getAccountId(), 0L, snapshotId, snapshot.getName(), null, null, 0L); + _usageEventDao.persist(usageEvent); + } _snapshotDao.remove(snapshotId); } txn.commit(); @@ -987,6 +997,9 @@ public class SnapshotManagerImpl implements SnapshotManager, SnapshotService, Ma if (volume == null) { throw new InvalidParameterValueException("Failed to create snapshot policy, unable to find a volume with id " + volumeId); } + + AccountVO owner = _accountDao.findById(volume.getAccountId()); + DomainVO domain = _domainDao.findById(owner.getDomainId()); // If an account was passed in, make sure that it matches the account of the volume checkAccountPermissions(volume.getAccountId(), volume.getDomainId(), "volume", volumeId); @@ -1028,6 +1041,15 @@ public class SnapshotManagerImpl implements SnapshotManager, SnapshotService, Ma if (cmd.getMaxSnaps() > intervalMaxSnaps) { throw new InvalidParameterValueException("maxSnaps exceeds limit: " + intervalMaxSnaps + " for interval type: " + cmd.getIntervalType()); } + + + //Verify that max doesn't exceed domain and account snapshot limits + long accountLimit = _accountMgr.findCorrectResourceLimit(owner, ResourceType.snapshot); + long domainLimit = _accountMgr.findCorrectResourceLimit(domain, ResourceType.snapshot); + int max = cmd.getMaxSnaps().intValue(); + if (owner.getType() != Account.ACCOUNT_TYPE_ADMIN && ((accountLimit != -1 && max > accountLimit ) || (domainLimit != -1 && max > domainLimit))){ + throw new InvalidParameterValueException("Max number of snapshots shouldn't exceed the domain/account level snapshot limit"); + } SnapshotPolicyVO policy = new SnapshotPolicyVO(volumeId, cmd.getSchedule(), timezoneId, (short)type.ordinal(), cmd.getMaxSnaps()); // Create an event