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+ }
0 commit comments