summaryrefslogtreecommitdiffstats
path: root/compass-tasks-base/db/api/network.py
diff options
context:
space:
mode:
authorHarry Huang <huangxiangyu5@huawei.com>2018-12-20 18:54:44 +0800
committerHarry Huang <huangxiangyu5@huawei.com>2018-12-20 19:43:41 +0800
commitca317c8a9891c38ce0777ef2eba4f51716092a14 (patch)
tree09d3d340871093c95415673dcb2a92f675ae3926 /compass-tasks-base/db/api/network.py
parentf347757444c74a3d597dc353aa92d88a0f6282bb (diff)
Add reserved_range in subnet table
JIRA: COMPASS-612 1. reserved_range can be range and individual ips e.g. "10.1.0.0-10.1.0.50, 10.1.0.66" 2. IP within reserved range will cause an error Change-Id: If2160af165a57cab3bc8b528379879cad42a5db0 Signed-off-by: Harry Huang <huangxiangyu5@huawei.com>
Diffstat (limited to 'compass-tasks-base/db/api/network.py')
-rw-r--r--compass-tasks-base/db/api/network.py40
1 files changed, 35 insertions, 5 deletions
diff --git a/compass-tasks-base/db/api/network.py b/compass-tasks-base/db/api/network.py
index e505344..763b0b3 100644
--- a/compass-tasks-base/db/api/network.py
+++ b/compass-tasks-base/db/api/network.py
@@ -15,6 +15,7 @@
"""Network related database operations."""
import logging
import netaddr
+import ipaddress
import re
from compass.db.api import database
@@ -27,14 +28,15 @@ from compass.db import models
SUPPORTED_FIELDS = ['subnet', 'name', 'gateway']
RESP_FIELDS = [
- 'id', 'name', 'subnet', 'gateway', 'created_at', 'updated_at'
+ 'id', 'name', 'subnet', 'gateway', 'created_at',
+ 'updated_at', 'reserved_range'
]
ADDED_FIELDS = ['subnet']
-OPTIONAL_ADDED_FIELDS = ['name', 'gateway']
+OPTIONAL_ADDED_FIELDS = ['name', 'gateway', 'reserved_range']
IGNORE_FIELDS = [
'id', 'created_at', 'updated_at'
]
-UPDATED_FIELDS = ['subnet', 'name', 'gateway']
+UPDATED_FIELDS = ['subnet', 'name', 'gateway', 'reserved_range']
def _check_subnet(subnet):
@@ -47,6 +49,29 @@ def _check_subnet(subnet):
'subnet %s format unrecognized' % subnet)
+def _check_ip_range(ip_ranges):
+ """Check if the ip range is valid.
+ The valid range can be a range or individual ips.
+ Range should be two ips jointed with "-", different ip
+ ranges and ips should be separated by ","
+ e.g. "10.1.0.0-10.1.0.50, 10.1.0.60"
+ """
+ for ip_range in ip_ranges.split(','):
+ ip_ends = ip_range.split('-')
+ try:
+ ipaddress.IPv4Address(ip_ends[0].decode())
+ if len(ip_ends) == 2:
+ ipaddress.IPv4Address(ip_ends[1].decode())
+ except Exception as error:
+ logging.exception(error)
+ raise exception.InvalidParameter(
+ 'ip range %s format unrecognized' % ip_ranges)
+ finally:
+ if len(ip_ends) > 2:
+ raise exception.InvalidParameter(
+ 'ip range %s format unrecognized' % ip_ranges)
+
+
@utils.supported_filters(optional_support_keys=SUPPORTED_FIELDS)
@database.run_in_session()
@user_api.check_user_permission(
@@ -72,6 +97,11 @@ def _get_subnet(subnet_id, session=None, **kwargs):
)
+def get_subnet_internal(subnet_id, session=None, **kwargs):
+ """"Helper function to get subnet."""
+ return _get_subnet(subnet_id=subnet_id, session=session, **kwargs)
+
+
@utils.supported_filters([])
@database.run_in_session()
@user_api.check_user_permission(
@@ -93,7 +123,7 @@ def get_subnet(
ADDED_FIELDS, optional_support_keys=OPTIONAL_ADDED_FIELDS,
ignore_support_keys=IGNORE_FIELDS
)
-@utils.input_validates(subnet=_check_subnet)
+@utils.input_validates(subnet=_check_subnet, reserved_range=_check_ip_range)
@database.run_in_session()
@user_api.check_user_permission(
permission.PERMISSION_ADD_SUBNET
@@ -114,7 +144,7 @@ def add_subnet(
optional_support_keys=UPDATED_FIELDS,
ignore_support_keys=IGNORE_FIELDS
)
-@utils.input_validates(subnet=_check_subnet)
+@utils.input_validates(subnet=_check_subnet, reserved_range=_check_ip_range)
@database.run_in_session()
@user_api.check_user_permission(
permission.PERMISSION_ADD_SUBNET