Skip to content

Commit d184c56

Browse files
committed
cleanup snapshot code
1 parent 87a5ee2 commit d184c56

10 files changed

Lines changed: 152 additions & 237 deletions

File tree

api/src/com/cloud/api/commands/ListSnapshotsCmd.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ public AsyncJob.Type getInstanceType() {
109109

110110
@Override
111111
public void execute(){
112-
List<? extends Snapshot> result = _mgr.listSnapshots(this);
112+
List<? extends Snapshot> result = _snapshotMgr.listSnapshots(this);
113113
ListResponse<SnapshotResponse> response = new ListResponse<SnapshotResponse>();
114114
List<SnapshotResponse> snapshotResponses = new ArrayList<SnapshotResponse>();
115115
for (Snapshot snapshot : result) {

api/src/com/cloud/server/ManagementService.java

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -344,15 +344,6 @@ public interface ManagementService {
344344
*/
345345
List<? extends Capacity> listCapacities(ListCapacityCmd cmd);
346346

347-
/**
348-
* List all snapshots of a disk volume. Optionaly lists snapshots created by specified interval
349-
* @param cmd the command containing the search criteria (order by, limit, etc.)
350-
* @return list of snapshots
351-
* @throws InvalidParameterValueException
352-
* @throws PermissionDeniedException
353-
*/
354-
List<? extends Snapshot> listSnapshots(ListSnapshotsCmd cmd);
355-
356347
/**
357348
* List the permissions on a template. This will return a list of account names that have been granted permission to launch instances from the template.
358349
* @param cmd the command wrapping the search criteria (template id)

api/src/com/cloud/storage/snapshot/SnapshotService.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,9 @@
2626
import com.cloud.api.commands.DeleteSnapshotPoliciesCmd;
2727
import com.cloud.api.commands.ListRecurringSnapshotScheduleCmd;
2828
import com.cloud.api.commands.ListSnapshotPoliciesCmd;
29+
import com.cloud.api.commands.ListSnapshotsCmd;
2930
import com.cloud.exception.InvalidParameterValueException;
31+
import com.cloud.exception.PermissionDeniedException;
3032
import com.cloud.exception.ResourceAllocationException;
3133
import com.cloud.storage.Snapshot;
3234

@@ -46,6 +48,15 @@ public interface SnapshotService {
4648
*/
4749
Snapshot createSnapshotInternal(CreateSnapshotInternalCmd cmd) throws ResourceAllocationException;
4850

51+
/**
52+
* List all snapshots of a disk volume. Optionally lists snapshots created by specified interval
53+
* @param cmd the command containing the search criteria (order by, limit, etc.)
54+
* @return list of snapshots
55+
* @throws InvalidParameterValueException
56+
* @throws PermissionDeniedException
57+
*/
58+
List<? extends Snapshot> listSnapshots(ListSnapshotsCmd cmd);
59+
4960
/**
5061
* Delete specified snapshot from the specified.
5162
* If no other policies are assigned it calls destroy snapshot.

server/src/com/cloud/server/ManagementServer.java

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -503,13 +503,6 @@ public interface ManagementServer extends ManagementService {
503503

504504
public long getMemoryUsagebyHost(Long hostId);
505505

506-
/**
507-
* Destroy a snapshot
508-
* @param snapshotId the id of the snapshot to destroy
509-
* @return true if snapshot successfully destroyed, false otherwise
510-
*/
511-
boolean destroyTemplateSnapshot(Long userId, long snapshotId);
512-
513506
/**
514507
* Finds a diskOffering by the specified ID.
515508
* @param diskOfferingId
@@ -547,8 +540,6 @@ public interface ManagementServer extends ManagementService {
547540
StoragePoolVO findPoolById(Long id);
548541
List<? extends StoragePoolVO> searchForStoragePools(Criteria c);
549542

550-
SnapshotPolicyVO findSnapshotPolicyById(Long policyId);
551-
552543
/**
553544
* Return whether a domain is a child domain of a given domain.
554545
* @param parentId

server/src/com/cloud/server/ManagementServerImpl.java

Lines changed: 1 addition & 166 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,6 @@
107107
import com.cloud.api.commands.ListRemoteAccessVpnsCmd;
108108
import com.cloud.api.commands.ListRoutersCmd;
109109
import com.cloud.api.commands.ListServiceOfferingsCmd;
110-
import com.cloud.api.commands.ListSnapshotsCmd;
111110
import com.cloud.api.commands.ListStoragePoolsCmd;
112111
import com.cloud.api.commands.ListSystemVMsCmd;
113112
import com.cloud.api.commands.ListTemplateOrIsoPermissionsCmd;
@@ -218,10 +217,6 @@
218217
import com.cloud.storage.GuestOSCategoryVO;
219218
import com.cloud.storage.GuestOSVO;
220219
import com.cloud.storage.LaunchPermissionVO;
221-
import com.cloud.storage.Snapshot;
222-
import com.cloud.storage.Snapshot.Type;
223-
import com.cloud.storage.SnapshotPolicyVO;
224-
import com.cloud.storage.SnapshotVO;
225220
import com.cloud.storage.Storage;
226221
import com.cloud.storage.Storage.ImageFormat;
227222
import com.cloud.storage.Storage.TemplateType;
@@ -234,15 +229,12 @@
234229
import com.cloud.storage.UploadVO;
235230
import com.cloud.storage.VMTemplateVO;
236231
import com.cloud.storage.Volume;
237-
import com.cloud.storage.Volume.VolumeType;
238232
import com.cloud.storage.VolumeStats;
239233
import com.cloud.storage.VolumeVO;
240234
import com.cloud.storage.dao.DiskOfferingDao;
241235
import com.cloud.storage.dao.GuestOSCategoryDao;
242236
import com.cloud.storage.dao.GuestOSDao;
243237
import com.cloud.storage.dao.LaunchPermissionDao;
244-
import com.cloud.storage.dao.SnapshotDao;
245-
import com.cloud.storage.dao.SnapshotPolicyDao;
246238
import com.cloud.storage.dao.StoragePoolDao;
247239
import com.cloud.storage.dao.StoragePoolHostDao;
248240
import com.cloud.storage.dao.UploadDao;
@@ -346,8 +338,6 @@ public class ManagementServerImpl implements ManagementServer {
346338
private final UserAccountDao _userAccountDao;
347339
private final AlertDao _alertDao;
348340
private final CapacityDao _capacityDao;
349-
private final SnapshotDao _snapshotDao;
350-
private final SnapshotPolicyDao _snapshotPolicyDao;
351341
private final GuestOSDao _guestOSDao;
352342
private final GuestOSCategoryDao _guestOSCategoryDao;
353343
private final StoragePoolDao _poolDao;
@@ -443,8 +433,6 @@ protected ManagementServerImpl() {
443433
_userAccountDao = locator.getDao(UserAccountDao.class);
444434
_alertDao = locator.getDao(AlertDao.class);
445435
_capacityDao = locator.getDao(CapacityDao.class);
446-
_snapshotDao = locator.getDao(SnapshotDao.class);
447-
_snapshotPolicyDao = locator.getDao(SnapshotPolicyDao.class);
448436
_guestOSDao = locator.getDao(GuestOSDao.class);
449437
_guestOSCategoryDao = locator.getDao(GuestOSCategoryDao.class);
450438
_poolDao = locator.getDao(StoragePoolDao.class);
@@ -4121,117 +4109,6 @@ public long getMemoryUsagebyHost(Long hostId) {
41214109
return mem;
41224110
}
41234111

4124-
@Override
4125-
public boolean destroyTemplateSnapshot(Long userId, long snapshotId) {
4126-
return _vmMgr.destroyTemplateSnapshot(userId, snapshotId);
4127-
}
4128-
4129-
@Override
4130-
public List<SnapshotVO> listSnapshots(ListSnapshotsCmd cmd) throws InvalidParameterValueException, PermissionDeniedException {
4131-
Long volumeId = cmd.getVolumeId();
4132-
4133-
// Verify parameters
4134-
if(volumeId != null){
4135-
VolumeVO volume = _volumeDao.findById(volumeId);
4136-
if (volume == null) {
4137-
throw new InvalidParameterValueException("unable to find a volume with id " + volumeId);
4138-
}
4139-
checkAccountPermissions(volume.getAccountId(), volume.getDomainId(), "volume", volumeId);
4140-
}
4141-
4142-
Account account = UserContext.current().getAccount();
4143-
Long domainId = cmd.getDomainId();
4144-
String accountName = cmd.getAccountName();
4145-
Long accountId = null;
4146-
if ((account == null) || isAdmin(account.getType())) {
4147-
if (domainId != null) {
4148-
if ((account != null) && !_domainDao.isChildDomain(account.getDomainId(), domainId)) {
4149-
throw new PermissionDeniedException("Unable to list templates for domain " + domainId + ", permission denied.");
4150-
}
4151-
} else if ((account != null) && (account.getType() == Account.ACCOUNT_TYPE_DOMAIN_ADMIN)) {
4152-
domainId = account.getDomainId();
4153-
}
4154-
4155-
if (domainId != null && accountName != null) {
4156-
Account userAccount = _accountDao.findActiveAccount(accountName, domainId);
4157-
if (userAccount != null) {
4158-
accountId = userAccount.getId();
4159-
}
4160-
}
4161-
} else {
4162-
accountId = account.getId();
4163-
}
4164-
4165-
Object name = cmd.getSnapshotName();
4166-
Object id = cmd.getId();
4167-
Object keyword = cmd.getKeyword();
4168-
Object snapshotTypeStr = cmd.getSnapshotType();
4169-
4170-
Filter searchFilter = new Filter(SnapshotVO.class, "created", false, cmd.getStartIndex(), cmd.getPageSizeVal());
4171-
SearchBuilder<SnapshotVO> sb = _snapshotDao.createSearchBuilder();
4172-
sb.and("status", sb.entity().getStatus(), SearchCriteria.Op.EQ);
4173-
sb.and("volumeId", sb.entity().getVolumeId(), SearchCriteria.Op.EQ);
4174-
sb.and("name", sb.entity().getName(), SearchCriteria.Op.LIKE);
4175-
sb.and("id", sb.entity().getId(), SearchCriteria.Op.EQ);
4176-
sb.and("accountId", sb.entity().getAccountId(), SearchCriteria.Op.EQ);
4177-
sb.and("snapshotTypeEQ", sb.entity().getSnapshotType(), SearchCriteria.Op.EQ);
4178-
sb.and("snapshotTypeNEQ", sb.entity().getSnapshotType(), SearchCriteria.Op.NEQ);
4179-
4180-
if ((accountId == null) && (domainId != null)) {
4181-
// if accountId isn't specified, we can do a domain match for the admin case
4182-
SearchBuilder<AccountVO> accountSearch = _accountDao.createSearchBuilder();
4183-
sb.join("accountSearch", accountSearch, sb.entity().getAccountId(), accountSearch.entity().getId(), JoinType.INNER);
4184-
4185-
SearchBuilder<DomainVO> domainSearch = _domainDao.createSearchBuilder();
4186-
domainSearch.and("path", domainSearch.entity().getPath(), SearchCriteria.Op.LIKE);
4187-
accountSearch.join("domainSearch", domainSearch, accountSearch.entity().getDomainId(), domainSearch.entity().getId(), JoinType.INNER);
4188-
}
4189-
4190-
SearchCriteria<SnapshotVO> sc = sb.create();
4191-
4192-
sc.setParameters("status", Snapshot.Status.BackedUp);
4193-
4194-
if (volumeId != null) {
4195-
sc.setParameters("volumeId", volumeId);
4196-
}
4197-
4198-
if (name != null) {
4199-
sc.setParameters("name", "%" + name + "%");
4200-
}
4201-
4202-
if (id != null) {
4203-
sc.setParameters("id", id);
4204-
}
4205-
4206-
if (keyword != null) {
4207-
SearchCriteria<SnapshotVO> ssc = _snapshotDao.createSearchCriteria();
4208-
ssc.addOr("name", SearchCriteria.Op.LIKE, "%" + keyword + "%");
4209-
4210-
sc.addAnd("name", SearchCriteria.Op.SC, ssc);
4211-
}
4212-
4213-
if (accountId != null) {
4214-
sc.setParameters("accountId", accountId);
4215-
} else if (domainId != null) {
4216-
DomainVO domain = _domainDao.findById(domainId);
4217-
SearchCriteria<?> joinSearch = sc.getJoin("accountSearch");
4218-
joinSearch.setJoinParameters("domainSearch", "path", domain.getPath() + "%");
4219-
}
4220-
4221-
if (snapshotTypeStr != null) {
4222-
Type snapshotType = SnapshotVO.getSnapshotType((String)snapshotTypeStr);
4223-
if (snapshotType == null) {
4224-
throw new InvalidParameterValueException("Unsupported snapshot type " + snapshotTypeStr);
4225-
}
4226-
sc.setParameters("snapshotTypeEQ", snapshotType.ordinal());
4227-
} else {
4228-
// Show only MANUAL and RECURRING snapshot types
4229-
sc.setParameters("snapshotTypeNEQ", Snapshot.Type.TEMPLATE.ordinal());
4230-
}
4231-
4232-
return _snapshotDao.search(sc, searchFilter);
4233-
}
4234-
42354112
@Override
42364113
public DiskOfferingVO findDiskOfferingById(long diskOfferingId) {
42374114
return _diskOfferingDao.findById(diskOfferingId);
@@ -5082,11 +4959,6 @@ public List<AsyncJobVO> searchForAsyncJobs(ListAsyncJobsCmd cmd) throws InvalidP
50824959
return _jobDao.search(sc, searchFilter);
50834960
}
50844961

5085-
@Override
5086-
public SnapshotPolicyVO findSnapshotPolicyById(Long policyId) {
5087-
return _snapshotPolicyDao.findById(policyId);
5088-
}
5089-
50904962
@Override
50914963
public boolean isChildDomain(Long parentId, Long childId) {
50924964
return _domainDao.isChildDomain(parentId, childId);
@@ -5261,25 +5133,6 @@ public VirtualMachine stopSystemVm(long vmId) {
52615133
return stopSecondaryStorageVm(vmId, eventId);
52625134
}
52635135
}
5264-
5265-
private void checkIfStoragePoolAvailable(Long id) throws StorageUnavailableException {
5266-
//check if the sp is up before starting
5267-
List<VolumeVO> rootVolList = _volumeDao.findByInstanceAndType(id, VolumeType.ROOT);
5268-
if(rootVolList == null || rootVolList.size() == 0){
5269-
throw new StorageUnavailableException("Could not find the root disk for this vm to verify if the pool on which it exists is Up or not");
5270-
}else{
5271-
Long poolId = rootVolList.get(0).getPoolId();//each vm has 1 root vol
5272-
StoragePoolVO sp = _poolDao.findById(poolId);
5273-
if(sp == null){
5274-
throw new StorageUnavailableException("Could not find the pool for the root disk of vm"+id+", to confirm if it is Up or not");
5275-
}else{
5276-
//found pool
5277-
if(!sp.getStatus().equals(com.cloud.host.Status.Up)){
5278-
throw new StorageUnavailableException("Could not start the vm; the associated storage pool is in:"+sp.getStatus().toString()+" state");
5279-
}
5280-
}
5281-
}
5282-
}
52835136

52845137
@Override
52855138
public VMInstanceVO stopSystemVM(StopSystemVmCmd cmd) {
@@ -5963,25 +5816,7 @@ public String[] getHypervisors(ListHypervisorsCmd cmd) {
59635816
return hypers.split(",");
59645817
}
59655818

5966-
private Long checkAccountPermissions(long targetAccountId, long targetDomainId, String targetDesc, long targetId) throws ServerApiException {
5967-
Long accountId = null;
5968-
5969-
Account account = UserContext.current().getAccount();
5970-
if (account != null) {
5971-
if (!isAdmin(account.getType())) {
5972-
if (account.getId() != targetAccountId) {
5973-
throw new ServerApiException(BaseCmd.PARAM_ERROR, "Unable to find a " + targetDesc + " with id " + targetId + " for this account");
5974-
}
5975-
} else if (!_domainDao.isChildDomain(account.getDomainId(), targetDomainId)) {
5976-
throw new ServerApiException(BaseCmd.PARAM_ERROR, "Unable to perform operation for " + targetDesc + " with id " + targetId + ", permission denied.");
5977-
}
5978-
accountId = account.getId();
5979-
}
5980-
5981-
return accountId;
5982-
}
5983-
5984-
@Override
5819+
@Override
59855820
public List<RemoteAccessVpnVO> searchForRemoteAccessVpns(ListRemoteAccessVpnsCmd cmd) throws InvalidParameterValueException,
59865821
PermissionDeniedException {
59875822
// do some parameter validation

server/src/com/cloud/storage/dao/SnapshotDaoImpl.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,6 @@ public SnapshotVO findNextSnapshot(long snapshotId) {
4949
public List<SnapshotVO> listByVolumeIdType(long volumeId, String type ) {
5050
return listByVolumeIdType(null, volumeId, type);
5151
}
52-
5352

5453
@Override
5554
public List<SnapshotVO> listByVolumeId(long volumeId) {

server/src/com/cloud/storage/snapshot/SnapshotManager.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@
1919

2020
import java.util.List;
2121

22+
import com.cloud.api.commands.ListSnapshotsCmd;
23+
import com.cloud.exception.InvalidParameterValueException;
24+
import com.cloud.exception.PermissionDeniedException;
2225
import com.cloud.exception.ResourceAllocationException;
2326
import com.cloud.storage.SnapshotPolicyVO;
2427
import com.cloud.storage.SnapshotVO;

0 commit comments

Comments
 (0)