From f653e54ce6d0c7c38ec7c4fee61d9cf5aeef7e07 Mon Sep 17 00:00:00 2001 From: Kris McQueen Date: Fri, 3 Sep 2010 14:41:26 -0700 Subject: [PATCH] Refactoring listRecurringSnapshotSchedule to new API framework. --- .../ListRecurringSnapshotScheduleCmd.java | 87 ++++++------------- .../response/SnapshotScheduleResponse.java | 52 +++++++++++ .../com/cloud/server/ManagementServer.java | 26 ++---- .../cloud/server/ManagementServerImpl.java | 9 -- .../storage/snapshot/SnapshotManager.java | 7 +- .../storage/snapshot/SnapshotManagerImpl.java | 50 +++++++---- 6 files changed, 120 insertions(+), 111 deletions(-) create mode 100644 server/src/com/cloud/api/response/SnapshotScheduleResponse.java diff --git a/server/src/com/cloud/api/commands/ListRecurringSnapshotScheduleCmd.java b/server/src/com/cloud/api/commands/ListRecurringSnapshotScheduleCmd.java index 843da134a6a..b68eabb9c5d 100644 --- a/server/src/com/cloud/api/commands/ListRecurringSnapshotScheduleCmd.java +++ b/server/src/com/cloud/api/commands/ListRecurringSnapshotScheduleCmd.java @@ -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> s_properties = new ArrayList>(); - - static { - s_properties.add(new Pair(BaseCmd.Properties.VOLUME_ID, Boolean.TRUE)); - s_properties.add(new Pair(BaseCmd.Properties.SNAPSHOT_POLICY_ID, Boolean.FALSE)); - s_properties.add(new Pair(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> getProperties() { - return s_properties; - } - - @Override - public List> execute(Map 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 recurringSnapshotSchedules = getManagementServer().findRecurringSnapshotSchedule(volumeId, policyId); - Object[] snapshotTag = new Object[recurringSnapshotSchedules.size()]; - int i = 0; - - for (SnapshotScheduleVO recurringSnapshotSchedule : recurringSnapshotSchedules) { - List> snapshotData = new ArrayList>(); - snapshotData.add(new Pair(BaseCmd.Properties.ID.getName(), recurringSnapshotSchedule.getId().toString())); - snapshotData.add(new Pair(BaseCmd.Properties.VOLUME_ID.getName(), recurringSnapshotSchedule.getVolumeId().toString())); - snapshotData.add(new Pair(BaseCmd.Properties.SNAPSHOT_POLICY_ID.getName(), recurringSnapshotSchedule.getPolicyId().toString())); - snapshotData.add(new Pair(BaseCmd.Properties.SCHEDULED.getName(), getDateString(recurringSnapshotSchedule.getScheduledTimestamp()))); - snapshotTag[i++] = snapshotData; - } - List> returnTags = new ArrayList>(); - Pair snapshotTags = new Pair("snapshot", snapshotTag); - returnTags.add(snapshotTags); - return returnTags; + } + + @Override @SuppressWarnings("unchecked") + public String getResponse() { + List snapshotSchedules = (List)getResponseObject(); + + List response = new ArrayList(); + 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); } } diff --git a/server/src/com/cloud/api/response/SnapshotScheduleResponse.java b/server/src/com/cloud/api/response/SnapshotScheduleResponse.java new file mode 100644 index 00000000000..a1ea9bad927 --- /dev/null +++ b/server/src/com/cloud/api/response/SnapshotScheduleResponse.java @@ -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; + } +} diff --git a/server/src/com/cloud/server/ManagementServer.java b/server/src/com/cloud/server/ManagementServer.java index d1a092f6884..2b07fc968ab 100644 --- a/server/src/com/cloud/server/ManagementServer.java +++ b/server/src/com/cloud/server/ManagementServer.java @@ -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 searchForStoragePools(Criteria c); - + /** * List all snapshot policies which are created for the specified volume * @param volumeId @@ -1825,20 +1824,7 @@ public interface ManagementServer { */ List listSnapshotPolicies(long volumeId); SnapshotPolicyVO findSnapshotPolicyById(Long policyId); - - /** - * Deletes snapshot scheduling policies - */ -// boolean deleteSnapshotPolicies(long userId, List 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 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 searchForSecondaryStorageVm(Criteria c); - + /** * Returns back a SHA1 signed response * @param userId -- id for the user * @return -- ArrayList of */ ArrayList getCloudIdentifierResponse(GetCloudIdentifierCmd cmd) throws InvalidParameterValueException; - + NetworkGroupVO findNetworkGroupByName(Long accountId, String groupName); /** diff --git a/server/src/com/cloud/server/ManagementServerImpl.java b/server/src/com/cloud/server/ManagementServerImpl.java index 3b1d852c1e0..8321b5df7df 100755 --- a/server/src/com/cloud/server/ManagementServerImpl.java +++ b/server/src/com/cloud/server/ManagementServerImpl.java @@ -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 findRecurringSnapshotSchedule(Long volumeId, Long policyId) { - return _snapMgr.findRecurringSnapshotSchedule(volumeId, policyId); - } - @Override public List listSnapshotPolicies(long volumeId) { if( _volumeDao.findById(volumeId) == null){ diff --git a/server/src/com/cloud/storage/snapshot/SnapshotManager.java b/server/src/com/cloud/storage/snapshot/SnapshotManager.java index c6ee7e908b4..dae6d9e3b24 100644 --- a/server/src/com/cloud/storage/snapshot/SnapshotManager.java +++ b/server/src/com/cloud/storage/snapshot/SnapshotManager.java @@ -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 findRecurringSnapshotSchedule(Long volumeId, Long policyId); + public List findRecurringSnapshotSchedule(ListRecurringSnapshotScheduleCmd cmd) throws InvalidParameterValueException, PermissionDeniedException; SnapshotPolicyVO getPolicyForVolumeByInterval(long volumeId, short interval); diff --git a/server/src/com/cloud/storage/snapshot/SnapshotManagerImpl.java b/server/src/com/cloud/storage/snapshot/SnapshotManagerImpl.java index 3c20d8fcba0..887e2c2df7b 100644 --- a/server/src/com/cloud/storage/snapshot/SnapshotManagerImpl.java +++ b/server/src/com/cloud/storage/snapshot/SnapshotManagerImpl.java @@ -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 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 findRecurringSnapshotSchedule(Long volumeId, Long policyId) { + public List 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 snapshotSchedules = new ArrayList(); 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 params) - throws ConfigurationException { + public boolean configure(String name, Map params) throws ConfigurationException { _name = name; ComponentLocator locator = ComponentLocator.getCurrentLocator();