diff --git a/src/config/api-server/tests/test_crud_basic.py b/src/config/api-server/tests/test_crud_basic.py index 62700ae4b4c..c476f3e0d8d 100644 --- a/src/config/api-server/tests/test_crud_basic.py +++ b/src/config/api-server/tests/test_crud_basic.py @@ -31,6 +31,7 @@ from vnc_api.common import exceptions as vnc_exceptions import vnc_api.gen.vnc_api_test_gen from vnc_api.gen.resource_test import * +import cfgm_common sys.path.append('../common/tests') from test_utils import * diff --git a/src/config/api-server/vnc_cfg_types.py b/src/config/api-server/vnc_cfg_types.py index 54502cb39f6..48dd9b53ef8 100644 --- a/src/config/api-server/vnc_cfg_types.py +++ b/src/config/api-server/vnc_cfg_types.py @@ -168,6 +168,17 @@ def _get_subnet_name(cls, vn_dict, subnet_uuid): subnet_dict['ip_prefix_len']) return subnet_name + @classmethod + def _is_gateway_ip(cls, vn_dict, ip_addr): + ipam_refs = vn_dict.get('network_ipam_refs', []) + for ipam in ipam_refs: + ipam_subnets = ipam['attr'].get('ipam_subnets', []) + for subnet in ipam_subnets: + if subnet['default_gateway'] == ip_addr: + return True + + return False + @classmethod def http_post_collection(cls, tenant_name, obj_dict, db_conn): vn_fq_name = obj_dict['virtual_network_refs'][0]['to'] @@ -187,6 +198,15 @@ def http_post_collection(cls, tenant_name, obj_dict, db_conn): sub = cls._get_subnet_name(vn_dict, subnet_uuid) if subnet_uuid else None if subnet_uuid and not sub: return (False, (404, "Subnet id " + subnet_uuid + " not found")) + + # If its an external network, check whether floating IP equivalent to + # requested fixed-IP is already reserved. + router_external = vn_dict.get('router_external', None) + if req_ip and router_external and \ + not cls._is_gateway_ip(vn_dict, req_ip) and \ + cls.addr_mgmt.is_ip_allocated(req_ip, vn_fq_name): + return (False, (403, 'Ip address already in use')) + try: ip_addr = cls.addr_mgmt.ip_alloc_req( vn_fq_name, sub=sub, asked_ip_addr=req_ip) diff --git a/src/config/common/tests/test_utils.py b/src/config/common/tests/test_utils.py index 0dc5608e7d4..2622c502e5a 100644 --- a/src/config/common/tests/test_utils.py +++ b/src/config/common/tests/test_utils.py @@ -781,6 +781,10 @@ def create(self, path, value='', *args, **kwargs): self._values[path] = value # end create + def get(self, path): + return self._values[path] + # end get + def delete(self, path, recursive=False): if not recursive: try: