diff --git a/server/src/com/cloud/api/commands/ListSnapshotPoliciesCmd.java b/server/src/com/cloud/api/commands/ListSnapshotPoliciesCmd.java index 5ecb57e1e86..abada0404fe 100644 --- a/server/src/com/cloud/api/commands/ListSnapshotPoliciesCmd.java +++ b/server/src/com/cloud/api/commands/ListSnapshotPoliciesCmd.java @@ -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> s_properties = new ArrayList>(); - - static { - s_properties.add(new Pair(BaseCmd.Properties.ACCOUNT_OBJ, Boolean.FALSE)); - s_properties.add(new Pair(BaseCmd.Properties.ACCOUNT, Boolean.FALSE)); - s_properties.add(new Pair(BaseCmd.Properties.DOMAIN_ID, Boolean.FALSE)); - s_properties.add(new Pair(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> getProperties() { - return s_properties; - } + } - @Override - public List> execute(Map 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 polices = getManagementServer().listSnapshotPolicies(volumeId); - - List> policesTags = new ArrayList>(); - Object[] policyTag = new Object[polices.size()]; - int i = 0; - for (SnapshotPolicyVO policy : polices) { - List> policyData = new ArrayList>(); - policyData.add(new Pair(BaseCmd.Properties.ID.getName(), policy.getId())); - policyData.add(new Pair(BaseCmd.Properties.VOLUME_ID.getName(), policy.getVolumeId())); - policyData.add(new Pair(BaseCmd.Properties.SCHEDULE.getName(), policy.getSchedule())); - policyData.add(new Pair(BaseCmd.Properties.INTERVAL_TYPE.getName(), policy.getInterval())); - policyData.add(new Pair(BaseCmd.Properties.MAX_SNAPS.getName(), policy.getMaxSnaps())); - policyData.add(new Pair(BaseCmd.Properties.TIMEZONE.getName(), policy.getTimezone())); - policyTag[i++] = policyData; - } - Pair eventTag = new Pair("snapshotpolicy", policyTag); - policesTags.add(eventTag); - return policesTags; - + @Override @SuppressWarnings("unchecked") + public String getResponse() { + List policies = (List)getResponseObject(); + + List response = new ArrayList(); + 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); } } diff --git a/server/src/com/cloud/api/response/SnapshotPolicyResponse.java b/server/src/com/cloud/api/response/SnapshotPolicyResponse.java index 106568015aa..ebb80aa5c63 100644 --- a/server/src/com/cloud/api/response/SnapshotPolicyResponse.java +++ b/server/src/com/cloud/api/response/SnapshotPolicyResponse.java @@ -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; + } } diff --git a/server/src/com/cloud/server/ManagementServer.java b/server/src/com/cloud/server/ManagementServer.java index 6a713278380..bad3616cf49 100644 --- a/server/src/com/cloud/server/ManagementServer.java +++ b/server/src/com/cloud/server/ManagementServer.java @@ -1810,12 +1810,6 @@ public interface ManagementServer { StoragePoolVO findPoolById(Long id); List searchForStoragePools(Criteria c); - /** - * List all snapshot policies which are created for the specified volume - * @param volumeId - * @return - */ - List listSnapshotPolicies(long volumeId); SnapshotPolicyVO findSnapshotPolicyById(Long policyId); /** diff --git a/server/src/com/cloud/server/ManagementServerImpl.java b/server/src/com/cloud/server/ManagementServerImpl.java index 7963f2caf8b..3809fab0e54 100755 --- a/server/src/com/cloud/server/ManagementServerImpl.java +++ b/server/src/com/cloud/server/ManagementServerImpl.java @@ -8012,14 +8012,6 @@ public class ManagementServerImpl implements ManagementServer { return intervalTypes; } - @Override - public List 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); diff --git a/server/src/com/cloud/storage/snapshot/SnapshotManager.java b/server/src/com/cloud/storage/snapshot/SnapshotManager.java index dae6d9e3b24..367ca46b30c 100644 --- a/server/src/com/cloud/storage/snapshot/SnapshotManager.java +++ b/server/src/com/cloud/storage/snapshot/SnapshotManager.java @@ -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 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 listPoliciesforVolume(ListSnapshotPoliciesCmd cmd) throws InvalidParameterValueException; /** * List all policies to which a specified snapshot belongs. For ex: A snapshot diff --git a/server/src/com/cloud/storage/snapshot/SnapshotManagerImpl.java b/server/src/com/cloud/storage/snapshot/SnapshotManagerImpl.java index 887e2c2df7b..3a27cf877b8 100644 --- a/server/src/com/cloud/storage/snapshot/SnapshotManagerImpl.java +++ b/server/src/com/cloud/storage/snapshot/SnapshotManagerImpl.java @@ -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 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 listPoliciesforVolume(long volumeId) { return _snapshotPolicyDao.listByVolumeId(volumeId);