Refactor listSnapshotPolicies to new API framework.

This commit is contained in:
Kris McQueen 2010-09-07 17:46:55 -07:00
parent e6ebe3ac77
commit 3b703c76cf
6 changed files with 64 additions and 76 deletions

View File

@ -20,29 +20,22 @@ package com.cloud.api.commands;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import org.apache.log4j.Logger;
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.SnapshotPolicyResponse;
import com.cloud.serializer.SerializerHelper;
import com.cloud.storage.SnapshotPolicyVO;
import com.cloud.storage.VolumeVO;
import com.cloud.utils.Pair;
public class ListSnapshotPoliciesCmd extends BaseCmd {
@Implementation(method="listPoliciesforVolume", manager=Manager.SnapshotManager)
public class ListSnapshotPoliciesCmd extends BaseListCmd {
public static final Logger s_logger = Logger.getLogger(ListSnapshotPoliciesCmd.class.getName());
private static final String s_name = "listsnapshotpoliciesresponse";
private static final List<Pair<Enum, Boolean>> s_properties = new ArrayList<Pair<Enum, Boolean>>();
static {
s_properties.add(new Pair<Enum, Boolean>(BaseCmd.Properties.ACCOUNT_OBJ, Boolean.FALSE));
s_properties.add(new Pair<Enum, Boolean>(BaseCmd.Properties.ACCOUNT, Boolean.FALSE));
s_properties.add(new Pair<Enum, Boolean>(BaseCmd.Properties.DOMAIN_ID, Boolean.FALSE));
s_properties.add(new Pair<Enum, Boolean>(BaseCmd.Properties.VOLUME_ID, Boolean.TRUE));
}
/////////////////////////////////////////////////////
//////////////// API parameters /////////////////////
@ -80,41 +73,25 @@ public class ListSnapshotPoliciesCmd extends BaseCmd {
@Override
public String getName() {
return s_name;
}
@Override
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());
// Verify that a volume exists with the specified volume ID
VolumeVO volume = getManagementServer().findVolumeById(volumeId);
if (volume == null) {
throw new ServerApiException (BaseCmd.PARAM_ERROR, "Unable to find a volume with id " + volumeId);
}
checkAccountPermissions(params, volume.getAccountId(), volume.getDomainId(), "volume", volumeId);
List<SnapshotPolicyVO> polices = getManagementServer().listSnapshotPolicies(volumeId);
List<Pair<String, Object>> policesTags = new ArrayList<Pair<String, Object>>();
Object[] policyTag = new Object[polices.size()];
int i = 0;
for (SnapshotPolicyVO policy : polices) {
List<Pair<String, Object>> policyData = new ArrayList<Pair<String, Object>>();
policyData.add(new Pair<String, Object>(BaseCmd.Properties.ID.getName(), policy.getId()));
policyData.add(new Pair<String, Object>(BaseCmd.Properties.VOLUME_ID.getName(), policy.getVolumeId()));
policyData.add(new Pair<String, Object>(BaseCmd.Properties.SCHEDULE.getName(), policy.getSchedule()));
policyData.add(new Pair<String, Object>(BaseCmd.Properties.INTERVAL_TYPE.getName(), policy.getInterval()));
policyData.add(new Pair<String, Object>(BaseCmd.Properties.MAX_SNAPS.getName(), policy.getMaxSnaps()));
policyData.add(new Pair<String, Object>(BaseCmd.Properties.TIMEZONE.getName(), policy.getTimezone()));
policyTag[i++] = policyData;
}
Pair<String, Object> eventTag = new Pair<String, Object>("snapshotpolicy", policyTag);
policesTags.add(eventTag);
return policesTags;
@Override @SuppressWarnings("unchecked")
public String getResponse() {
List<SnapshotPolicyVO> policies = (List<SnapshotPolicyVO>)getResponseObject();
List<SnapshotPolicyResponse> response = new ArrayList<SnapshotPolicyResponse>();
for (SnapshotPolicyVO policy : policies) {
SnapshotPolicyResponse policyResponse = new SnapshotPolicyResponse();
policyResponse.setId(policy.getId());
policyResponse.setVolumeId(policy.getVolumeId());
policyResponse.setSchedule(policy.getSchedule());
policyResponse.setIntervalType(policy.getInterval());
policyResponse.setMaxSnaps(policy.getMaxSnaps());
policyResponse.setTimezone(policy.getTimezone());
response.add(policyResponse);
}
return SerializerHelper.toSerializedString(response);
}
}

View File

@ -19,6 +19,9 @@ public class SnapshotPolicyResponse implements ResponseObject {
@Param(name="maxsnaps")
private int maxSnaps;
@Param(name="timezone")
private String timezone;
public Long getId() {
return id;
}
@ -58,4 +61,12 @@ public class SnapshotPolicyResponse implements ResponseObject {
public void setMaxSnaps(int maxSnaps) {
this.maxSnaps = maxSnaps;
}
public String getTimezone() {
return timezone;
}
public void setTimezone(String timezone) {
this.timezone = timezone;
}
}

View File

@ -1810,12 +1810,6 @@ public interface ManagementServer {
StoragePoolVO findPoolById(Long id);
List<? extends StoragePoolVO> searchForStoragePools(Criteria c);
/**
* List all snapshot policies which are created for the specified volume
* @param volumeId
* @return
*/
List<SnapshotPolicyVO> listSnapshotPolicies(long volumeId);
SnapshotPolicyVO findSnapshotPolicyById(Long policyId);
/**

View File

@ -8012,14 +8012,6 @@ public class ManagementServerImpl implements ManagementServer {
return intervalTypes;
}
@Override
public List<SnapshotPolicyVO> listSnapshotPolicies(long volumeId) {
if( _volumeDao.findById(volumeId) == null){
return null;
}
return _snapMgr.listPoliciesforVolume(volumeId);
}
@Override
public boolean isChildDomain(Long parentId, Long childId) {
return _domainDao.isChildDomain(parentId, childId);

View File

@ -23,6 +23,7 @@ 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.api.commands.ListSnapshotPoliciesCmd;
import com.cloud.exception.InternalErrorException;
import com.cloud.exception.InvalidParameterValueException;
import com.cloud.exception.PermissionDeniedException;
@ -121,6 +122,13 @@ public interface SnapshotManager extends Manager {
* List all policies which are assigned to the specified volume
*/
List<SnapshotPolicyVO> listPoliciesforVolume(long volumeId);
/**
* list all snapshot policies assigned to the specified volume
* @param cmd the command that specifies the volume criteria
* @return list of snapshot policies
*/
List<SnapshotPolicyVO> listPoliciesforVolume(ListSnapshotPoliciesCmd cmd) throws InvalidParameterValueException;
/**
* List all policies to which a specified snapshot belongs. For ex: A snapshot

View File

@ -47,6 +47,7 @@ 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.api.commands.ListSnapshotPoliciesCmd;
import com.cloud.async.AsyncJobExecutor;
import com.cloud.async.AsyncJobManager;
import com.cloud.async.AsyncJobVO;
@ -779,22 +780,16 @@ public class SnapshotManagerImpl implements SnapshotManager {
}
protected Long checkAccountPermissions(long targetAccountId,long targetDomainId,String targetDesc,long targetId) throws ServerApiException
{
private Long checkAccountPermissions(long targetAccountId,long targetDomainId,String targetDesc,long targetId) throws ServerApiException {
Long accountId = null;
Account account = (Account)UserContext.current().getAccountObject();
if (account != null)
{
if (!isAdmin(account.getType()))
{
if (account.getId().longValue() != targetAccountId)
{
if (account != null) {
if (!isAdmin(account.getType())) {
if (account.getId().longValue() != targetAccountId) {
throw new ServerApiException(BaseCmd.PARAM_ERROR, "Unable to find a " + targetDesc + " with id " + targetId + " for this account");
}
}
else if (!_domainDao.isChildDomain(account.getDomainId(), targetDomainId))
{
} else if (!_domainDao.isChildDomain(account.getDomainId(), targetDomainId)) {
throw new ServerApiException(BaseCmd.PARAM_ERROR, "Unable to perform operation for " + targetDesc + " with id " + targetId + ", permission denied.");
}
accountId = account.getId();
@ -1296,6 +1291,17 @@ public class SnapshotManagerImpl implements SnapshotManager {
return success;
}
@Override
public List<SnapshotPolicyVO> listPoliciesforVolume(ListSnapshotPoliciesCmd cmd) throws InvalidParameterValueException {
Long volumeId = cmd.getVolumeId();
VolumeVO volume = _volsDao.findById(volumeId);
if (volume == null) {
throw new InvalidParameterValueException("Unable to find a volume with id " + volumeId);
}
checkAccountPermissions(volume.getAccountId(), volume.getDomainId(), "volume", volumeId);
return listPoliciesforVolume(cmd.getVolumeId());
}
@Override
public List<SnapshotPolicyVO> listPoliciesforVolume(long volumeId) {
return _snapshotPolicyDao.listByVolumeId(volumeId);