Skip to content

Commit e1eeca8

Browse files
committed
list by isEncrypted
1 parent 9b18243 commit e1eeca8

5 files changed

Lines changed: 54 additions & 3 deletions

File tree

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,7 @@ public class ApiConstants {
261261
public static final String IS_CLEANUP_REQUIRED = "iscleanuprequired";
262262
public static final String IS_DYNAMIC = "isdynamic";
263263
public static final String IS_EDGE = "isedge";
264+
public static final String IS_ENCRYPTED = "isencrypted";
264265
public static final String IS_EXTRACTABLE = "isextractable";
265266
public static final String IS_FEATURED = "isfeatured";
266267
public static final String IS_PORTABLE = "isportable";

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,9 @@ public class ListVolumesCmd extends BaseListRetrieveOnlyResourceCountCmd impleme
9494
@Parameter(name = ApiConstants.STATE, type = CommandType.STRING, description = "state of the volume. Possible values are: Ready, Allocated, Destroy, Expunging, Expunged.")
9595
private String state;
9696

97+
@Parameter(name = ApiConstants.IS_ENCRYPTED, type = CommandType.BOOLEAN, description = "list only volumes that are encrypted", since = "4.20",
98+
authorized = { RoleType.Admin })
99+
private Boolean encrypted;
97100
/////////////////////////////////////////////////////
98101
/////////////////// Accessors ///////////////////////
99102
/////////////////////////////////////////////////////
@@ -151,6 +154,10 @@ public String getState() {
151154
return state;
152155
}
153156

157+
public Boolean isEncrypted() {
158+
return encrypted;
159+
}
160+
154161
/////////////////////////////////////////////////////
155162
/////////////// API Implementation///////////////////
156163
/////////////////////////////////////////////////////

engine/schema/src/main/java/com/cloud/storage/dao/VolumeDao.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
import java.util.Date;
2020
import java.util.List;
21+
import java.util.Set;
2122

2223
import com.cloud.hypervisor.Hypervisor.HypervisorType;
2324
import com.cloud.storage.ScopeType;
@@ -155,4 +156,6 @@ public interface VolumeDao extends GenericDao<VolumeVO, Long>, StateDao<Volume.S
155156
VolumeVO findByPoolIdAndPath(long id, String path);
156157

157158
List<VolumeVO> listByIds(List<Long> ids);
159+
160+
Set<Long> listEncryptedVolumeIds();
158161
}

engine/schema/src/main/java/com/cloud/storage/dao/VolumeDaoImpl.java

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,16 @@
1616
// under the License.
1717
package com.cloud.storage.dao;
1818

19+
import java.sql.Connection;
1920
import java.sql.PreparedStatement;
2021
import java.sql.ResultSet;
2122
import java.sql.SQLException;
2223
import java.util.ArrayList;
2324
import java.util.Collections;
2425
import java.util.Date;
26+
import java.util.HashSet;
2527
import java.util.List;
28+
import java.util.Set;
2629

2730
import javax.inject.Inject;
2831

@@ -65,6 +68,7 @@ public class VolumeDaoImpl extends GenericDaoBase<VolumeVO, Long> implements Vol
6568
protected final SearchBuilder<VolumeVO> RootDiskStateSearch;
6669
private final SearchBuilder<VolumeVO> storeAndInstallPathSearch;
6770
private final SearchBuilder<VolumeVO> volumeIdSearch;
71+
private final SearchBuilder<VolumeVO> encryptedIdSearch;
6872
protected GenericSearchBuilder<VolumeVO, Long> CountByAccount;
6973
protected GenericSearchBuilder<VolumeVO, SumCount> primaryStorageSearch;
7074
protected GenericSearchBuilder<VolumeVO, SumCount> primaryStorageSearch2;
@@ -487,6 +491,10 @@ public VolumeDaoImpl() {
487491
volumeIdSearch.and("idIN", volumeIdSearch.entity().getId(), Op.IN);
488492
volumeIdSearch.done();
489493

494+
encryptedIdSearch = createSearchBuilder();
495+
encryptedIdSearch.and("encryptionMethod", encryptedIdSearch.entity().getId(), Op.NNULL);
496+
encryptedIdSearch.done();
497+
490498
poolAndPathSearch = createSearchBuilder();
491499
poolAndPathSearch.and("poolId", poolAndPathSearch.entity().getPoolId(), Op.EQ);
492500
poolAndPathSearch.and("path", poolAndPathSearch.entity().getPath(), Op.EQ);
@@ -839,4 +847,21 @@ public List<VolumeVO> listByIds(List<Long> ids) {
839847
sc.setParameters("idIN", ids.toArray());
840848
return listBy(sc, null);
841849
}
850+
851+
@Override
852+
public Set<Long> listEncryptedVolumeIds() {
853+
String selectSql = "SELECT id FROM volumes WHERE encrypt_format IS NOT NULL";
854+
Set<Long> ids = new HashSet<>();
855+
Connection conn = TransactionLegacy.getStandaloneConnection();
856+
try {
857+
PreparedStatement stmt = conn.prepareStatement(selectSql);
858+
ResultSet rs = stmt.executeQuery();
859+
while (rs != null && rs.next()) {
860+
ids.add(rs.getLong(1));
861+
}
862+
} catch (SQLException ex) {
863+
throw new CloudRuntimeException("Error while trying to find ids for encrypted volumes", ex);
864+
}
865+
return ids;
866+
}
842867
}

server/src/main/java/com/cloud/api/query/QueryManagerImpl.java

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2331,7 +2331,7 @@ public ListResponse<VolumeResponse> searchForVolumes(ListVolumesCmd cmd) {
23312331
private Pair<List<VolumeJoinVO>, Integer> searchForVolumesInternal(ListVolumesCmd cmd) {
23322332

23332333
Account caller = CallContext.current().getCallingAccount();
2334-
List<Long> permittedAccounts = new ArrayList<Long>();
2334+
List<Long> permittedAccounts = new ArrayList<>();
23352335

23362336
Long id = cmd.getId();
23372337
Long vmInstanceId = cmd.getVirtualMachineId();
@@ -2350,8 +2350,9 @@ private Pair<List<VolumeJoinVO>, Integer> searchForVolumesInternal(ListVolumesCm
23502350
Long podId = cmd.getPodId();
23512351

23522352
List<Long> ids = getIdsListFromCmd(cmd.getId(), cmd.getIds());
2353+
checkEncrypted(cmd, ids);
23532354

2354-
Ternary<Long, Boolean, ListProjectResourcesCriteria> domainIdRecursiveListProject = new Ternary<Long, Boolean, ListProjectResourcesCriteria>(cmd.getDomainId(), cmd.isRecursive(), null);
2355+
Ternary<Long, Boolean, ListProjectResourcesCriteria> domainIdRecursiveListProject = new Ternary<>(cmd.getDomainId(), cmd.isRecursive(), null);
23552356
accountMgr.buildACLSearchParameters(caller, id, cmd.getAccountName(), cmd.getProjectId(), permittedAccounts, domainIdRecursiveListProject, cmd.listAll(), false);
23562357
Long domainId = domainIdRecursiveListProject.first();
23572358
Boolean isRecursive = domainIdRecursiveListProject.second();
@@ -2495,7 +2496,21 @@ private Pair<List<VolumeJoinVO>, Integer> searchForVolumesInternal(ListVolumesCm
24952496
vrIds[i++] = v.getId();
24962497
}
24972498
List<VolumeJoinVO> vrs = _volumeJoinDao.searchByIds(vrIds);
2498-
return new Pair<List<VolumeJoinVO>, Integer>(vrs, count);
2499+
return new Pair<>(vrs, count);
2500+
}
2501+
2502+
private void checkEncrypted(ListVolumesCmd cmd, List<Long> ids) {
2503+
if (Boolean.TRUE.equals(cmd.isEncrypted())) {
2504+
Set<Long> encs = volumeDao.listEncryptedVolumeIds();
2505+
if (encs.contains(cmd.getId())) {
2506+
return;
2507+
}
2508+
for (Long id: ids) {
2509+
if (! encs.contains(id)) {
2510+
ids.remove(id);
2511+
}
2512+
}
2513+
}
24992514
}
25002515

25012516
private boolean shouldListSystemVms(ListVolumesCmd cmd, Long callerId) {

0 commit comments

Comments
 (0)