Skip to content

Commit 16d5fb5

Browse files
author
gitlab
committed
Merge branch 'ZSTAC-9944' into 'master'
Fix ZSTAC-9944 Closes ZSTAC-9944 See merge request zstackio/zstack!2334
2 parents 06717fe + cc6e01a commit 16d5fb5

2 files changed

Lines changed: 102 additions & 9 deletions

File tree

plugin/localstorage/src/main/java/org/zstack/storage/primary/local/LocalStorageBase.java

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,8 @@
5757
import javax.persistence.LockModeType;
5858
import javax.persistence.Tuple;
5959
import javax.persistence.TypedQuery;
60+
import javax.persistence.metamodel.SingularAttribute;
61+
6062
import java.util.*;
6163
import java.util.concurrent.Callable;
6264

@@ -947,20 +949,36 @@ private void handle(APIGetLocalStorageHostDiskCapacityMsg msg) {
947949
q.add(LocalStorageHostRefVO_.primaryStorageUuid, Op.EQ, msg.getPrimaryStorageUuid());
948950
q.add(LocalStorageHostRefVO_.hostUuid, Op.EQ, msg.getHostUuid());
949951
LocalStorageHostRefVO ref = q.find();
952+
953+
long total = 0;
954+
long available = 0;
955+
long availablePhy = 0;
956+
long totalPhy = 0;
957+
950958
if (ref == null) {
951-
reply.setError(errf.instantiateErrorCode(SysErrors.RESOURCE_NOT_FOUND,
952-
String.format("local primary storage[uuid:%s] doesn't have the host[uuid:%s]",
953-
self.getUuid(), msg.getHostUuid())));
954-
bus.reply(msg, reply);
955-
return;
959+
HostStatus status = Q.New(HostVO.class).select(HostVO_.status)
960+
.eq(HostVO_.uuid, msg.getHostUuid()).findValue();
961+
if (status == HostStatus.Connected) {
962+
reply.setError(errf.instantiateErrorCode(SysErrors.RESOURCE_NOT_FOUND,
963+
String.format(
964+
"local primary storage[uuid:%s] doesn't have the host[uuid:%s]",
965+
self.getUuid(), msg.getHostUuid())));
966+
bus.reply(msg, reply);
967+
return;
968+
}
969+
} else {
970+
total = ref.getTotalCapacity();
971+
available = ref.getAvailableCapacity();
972+
availablePhy = ref.getAvailablePhysicalCapacity();
973+
totalPhy = ref.getTotalPhysicalCapacity();
956974
}
957975

958976
HostDiskCapacity c = new HostDiskCapacity();
959977
c.setHostUuid(msg.getHostUuid());
960-
c.setTotalCapacity(ref.getTotalCapacity());
961-
c.setAvailableCapacity(ref.getAvailableCapacity());
962-
c.setAvailablePhysicalCapacity(ref.getAvailablePhysicalCapacity());
963-
c.setTotalPhysicalCapacity(ref.getTotalPhysicalCapacity());
978+
c.setTotalCapacity(total);
979+
c.setAvailableCapacity(available);
980+
c.setAvailablePhysicalCapacity(availablePhy);
981+
c.setTotalPhysicalCapacity(totalPhy);
964982
reply.setInventories(list(c));
965983
} else {
966984
SimpleQuery<LocalStorageHostRefVO> q = dbf.createQuery(LocalStorageHostRefVO.class);
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
package org.zstack.test.integration.storage.primary.local
2+
3+
import org.springframework.http.HttpEntity
4+
import org.zstack.kvm.KVMAgentCommands
5+
import org.zstack.kvm.KVMConstant
6+
import org.zstack.sdk.ClusterInventory
7+
import org.zstack.sdk.GetLocalStorageHostDiskCapacityAction
8+
import org.zstack.sdk.PrimaryStorageInventory
9+
import org.zstack.test.integration.kvm.Env
10+
import org.zstack.testlib.EnvSpec
11+
import org.zstack.testlib.SubCase
12+
13+
class LocalStorageCalculateCapacityCase extends SubCase {
14+
EnvSpec env
15+
@Override
16+
void clean() {
17+
env.delete()
18+
}
19+
20+
@Override
21+
void setup() {
22+
spring {
23+
sftpBackupStorage()
24+
localStorage()
25+
virtualRouter()
26+
securityGroup()
27+
kvm()
28+
}
29+
}
30+
31+
@Override
32+
void environment() {
33+
env = Env.oneVmBasicEnv()
34+
}
35+
36+
@Override
37+
void test() {
38+
env.create {
39+
testCalculateCapacityWhenAddLocalStorage()
40+
}
41+
}
42+
43+
void testCalculateCapacityWhenAddLocalStorage(){
44+
ClusterInventory cluster = env.inventoryByName("cluster")
45+
PrimaryStorageInventory ps = env.inventoryByName("local")
46+
47+
boolean temp = false
48+
49+
env.afterSimulator(KVMConstant.KVM_CONNECT_PATH) { rsp, HttpEntity<String> entity ->
50+
rsp.success = true
51+
52+
def cmd = json(entity.getBody(),KVMAgentCommands.ConnectCmd.class)
53+
GetLocalStorageHostDiskCapacityAction action = new GetLocalStorageHostDiskCapacityAction()
54+
action.hostUuid = cmd.hostUuid
55+
action.primaryStorageUuid = ps.uuid
56+
action.sessionId = adminSession()
57+
GetLocalStorageHostDiskCapacityAction.Result res = action.call()
58+
59+
assert res.error == null
60+
temp = true
61+
62+
return rsp
63+
}
64+
65+
addKVMHost {
66+
username = "test"
67+
password = "password"
68+
name = "adding-host"
69+
managementIp = "127.0.0.2"
70+
clusterUuid = cluster.uuid
71+
}
72+
73+
assert temp
74+
}
75+
}

0 commit comments

Comments
 (0)