diff --git a/plugins/storage/volume/linstor/src/main/java/com/cloud/hypervisor/kvm/storage/LinstorStorageAdaptor.java b/plugins/storage/volume/linstor/src/main/java/com/cloud/hypervisor/kvm/storage/LinstorStorageAdaptor.java index 2847484e30ef..3a703cdb426f 100644 --- a/plugins/storage/volume/linstor/src/main/java/com/cloud/hypervisor/kvm/storage/LinstorStorageAdaptor.java +++ b/plugins/storage/volume/linstor/src/main/java/com/cloud/hypervisor/kvm/storage/LinstorStorageAdaptor.java @@ -28,6 +28,7 @@ import javax.annotation.Nonnull; +import org.apache.cloudstack.storage.datastore.util.LinstorUtil; import org.apache.cloudstack.utils.qemu.QemuImg; import org.apache.cloudstack.utils.qemu.QemuImgException; import org.apache.cloudstack.utils.qemu.QemuImgFile; @@ -489,39 +490,8 @@ public KVMPhysicalDisk createTemplateFromDirectDownloadFile(String templateFileP } public long getCapacity(LinstorStoragePool pool) { - DevelopersApi linstorApi = getLinstorAPI(pool); final String rscGroupName = pool.getResourceGroup(); - try { - List rscGrps = linstorApi.resourceGroupList( - Collections.singletonList(rscGroupName), - null, - null, - null); - - if (rscGrps.isEmpty()) { - final String errMsg = String.format("Linstor: Resource group '%s' not found", rscGroupName); - s_logger.error(errMsg); - throw new CloudRuntimeException(errMsg); - } - - List storagePools = linstorApi.viewStoragePools( - Collections.emptyList(), - rscGrps.get(0).getSelectFilter().getStoragePoolList(), - null, - null, - null - ); - - final long capacity = storagePools.stream() - .filter(sp -> sp.getProviderKind() != ProviderKind.DISKLESS) - .mapToLong(sp -> sp.getTotalCapacity() != null ? sp.getTotalCapacity() : 0) - .sum() * 1024; // linstor uses kiB - s_logger.debug("Linstor: GetCapacity() -> " + capacity); - return capacity; - } catch (ApiException apiEx) { - s_logger.error(apiEx.getMessage()); - throw new CloudRuntimeException(apiEx.getBestMessage(), apiEx); - } + return LinstorUtil.getCapacityBytes(pool.getSourceHost(), rscGroupName); } public long getAvailable(LinstorStoragePool pool) { @@ -550,7 +520,7 @@ public long getAvailable(LinstorStoragePool pool) { final long free = storagePools.stream() .filter(sp -> sp.getProviderKind() != ProviderKind.DISKLESS) - .mapToLong(StoragePool::getFreeCapacity).sum() * 1024; // linstor uses KiB + .mapToLong(sp -> sp.getFreeCapacity() != null ? sp.getFreeCapacity() : 0L).sum() * 1024; // linstor uses KiB s_logger.debug("Linstor: getAvailable() -> " + free); return free; @@ -586,7 +556,9 @@ public long getUsed(LinstorStoragePool pool) { final long used = storagePools.stream() .filter(sp -> sp.getProviderKind() != ProviderKind.DISKLESS) - .mapToLong(sp -> sp.getTotalCapacity() - sp.getFreeCapacity()).sum() * 1024; // linstor uses Kib + .mapToLong(sp -> sp.getTotalCapacity() != null && sp.getFreeCapacity() != null ? + sp.getTotalCapacity() - sp.getFreeCapacity() : 0L) + .sum() * 1024; // linstor uses Kib s_logger.debug("Linstor: getUsed() -> " + used); return used; } catch (ApiException apiEx) { diff --git a/plugins/storage/volume/linstor/src/main/java/org/apache/cloudstack/storage/datastore/util/LinstorUtil.java b/plugins/storage/volume/linstor/src/main/java/org/apache/cloudstack/storage/datastore/util/LinstorUtil.java index f1760a003ab5..ddd15a5984ad 100644 --- a/plugins/storage/volume/linstor/src/main/java/org/apache/cloudstack/storage/datastore/util/LinstorUtil.java +++ b/plugins/storage/volume/linstor/src/main/java/org/apache/cloudstack/storage/datastore/util/LinstorUtil.java @@ -72,7 +72,8 @@ public static long getCapacityBytes(String linstorUrl, String rscGroupName) { return storagePools.stream() .filter(sp -> sp.getProviderKind() != ProviderKind.DISKLESS) - .mapToLong(StoragePool::getTotalCapacity).sum() * 1024; // linstor uses kiB + .mapToLong(sp -> sp.getTotalCapacity() != null ? sp.getTotalCapacity() : 0L) + .sum() * 1024; // linstor uses kiB } catch (ApiException apiEx) { s_logger.error(apiEx.getMessage()); throw new CloudRuntimeException(apiEx);