Merge "distribute ip from cidr when ip ranges were None" into stable/mitaka
This commit is contained in:
@@ -337,9 +337,12 @@ def check_ip_exist(cluster_id, network_plane_name,
|
|||||||
|
|
||||||
|
|
||||||
def check_ip_ranges(ip_ranges_one,available_ip_list):
|
def check_ip_ranges(ip_ranges_one,available_ip_list):
|
||||||
ip_range = copy.deepcopy(ip_ranges_one.values())
|
# ip_range = copy.deepcopy(ip_ranges_one.values())
|
||||||
ip_ranges_end = ip_range[1]
|
ip_ranges_end = ip_ranges_one[1]
|
||||||
ip_ranges_start = ip_range[0]
|
ip_ranges_start = ip_ranges_one[0]
|
||||||
|
ip_range_gw = ip_ranges_one[3]
|
||||||
|
if ip_range_gw:
|
||||||
|
available_ip_list.append(ip_range_gw)
|
||||||
inter_num = ip_into_int(ip_ranges_start)
|
inter_num = ip_into_int(ip_ranges_start)
|
||||||
ip_ranges_end_inter = ip_into_int(ip_ranges_end)
|
ip_ranges_end_inter = ip_into_int(ip_ranges_end)
|
||||||
while True:
|
while True:
|
||||||
@@ -385,17 +388,23 @@ def compare_same_cidr_ip(x, y):
|
|||||||
|
|
||||||
def sort_ip_ranges_with_cidr(ip_ranges, net_cidr=None):
|
def sort_ip_ranges_with_cidr(ip_ranges, net_cidr=None):
|
||||||
'''
|
'''
|
||||||
ip_ranges=[('12.18.1.5', '12.18.1.6', '12.18.1.1/24', '12.18.1.5'),
|
ip_ranges=[(start,end,cidr,gateway),
|
||||||
|
('12.18.1.5', '12.18.1.6', '12.18.1.1/24', '12.18.1.5'),
|
||||||
('12.18.1.15', '12.18.1.16', '12.18.1.1/24', '12.18.1.5'),
|
('12.18.1.15', '12.18.1.16', '12.18.1.1/24', '12.18.1.5'),
|
||||||
('13.18.1.5', '13.18.1.5', '13.18.1.1/24', '13.18.1.5'),
|
('13.18.1.5', '13.18.1.5', '13.18.1.1/24', '13.18.1.5'),
|
||||||
('2.1.1.12', '2.1.1.12', '2.1.1.1/24', '2.1.1.1'),
|
('2.1.1.12', '2.1.1.12', '2.1.1.1/24', '2.1.1.1'),
|
||||||
('2.1.1.17', '2.1.1.32', '2.1.1.1/24', '2.1.1.1'),
|
('2.1.1.17', '2.1.1.32', '2.1.1.1/24', '2.1.1.1'),
|
||||||
('9.18.1.1', '9.18.1.2', '9.18.1.1/24', '9.18.1.1')]
|
('9.18.1.1', '9.18.1.2', '9.18.1.1/24', '9.18.1.1'),
|
||||||
|
('', '', '9.8.1.1/24', '9.8.1.1'),
|
||||||
|
('9.18.1.1', '9.18.1.2', '', ''),]
|
||||||
'''
|
'''
|
||||||
tmp_ip_ranges = copy.deepcopy(ip_ranges)
|
tmp_ip_ranges = copy.deepcopy(ip_ranges)
|
||||||
convert_ip_ranges=[]
|
convert_ip_ranges=[]
|
||||||
for ip_range in tmp_ip_ranges:
|
for ip_range in tmp_ip_ranges:
|
||||||
|
if ip_range[0]:
|
||||||
int_start_ip = ip_into_int(ip_range[0])
|
int_start_ip = ip_into_int(ip_range[0])
|
||||||
|
else:
|
||||||
|
int_start_ip = 0
|
||||||
cidr = ip_range[2]
|
cidr = ip_range[2]
|
||||||
if cidr and cidr != 'None':
|
if cidr and cidr != 'None':
|
||||||
cidr_ip = cidr.split('/')[0]
|
cidr_ip = cidr.split('/')[0]
|
||||||
@@ -418,8 +427,8 @@ def according_to_cidr_distribution_ip(cluster_id, network_plane_name,
|
|||||||
ip_ranges_cidr = []
|
ip_ranges_cidr = []
|
||||||
distribution_ip = ""
|
distribution_ip = ""
|
||||||
|
|
||||||
sql_network_plane_info = "select networks.id,networks.cidr,\
|
sql_network_plane_info = "select networks.id,cidr,network_type,\
|
||||||
networks.network_type,networks.segmentation_type from networks \
|
segmentation_type,gateway from networks \
|
||||||
where networks.name='" + network_plane_name + \
|
where networks.name='" + network_plane_name + \
|
||||||
"' and networks.cluster_id='" + cluster_id + \
|
"' and networks.cluster_id='" + cluster_id + \
|
||||||
"' and networks.deleted=0"
|
"' and networks.deleted=0"
|
||||||
@@ -429,6 +438,7 @@ def according_to_cidr_distribution_ip(cluster_id, network_plane_name,
|
|||||||
network_cidr = query_network_plane_info.values()[1]
|
network_cidr = query_network_plane_info.values()[1]
|
||||||
network_type = query_network_plane_info.values()[2]
|
network_type = query_network_plane_info.values()[2]
|
||||||
segmentation_type = query_network_plane_info.values()[3]
|
segmentation_type = query_network_plane_info.values()[3]
|
||||||
|
network_gw = query_network_plane_info.values()[4]
|
||||||
if network_type not in ['EXTERNAL']:
|
if network_type not in ['EXTERNAL']:
|
||||||
if network_type == 'DATAPLANE' and segmentation_type == 'vlan':
|
if network_type == 'DATAPLANE' and segmentation_type == 'vlan':
|
||||||
return distribution_ip
|
return distribution_ip
|
||||||
@@ -436,15 +446,50 @@ def according_to_cidr_distribution_ip(cluster_id, network_plane_name,
|
|||||||
cluster_id, network_plane_name, session, exclude_ips)
|
cluster_id, network_plane_name, session, exclude_ips)
|
||||||
sql_ip_ranges = "select ip_ranges.start,end,cidr,gateway from \
|
sql_ip_ranges = "select ip_ranges.start,end,cidr,gateway from \
|
||||||
ip_ranges where network_id='" + network_id + \
|
ip_ranges where network_id='" + network_id + \
|
||||||
"' and ip_ranges.deleted=0 and start"
|
"' and ip_ranges.deleted=0"
|
||||||
query_ip_ranges = session.execute(sql_ip_ranges).fetchall()
|
query_ip_ranges = session.execute(sql_ip_ranges).fetchall()
|
||||||
LOG.info('query_ip_ranges: %s ' % query_ip_ranges)
|
LOG.info('query_ip_ranges: %s ' % query_ip_ranges)
|
||||||
|
|
||||||
#query_ip_ranges = sorted(query_ip_ranges, cmp=compare_same_cidr_ip)
|
used_cidrs = []
|
||||||
LOG.info('sorted query_ip_ranges: %s ' % query_ip_ranges)
|
all_full_ip_ranges = []
|
||||||
if query_ip_ranges:
|
if query_ip_ranges:
|
||||||
|
for ip_range in query_ip_ranges:
|
||||||
|
tmp_full_ip_ranges = []
|
||||||
|
if ip_range[0] and not ip_range[2]:
|
||||||
|
# with start_ip, without ip_range_cidr
|
||||||
|
if is_in_cidr_range(ip_range[0], network_cidr):
|
||||||
|
tmp_full_ip_ranges = \
|
||||||
|
[ip_range[0], ip_range[1], network_cidr, '']
|
||||||
|
used_cidrs.append(network_cidr)
|
||||||
|
else:
|
||||||
|
msg = 'Ip range: %s maybe invalid data.' % ip_range
|
||||||
|
LOG.error(msg)
|
||||||
|
elif ip_range[0] and ip_range[2]:
|
||||||
|
# with start_ip and ip_range_cidr
|
||||||
|
tmp_full_ip_ranges = \
|
||||||
|
[ip_range[0], ip_range[1], ip_range[2], ip_range[3]]
|
||||||
|
used_cidrs.append(ip_range[2])
|
||||||
|
elif not ip_range[0] and ip_range[2]:
|
||||||
|
# without start_ip, with ip_range_cidr
|
||||||
|
tmp_cidr_ranges = cidr_convert_ip_ranges(ip_range[2])
|
||||||
|
tmp_full_ip_ranges = [tmp_cidr_ranges[0],
|
||||||
|
tmp_cidr_ranges[1], ip_range[2], ip_range[3]]
|
||||||
|
used_cidrs.append(ip_range[2])
|
||||||
|
all_full_ip_ranges.append(tmp_full_ip_ranges)
|
||||||
|
if network_cidr and network_cidr not in used_cidrs:
|
||||||
|
tmp_gw = ''
|
||||||
|
if network_type == 'DATAPLANE':
|
||||||
|
tmp_gw = network_gw
|
||||||
|
tmp_cidr_ranges = cidr_convert_ip_ranges(network_cidr)
|
||||||
|
tmp_full_ip_ranges = [tmp_cidr_ranges[0], tmp_cidr_ranges[1],
|
||||||
|
network_cidr, tmp_gw]
|
||||||
|
used_cidrs.append(network_cidr)
|
||||||
|
all_full_ip_ranges.append(tmp_full_ip_ranges)
|
||||||
|
LOG.info("network_type: %s, all_full_ip_ranges: %s" % (network_type,
|
||||||
|
all_full_ip_ranges))
|
||||||
query_ip_ranges = \
|
query_ip_ranges = \
|
||||||
sort_ip_ranges_with_cidr(query_ip_ranges, network_cidr)
|
sort_ip_ranges_with_cidr(all_full_ip_ranges, network_cidr)
|
||||||
|
|
||||||
for ip_ranges_one in query_ip_ranges:
|
for ip_ranges_one in query_ip_ranges:
|
||||||
check_ip_exist_list = \
|
check_ip_exist_list = \
|
||||||
check_ip_ranges(ip_ranges_one, available_ip_list)
|
check_ip_ranges(ip_ranges_one, available_ip_list)
|
||||||
@@ -455,23 +500,6 @@ def according_to_cidr_distribution_ip(cluster_id, network_plane_name,
|
|||||||
ip ranges is already insufficient."
|
ip ranges is already insufficient."
|
||||||
LOG.error(msg)
|
LOG.error(msg)
|
||||||
raise exception.Forbidden(msg)
|
raise exception.Forbidden(msg)
|
||||||
else:
|
|
||||||
ip_ranges_cidr = cidr_convert_ip_ranges(network_cidr)
|
|
||||||
ip_min_inter = ip_into_int(ip_ranges_cidr[0])
|
|
||||||
ip_max_inter = ip_into_int(ip_ranges_cidr[1])
|
|
||||||
while True:
|
|
||||||
distribution_ip = inter_into_ip(ip_min_inter + 1)
|
|
||||||
if distribution_ip not in available_ip_list:
|
|
||||||
distribution_ip_inter = ip_into_int(distribution_ip)
|
|
||||||
if distribution_ip_inter < ip_max_inter:
|
|
||||||
break
|
|
||||||
else:
|
|
||||||
msg = "Error:The IP address assigned by \
|
|
||||||
CIDR is already insufficient."
|
|
||||||
LOG.error(msg)
|
|
||||||
raise exception.Forbidden(msg)
|
|
||||||
else:
|
|
||||||
ip_min_inter = ip_min_inter + 1
|
|
||||||
return distribution_ip
|
return distribution_ip
|
||||||
|
|
||||||
|
|
||||||
@@ -4319,7 +4347,7 @@ def get_network_ip_range(context, network_id):
|
|||||||
with session.begin():
|
with session.begin():
|
||||||
sql_ip_ranges="select ip_ranges.start,end,cidr,gateway from ip_ranges \
|
sql_ip_ranges="select ip_ranges.start,end,cidr,gateway from ip_ranges \
|
||||||
where ip_ranges.network_id='"+network_id+"' and \
|
where ip_ranges.network_id='"+network_id+"' and \
|
||||||
ip_ranges.deleted=0 and start order by ip_ranges.start"
|
ip_ranges.deleted=0;"
|
||||||
ip_ranges = session.execute(sql_ip_ranges).fetchall()
|
ip_ranges = session.execute(sql_ip_ranges).fetchall()
|
||||||
#ip_ranges_sorted = sorted(ip_ranges, cmp=compare_same_cidr_ip)
|
#ip_ranges_sorted = sorted(ip_ranges, cmp=compare_same_cidr_ip)
|
||||||
ip_ranges_sorted=sort_ip_ranges_with_cidr(ip_ranges)
|
ip_ranges_sorted=sort_ip_ranges_with_cidr(ip_ranges)
|
||||||
|
|||||||
@@ -670,7 +670,8 @@ class TestSqlalchemyApi(test.TestCase):
|
|||||||
session = api.get_session()
|
session = api.get_session()
|
||||||
distribution_ip1 = api.according_to_cidr_distribution_ip(
|
distribution_ip1 = api.according_to_cidr_distribution_ip(
|
||||||
cluster_id, network_plane_name, session)
|
cluster_id, network_plane_name, session)
|
||||||
self.assertEqual('112.18.1.2', distribution_ip1)
|
# gateway = '112.18.1.2'
|
||||||
|
self.assertEqual('112.18.1.3', distribution_ip1)
|
||||||
|
|
||||||
def test_get_network_ip_range(self):
|
def test_get_network_ip_range(self):
|
||||||
network_values = {
|
network_values = {
|
||||||
@@ -815,3 +816,127 @@ class TestSqlalchemyApi(test.TestCase):
|
|||||||
ret = api.host_interfaces_get_all(self.req.context)
|
ret = api.host_interfaces_get_all(self.req.context)
|
||||||
self.assertEqual(ret[0]["host_id"], host_id)
|
self.assertEqual(ret[0]["host_id"], host_id)
|
||||||
api.host_destroy(self.req.context, host_id)
|
api.host_destroy(self.req.context, host_id)
|
||||||
|
|
||||||
|
def test_according_to_cidr_distribution_ip_without_cidr(self):
|
||||||
|
ip_ranges = [{'start': '112.18.1.2', 'end': '112.18.1.5', }, ]
|
||||||
|
network_values = {
|
||||||
|
'cluster_id': '737adb89-ee6f-4642-8196-9ee6926fbe50',
|
||||||
|
'cidr': '112.18.1.2/25',
|
||||||
|
'gateway': '112.18.1.1',
|
||||||
|
'ip_ranges': str(ip_ranges),
|
||||||
|
'name': 'physnet1',
|
||||||
|
'segmentation_type': 'vxlan',
|
||||||
|
'physnet_name': 'physnet_enp2s0',
|
||||||
|
'type': 'default',
|
||||||
|
'network_type': 'DATAPLANE',
|
||||||
|
}
|
||||||
|
network_info = api.network_add(self.req.context, network_values)
|
||||||
|
network_id = network_info.__dict__['id']
|
||||||
|
|
||||||
|
cluster_id = '737adb89-ee6f-4642-8196-9ee6926fbe50'
|
||||||
|
network_plane_name = 'physnet1'
|
||||||
|
session = api.get_session()
|
||||||
|
distribution_ip1 = api.according_to_cidr_distribution_ip(
|
||||||
|
cluster_id, network_plane_name, session)
|
||||||
|
self.assertEqual('112.18.1.2', distribution_ip1)
|
||||||
|
|
||||||
|
def test_according_to_cidr_distribution_ip_without_ip(self):
|
||||||
|
network_values = {
|
||||||
|
'cluster_id': '737adb89-ee6f-4642-8196-9ee6926fbe50',
|
||||||
|
'cidr': None,
|
||||||
|
'gateway': None,
|
||||||
|
'name': 'physnet1',
|
||||||
|
'segmentation_type': 'vxlan',
|
||||||
|
'physnet_name': 'physnet_enp2s0',
|
||||||
|
'type': 'default',
|
||||||
|
'network_type': 'DATAPLANE',
|
||||||
|
}
|
||||||
|
network_info = api.network_add(self.req.context, network_values)
|
||||||
|
network_id = network_info.__dict__['id']
|
||||||
|
|
||||||
|
cluster_id = '737adb89-ee6f-4642-8196-9ee6926fbe50'
|
||||||
|
network_plane_name = 'physnet1'
|
||||||
|
session = api.get_session()
|
||||||
|
self.assertRaises(exception.Forbidden,
|
||||||
|
api.according_to_cidr_distribution_ip, cluster_id,
|
||||||
|
network_plane_name, session)
|
||||||
|
|
||||||
|
def test_according_to_cidr_distribution_ip_from_net_cidr(self):
|
||||||
|
network_values = {
|
||||||
|
'cluster_id': '737adb89-ee6f-4642-8196-9ee6926fbe50',
|
||||||
|
'cidr': '112.18.1.2/25',
|
||||||
|
'gateway': '112.18.1.1',
|
||||||
|
'name': 'physnet1',
|
||||||
|
'segmentation_type': 'vxlan',
|
||||||
|
'physnet_name': 'physnet_enp2s0',
|
||||||
|
'type': 'default',
|
||||||
|
'network_type': 'DATAPLANE',
|
||||||
|
}
|
||||||
|
network_info = api.network_add(self.req.context, network_values)
|
||||||
|
network_id = network_info.__dict__['id']
|
||||||
|
|
||||||
|
cluster_id = '737adb89-ee6f-4642-8196-9ee6926fbe50'
|
||||||
|
network_plane_name = 'physnet1'
|
||||||
|
session = api.get_session()
|
||||||
|
distribution_ip1 = api.according_to_cidr_distribution_ip(
|
||||||
|
cluster_id, network_plane_name, session)
|
||||||
|
self.assertEqual('112.18.1.2', distribution_ip1)
|
||||||
|
|
||||||
|
def test_according_to_cidr_distribution_ip_from_range_cidr(self):
|
||||||
|
ip_ranges = [
|
||||||
|
{'start': None,
|
||||||
|
'cidr': '112.18.1.1/24',
|
||||||
|
'end': None,
|
||||||
|
'gateway': '112.18.1.2'
|
||||||
|
}, ]
|
||||||
|
network_values = {
|
||||||
|
'cluster_id': '737adb89-ee6f-4642-8196-9ee6926fbe50',
|
||||||
|
'cidr': '112.18.1.2/25',
|
||||||
|
'gateway': '112.18.1.1',
|
||||||
|
'ip_ranges': str(ip_ranges),
|
||||||
|
'name': 'physnet1',
|
||||||
|
'segmentation_type': 'vxlan',
|
||||||
|
'physnet_name': 'physnet_enp2s0',
|
||||||
|
'type': 'default',
|
||||||
|
'network_type': 'DATAPLANE',
|
||||||
|
}
|
||||||
|
network_info = api.network_add(self.req.context, network_values)
|
||||||
|
network_id = network_info.__dict__['id']
|
||||||
|
|
||||||
|
cluster_id = '737adb89-ee6f-4642-8196-9ee6926fbe50'
|
||||||
|
network_plane_name = 'physnet1'
|
||||||
|
session = api.get_session()
|
||||||
|
distribution_ip1 = api.according_to_cidr_distribution_ip(
|
||||||
|
cluster_id, network_plane_name, session)
|
||||||
|
self.assertEqual('112.18.1.1', distribution_ip1)
|
||||||
|
|
||||||
|
def test_check_ip_ranges(self):
|
||||||
|
ip_ranges = ('12.1.1.10', '12.1.1.15', '12.1.1.1/24', '12.1.1.1')
|
||||||
|
check_result = api.check_ip_ranges(ip_ranges, [])
|
||||||
|
self.assertEqual('12.1.1.10', check_result[1])
|
||||||
|
|
||||||
|
def test_sort_ip_ranges_with_cidr(self):
|
||||||
|
ip_ranges = \
|
||||||
|
[('12.1.1.10', '12.1.1.10', '12.1.1.1/24', '12.1.1.1'),
|
||||||
|
('16.168.1.100', '16.168.1.100', '16.168.1.10/24', '16.168.1.1'),
|
||||||
|
('17.168.1.10', '17.168.1.10', '17.168.1.1/24', '17.168.1.1'),
|
||||||
|
('5.5.5.5', '5.5.5.5', '5.5.5.1/24', '5.5.5.1')]
|
||||||
|
sorted_ip_ranges = api.sort_ip_ranges_with_cidr(ip_ranges)
|
||||||
|
# print 'sorted_ip_ranges:', sorted_ip_ranges
|
||||||
|
self.assertEqual(('5.5.5.5', '5.5.5.5', '5.5.5.1/24', '5.5.5.1'),
|
||||||
|
sorted_ip_ranges[0])
|
||||||
|
self.assertEqual(('12.1.1.10', '12.1.1.10', '12.1.1.1/24', '12.1.1.1'),
|
||||||
|
sorted_ip_ranges[1])
|
||||||
|
|
||||||
|
def test_sort_ip_ranges_with_cidr_no_startip(self):
|
||||||
|
ip_ranges = \
|
||||||
|
[('12.1.1.10', '12.1.1.10', '12.1.1.1/24', '12.1.1.1'),
|
||||||
|
('16.168.1.100', '16.168.1.100', '16.168.1.10/24', '16.168.1.1'),
|
||||||
|
(None, None, '17.168.1.1/24', '17.168.1.1'),
|
||||||
|
('5.5.5.5', '5.5.5.5', '5.5.5.1/24', '5.5.5.1')]
|
||||||
|
sorted_ip_ranges = api.sort_ip_ranges_with_cidr(ip_ranges)
|
||||||
|
# print 'sorted_ip_ranges:', sorted_ip_ranges
|
||||||
|
self.assertEqual(('5.5.5.5', '5.5.5.5', '5.5.5.1/24', '5.5.5.1'),
|
||||||
|
sorted_ip_ranges[0])
|
||||||
|
self.assertEqual(('12.1.1.10', '12.1.1.10', '12.1.1.1/24', '12.1.1.1'),
|
||||||
|
sorted_ip_ranges[1])
|
||||||
|
|||||||
Reference in New Issue
Block a user