@@ -318,7 +318,7 @@ def _validate_projects_have_access_to_network(self, network, project_ids):
318318 if project_ids - allowed_projects :
319319 raise exc .InvalidSharedSetting (network = network .name )
320320
321- def _validate_ipv6_attributes (self , subnet , cur_subnet ):
321+ def _validate_ipv6_attributes (self , subnet , cur_subnet , has_cidr ):
322322 if cur_subnet :
323323 self ._validate_ipv6_update_dhcp (subnet , cur_subnet )
324324 return
@@ -330,7 +330,7 @@ def _validate_ipv6_attributes(self, subnet, cur_subnet):
330330 if ra_mode_set and address_mode_set :
331331 self ._validate_ipv6_combination (subnet ['ipv6_ra_mode' ],
332332 subnet ['ipv6_address_mode' ])
333- if address_mode_set or ra_mode_set :
333+ if has_cidr and ( address_mode_set or ra_mode_set ) :
334334 self ._validate_eui64_applicable (subnet )
335335
336336 def _validate_eui64_applicable (self , subnet ):
@@ -607,16 +607,21 @@ def _validate_subnet(self, context, s, cur_subnet=None):
607607
608608 ip_ver = s ['ip_version' ]
609609
610+ # We could be called without a cidr if a subnet pool is being used,
611+ # so remember that since some checks below require one and should
612+ # be skipped.
613+ has_cidr = False
610614 if validators .is_attr_set (s .get ('cidr' )):
611615 self ._validate_ip_version (ip_ver , s ['cidr' ], 'cidr' )
616+ has_cidr = True
612617
613618 # TODO(watanabe.isao): After we found a way to avoid the re-sync
614619 # from the agent side, this restriction could be removed.
615620 if cur_subnet :
616621 dhcp_was_enabled = cur_subnet .enable_dhcp
617622 else :
618623 dhcp_was_enabled = False
619- if s .get ('enable_dhcp' ) and not dhcp_was_enabled :
624+ if has_cidr and s .get ('enable_dhcp' ) and not dhcp_was_enabled :
620625 subnet_prefixlen = netaddr .IPNetwork (s ['cidr' ]).prefixlen
621626 error_message = _ ("Subnet has a prefix length that is "
622627 "incompatible with DHCP service enabled" )
@@ -640,12 +645,13 @@ def _validate_subnet(self, context, s, cur_subnet=None):
640645
641646 if validators .is_attr_set (s .get ('gateway_ip' )):
642647 self ._validate_ip_version (ip_ver , s ['gateway_ip' ], 'gateway_ip' )
643- is_gateway_not_valid = (
644- ipam .utils .check_gateway_invalid_in_subnet (
645- s ['cidr' ], s ['gateway_ip' ]))
646- if is_gateway_not_valid :
647- error_message = _ ("Gateway is not valid on subnet" )
648- raise exc .InvalidInput (error_message = error_message )
648+ if has_cidr :
649+ is_gateway_not_valid = (
650+ ipam .utils .check_gateway_invalid_in_subnet (
651+ s ['cidr' ], s ['gateway_ip' ]))
652+ if is_gateway_not_valid :
653+ error_message = _ ("Gateway is not valid on subnet" )
654+ raise exc .InvalidInput (error_message = error_message )
649655 # Ensure the gateway IP is not assigned to any port
650656 # skip this check in case of create (s parameter won't have id)
651657 # NOTE(salv-orlando): There is slight chance of a race, when
@@ -698,7 +704,7 @@ def _validate_subnet(self, context, s, cur_subnet=None):
698704 error_message = (_ ("ipv6_address_mode is not valid when "
699705 "ip_version is 4" )))
700706 if ip_ver == 6 :
701- self ._validate_ipv6_attributes (s , cur_subnet )
707+ self ._validate_ipv6_attributes (s , cur_subnet , has_cidr )
702708
703709 def _validate_subnet_for_pd (self , subnet ):
704710 """Validates that subnet parameters are correct for IPv6 PD"""
@@ -860,6 +866,7 @@ def _create_subnet_precommit(self, context, subnet):
860866 msg = _ ('a subnetpool must be specified in the absence of a cidr' )
861867 raise exc .BadRequest (resource = 'subnets' , msg = msg )
862868
869+ validate = True
863870 if subnetpool_id :
864871 self .ipam .validate_pools_with_subnetpool (s )
865872 if subnetpool_id == constants .IPV6_PD_POOL_ID :
@@ -868,16 +875,18 @@ def _create_subnet_precommit(self, context, subnet):
868875 # cidr with IPv6 prefix delegation. Set the subnetpool_id
869876 # to None and allow the request to continue as normal.
870877 subnetpool_id = None
871- self ._validate_subnet (context , s )
872878 else :
873879 prefix = constants .PROVISIONAL_IPV6_PD_PREFIX
874880 subnet ['subnet' ]['cidr' ] = prefix
875881 self ._validate_subnet_for_pd (s )
882+ validate = False
876883 else :
877884 if not has_cidr :
878885 msg = _ ('A cidr must be specified in the absence of a '
879886 'subnet pool' )
880887 raise exc .BadRequest (resource = 'subnets' , msg = msg )
888+
889+ if validate :
881890 self ._validate_subnet (context , s )
882891
883892 with db_api .CONTEXT_WRITER .using (context ):
0 commit comments