Skip to content

Commit fd5bea8

Browse files
New feature: Add support to destroy/recover volumes (#3688)
* server: fix resource count of primary storage if some volumes are Expunged but not removed Steps to reproduce the issue (1) create a vm and stop it. check resource count of primary storage (2) download volume. resource count of primary storage is not changed. (3) expunge the vm, the volume will be Expunged state as there is a volume snapshot on secondary storage. The resource count of primary storage decreased. (4) update resource count of the account (or domain), the resource count of primary storage is reset to the value in step (2). * New feature: Add support to destroy/recover volumes * Add integration test for volume destroy/recover * marvin: check resource count of more types * messages translate to JP * Update messages for CN * translate message for NL * fix two issues per Daan's comments Co-authored-by: Andrija Panic <45762285+andrijapanicsb@users.noreply.github.com>
1 parent d8906d3 commit fd5bea8

File tree

40 files changed

+1221
-63
lines changed

40 files changed

+1221
-63
lines changed

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,8 @@ public class EventTypes {
239239
public static final String EVENT_VOLUME_DETAIL_ADD = "VOLUME.DETAIL.ADD";
240240
public static final String EVENT_VOLUME_DETAIL_REMOVE = "VOLUME.DETAIL.REMOVE";
241241
public static final String EVENT_VOLUME_UPDATE = "VOLUME.UPDATE";
242+
public static final String EVENT_VOLUME_DESTROY = "VOLUME.DESTROY";
243+
public static final String EVENT_VOLUME_RECOVER = "VOLUME.RECOVER";
242244

243245
// Domains
244246
public static final String EVENT_DOMAIN_CREATE = "DOMAIN.CREATE";
@@ -706,6 +708,8 @@ public class EventTypes {
706708
entityEventDetails.put(EVENT_VOLUME_UPLOAD, Volume.class);
707709
entityEventDetails.put(EVENT_VOLUME_MIGRATE, Volume.class);
708710
entityEventDetails.put(EVENT_VOLUME_RESIZE, Volume.class);
711+
entityEventDetails.put(EVENT_VOLUME_DESTROY, Volume.class);
712+
entityEventDetails.put(EVENT_VOLUME_RECOVER, Volume.class);
709713

710714
// Domains
711715
entityEventDetails.put(EVENT_DOMAIN_CREATE, Domain.class);

api/src/main/java/com/cloud/storage/Volume.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,7 @@ public String getDescription() {
124124
s_fsm.addTransition(new StateMachine2.Transition<State, Event>(Ready, Event.AttachRequested, Attaching, null));
125125
s_fsm.addTransition(new StateMachine2.Transition<State, Event>(Attaching, Event.OperationSucceeded, Ready, null));
126126
s_fsm.addTransition(new StateMachine2.Transition<State, Event>(Attaching, Event.OperationFailed, Ready, null));
127+
s_fsm.addTransition(new StateMachine2.Transition<State, Event>(Destroy, Event.RecoverRequested, Ready, null));
127128
}
128129
}
129130

@@ -143,6 +144,7 @@ enum Event {
143144
SnapshotRequested,
144145
RevertSnapshotRequested,
145146
DestroyRequested,
147+
RecoverRequested,
146148
ExpungingRequested,
147149
ResizeRequested,
148150
AttachRequested,

api/src/main/java/com/cloud/storage/VolumeApiService.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,4 +148,8 @@ Snapshot takeSnapshot(Long volumeId, Long policyId, Long snapshotId, Account acc
148148
* </table>
149149
*/
150150
boolean doesTargetStorageSupportDiskOffering(StoragePool destPool, String diskOfferingTags);
151-
}
151+
152+
Volume destroyVolume(long volumeId, Account caller, boolean expunge, boolean forceExpunge);
153+
154+
Volume recoverVolume(long volumeId);
155+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
// Licensed to the Apache Software Foundation (ASF) under one
2+
// or more contributor license agreements. See the NOTICE file
3+
// distributed with this work for additional information
4+
// regarding copyright ownership. The ASF licenses this file
5+
// to you under the Apache License, Version 2.0 (the
6+
// "License"); you may not use this file except in compliance
7+
// with the License. You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing,
12+
// software distributed under the License is distributed on an
13+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
// KIND, either express or implied. See the License for the
15+
// specific language governing permissions and limitations
16+
// under the License.
17+
package org.apache.cloudstack.api.command.admin.volume;
18+
19+
import org.apache.log4j.Logger;
20+
21+
import org.apache.cloudstack.acl.RoleType;
22+
import org.apache.cloudstack.api.APICommand;
23+
import org.apache.cloudstack.api.ApiErrorCode;
24+
import org.apache.cloudstack.api.ResponseObject.ResponseView;
25+
import org.apache.cloudstack.api.ServerApiException;
26+
import org.apache.cloudstack.api.command.admin.AdminCmd;
27+
import org.apache.cloudstack.api.command.user.volume.DestroyVolumeCmd;
28+
import org.apache.cloudstack.api.response.VolumeResponse;
29+
import org.apache.cloudstack.context.CallContext;
30+
31+
import com.cloud.storage.Volume;
32+
33+
@APICommand(name = "destroyVolume", description = "Destroys a Volume.", responseObject = VolumeResponse.class, responseView = ResponseView.Full, entityType = {Volume.class},
34+
since = "4.14.0",
35+
authorized = {RoleType.Admin},
36+
requestHasSensitiveInfo = false,
37+
responseHasSensitiveInfo = true)
38+
public class DestroyVolumeCmdByAdmin extends DestroyVolumeCmd implements AdminCmd {
39+
40+
public static final Logger s_logger = Logger.getLogger(DestroyVolumeCmdByAdmin.class.getName());
41+
42+
@Override
43+
public void execute() {
44+
CallContext.current().setEventDetails("Volume Id: " + getId());
45+
Volume result = _volumeService.destroyVolume(getId(), CallContext.current().getCallingAccount(), getExpunge(), false);
46+
if (result != null) {
47+
VolumeResponse response = _responseGenerator.createVolumeResponse(ResponseView.Full, result);
48+
response.setResponseName(getCommandName());
49+
setResponseObject(response);
50+
} else {
51+
throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, "Failed to destroy volume");
52+
}
53+
}
54+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
// Licensed to the Apache Software Foundation (ASF) under one
2+
// or more contributor license agreements. See the NOTICE file
3+
// distributed with this work for additional information
4+
// regarding copyright ownership. The ASF licenses this file
5+
// to you under the Apache License, Version 2.0 (the
6+
// "License"); you may not use this file except in compliance
7+
// with the License. You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing,
12+
// software distributed under the License is distributed on an
13+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
// KIND, either express or implied. See the License for the
15+
// specific language governing permissions and limitations
16+
// under the License.
17+
package org.apache.cloudstack.api.command.admin.volume;
18+
19+
import org.apache.log4j.Logger;
20+
21+
import org.apache.cloudstack.acl.RoleType;
22+
import org.apache.cloudstack.api.APICommand;
23+
import org.apache.cloudstack.api.ApiErrorCode;
24+
import org.apache.cloudstack.api.ResponseObject.ResponseView;
25+
import org.apache.cloudstack.api.ServerApiException;
26+
import org.apache.cloudstack.api.command.admin.AdminCmd;
27+
import org.apache.cloudstack.api.command.user.volume.RecoverVolumeCmd;
28+
import org.apache.cloudstack.api.response.VolumeResponse;
29+
import org.apache.cloudstack.context.CallContext;
30+
31+
import com.cloud.storage.Volume;
32+
33+
@APICommand(name = "recoverVolume", description = "Recovers a Destroy volume.", responseObject = VolumeResponse.class, responseView = ResponseView.Full, entityType = {Volume.class},
34+
since = "4.14.0",
35+
authorized = {RoleType.Admin},
36+
requestHasSensitiveInfo = false,
37+
responseHasSensitiveInfo = true)
38+
public class RecoverVolumeCmdByAdmin extends RecoverVolumeCmd implements AdminCmd {
39+
public static final Logger s_logger = Logger.getLogger(RecoverVolumeCmdByAdmin.class.getName());
40+
41+
@Override
42+
public void execute() {
43+
CallContext.current().setEventDetails("Volume Id: " + getId());
44+
Volume result = _volumeService.recoverVolume(getId());
45+
if (result != null) {
46+
VolumeResponse response = _responseGenerator.createVolumeResponse(ResponseView.Full, result);
47+
response.setResponseName(getCommandName());
48+
setResponseObject(response);
49+
} else {
50+
throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, "Failed to recover volume");
51+
}
52+
}
53+
}

api/src/main/java/org/apache/cloudstack/api/command/user/config/ListCapabilitiesCmd.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ public void execute() {
5959
response.setKVMSnapshotEnabled((Boolean)capabilities.get("KVMSnapshotEnabled"));
6060
response.setAllowUserViewDestroyedVM((Boolean)capabilities.get("allowUserViewDestroyedVM"));
6161
response.setAllowUserExpungeRecoverVM((Boolean)capabilities.get("allowUserExpungeRecoverVM"));
62+
response.setAllowUserExpungeRecoverVolume((Boolean)capabilities.get("allowUserExpungeRecoverVolume"));
6263
response.setAllowUserViewAllDomainAccounts((Boolean)capabilities.get("allowUserViewAllDomainAccounts"));
6364
if (capabilities.containsKey("apiLimitInterval")) {
6465
response.setApiLimitInterval((Integer)capabilities.get("apiLimitInterval"));

api/src/main/java/org/apache/cloudstack/api/command/user/volume/DeleteVolumeCmd.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,8 +82,8 @@ public long getEntityOwnerId() {
8282
@Override
8383
public void execute() throws ConcurrentOperationException {
8484
CallContext.current().setEventDetails("Volume Id: " + this._uuidMgr.getUuid(Volume.class, getId()));
85-
boolean result = _volumeService.deleteVolume(id, CallContext.current().getCallingAccount());
86-
if (result) {
85+
Volume result = _volumeService.destroyVolume(id, CallContext.current().getCallingAccount(), true, false);
86+
if (result != null) {
8787
SuccessResponse response = new SuccessResponse(getCommandName());
8888
setResponseObject(response);
8989
} else {
Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
// Licensed to the Apache Software Foundation (ASF) under one
2+
// or more contributor license agreements. See the NOTICE file
3+
// distributed with this work for additional information
4+
// regarding copyright ownership. The ASF licenses this file
5+
// to you under the Apache License, Version 2.0 (the
6+
// "License"); you may not use this file except in compliance
7+
// with the License. You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing,
12+
// software distributed under the License is distributed on an
13+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
// KIND, either express or implied. See the License for the
15+
// specific language governing permissions and limitations
16+
// under the License.
17+
package org.apache.cloudstack.api.command.user.volume;
18+
19+
import org.apache.log4j.Logger;
20+
21+
import org.apache.cloudstack.acl.RoleType;
22+
import org.apache.cloudstack.acl.SecurityChecker.AccessType;
23+
import org.apache.cloudstack.api.ACL;
24+
import org.apache.cloudstack.api.APICommand;
25+
import org.apache.cloudstack.api.ApiCommandJobType;
26+
import org.apache.cloudstack.api.ApiConstants;
27+
import org.apache.cloudstack.api.ApiErrorCode;
28+
import org.apache.cloudstack.api.BaseAsyncCmd;
29+
import org.apache.cloudstack.api.Parameter;
30+
import org.apache.cloudstack.api.ResponseObject.ResponseView;
31+
import org.apache.cloudstack.api.ServerApiException;
32+
import org.apache.cloudstack.api.response.VolumeResponse;
33+
import org.apache.cloudstack.context.CallContext;
34+
35+
import com.cloud.event.EventTypes;
36+
import com.cloud.storage.Volume;
37+
import com.cloud.user.Account;
38+
39+
@APICommand(name = "destroyVolume", description = "Destroys a Volume.", responseObject = VolumeResponse.class, responseView = ResponseView.Restricted, entityType = {Volume.class},
40+
since = "4.14.0",
41+
authorized = {RoleType.Admin, RoleType.ResourceAdmin, RoleType.DomainAdmin, RoleType.User},
42+
requestHasSensitiveInfo = false,
43+
responseHasSensitiveInfo = true)
44+
public class DestroyVolumeCmd extends BaseAsyncCmd {
45+
public static final Logger s_logger = Logger.getLogger(DestroyVolumeCmd.class.getName());
46+
47+
private static final String s_name = "destroyvolumeresponse";
48+
49+
/////////////////////////////////////////////////////
50+
//////////////// API parameters /////////////////////
51+
/////////////////////////////////////////////////////
52+
53+
@ACL(accessType = AccessType.OperateEntry)
54+
@Parameter(name=ApiConstants.ID, type=CommandType.UUID, entityType=VolumeResponse.class,
55+
required=true, description="The ID of the volume")
56+
private Long id;
57+
58+
@Parameter(name = ApiConstants.EXPUNGE,
59+
type = CommandType.BOOLEAN,
60+
description = "If true is passed, the volume is expunged immediately. False by default.",
61+
since = "4.6.0")
62+
private Boolean expunge;
63+
64+
/////////////////////////////////////////////////////
65+
/////////////////// Accessors ///////////////////////
66+
/////////////////////////////////////////////////////
67+
68+
public Long getId() {
69+
return id;
70+
}
71+
72+
public boolean getExpunge() {
73+
if (expunge == null) {
74+
return false;
75+
}
76+
return expunge;
77+
}
78+
79+
/////////////////////////////////////////////////////
80+
/////////////// API Implementation///////////////////
81+
/////////////////////////////////////////////////////
82+
83+
@Override
84+
public String getCommandName() {
85+
return s_name;
86+
}
87+
88+
@Override
89+
public long getEntityOwnerId() {
90+
Volume volume = _entityMgr.findById(Volume.class, getId());
91+
if (volume != null) {
92+
return volume.getAccountId();
93+
}
94+
95+
return Account.ACCOUNT_ID_SYSTEM; // no account info given, parent this command to SYSTEM so ERROR events are tracked
96+
}
97+
98+
@Override
99+
public String getEventType() {
100+
return EventTypes.EVENT_VOLUME_DESTROY;
101+
}
102+
103+
@Override
104+
public String getEventDescription() {
105+
return "destroying volume: " + getId();
106+
}
107+
108+
@Override
109+
public ApiCommandJobType getInstanceType() {
110+
return ApiCommandJobType.Volume;
111+
}
112+
113+
@Override
114+
public Long getInstanceId() {
115+
return getId();
116+
}
117+
118+
@Override
119+
public void execute() {
120+
CallContext.current().setEventDetails("Volume Id: " + getId());
121+
Volume result = _volumeService.destroyVolume(getId(), CallContext.current().getCallingAccount(), getExpunge(), false);
122+
if (result != null) {
123+
VolumeResponse response = _responseGenerator.createVolumeResponse(ResponseView.Restricted, result);
124+
response.setResponseName(getCommandName());
125+
setResponseObject(response);
126+
} else {
127+
throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, "Failed to destroy volume");
128+
}
129+
}
130+
}

api/src/main/java/org/apache/cloudstack/api/command/user/volume/ListVolumesCmd.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import org.apache.cloudstack.api.ApiConstants;
2525
import org.apache.cloudstack.api.BaseListTaggedResourcesCmd;
2626
import org.apache.cloudstack.api.Parameter;
27+
import org.apache.cloudstack.api.BaseCmd.CommandType;
2728
import org.apache.cloudstack.api.ResponseObject.ResponseView;
2829
import org.apache.cloudstack.api.command.user.UserCmd;
2930
import org.apache.cloudstack.api.response.ClusterResponse;
@@ -88,6 +89,9 @@ public class ListVolumesCmd extends BaseListTaggedResourcesCmd implements UserCm
8889
RoleType.Admin})
8990
private Boolean display;
9091

92+
@Parameter(name = ApiConstants.STATE, type = CommandType.STRING, description = "state of the volume. Possible values are: Ready, Allocated, Destroy, Expunging, Expunged.")
93+
private String state;
94+
9195
/////////////////////////////////////////////////////
9296
/////////////////// Accessors ///////////////////////
9397
/////////////////////////////////////////////////////
@@ -139,6 +143,10 @@ public Boolean getDisplay() {
139143
}
140144
return super.getDisplay();
141145
}
146+
147+
public String getState() {
148+
return state;
149+
}
142150
/////////////////////////////////////////////////////
143151
/////////////// API Implementation///////////////////
144152
/////////////////////////////////////////////////////

0 commit comments

Comments
 (0)