Skip to content

Commit 138e9b5

Browse files
committed
[GetEipAttachableVmNics]GetEipAttachableVmNics from flat network shows vrouter private network #1522
for https://github.com/zxwing/premium/issues/1522
1 parent f7c9a4f commit 138e9b5

File tree

9 files changed

+371
-18
lines changed

9 files changed

+371
-18
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
package org.zstack.core.db;
2+
3+
import org.springframework.beans.factory.annotation.Autowire;
4+
import org.springframework.beans.factory.annotation.Autowired;
5+
import org.springframework.beans.factory.annotation.Configurable;
6+
import org.springframework.transaction.annotation.Transactional;
7+
8+
import javax.persistence.LockModeType;
9+
import javax.persistence.Query;
10+
import java.util.List;
11+
12+
/**
13+
* Created by xing5 on 2017/1/11.
14+
*/
15+
@Configurable(preConstruction = true, autowire = Autowire.BY_TYPE)
16+
public class SQL {
17+
@Autowired
18+
private DatabaseFacade dbf;
19+
20+
private String sql;
21+
private Query query;
22+
private boolean useTransaction;
23+
24+
private SQL(String sql) {
25+
this.sql = sql;
26+
query = dbf.getEntityManager().createQuery(this.sql);
27+
}
28+
29+
public SQL transactional() {
30+
useTransaction = true;
31+
return this;
32+
}
33+
34+
public SQL param(String key, Object o) {
35+
query.setParameter(key, o);
36+
return this;
37+
}
38+
39+
public SQL first(int offset) {
40+
query.setFirstResult(offset);
41+
return this;
42+
}
43+
44+
public SQL max(int max) {
45+
query.setMaxResults(max);
46+
return this;
47+
}
48+
49+
public SQL lock(LockModeType mode) {
50+
query.setLockMode(mode);
51+
return this;
52+
}
53+
54+
@Transactional(readOnly = true)
55+
private List transactionalList() {
56+
return query.getResultList();
57+
}
58+
59+
public <T> List<T> list() {
60+
return useTransaction ? transactionalList() : query.getResultList();
61+
}
62+
63+
@Transactional(readOnly = true)
64+
private <K> K transactionalFind() {
65+
List lst = query.getResultList();
66+
return lst.isEmpty() ? null : (K) lst.get(0);
67+
}
68+
69+
public <K> K find() {
70+
if (useTransaction) {
71+
return transactionalFind();
72+
} else {
73+
List lst = query.getResultList();
74+
return lst.isEmpty() ? null : (K) lst.get(0);
75+
}
76+
}
77+
78+
@Transactional(readOnly = true)
79+
private int transactionalExecute() {
80+
return query.executeUpdate();
81+
}
82+
83+
public int execute() {
84+
return useTransaction ? transactionalExecute() : query.executeUpdate();
85+
}
86+
87+
public static SQL New(String sql) {
88+
return new SQL(sql);
89+
}
90+
}

plugin/eip/src/main/java/org/zstack/network/service/eip/EipManagerImpl.java

+49-17
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import org.zstack.core.cloudbus.MessageSafe;
88
import org.zstack.core.componentloader.PluginRegistry;
99
import org.zstack.core.db.DatabaseFacade;
10+
import org.zstack.core.db.SQL;
1011
import org.zstack.core.db.SimpleQuery;
1112
import org.zstack.core.db.SimpleQuery.Op;
1213
import org.zstack.core.db.UpdateQuery;
@@ -130,7 +131,9 @@ private void handle(APIUpdateEipMsg msg) {
130131

131132
@Transactional(readOnly = true)
132133
private List<VmNicInventory> getAttachableVmNicForEip(String eipUuid, String vipUuid) {
133-
String zoneUuid = null;
134+
String zoneUuid;
135+
String providerType;
136+
134137
if (eipUuid != null) {
135138
String sql = "select l3.zoneUuid, vip.uuid" +
136139
" from L3NetworkVO l3, VipVO vip, EipVO eip" +
@@ -142,6 +145,10 @@ private List<VmNicInventory> getAttachableVmNicForEip(String eipUuid, String vip
142145
Tuple t = q.getSingleResult();
143146
zoneUuid = t.get(0, String.class);
144147
vipUuid = t.get(1, String.class);
148+
149+
providerType = SQL.New("select v.serviceProvider from VipVO v, EipVO e where e.vipUuid = v.uuid" +
150+
" and e.uuid = :euuid").param("euuid", eipUuid).find();
151+
145152
} else {
146153
String sql = "select l3.zoneUuid" +
147154
" from L3NetworkVO l3, VipVO vip" +
@@ -150,28 +157,53 @@ private List<VmNicInventory> getAttachableVmNicForEip(String eipUuid, String vip
150157
TypedQuery<String> q = dbf.getEntityManager().createQuery(sql, String.class);
151158
q.setParameter("vipUuid", vipUuid);
152159
zoneUuid = q.getSingleResult();
160+
161+
providerType = SQL.New("select v.serviceProvider from VipVO v where v.uuid = :uuid")
162+
.param("uuid", vipUuid).find();
153163
}
154164

165+
List<String> l3Uuids;
166+
167+
if (providerType != null) {
168+
// the eip is created on the backend
169+
l3Uuids = SQL.New("select l3.uuid" +
170+
" from L3NetworkVO l3, VipVO vip, NetworkServiceL3NetworkRefVO ref, NetworkServiceProviderVO np" +
171+
" where l3.system = :system" +
172+
" and l3.uuid != vip.l3NetworkUuid" +
173+
" and l3.uuid = ref.l3NetworkUuid" +
174+
" and ref.networkServiceType = :nsType" +
175+
" and l3.zoneUuid = :zoneUuid" +
176+
" and vip.uuid = :vipUuid" +
177+
" and np.uuid = ref.networkServiceProviderUuid" +
178+
" and np.type = :npType")
179+
.param("system", false)
180+
.param("zoneUuid", zoneUuid)
181+
.param("nsType", EipConstant.EIP_NETWORK_SERVICE_TYPE)
182+
.param("npType", providerType)
183+
.param("vipUuid", vipUuid)
184+
.list();
185+
} else {
186+
// the eip is not created on the backend
187+
l3Uuids = SQL.New("select l3.uuid" +
188+
" from L3NetworkVO l3, VipVO vip, NetworkServiceL3NetworkRefVO ref" +
189+
" where l3.system = :system" +
190+
" and l3.uuid != vip.l3NetworkUuid" +
191+
" and l3.uuid = ref.l3NetworkUuid" +
192+
" and ref.networkServiceType = :nsType" +
193+
" and l3.zoneUuid = :zoneUuid" +
194+
" and vip.uuid = :vipUuid")
195+
.param("system", false)
196+
.param("zoneUuid", zoneUuid)
197+
.param("nsType", EipConstant.EIP_NETWORK_SERVICE_TYPE)
198+
.param("vipUuid", vipUuid)
199+
.list();
200+
}
155201

156-
String sql = "select l3.uuid" +
157-
" from L3NetworkVO l3, VipVO vip, NetworkServiceL3NetworkRefVO ref" +
158-
" where l3.system = :system" +
159-
" and l3.uuid != vip.l3NetworkUuid" +
160-
" and l3.uuid = ref.l3NetworkUuid" +
161-
" and ref.networkServiceType = :nsType" +
162-
" and l3.zoneUuid = :zoneUuid" +
163-
" and vip.uuid = :vipUuid";
164-
TypedQuery<String> l3q = dbf.getEntityManager().createQuery(sql, String.class);
165-
l3q.setParameter("vipUuid", vipUuid);
166-
l3q.setParameter("system", false);
167-
l3q.setParameter("zoneUuid", zoneUuid);
168-
l3q.setParameter("nsType", EipConstant.EIP_NETWORK_SERVICE_TYPE);
169-
List<String> l3Uuids = l3q.getResultList();
170202
if (l3Uuids.isEmpty()) {
171-
return new ArrayList<VmNicInventory>();
203+
return new ArrayList<>();
172204
}
173205

174-
sql = "select nic" +
206+
String sql = "select nic" +
175207
" from VmNicVO nic, VmInstanceVO vm" +
176208
" where nic.l3NetworkUuid in (:l3Uuids)" +
177209
" and nic.vmInstanceUuid = vm.uuid" +

runMavenProfile

+8
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,12 @@ usage() {
44
echo "usage:$0 [deploydb|deploy|xml-java-schema|premium|sdk|doc]"
55
}
66

7+
py() {
8+
cd test
9+
mvn test -Dtest=TestGenerateApiPythonClassAndJsonTemplate
10+
cd - > /dev/null
11+
}
12+
713
doc() {
814
cd test
915
mvn test -Dtest=TestGenerateDocTemplate
@@ -43,6 +49,8 @@ run_profile() {
4349
sdk
4450
elif test x$1 = x'doc'; then
4551
doc
52+
elif test x$1 = x'py'; then
53+
py
4654
else
4755
mvn -pl build -P $1 exec:exec -D$1
4856
fi

sdk/src/main/java/org/zstack/sdk/ZSClient.java

+14-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
* Created by xing5 on 2016/12/9.
2020
*/
2121
public class ZSClient {
22-
private static final OkHttpClient http = new OkHttpClient();
22+
private static OkHttpClient http = new OkHttpClient();
2323

2424
static final Gson gson;
2525
static final Gson prettyGson;
@@ -39,6 +39,19 @@ public static ZSConfig getConfig() {
3939

4040
public static void configure(ZSConfig c) {
4141
config = c;
42+
43+
if (c.readTimeout != null || c.writeTimeout != null) {
44+
OkHttpClient.Builder b = new OkHttpClient.Builder();
45+
46+
if (c.readTimeout != null) {
47+
b.readTimeout(c.readTimeout, TimeUnit.MILLISECONDS);
48+
}
49+
if (c.writeTimeout != null) {
50+
b.writeTimeout(c.writeTimeout, TimeUnit.MILLISECONDS);
51+
}
52+
53+
http = b.build();
54+
}
4255
}
4356

4457
public static void webHookCallback(HttpServletRequest req, HttpServletResponse rsp) {

sdk/src/main/java/org/zstack/sdk/ZSConfig.java

+13
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ public class ZSConfig {
1111
long defaultPollingTimeout = TimeUnit.HOURS.toMillis(3);
1212
long defaultPollingInterval = TimeUnit.SECONDS.toMillis(1);
1313
String webHook;
14+
Long readTimeout;
15+
Long writeTimeout;
1416

1517
public String getHostname() {
1618
return hostname;
@@ -56,6 +58,17 @@ public Builder setDefaultPollingInterval(long value, TimeUnit unit) {
5658
return this;
5759
}
5860

61+
public Builder setReadTimeout(long value, TimeUnit unit) {
62+
config.readTimeout = unit.toMillis(value);
63+
return this;
64+
}
65+
66+
public Builder setWriteTimeout(long value, TimeUnit unit) {
67+
config.writeTimeout = unit.toMillis(value);
68+
return this;
69+
}
70+
71+
5972
public ZSConfig build() {
6073
return config;
6174
}

test/src/test/java/org/zstack/test/Api.java

+2
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,8 @@ public class Api implements CloudBusEventListener {
174174
.setPort(8989)
175175
.setDefaultPollingInterval(100, TimeUnit.MILLISECONDS)
176176
.setDefaultPollingTimeout(15, TimeUnit.SECONDS)
177+
.setReadTimeout(10, TimeUnit.MINUTES)
178+
.setWriteTimeout(10, TimeUnit.MINUTES)
177179
.build()
178180
);
179181
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
package org.zstack.test.eip.flatnetwork;
2+
3+
import junit.framework.Assert;
4+
import org.junit.Before;
5+
import org.junit.Test;
6+
import org.zstack.core.cloudbus.CloudBus;
7+
import org.zstack.core.componentloader.ComponentLoader;
8+
import org.zstack.core.db.DatabaseFacade;
9+
import org.zstack.header.identity.SessionInventory;
10+
import org.zstack.header.vm.VmInstanceInventory;
11+
import org.zstack.header.vm.VmNicInventory;
12+
import org.zstack.network.service.eip.EipInventory;
13+
import org.zstack.network.service.flat.FlatNetworkServiceSimulatorConfig;
14+
import org.zstack.simulator.kvm.KVMSimulatorConfig;
15+
import org.zstack.simulator.virtualrouter.VirtualRouterSimulatorConfig;
16+
import org.zstack.test.Api;
17+
import org.zstack.test.ApiSenderException;
18+
import org.zstack.test.DBUtil;
19+
import org.zstack.test.WebBeanConstructor;
20+
import org.zstack.test.deployer.Deployer;
21+
22+
import java.util.List;
23+
24+
/**
25+
* 1. create two vm on two l3 networks
26+
* 2. one network has flat network provider and another network uses virtual router
27+
* 3. create an eip
28+
*
29+
* confirm the eip is attachable to two vms
30+
*
31+
* 4. attach/detach the eip so the eip is created on the backend
32+
*
33+
* confirm the eip is only attachable to the vm that it used to attach to
34+
*/
35+
public class TestFlatNetworkEip17 {
36+
Deployer deployer;
37+
Api api;
38+
ComponentLoader loader;
39+
CloudBus bus;
40+
DatabaseFacade dbf;
41+
SessionInventory session;
42+
VirtualRouterSimulatorConfig vconfig;
43+
KVMSimulatorConfig kconfig;
44+
FlatNetworkServiceSimulatorConfig fconfig;
45+
46+
@Before
47+
public void setUp() throws Exception {
48+
DBUtil.reDeployDB();
49+
WebBeanConstructor con = new WebBeanConstructor();
50+
deployer = new Deployer("deployerXml/eip/TestFlatNetworkEip17.xml", con);
51+
deployer.addSpringConfig("VirtualRouter.xml");
52+
deployer.addSpringConfig("VirtualRouterSimulator.xml");
53+
deployer.addSpringConfig("flatNetworkServiceSimulator.xml");
54+
deployer.addSpringConfig("flatNetworkProvider.xml");
55+
deployer.addSpringConfig("KVMRelated.xml");
56+
deployer.addSpringConfig("vip.xml");
57+
deployer.addSpringConfig("eip.xml");
58+
deployer.build();
59+
api = deployer.getApi();
60+
loader = deployer.getComponentLoader();
61+
vconfig = loader.getComponent(VirtualRouterSimulatorConfig.class);
62+
kconfig = loader.getComponent(KVMSimulatorConfig.class);
63+
bus = loader.getComponent(CloudBus.class);
64+
dbf = loader.getComponent(DatabaseFacade.class);
65+
fconfig = loader.getComponent(FlatNetworkServiceSimulatorConfig.class);
66+
session = api.loginAsAdmin();
67+
}
68+
69+
@Test
70+
public void test() throws ApiSenderException {
71+
VmInstanceInventory vm = deployer.vms.get("TestVm");
72+
VmInstanceInventory vm1 = deployer.vms.get("TestVm1");
73+
EipInventory eip = deployer.eips.get("eip");
74+
75+
List<VmNicInventory> nics = api.getEipAttachableVmNicsByEipUuid(eip.getUuid());
76+
Assert.assertEquals(2, nics.size());
77+
nics = api.getEipAttachableVmNicsByVipUuid(eip.getVipUuid());
78+
Assert.assertEquals(2, nics.size());
79+
80+
// this creates the EIP on the backend
81+
api.attachEip(eip.getUuid(), vm.getVmNics().get(0).getUuid());
82+
api.detachEip(eip.getUuid());
83+
84+
nics = api.getEipAttachableVmNicsByEipUuid(eip.getUuid());
85+
Assert.assertEquals(1, nics.size());
86+
VmNicInventory nic = nics.get(0);
87+
Assert.assertEquals(vm.getVmNics().get(0).getUuid(), nic.getUuid());
88+
89+
nics = api.getEipAttachableVmNicsByVipUuid(eip.getVipUuid());
90+
Assert.assertEquals(1, nics.size());
91+
nic = nics.get(0);
92+
Assert.assertEquals(vm.getVmNics().get(0).getUuid(), nic.getUuid());
93+
}
94+
}

0 commit comments

Comments
 (0)