@@ -80,11 +80,8 @@ public Map<String, NicIpAddressInfo> getNicNetworkInfoBySystemTag(List<String> s
8080 if (VmSystemTags .STATIC_IP .isMatch (sysTag )) {
8181 Map <String , String > token = TagUtils .parse (VmSystemTags .STATIC_IP .getTagFormat (), sysTag );
8282 String l3Uuid = token .get (VmSystemTags .STATIC_IP_L3_UUID_TOKEN );
83- NicIpAddressInfo nicIpAddressInfo = ret .get (l3Uuid );
84- if (nicIpAddressInfo == null ) {
85- ret .put (l3Uuid , new NicIpAddressInfo ("" , "" , "" ,
86- "" , "" , "" ));
87- }
83+ ret .computeIfAbsent (l3Uuid , k -> new NicIpAddressInfo ("" , "" , "" ,
84+ "" , "" , "" ));
8885 String ip = token .get (VmSystemTags .STATIC_IP_TOKEN );
8986 ip = IPv6NetworkUtils .ipv6TagValueToAddress (ip );
9087 if (NetworkUtils .isIpv4Address (ip )) {
@@ -109,30 +106,42 @@ public Map<String, NicIpAddressInfo> getNicNetworkInfoBySystemTag(List<String> s
109106 continue ;
110107 }
111108 ret .get (l3Uuid ).ipv4Gateway = token .get (VmSystemTags .IPV4_GATEWAY_TOKEN );
112- }
113- if (VmSystemTags .IPV4_NETMASK .isMatch (sysTag )) {
109+ } else if (VmSystemTags .IPV4_NETMASK .isMatch (sysTag )) {
114110 Map <String , String > token = TagUtils .parse (VmSystemTags .IPV4_NETMASK .getTagFormat (), sysTag );
115111 String l3Uuid = token .get (VmSystemTags .IPV4_NETMASK_L3_UUID_TOKEN );
116112 if (ret .get (l3Uuid ) == null ) {
117113 continue ;
118114 }
119115 ret .get (l3Uuid ).ipv4Netmask = token .get (VmSystemTags .IPV4_NETMASK_TOKEN );
120- }
121- if (VmSystemTags .IPV6_GATEWAY .isMatch (sysTag )) {
116+ } else if (VmSystemTags .IPV6_GATEWAY .isMatch (sysTag )) {
122117 Map <String , String > token = TagUtils .parse (VmSystemTags .IPV6_GATEWAY .getTagFormat (), sysTag );
123118 String l3Uuid = token .get (VmSystemTags .IPV6_GATEWAY_L3_UUID_TOKEN );
124119 if (ret .get (l3Uuid ) == null ) {
125120 continue ;
126121 }
127122 ret .get (l3Uuid ).ipv6Gateway = IPv6NetworkUtils .ipv6TagValueToAddress (token .get (VmSystemTags .IPV6_GATEWAY_TOKEN ));
128- }
129- if (VmSystemTags .IPV6_PREFIX .isMatch (sysTag )) {
123+ } else if (VmSystemTags .IPV6_PREFIX .isMatch (sysTag )) {
130124 Map <String , String > token = TagUtils .parse (VmSystemTags .IPV6_PREFIX .getTagFormat (), sysTag );
131125 String l3Uuid = token .get (VmSystemTags .IPV6_PREFIX_L3_UUID_TOKEN );
132126 if (ret .get (l3Uuid ) == null ) {
133127 continue ;
134128 }
135129 ret .get (l3Uuid ).ipv6Prefix = token .get (VmSystemTags .IPV6_PREFIX_TOKEN );
130+ } else if (VmSystemTags .STATIC_DNS .isMatch (sysTag )) {
131+ Map <String , String > token = TagUtils .parse (VmSystemTags .STATIC_DNS .getTagFormat (), sysTag );
132+ String l3Uuid = token .get (VmSystemTags .STATIC_DNS_L3_UUID_TOKEN );
133+ if (ret .get (l3Uuid ) == null ) {
134+ continue ;
135+ }
136+ String dnsStr = token .get (VmSystemTags .STATIC_DNS_TOKEN );
137+ if (dnsStr != null && !dnsStr .isEmpty ()) {
138+ // Convert back from tag value: replace '--' with '::' for IPv6 addresses
139+ List <String > dnsList = new ArrayList <>();
140+ for (String dns : dnsStr .split ("," )) {
141+ dnsList .add (IPv6NetworkUtils .ipv6TagValueToAddress (dns ));
142+ }
143+ ret .get (l3Uuid ).dnsAddresses = dnsList ;
144+ }
136145 }
137146 }
138147
@@ -222,6 +231,49 @@ public void deleteStaticIpByL3NetworkUuid(String l3Uuid) {
222231 )));
223232 }
224233
234+ public void setStaticDns (String vmUuid , String l3Uuid , List <String > dnsAddresses ) {
235+ if (dnsAddresses == null || dnsAddresses .isEmpty ()) {
236+ deleteStaticDnsByVmUuidAndL3Uuid (vmUuid , l3Uuid );
237+ return ;
238+ }
239+
240+ // Convert IPv6 addresses: replace '::' with '--' to avoid conflict with system tag delimiter
241+ List <String > tagSafeDns = new ArrayList <>();
242+ for (String dns : dnsAddresses ) {
243+ tagSafeDns .add (IPv6NetworkUtils .ipv6AddessToTagValue (dns ));
244+ }
245+ String dnsStr = String .join ("," , tagSafeDns );
246+
247+ SimpleQuery <SystemTagVO > q = dbf .createQuery (SystemTagVO .class );
248+ q .select (SystemTagVO_ .uuid );
249+ q .add (SystemTagVO_ .resourceType , Op .EQ , VmInstanceVO .class .getSimpleName ());
250+ q .add (SystemTagVO_ .resourceUuid , Op .EQ , vmUuid );
251+ q .add (SystemTagVO_ .tag , Op .LIKE , TagUtils .tagPatternToSqlPattern (VmSystemTags .STATIC_DNS .instantiateTag (
252+ map (e (VmSystemTags .STATIC_DNS_L3_UUID_TOKEN , l3Uuid ))
253+ )));
254+ String tagUuid = q .findValue ();
255+
256+ if (tagUuid == null ) {
257+ SystemTagCreator creator = VmSystemTags .STATIC_DNS .newSystemTagCreator (vmUuid );
258+ creator .setTagByTokens (map (
259+ e (VmSystemTags .STATIC_DNS_L3_UUID_TOKEN , l3Uuid ),
260+ e (VmSystemTags .STATIC_DNS_TOKEN , dnsStr )
261+ ));
262+ creator .create ();
263+ } else {
264+ VmSystemTags .STATIC_DNS .updateByTagUuid (tagUuid , VmSystemTags .STATIC_DNS .instantiateTag (map (
265+ e (VmSystemTags .STATIC_DNS_L3_UUID_TOKEN , l3Uuid ),
266+ e (VmSystemTags .STATIC_DNS_TOKEN , dnsStr )
267+ )));
268+ }
269+ }
270+
271+ public void deleteStaticDnsByVmUuidAndL3Uuid (String vmUuid , String l3Uuid ) {
272+ VmSystemTags .STATIC_DNS .delete (vmUuid , TagUtils .tagPatternToSqlPattern (VmSystemTags .STATIC_DNS .instantiateTag (
273+ map (e (VmSystemTags .STATIC_DNS_L3_UUID_TOKEN , l3Uuid ))
274+ )));
275+ }
276+
225277 public Map <Integer , String > getNicStaticIpMap (List <String > nicStaticIpList ) {
226278 Map <Integer , String > nicStaticIpMap = new HashMap <>();
227279 if (nicStaticIpList != null ) {
@@ -263,14 +315,14 @@ public boolean isIpChange(String vmUuid, String l3Uuid) {
263315 return false ;
264316 }
265317
266- public Boolean checkIpRangeConflict (VmNicVO nicVO ){
318+ public Boolean isNicIpInL3IpRanges (VmNicVO nicVO ){
267319 if (Q .New (IpRangeVO .class ).eq (IpRangeVO_ .l3NetworkUuid , nicVO .getL3NetworkUuid ()).list ().isEmpty ()) {
268- return Boolean .FALSE ;
320+ return Boolean .TRUE ;
269321 }
270322 if (getIpRangeUuid (nicVO .getL3NetworkUuid (), nicVO .getIp ()) == null ) {
271- return Boolean .TRUE ;
323+ return Boolean .FALSE ;
272324 }
273- return Boolean .FALSE ;
325+ return Boolean .TRUE ;
274326 }
275327
276328 public String getIpRangeUuid (String l3Uuid , String ip ) {
@@ -297,10 +349,19 @@ public String getIpRangeUuid(String l3Uuid, String ip) {
297349 return null ;
298350 }
299351
352+ public NormalIpRangeVO findMatchedNormalIpRange (String l3Uuid , String ip ) {
353+ String rangeUuid = getIpRangeUuid (l3Uuid , ip );
354+ if (rangeUuid == null ) {
355+ return null ;
356+ }
357+ return dbf .findByUuid (rangeUuid , NormalIpRangeVO .class );
358+ }
359+
300360 public void checkIpAvailability (String l3Uuid , String ip ) {
301361 CheckIpAvailabilityMsg cmsg = new CheckIpAvailabilityMsg ();
302362 cmsg .setIp (ip );
303363 cmsg .setL3NetworkUuid (l3Uuid );
364+ cmsg .setIpRangeCheck (false );
304365 bus .makeLocalServiceId (cmsg , L3NetworkConstant .SERVICE_ID );
305366 MessageReply r = bus .call (cmsg );
306367 if (!r .isSuccess ()) {
@@ -334,10 +395,7 @@ public List<String> fillUpStaticIpInfoToVmNics(Map<String, NicIpAddressInfo> sta
334395 }
335396
336397 if (!StringUtils .isEmpty (nicIp .ipv4Address )) {
337- NormalIpRangeVO ipRangeVO = Q .New (NormalIpRangeVO .class )
338- .eq (NormalIpRangeVO_ .l3NetworkUuid , l3Uuid )
339- .eq (NormalIpRangeVO_ .ipVersion , IPv6Constants .IPv4 )
340- .limit (1 ).find ();
398+ NormalIpRangeVO ipRangeVO = findMatchedNormalIpRange (l3Uuid , nicIp .ipv4Address );
341399 if (ipRangeVO == null ) {
342400 if (StringUtils .isEmpty (nicIp .ipv4Netmask )) {
343401 throw new ApiMessageInterceptionException (operr (ORG_ZSTACK_COMPUTE_VM_10310 , "netmask must be set" ));
@@ -349,8 +407,10 @@ public List<String> fillUpStaticIpInfoToVmNics(Map<String, NicIpAddressInfo> sta
349407 e (VmSystemTags .IPV4_NETMASK_TOKEN , ipRangeVO .getNetmask ()))
350408 ));
351409 } else if (!nicIp .ipv4Netmask .equals (ipRangeVO .getNetmask ())) {
352- throw new ApiMessageInterceptionException (operr (ORG_ZSTACK_COMPUTE_VM_10311 , "netmask error, expect: %s, got: %s" ,
353- ipRangeVO .getNetmask (), nicIp .ipv4Netmask ));
410+ newSystags .add (VmSystemTags .IPV4_NETMASK .instantiateTag (
411+ map (e (VmSystemTags .IPV4_NETMASK_L3_UUID_TOKEN , l3Uuid ),
412+ e (VmSystemTags .IPV4_NETMASK_TOKEN , nicIp .ipv4Netmask ))
413+ ));
354414 }
355415
356416 if (StringUtils .isEmpty (nicIp .ipv4Gateway )) {
@@ -359,17 +419,16 @@ public List<String> fillUpStaticIpInfoToVmNics(Map<String, NicIpAddressInfo> sta
359419 e (VmSystemTags .IPV4_GATEWAY_TOKEN , ipRangeVO .getGateway ()))
360420 ));
361421 } else if (!nicIp .ipv4Gateway .equals (ipRangeVO .getGateway ())) {
362- throw new ApiMessageInterceptionException (operr (ORG_ZSTACK_COMPUTE_VM_10312 , "gateway error, expect: %s, got: %s" ,
363- ipRangeVO .getGateway (), nicIp .ipv4Gateway ));
422+ newSystags .add (VmSystemTags .IPV4_GATEWAY .instantiateTag (
423+ map (e (VmSystemTags .IPV4_GATEWAY_L3_UUID_TOKEN , l3Uuid ),
424+ e (VmSystemTags .IPV4_GATEWAY_TOKEN , nicIp .ipv4Gateway ))
425+ ));
364426 }
365427 }
366428 }
367429
368430 if (!StringUtils .isEmpty (nicIp .ipv6Address )) {
369- NormalIpRangeVO ipRangeVO = Q .New (NormalIpRangeVO .class )
370- .eq (NormalIpRangeVO_ .l3NetworkUuid , l3Uuid )
371- .eq (NormalIpRangeVO_ .ipVersion , IPv6Constants .IPv6 )
372- .limit (1 ).find ();
431+ NormalIpRangeVO ipRangeVO = findMatchedNormalIpRange (l3Uuid , nicIp .ipv6Address );
373432 if (ipRangeVO == null ) {
374433 if (StringUtils .isEmpty (nicIp .ipv6Prefix )) {
375434 throw new ApiMessageInterceptionException (operr (ORG_ZSTACK_COMPUTE_VM_10313 , "ipv6 prefix length must be set" ));
@@ -381,8 +440,10 @@ public List<String> fillUpStaticIpInfoToVmNics(Map<String, NicIpAddressInfo> sta
381440 e (VmSystemTags .IPV6_PREFIX_TOKEN , ipRangeVO .getPrefixLen ()))
382441 ));
383442 } else if (!nicIp .ipv6Prefix .equals (ipRangeVO .getPrefixLen ().toString ())) {
384- throw new ApiMessageInterceptionException (operr (ORG_ZSTACK_COMPUTE_VM_10314 , "ipv6 prefix length error, expect: %s, got: %s" ,
385- ipRangeVO .getPrefixLen (), nicIp .ipv6Prefix ));
443+ newSystags .add (VmSystemTags .IPV6_PREFIX .instantiateTag (
444+ map (e (VmSystemTags .IPV6_PREFIX_L3_UUID_TOKEN , l3Uuid ),
445+ e (VmSystemTags .IPV6_PREFIX_TOKEN , nicIp .ipv6Prefix ))
446+ ));
386447 }
387448
388449 if (StringUtils .isEmpty (nicIp .ipv6Gateway )) {
@@ -392,8 +453,11 @@ public List<String> fillUpStaticIpInfoToVmNics(Map<String, NicIpAddressInfo> sta
392453 IPv6NetworkUtils .ipv6AddressToTagValue (ipRangeVO .getGateway ())))
393454 ));
394455 } else if (!nicIp .ipv6Gateway .equals (ipRangeVO .getGateway ())) {
395- throw new ApiMessageInterceptionException (operr (ORG_ZSTACK_COMPUTE_VM_10315 , "gateway error, expect: %s, got: %s" ,
396- ipRangeVO .getGateway (), nicIp .ipv6Gateway ));
456+ newSystags .add (VmSystemTags .IPV6_GATEWAY .instantiateTag (
457+ map (e (VmSystemTags .IPV6_GATEWAY_L3_UUID_TOKEN , l3Uuid ),
458+ e (VmSystemTags .IPV6_GATEWAY_TOKEN ,
459+ IPv6NetworkUtils .ipv6AddressToTagValue (nicIp .ipv6Gateway )))
460+ ));
397461 }
398462 }
399463 }
0 commit comments