Skip to content

Commit 80ef915

Browse files
committed
CLOUDSTACK-7000: ListSnapshotPolicy should list by policyid. Introduce updateSnapshotPolicy command
(cherry picked from commit fce2143)
1 parent 06d3043 commit 80ef915

10 files changed

Lines changed: 203 additions & 6 deletions

File tree

api/src/com/cloud/event/EventTypes.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import com.cloud.network.vpc.NetworkACLItem;
3030
import com.cloud.network.Site2SiteVpnConnection;
3131
import com.cloud.server.ResourceTag;
32+
import com.cloud.storage.snapshot.SnapshotPolicy;
3233
import com.cloud.vm.ConsoleProxy;
3334
import com.cloud.vm.SecondaryStorageVm;
3435
import org.apache.cloudstack.config.Configuration;
@@ -630,9 +631,9 @@ public class EventTypes {
630631
// Snapshots
631632
entityEventDetails.put(EVENT_SNAPSHOT_CREATE, Snapshot.class);
632633
entityEventDetails.put(EVENT_SNAPSHOT_DELETE, Snapshot.class);
633-
entityEventDetails.put(EVENT_SNAPSHOT_POLICY_CREATE, Snapshot.class);
634-
entityEventDetails.put(EVENT_SNAPSHOT_POLICY_UPDATE, Snapshot.class);
635-
entityEventDetails.put(EVENT_SNAPSHOT_POLICY_DELETE, Snapshot.class);
634+
entityEventDetails.put(EVENT_SNAPSHOT_POLICY_CREATE, SnapshotPolicy.class);
635+
entityEventDetails.put(EVENT_SNAPSHOT_POLICY_UPDATE, SnapshotPolicy.class);
636+
entityEventDetails.put(EVENT_SNAPSHOT_POLICY_DELETE, SnapshotPolicy.class);
636637

637638
// ISO
638639
entityEventDetails.put(EVENT_ISO_CREATE, "Iso");

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import com.cloud.storage.Volume;
3030
import com.cloud.user.Account;
3131
import com.cloud.utils.Pair;
32+
import org.apache.cloudstack.api.command.user.snapshot.UpdateSnapshotPolicyCmd;
3233

3334
public interface SnapshotApiService {
3435

@@ -106,4 +107,6 @@ public interface SnapshotApiService {
106107
Long getHostIdForSnapshotOperation(Volume vol);
107108

108109
boolean revertSnapshot(Long snapshotId);
110+
111+
SnapshotPolicy updateSnapshotPolicy(UpdateSnapshotPolicyCmd updateSnapshotPolicyCmd);
109112
}

api/src/org/apache/cloudstack/api/command/user/snapshot/ListSnapshotPoliciesCmd.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,12 @@ public class ListSnapshotPoliciesCmd extends BaseListCmd {
4444
//////////////// API parameters /////////////////////
4545
/////////////////////////////////////////////////////
4646

47-
@Parameter(name = ApiConstants.VOLUME_ID, type = CommandType.UUID, entityType = VolumeResponse.class, required = true, description = "the ID of the disk volume")
47+
@Parameter(name = ApiConstants.VOLUME_ID, type = CommandType.UUID, entityType = VolumeResponse.class, description = "the ID of the disk volume")
4848
private Long volumeId;
4949

50+
@Parameter(name = ApiConstants.ID, type = CommandType.UUID, entityType = SnapshotPolicyResponse.class, description = "the ID of the snapshot policy")
51+
private Long id;
52+
5053
@Parameter(name = ApiConstants.FOR_DISPLAY, type = CommandType.BOOLEAN, description = "list resources by display flag; only ROOT admin is eligible to pass this parameter", since = "4.4", authorized = {RoleType.Admin})
5154
private Boolean display;
5255

@@ -66,6 +69,9 @@ public boolean isDisplay() {
6669
return true;
6770
}
6871

72+
public Long getId() {
73+
return id;
74+
}
6975
/////////////////////////////////////////////////////
7076
/////////////// API Implementation///////////////////
7177
/////////////////////////////////////////////////////
Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
package org.apache.cloudstack.api.command.user.snapshot;
2+
3+
// Licensed to the Apache Software Foundation (ASF) under one
4+
// or more contributor license agreements. See the NOTICE file
5+
// distributed with this work for additional information
6+
// regarding copyright ownership. The ASF licenses this file
7+
// to you under the Apache License, Version 2.0 (the
8+
// "License"); you may not use this file except in compliance
9+
// with the License. You may obtain a copy of the License at
10+
//
11+
// http://www.apache.org/licenses/LICENSE-2.0
12+
//
13+
// Unless required by applicable law or agreed to in writing,
14+
// software distributed under the License is distributed on an
15+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16+
// KIND, either express or implied. See the License for the
17+
// specific language governing permissions and limitations
18+
// under the License.
19+
20+
import com.cloud.event.EventTypes;
21+
import com.cloud.exception.InvalidParameterValueException;
22+
import com.cloud.storage.Volume;
23+
import com.cloud.storage.snapshot.SnapshotPolicy;
24+
import org.apache.cloudstack.acl.RoleType;
25+
import org.apache.cloudstack.acl.SecurityChecker;
26+
import org.apache.cloudstack.api.ACL;
27+
import org.apache.cloudstack.api.APICommand;
28+
import org.apache.cloudstack.api.ApiConstants;
29+
import org.apache.cloudstack.api.ApiErrorCode;
30+
import org.apache.cloudstack.api.BaseAsyncCustomIdCmd;
31+
import org.apache.cloudstack.api.Parameter;
32+
import org.apache.cloudstack.api.ResponseObject;
33+
import org.apache.cloudstack.api.ServerApiException;
34+
import org.apache.cloudstack.api.response.SnapshotPolicyResponse;
35+
import org.apache.cloudstack.context.CallContext;
36+
import org.apache.log4j.Logger;
37+
38+
39+
@APICommand(name = "updateSnapshotPolicy", description = "Updates the snapshot policy.", responseObject = SnapshotPolicyResponse.class, responseView = ResponseObject.ResponseView.Restricted, entityType = {Volume.class},
40+
requestHasSensitiveInfo = false, responseHasSensitiveInfo = false)
41+
public class UpdateSnapshotPolicyCmd extends BaseAsyncCustomIdCmd {
42+
public static final Logger s_logger = Logger.getLogger(UpdateSnapshotPolicyCmd.class.getName());
43+
private static final String s_name = "updatesnapshotpolicyresponse";
44+
45+
/////////////////////////////////////////////////////
46+
//////////////// API parameters /////////////////////
47+
/////////////////////////////////////////////////////
48+
49+
@ACL(accessType = SecurityChecker.AccessType.OperateEntry)
50+
@Parameter(name= ApiConstants.ID, type=CommandType.UUID, entityType=SnapshotPolicyResponse.class, description="the ID of the snapshot policy")
51+
private Long id;
52+
53+
@Parameter(name = ApiConstants.FOR_DISPLAY,
54+
type = CommandType.BOOLEAN,
55+
description = "an optional field, whether to the display the snapshot policy to the end user or not.",
56+
since = "4.4",
57+
authorized = {RoleType.Admin})
58+
private Boolean display;
59+
60+
/////////////////////////////////////////////////////
61+
/////////////////// Accessors ///////////////////////
62+
/////////////////////////////////////////////////////
63+
64+
public Long getId() {
65+
return id;
66+
}
67+
68+
public Boolean getDisplay() {
69+
return display;
70+
}
71+
72+
/////////////////////////////////////////////////////
73+
/////////////// API Implementation///////////////////
74+
/////////////////////////////////////////////////////
75+
76+
@Override
77+
public String getCommandName() {
78+
return s_name;
79+
}
80+
81+
@Override
82+
public Long getInstanceId() {
83+
return getId();
84+
}
85+
86+
@Override
87+
public long getEntityOwnerId() {
88+
89+
SnapshotPolicy policy = _entityMgr.findById(SnapshotPolicy.class, getId());
90+
if (policy == null) {
91+
throw new InvalidParameterValueException("Invalid snapshot policy id was provided");
92+
}
93+
Volume volume = _responseGenerator.findVolumeById(policy.getVolumeId());
94+
if (volume == null) {
95+
throw new InvalidParameterValueException("Snapshot policy's volume id doesnt exist");
96+
}else{
97+
return volume.getAccountId();
98+
}
99+
}
100+
101+
@Override
102+
public String getEventType() {
103+
return EventTypes.EVENT_SNAPSHOT_POLICY_UPDATE;
104+
}
105+
106+
@Override
107+
public String getEventDescription() {
108+
StringBuilder desc = new StringBuilder("Updating snapshot policy: ");
109+
desc.append(getId());
110+
return desc.toString();
111+
}
112+
113+
@Override
114+
public void execute() {
115+
CallContext.current().setEventDetails("SnapshotPolicy Id: " + getId());
116+
SnapshotPolicy result = _snapshotService.updateSnapshotPolicy(this);
117+
if (result != null) {
118+
SnapshotPolicyResponse response = _responseGenerator.createSnapshotPolicyResponse(result);
119+
response.setResponseName(getCommandName());
120+
this.setResponseObject(response);
121+
} else {
122+
throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, "Failed to update snapshot policy");
123+
}
124+
}
125+
126+
@Override
127+
public void checkUuid() {
128+
if (getCustomId() != null) {
129+
_uuidMgr.checkUuid(getCustomId(), SnapshotPolicy.class);
130+
}
131+
}
132+
133+
}

client/tomcatconf/commands.properties.in

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ createSnapshot=15
7979
listSnapshots=15
8080
deleteSnapshot=15
8181
createSnapshotPolicy=15
82+
updateSnapshotPolicy=15
8283
deleteSnapshotPolicies=15
8384
listSnapshotPolicies=15
8485
revertSnapshot=15

engine/schema/src/com/cloud/storage/dao/SnapshotPolicyDao.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,4 +41,6 @@ public interface SnapshotPolicyDao extends GenericDao<SnapshotPolicyVO, Long> {
4141
List<SnapshotPolicyVO> listActivePolicies();
4242

4343
SnapshotPolicyVO findOneByVolume(long volumeId);
44+
45+
Pair<List<SnapshotPolicyVO>, Integer> listAndCountById(long id, boolean display, Filter filter);
4446
}

engine/schema/src/com/cloud/storage/dao/SnapshotPolicyDaoImpl.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ public class SnapshotPolicyDaoImpl extends GenericDaoBase<SnapshotPolicyVO, Long
3636
private final SearchBuilder<SnapshotPolicyVO> VolumeIdSearch;
3737
private final SearchBuilder<SnapshotPolicyVO> VolumeIdIntervalSearch;
3838
private final SearchBuilder<SnapshotPolicyVO> ActivePolicySearch;
39+
private final SearchBuilder<SnapshotPolicyVO> SnapshotPolicySearch;
3940

4041
@Override
4142
public SnapshotPolicyVO findOneByVolumeInterval(long volumeId, IntervalType intvType) {
@@ -79,6 +80,14 @@ public Pair<List<SnapshotPolicyVO>, Integer> listAndCountByVolumeId(long volumeI
7980
return searchAndCount(sc, filter);
8081
}
8182

83+
@Override
84+
public Pair<List<SnapshotPolicyVO>, Integer> listAndCountById(long id, boolean display, Filter filter){
85+
SearchCriteria<SnapshotPolicyVO> sc = SnapshotPolicySearch.create();
86+
sc.setParameters("id", id);
87+
sc.setParameters("display", display);
88+
return searchAndCount(sc, filter);
89+
}
90+
8291
protected SnapshotPolicyDaoImpl() {
8392
VolumeIdSearch = createSearchBuilder();
8493
VolumeIdSearch.and("volumeId", VolumeIdSearch.entity().getVolumeId(), SearchCriteria.Op.EQ);
@@ -94,6 +103,11 @@ protected SnapshotPolicyDaoImpl() {
94103
ActivePolicySearch = createSearchBuilder();
95104
ActivePolicySearch.and("active", ActivePolicySearch.entity().isActive(), SearchCriteria.Op.EQ);
96105
ActivePolicySearch.done();
106+
107+
SnapshotPolicySearch = createSearchBuilder();
108+
SnapshotPolicySearch.and("id", SnapshotPolicySearch.entity().getId(), SearchCriteria.Op.EQ);
109+
SnapshotPolicySearch.and("display", SnapshotPolicySearch.entity().isDisplay(), SearchCriteria.Op.EQ);
110+
SnapshotPolicySearch.done();
97111
}
98112

99113
@Override

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1251,6 +1251,7 @@ public VolumeVO findVolumeById(Long volumeId) {
12511251
return ApiDBUtils.findVolumeById(volumeId);
12521252
}
12531253

1254+
12541255
@Override
12551256
public Account findAccountByNameDomain(String accountName, Long domainId) {
12561257
return ApiDBUtils.findAccountByNameDomain(accountName, domainId);

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
import javax.inject.Inject;
3838
import javax.naming.ConfigurationException;
3939

40+
import org.apache.cloudstack.api.command.user.snapshot.UpdateSnapshotPolicyCmd;
4041
import org.apache.commons.codec.binary.Base64;
4142
import org.apache.log4j.Logger;
4243

@@ -2771,8 +2772,9 @@ public List<Class<?>> getCommands() {
27712772
cmdList.add(RevokeSecurityGroupEgressCmd.class);
27722773
cmdList.add(RevokeSecurityGroupIngressCmd.class);
27732774
cmdList.add(CreateSnapshotCmd.class);
2774-
cmdList.add(CreateSnapshotPolicyCmd.class);
27752775
cmdList.add(DeleteSnapshotCmd.class);
2776+
cmdList.add(CreateSnapshotPolicyCmd.class);
2777+
cmdList.add(UpdateSnapshotPolicyCmd.class);
27762778
cmdList.add(DeleteSnapshotPoliciesCmd.class);
27772779
cmdList.add(ListSnapshotPoliciesCmd.class);
27782780
cmdList.add(ListSnapshotsCmd.class);

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

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import javax.inject.Inject;
2727
import javax.naming.ConfigurationException;
2828

29+
import org.apache.cloudstack.api.command.user.snapshot.UpdateSnapshotPolicyCmd;
2930
import org.apache.log4j.Logger;
3031
import org.springframework.stereotype.Component;
3132

@@ -271,6 +272,27 @@ public boolean revertSnapshot(Long snapshotId) {
271272
return snapshotStrategy.revertSnapshot(snapshotId);
272273
}
273274

275+
@Override
276+
@ActionEvent(eventType = EventTypes.EVENT_SNAPSHOT_POLICY_UPDATE, eventDescription = "updating snapshot policy", async = true)
277+
public SnapshotPolicy updateSnapshotPolicy(UpdateSnapshotPolicyCmd cmd) {
278+
279+
Long id = cmd.getId();
280+
String customUUID = cmd.getCustomId();
281+
Boolean display = cmd.getDisplay();
282+
283+
SnapshotPolicyVO policyVO = _snapshotPolicyDao.findById(id);
284+
if(display != null)
285+
policyVO.setDisplay(display);
286+
287+
if(customUUID != null)
288+
policyVO.setUuid(customUUID);
289+
290+
_snapshotPolicyDao.update(id, policyVO);
291+
292+
return policyVO;
293+
294+
}
295+
274296
@Override
275297
@DB
276298
@ActionEvent(eventType = EventTypes.EVENT_SNAPSHOT_CREATE, eventDescription = "creating snapshot", async = true)
@@ -723,12 +745,24 @@ protected boolean deletePolicy(long userId, Long policyId) {
723745
public Pair<List<? extends SnapshotPolicy>, Integer> listPoliciesforVolume(ListSnapshotPoliciesCmd cmd) {
724746
Long volumeId = cmd.getVolumeId();
725747
boolean display = cmd.isDisplay();
748+
Long id = cmd.getId();
749+
Pair<List<SnapshotPolicyVO>, Integer> result = null;
750+
// TODO - Have a better way of doing this.
751+
if(id != null){
752+
result = _snapshotPolicyDao.listAndCountById(id, display, null);
753+
if(result != null && result.first() != null && !result.first().isEmpty()){
754+
SnapshotPolicyVO snapshotPolicy = result.first().get(0);
755+
volumeId = snapshotPolicy.getId();
756+
}
757+
}
726758
VolumeVO volume = _volsDao.findById(volumeId);
727759
if (volume == null) {
728760
throw new InvalidParameterValueException("Unable to find a volume with id " + volumeId);
729761
}
730762
_accountMgr.checkAccess(CallContext.current().getCallingAccount(), null, true, volume);
731-
Pair<List<SnapshotPolicyVO>, Integer> result = _snapshotPolicyDao.listAndCountByVolumeId(volumeId, display);
763+
if(result != null)
764+
return new Pair<List<? extends SnapshotPolicy>, Integer>(result.first(), result.second());
765+
result = _snapshotPolicyDao.listAndCountByVolumeId(volumeId, display);
732766
return new Pair<List<? extends SnapshotPolicy>, Integer>(result.first(), result.second());
733767
}
734768

0 commit comments

Comments
 (0)