Skip to content

Commit 1303a4f

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

File tree

30 files changed

+261
-28
lines changed

30 files changed

+261
-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: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,9 @@ Snapshot takeSnapshot(Long volumeId, Long policyId, Long snapshotId, Account acc
117117

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

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

122124
/**
123125
* Extracts the volume to a particular location.

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
@@ -77,6 +77,14 @@ public class UpdateVolumeCmd extends BaseAsyncCustomIdCmd implements UserCmd {
7777
@Parameter(name = ApiConstants.NAME, type = CommandType.STRING, description = "new name of the volume", since = "4.16")
7878
private String name;
7979

80+
@Parameter(name = ApiConstants.DELETE_PROTECTION,
81+
type = CommandType.BOOLEAN, since = "4.20.0",
82+
description = "Set delete protection for the volume. If true, The volume " +
83+
"will be protected from deletion. Note: If the volume is managed by " +
84+
"another service like autoscaling groups or CKS, delete protection will be " +
85+
"ignored.")
86+
private Boolean deleteProtection;
87+
8088
/////////////////////////////////////////////////////
8189
/////////////////// Accessors ///////////////////////
8290
/////////////////////////////////////////////////////
@@ -109,6 +117,10 @@ public String getName() {
109117
return name;
110118
}
111119

120+
public Boolean getDeleteProtection() {
121+
return deleteProtection;
122+
}
123+
112124
/////////////////////////////////////////////////////
113125
/////////////// API Implementation///////////////////
114126
/////////////////////////////////////////////////////
@@ -168,7 +180,7 @@ public String getEventDescription() {
168180
public void execute() {
169181
CallContext.current().setEventDetails("Volume Id: " + this._uuidMgr.getUuid(Volume.class, getId()));
170182
Volume result = _volumeService.updateVolume(getId(), getPath(), getState(), getStorageId(), getDisplayVolume(),
171-
getCustomId(), getEntityOwnerId(), getChainInfo(), getName());
183+
getDeleteProtection(), getCustomId(), getEntityOwnerId(), getChainInfo(), getName());
172184
if (result != null) {
173185
VolumeResponse response = _responseGenerator.createVolumeResponse(getResponseView(), result);
174186
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
@@ -320,6 +320,10 @@ public class UserVmResponse extends BaseResponseWithTagInformation implements Co
320320
@Param(description = "true if vm contains XS/VMWare tools inorder to support dynamic scaling of VM cpu/memory.")
321321
private Boolean isDynamicallyScalable;
322322

323+
@SerializedName(ApiConstants.DELETE_PROTECTION)
324+
@Param(description = "true if vm has delete protection.", since = "4.20.0")
325+
private boolean deleteProtection;
326+
323327
@SerializedName(ApiConstants.SERVICE_STATE)
324328
@Param(description = "State of the Service from LB rule")
325329
private String serviceState;
@@ -995,6 +999,14 @@ public void setDynamicallyScalable(Boolean dynamicallyScalable) {
995999
isDynamicallyScalable = dynamicallyScalable;
9961000
}
9971001

1002+
public boolean isDeleteProtection() {
1003+
return deleteProtection;
1004+
}
1005+
1006+
public void setDeleteProtection(boolean deleteProtection) {
1007+
this.deleteProtection = deleteProtection;
1008+
}
1009+
9981010
public String getOsTypeId() {
9991011
return osTypeId;
10001012
}

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: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,9 @@ 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+
185188

186189
// Real Constructor
187190
public VolumeVO(Type type, String name, long dcId, long domainId,
@@ -678,4 +681,13 @@ public void setExternalUuid(String externalUuid) {
678681
public String getEncryptFormat() { return encryptFormat; }
679682

680683
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+
}
681693
}

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;
@@ -542,6 +540,14 @@ public boolean isDynamicallyScalable() {
542540
return dynamicallyScalable;
543541
}
544542

543+
public boolean isDeleteProtection() {
544+
return deleteProtection;
545+
}
546+
547+
public void setDeleteProtection(boolean deleteProtection) {
548+
this.deleteProtection = deleteProtection;
549+
}
550+
545551
@Override
546552
public Class<?> getEntityType() {
547553
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)