Skip to content

Commit f8adbcd

Browse files
vishesh92sureshanaparti
authored andcommitted
Feature: Allow adding delete protection for VMs & volumes (apache#9633)
Co-authored-by: Suresh Kumar Anaparti <[email protected]>
1 parent 1fbfefb commit f8adbcd

File tree

30 files changed

+269
-28
lines changed

30 files changed

+269
-28
lines changed

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

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -271,11 +271,13 @@ enum Event {
271271

272272
void setExternalUuid(String externalUuid);
273273

274-
public Long getPassphraseId();
274+
Long getPassphraseId();
275275

276-
public void setPassphraseId(Long id);
276+
void setPassphraseId(Long id);
277277

278-
public String getEncryptFormat();
278+
String getEncryptFormat();
279279

280-
public void setEncryptFormat(String encryptFormat);
280+
void setEncryptFormat(String encryptFormat);
281+
282+
boolean isDeleteProtection();
281283
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ Snapshot takeSnapshot(Long volumeId, Long policyId, Long snapshotId, Account acc
118118

119119
Snapshot allocSnapshot(Long volumeId, Long policyId, String snapshotName, Snapshot.LocationType locationType, List<Long> zoneIds) throws ResourceAllocationException;
120120

121-
Volume updateVolume(long volumeId, String path, String state, Long storageId, Boolean displayVolume, String customId, long owner, String chainInfo, String name, String type);
121+
Volume updateVolume(long volumeId, String path, String state, Long storageId, Boolean displayVolume, Boolean deleteProtection, String customId, long owner, String chainInfo, String name, String type);
122122

123123
Volume attachVolumeToVM(Long vmId, Long volumeId, Long deviceId);
124124

api/src/main/java/org/apache/cloudstack/api/ApiConstants.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,7 @@ public class ApiConstants {
138138
public static final String DATACENTER_NAME = "datacentername";
139139
public static final String DATADISK_OFFERING_LIST = "datadiskofferinglist";
140140
public static final String DEFAULT_VALUE = "defaultvalue";
141+
public static final String DELETE_PROTECTION = "deleteprotection";
141142
public static final String DESCRIPTION = "description";
142143
public static final String DESTINATION = "destination";
143144
public static final String DESTINATION_ZONE_ID = "destzoneid";

api/src/main/java/org/apache/cloudstack/api/command/user/vm/UpdateVMCmd.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,14 @@ public class UpdateVMCmd extends BaseCustomIdCmd implements SecurityGroupAction,
146146
@Parameter(name = ApiConstants.EXTRA_CONFIG, type = CommandType.STRING, since = "4.12", description = "an optional URL encoded string that can be passed to the virtual machine upon successful deployment", length = 5120)
147147
private String extraConfig;
148148

149+
@Parameter(name = ApiConstants.DELETE_PROTECTION,
150+
type = CommandType.BOOLEAN, since = "4.20.0",
151+
description = "Set delete protection for the virtual machine. If " +
152+
"true, the instance will be protected from deletion. " +
153+
"Note: If the instance is managed by another service like" +
154+
" autoscaling groups or CKS, delete protection will be ignored.")
155+
private Boolean deleteProtection;
156+
149157
/////////////////////////////////////////////////////
150158
/////////////////// Accessors ///////////////////////
151159
/////////////////////////////////////////////////////
@@ -215,6 +223,10 @@ public boolean isCleanupDetails(){
215223
return cleanupDetails == null ? false : cleanupDetails.booleanValue();
216224
}
217225

226+
public Boolean getDeleteProtection() {
227+
return deleteProtection;
228+
}
229+
218230
public Map<String, Map<Integer, String>> getDhcpOptionsMap() {
219231
Map<String, Map<Integer, String>> dhcpOptionsMap = new HashMap<>();
220232
if (dhcpOptionsNetworkList != null && !dhcpOptionsNetworkList.isEmpty()) {

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

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,14 @@ public class UpdateVolumeCmd extends BaseAsyncCustomIdCmd implements UserCmd {
8080
@Parameter(name = ApiConstants.NAME, type = CommandType.STRING, description = "new name of the volume", since = "4.16")
8181
private String name;
8282

83+
@Parameter(name = ApiConstants.DELETE_PROTECTION,
84+
type = CommandType.BOOLEAN, since = "4.20.0",
85+
description = "Set delete protection for the volume. If true, The volume " +
86+
"will be protected from deletion. Note: If the volume is managed by " +
87+
"another service like autoscaling groups or CKS, delete protection will be " +
88+
"ignored.")
89+
private Boolean deleteProtection;
90+
8391
/////////////////////////////////////////////////////
8492
/////////////////// Accessors ///////////////////////
8593
/////////////////////////////////////////////////////
@@ -116,6 +124,10 @@ public String getName() {
116124
return name;
117125
}
118126

127+
public Boolean getDeleteProtection() {
128+
return deleteProtection;
129+
}
130+
119131
/////////////////////////////////////////////////////
120132
/////////////// API Implementation///////////////////
121133
/////////////////////////////////////////////////////
@@ -179,7 +191,7 @@ public String getEventDescription() {
179191
public void execute() {
180192
CallContext.current().setEventDetails("Volume Id: " + this._uuidMgr.getUuid(Volume.class, getId()));
181193
Volume result = _volumeService.updateVolume(getId(), getPath(), getState(), getStorageId(), getDisplayVolume(),
182-
getCustomId(), getEntityOwnerId(), getChainInfo(), getName(), getVolumeType());
194+
getDeleteProtection(), getCustomId(), getEntityOwnerId(), getChainInfo(), getName(), getVolumeType());
183195
if (result != null) {
184196
VolumeResponse response = _responseGenerator.createVolumeResponse(getResponseView(), result);
185197
response.setResponseName(getCommandName());

api/src/main/java/org/apache/cloudstack/api/response/UserVmResponse.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -324,6 +324,10 @@ public class UserVmResponse extends BaseResponseWithTagInformation implements Co
324324
@Param(description = "true if vm contains XS/VMWare tools inorder to support dynamic scaling of VM cpu/memory.")
325325
private Boolean isDynamicallyScalable;
326326

327+
@SerializedName(ApiConstants.DELETE_PROTECTION)
328+
@Param(description = "true if vm has delete protection.", since = "4.20.0")
329+
private boolean deleteProtection;
330+
327331
@SerializedName(ApiConstants.SERVICE_STATE)
328332
@Param(description = "State of the Service from LB rule")
329333
private String serviceState;
@@ -1019,6 +1023,14 @@ public void setDynamicallyScalable(Boolean dynamicallyScalable) {
10191023
isDynamicallyScalable = dynamicallyScalable;
10201024
}
10211025

1026+
public boolean isDeleteProtection() {
1027+
return deleteProtection;
1028+
}
1029+
1030+
public void setDeleteProtection(boolean deleteProtection) {
1031+
this.deleteProtection = deleteProtection;
1032+
}
1033+
10221034
public String getOsTypeId() {
10231035
return osTypeId;
10241036
}

api/src/main/java/org/apache/cloudstack/api/response/VolumeResponse.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,10 @@ public class VolumeResponse extends BaseResponseWithTagInformation implements Co
261261
@Param(description = "true if storage snapshot is supported for the volume, false otherwise", since = "4.16")
262262
private boolean supportsStorageSnapshot;
263263

264+
@SerializedName(ApiConstants.DELETE_PROTECTION)
265+
@Param(description = "true if volume has delete protection.", since = "4.20.0")
266+
private boolean deleteProtection;
267+
264268
@SerializedName(ApiConstants.PHYSICAL_SIZE)
265269
@Param(description = "the bytes actually consumed on disk")
266270
private Long physicalsize;
@@ -584,6 +588,14 @@ public boolean getSupportsStorageSnapshot() {
584588
return this.supportsStorageSnapshot;
585589
}
586590

591+
public boolean isDeleteProtection() {
592+
return deleteProtection;
593+
}
594+
595+
public void setDeleteProtection(boolean deleteProtection) {
596+
this.deleteProtection = deleteProtection;
597+
}
598+
587599
public String getIsoId() {
588600
return isoId;
589601
}

engine/schema/src/main/java/com/cloud/storage/VolumeVO.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,10 @@ public class VolumeVO implements Volume {
182182
@Column(name = "encrypt_format")
183183
private String encryptFormat;
184184

185+
@Column(name = "delete_protection")
186+
private boolean deleteProtection;
187+
188+
185189
// Real Constructor
186190
public VolumeVO(Type type, String name, long dcId, long domainId,
187191
long accountId, long diskOfferingId, Storage.ProvisioningType provisioningType, long size,
@@ -677,4 +681,13 @@ public void setExternalUuid(String externalUuid) {
677681
public String getEncryptFormat() { return encryptFormat; }
678682

679683
public void setEncryptFormat(String encryptFormat) { this.encryptFormat = encryptFormat; }
684+
685+
@Override
686+
public boolean isDeleteProtection() {
687+
return deleteProtection;
688+
}
689+
690+
public void setDeleteProtection(boolean deleteProtection) {
691+
this.deleteProtection = deleteProtection;
692+
}
680693
}

engine/schema/src/main/java/com/cloud/vm/VMInstanceVO.java

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -167,10 +167,8 @@ public class VMInstanceVO implements VirtualMachine, FiniteStateObject<State, Vi
167167
@Column(name = "dynamically_scalable")
168168
protected boolean dynamicallyScalable;
169169

170-
/*
171-
@Column(name="tags")
172-
protected String tags;
173-
*/
170+
@Column(name = "delete_protection")
171+
protected boolean deleteProtection;
174172

175173
@Transient
176174
Map<String, String> details;
@@ -545,6 +543,14 @@ public boolean isDynamicallyScalable() {
545543
return dynamicallyScalable;
546544
}
547545

546+
public boolean isDeleteProtection() {
547+
return deleteProtection;
548+
}
549+
550+
public void setDeleteProtection(boolean deleteProtection) {
551+
this.deleteProtection = deleteProtection;
552+
}
553+
548554
@Override
549555
public Class<?> getEntityType() {
550556
return VirtualMachine.class;

engine/schema/src/main/java/com/cloud/vm/dao/UserVmDao.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,11 @@ public interface UserVmDao extends GenericDao<UserVmVO, Long> {
5353
* @param hostName TODO
5454
* @param instanceName
5555
*/
56-
void updateVM(long id, String displayName, boolean enable, Long osTypeId, String userData, Long userDataId, String userDataDetails, boolean displayVm, boolean isDynamicallyScalable, String customId, String hostName, String instanceName);
56+
void updateVM(long id, String displayName, boolean enable, Long osTypeId,
57+
String userData, Long userDataId, String userDataDetails,
58+
boolean displayVm, boolean isDynamicallyScalable,
59+
boolean deleteProtection, String customId, String hostName,
60+
String instanceName);
5761

5862
List<UserVmVO> findDestroyedVms(Date date);
5963

0 commit comments

Comments
 (0)