Refactoring listRecurringSnapshotSchedule to new API framework.

This commit is contained in:
Kris McQueen 2010-09-03 14:41:26 -07:00
parent d3b4a56520
commit f653e54ce6
6 changed files with 120 additions and 111 deletions

View File

@ -20,25 +20,18 @@ package com.cloud.api.commands;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import com.cloud.api.BaseCmd;
import com.cloud.api.BaseCmd.Manager;
import com.cloud.api.BaseListCmd;
import com.cloud.api.Implementation;
import com.cloud.api.Parameter;
import com.cloud.api.ServerApiException;
import com.cloud.api.response.SnapshotScheduleResponse;
import com.cloud.serializer.SerializerHelper;
import com.cloud.storage.SnapshotScheduleVO;
import com.cloud.storage.VolumeVO;
import com.cloud.user.Account;
import com.cloud.utils.Pair;
public class ListRecurringSnapshotScheduleCmd extends BaseCmd {
@Implementation(method="findRecurringSnapshotSchedule", manager=Manager.SnapshotManager)
public class ListRecurringSnapshotScheduleCmd extends BaseListCmd {
private static final String s_name = "listrecurringsnapshotscheduleresponse";
private static final List<Pair<Enum, Boolean>> s_properties = new ArrayList<Pair<Enum, Boolean>>();
static {
s_properties.add(new Pair<Enum, Boolean>(BaseCmd.Properties.VOLUME_ID, Boolean.TRUE));
s_properties.add(new Pair<Enum, Boolean>(BaseCmd.Properties.SNAPSHOT_POLICY_ID, Boolean.FALSE));
s_properties.add(new Pair<Enum, Boolean>(BaseCmd.Properties.ACCOUNT_OBJ, Boolean.FALSE));
}
/////////////////////////////////////////////////////
//////////////// API parameters /////////////////////
@ -66,52 +59,26 @@ public class ListRecurringSnapshotScheduleCmd extends BaseCmd {
/////////////// API Implementation///////////////////
/////////////////////////////////////////////////////
@Override
public String getName() {
return s_name;
}
public List<Pair<Enum, Boolean>> getProperties() {
return s_properties;
}
@Override
public List<Pair<String, Object>> execute(Map<String, Object> params) {
Long volumeId = (Long)params.get(BaseCmd.Properties.VOLUME_ID.getName());
Long policyId = (Long)params.get(BaseCmd.Properties.SNAPSHOT_POLICY_ID.getName());
Account account = (Account)params.get(BaseCmd.Properties.ACCOUNT_OBJ.getName());
//Verify parameters
VolumeVO volume = getManagementServer().findVolumeById(volumeId);
if (volume == null) {
throw new ServerApiException (BaseCmd.SNAPSHOT_INVALID_PARAM_ERROR, "unable to find a volume with id " + volumeId);
}
if (account != null) {
long volAcctId = volume.getAccountId();
if (isAdmin(account.getType())) {
Account userAccount = getManagementServer().findAccountById(Long.valueOf(volAcctId));
if (!getManagementServer().isChildDomain(account.getDomainId(), userAccount.getDomainId())) {
throw new ServerApiException(BaseCmd.PARAM_ERROR, "Invalid volume id (" + volumeId + ") given, unable to list snapshots.");
}
} else if (account.getId().longValue() != volAcctId) {
throw new ServerApiException(BaseCmd.SNAPSHOT_INVALID_PARAM_ERROR, "account " + account.getAccountName() + " does not own volume id " + volAcctId);
}
}
List<SnapshotScheduleVO> recurringSnapshotSchedules = getManagementServer().findRecurringSnapshotSchedule(volumeId, policyId);
Object[] snapshotTag = new Object[recurringSnapshotSchedules.size()];
int i = 0;
for (SnapshotScheduleVO recurringSnapshotSchedule : recurringSnapshotSchedules) {
List<Pair<String, Object>> snapshotData = new ArrayList<Pair<String, Object>>();
snapshotData.add(new Pair<String, Object>(BaseCmd.Properties.ID.getName(), recurringSnapshotSchedule.getId().toString()));
snapshotData.add(new Pair<String, Object>(BaseCmd.Properties.VOLUME_ID.getName(), recurringSnapshotSchedule.getVolumeId().toString()));
snapshotData.add(new Pair<String, Object>(BaseCmd.Properties.SNAPSHOT_POLICY_ID.getName(), recurringSnapshotSchedule.getPolicyId().toString()));
snapshotData.add(new Pair<String, Object>(BaseCmd.Properties.SCHEDULED.getName(), getDateString(recurringSnapshotSchedule.getScheduledTimestamp())));
snapshotTag[i++] = snapshotData;
}
List<Pair<String, Object>> returnTags = new ArrayList<Pair<String, Object>>();
Pair<String, Object> snapshotTags = new Pair<String, Object>("snapshot", snapshotTag);
returnTags.add(snapshotTags);
return returnTags;
}
@Override @SuppressWarnings("unchecked")
public String getResponse() {
List<SnapshotScheduleVO> snapshotSchedules = (List<SnapshotScheduleVO>)getResponseObject();
List<SnapshotScheduleResponse> response = new ArrayList<SnapshotScheduleResponse>();
for (SnapshotScheduleVO snapshotSchedule : snapshotSchedules) {
SnapshotScheduleResponse snapSchedResponse = new SnapshotScheduleResponse();
snapSchedResponse.setId(snapshotSchedule.getId());
snapSchedResponse.setVolumeId(snapshotSchedule.getVolumeId());
snapSchedResponse.setSnapshotPolicyId(snapshotSchedule.getPolicyId());
snapSchedResponse.setScheduled(snapshotSchedule.getScheduledTimestamp());
response.add(snapSchedResponse);
}
return SerializerHelper.toSerializedString(response);
}
}

View File

@ -0,0 +1,52 @@
package com.cloud.api.response;
import java.util.Date;
import com.cloud.api.ResponseObject;
import com.cloud.serializer.Param;
public class SnapshotScheduleResponse implements ResponseObject {
@Param(name="id")
private Long id;
@Param(name="volumeid")
private Long volumeId;
@Param(name="snapshotpolicyid")
private Long snapshotPolicyId;
@Param(name="scheduled")
private Date scheduled;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public Long getVolumeId() {
return volumeId;
}
public void setVolumeId(Long volumeId) {
this.volumeId = volumeId;
}
public Long getSnapshotPolicyId() {
return snapshotPolicyId;
}
public void setSnapshotPolicyId(Long snapshotPolicyId) {
this.snapshotPolicyId = snapshotPolicyId;
}
public Date getScheduled() {
return scheduled;
}
public void setScheduled(Date scheduled) {
this.scheduled = scheduled;
}
}

View File

@ -105,7 +105,6 @@ import com.cloud.storage.GuestOSCategoryVO;
import com.cloud.storage.GuestOSVO;
import com.cloud.storage.Snapshot;
import com.cloud.storage.SnapshotPolicyVO;
import com.cloud.storage.SnapshotScheduleVO;
import com.cloud.storage.SnapshotVO;
import com.cloud.storage.StoragePoolVO;
import com.cloud.storage.StorageStats;
@ -1817,7 +1816,7 @@ public interface ManagementServer {
String[] getApiConfig();
StoragePoolVO findPoolById(Long id);
List<? extends StoragePoolVO> searchForStoragePools(Criteria c);
/**
* List all snapshot policies which are created for the specified volume
* @param volumeId
@ -1825,20 +1824,7 @@ public interface ManagementServer {
*/
List<SnapshotPolicyVO> listSnapshotPolicies(long volumeId);
SnapshotPolicyVO findSnapshotPolicyById(Long policyId);
/**
* Deletes snapshot scheduling policies
*/
// boolean deleteSnapshotPolicies(long userId, List<Long> policyIds) throws InvalidParameterValueException;
/**
* Get the recurring snapshots scheduled for this volume currently along with the time at which they are scheduled
* @param volumeId The volume for which the snapshots are required.
* @param policyId Show snapshots for only this policy.
* @return The list of snapshot schedules.
*/
List<SnapshotScheduleVO> findRecurringSnapshotSchedule(Long volumeId, Long policyId);
/**
* Return whether a domain is a child domain of a given domain.
* @param parentId
@ -1846,25 +1832,23 @@ public interface ManagementServer {
* @return True if the domainIds are equal, or if the second domain is a child of the first domain. False otherwise.
*/
boolean isChildDomain(Long parentId, Long childId);
/**
* List interval types the specified snapshot belongs to
* @param snapshotId
* @return
*/
String getSnapshotIntervalTypes(long snapshotId);
List<SecondaryStorageVmVO> searchForSecondaryStorageVm(Criteria c);
/**
* Returns back a SHA1 signed response
* @param userId -- id for the user
* @return -- ArrayList of <CloudId+Signature>
*/
ArrayList<String> getCloudIdentifierResponse(GetCloudIdentifierCmd cmd) throws InvalidParameterValueException;
NetworkGroupVO findNetworkGroupByName(Long accountId, String groupName);
/**

View File

@ -210,7 +210,6 @@ import com.cloud.storage.LaunchPermissionVO;
import com.cloud.storage.Snapshot;
import com.cloud.storage.Snapshot.SnapshotType;
import com.cloud.storage.SnapshotPolicyVO;
import com.cloud.storage.SnapshotScheduleVO;
import com.cloud.storage.SnapshotVO;
import com.cloud.storage.Storage;
import com.cloud.storage.Storage.ImageFormat;
@ -7984,14 +7983,6 @@ public class ManagementServerImpl implements ManagementServer {
return intervalTypes;
}
/**
* {@inheritDoc}
*/
@Override
public List<SnapshotScheduleVO> findRecurringSnapshotSchedule(Long volumeId, Long policyId) {
return _snapMgr.findRecurringSnapshotSchedule(volumeId, policyId);
}
@Override
public List<SnapshotPolicyVO> listSnapshotPolicies(long volumeId) {
if( _volumeDao.findById(volumeId) == null){

View File

@ -22,8 +22,10 @@ import java.util.List;
import com.cloud.api.commands.CreateSnapshotPolicyCmd;
import com.cloud.api.commands.DeleteSnapshotCmd;
import com.cloud.api.commands.DeleteSnapshotPoliciesCmd;
import com.cloud.api.commands.ListRecurringSnapshotScheduleCmd;
import com.cloud.exception.InternalErrorException;
import com.cloud.exception.InvalidParameterValueException;
import com.cloud.exception.PermissionDeniedException;
import com.cloud.exception.ResourceAllocationException;
import com.cloud.storage.SnapshotPolicyVO;
import com.cloud.storage.SnapshotScheduleVO;
@ -134,11 +136,10 @@ public interface SnapshotManager extends Manager {
/**
* Get the recurring snapshots scheduled for this volume currently along with the time at which they are scheduled
* @param volumeId The volume for which the snapshots are required.
* @param policyId Show snapshots for only this policy.
* @param cmd the command wrapping the volumeId (volume for which the snapshots are required) and policyId (to show snapshots for only this policy).
* @return The list of snapshot schedules.
*/
public List<SnapshotScheduleVO> findRecurringSnapshotSchedule(Long volumeId, Long policyId);
public List<SnapshotScheduleVO> findRecurringSnapshotSchedule(ListRecurringSnapshotScheduleCmd cmd) throws InvalidParameterValueException, PermissionDeniedException;
SnapshotPolicyVO getPolicyForVolumeByInterval(long volumeId, short interval);

View File

@ -22,7 +22,6 @@ import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.StringTokenizer;
import java.util.TimeZone;
import javax.ejb.Local;
@ -47,6 +46,7 @@ import com.cloud.api.commands.CreateSnapshotPolicyCmd;
import com.cloud.api.commands.CreateVolumeCmd;
import com.cloud.api.commands.DeleteSnapshotCmd;
import com.cloud.api.commands.DeleteSnapshotPoliciesCmd;
import com.cloud.api.commands.ListRecurringSnapshotScheduleCmd;
import com.cloud.async.AsyncJobExecutor;
import com.cloud.async.AsyncJobManager;
import com.cloud.async.AsyncJobVO;
@ -62,11 +62,11 @@ import com.cloud.event.EventVO;
import com.cloud.event.dao.EventDao;
import com.cloud.exception.InternalErrorException;
import com.cloud.exception.InvalidParameterValueException;
import com.cloud.exception.PermissionDeniedException;
import com.cloud.exception.ResourceAllocationException;
import com.cloud.host.dao.DetailsDao;
import com.cloud.host.dao.HostDao;
import com.cloud.serializer.GsonHelper;
import com.cloud.server.ManagementServer;
import com.cloud.storage.Snapshot;
import com.cloud.storage.Snapshot.SnapshotType;
import com.cloud.storage.Snapshot.Status;
@ -811,8 +811,6 @@ public class SnapshotManagerImpl implements SnapshotManager {
@Override @DB
public boolean deleteSnapshot(DeleteSnapshotCmd cmd) {
Account account = (Account)UserContext.current().getAccountObject();
Long userId = UserContext.current().getUserId();
Long snapshotId = cmd.getId();
@ -834,22 +832,19 @@ public class SnapshotManagerImpl implements SnapshotManager {
userId = Long.valueOf(1);
}
long volumeId = snapshotCheck.getVolumeId();
List<SnapshotPolicyVO> policies = listPoliciesforSnapshot(snapshotId);
boolean status = true;
for (SnapshotPolicyVO policy : policies) {
status = deleteSnapshotInternal(snapshotId,policy.getId(),userId);
if(!status)
{
if (!status) {
s_logger.warn("Failed to delete snapshot");
throw new ServerApiException(BaseCmd.INTERNAL_ERROR,"Failed to delete snapshot:"+snapshotId);
}
}
return status;
}
private boolean deleteSnapshotInternal(Long snapshotId, Long policyId, Long userId) {
@ -1400,12 +1395,34 @@ public class SnapshotManagerImpl implements SnapshotManager {
}
return success;
}
/**
* {@inheritDoc}
*/
@Override
public List<SnapshotScheduleVO> findRecurringSnapshotSchedule(Long volumeId, Long policyId) {
public List<SnapshotScheduleVO> findRecurringSnapshotSchedule(ListRecurringSnapshotScheduleCmd cmd) throws InvalidParameterValueException, PermissionDeniedException {
Long volumeId = cmd.getVolumeId();
Long policyId = cmd.getSnapshotPolicyId();
Account account = (Account)UserContext.current().getAccountObject();
//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 (isAdmin(account.getType())) {
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().longValue() != 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) {
@ -1415,22 +1432,19 @@ public class SnapshotManagerImpl implements SnapshotManager {
_snapshotScheduleDao.getCurrentSchedule(volumeId, policyInstance.getId(), false);
snapshotSchedules.add(snapshotSchedule);
}
}
else {
} else {
snapshotSchedules.add(_snapshotScheduleDao.getCurrentSchedule(volumeId, policyId, false));
}
return snapshotSchedules;
}
@Override
public SnapshotPolicyVO getPolicyForVolumeByInterval(long volumeId,
short interval) {
public SnapshotPolicyVO getPolicyForVolumeByInterval(long volumeId, short interval) {
return _snapshotPolicyDao.findOneByVolumeInterval(volumeId, interval);
}
@Override
public boolean configure(String name, Map<String, Object> params)
throws ConfigurationException {
public boolean configure(String name, Map<String, Object> params) throws ConfigurationException {
_name = name;
ComponentLocator locator = ComponentLocator.getCurrentLocator();