Skip to content

Commit 24d7c23

Browse files
committed
Merge branch 'master' of github.com:globocom/GloboNetworkAPI into split-deploy-foreman
remote is ahead of local
2 parents 819fc0a + ae0cc0b commit 24d7c23

File tree

22 files changed

+1601
-218
lines changed

22 files changed

+1601
-218
lines changed

networkapi/ambiente/models.py

Lines changed: 139 additions & 119 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
from networkapi.exception import EnvironmentVipError
3939
from networkapi.exception import EnvironmentVipNotFoundError
4040
from networkapi.exception import InvalidValueError
41+
from networkapi.api_rest.exceptions import ValidationAPIException
4142
from networkapi.filter.models import CannotDissociateFilterError
4243
from networkapi.filter.models import Filter
4344
from networkapi.filter.models import FilterNotFoundError
@@ -55,6 +56,7 @@
5556

5657
from netaddr import IPNetwork as NETADDR
5758

59+
5860
log = logging.getLogger(__name__)
5961

6062

@@ -1424,14 +1426,11 @@ def create_v3(self, env_map):
14241426

14251427
configs = env_map.get('configs', [])
14261428

1427-
for conf in configs:
1428-
conf["environment"] = self.id
1429-
14301429
# # save network on IPConfig tables
14311430
# configs = self.create_configs(configs, self.id)
14321431

14331432
# save network on CIDR tables
1434-
self.create_cidr(configs)
1433+
self.create_cidr(configs=configs, env_id=self.id)
14351434

14361435
delete_cached_searches_list(ENVIRONMENT_CACHE_ENTRY)
14371436

@@ -1512,32 +1511,32 @@ def update_v3(self, env_map):
15121511
delete_cached_searches_list(ENVIRONMENT_CACHE_ENTRY)
15131512
destroy_lock(locks_list)
15141513

1515-
def check_config(self, env_id=None, configs=[]):
1516-
1517-
ips_by_env = IPConfig.get_by_environment(None, env_id)
1518-
ids_conf_current = [ip_by_env.id for ip_by_env in ips_by_env]
1519-
1520-
# Configs with ids
1521-
ids_conf_receive = [cfg.get('id') for cfg in configs
1522-
if cfg.get('id')]
1523-
1524-
# Configs to update: configs with id
1525-
cfg_upt = [cfg for cfg in configs if cfg.get('id') and
1526-
cfg.get('id') in ids_conf_current]
1527-
1528-
# Configs to create: configs without id
1529-
cfg_ins = [cfg for cfg in configs if not cfg.get('id')]
1530-
1531-
# Configs to delete: configs not received
1532-
cfg_del = [id_conf for id_conf in ids_conf_current
1533-
if id_conf not in ids_conf_receive]
1534-
1535-
# Updates configs
1536-
self.update_configs(cfg_upt, self.id)
1537-
# Creates configs
1538-
self.create_configs(cfg_ins, self.id)
1539-
# Deletes configs
1540-
self.delete_configs(cfg_del, self.id)
1514+
# def check_config(self, env_id=None, configs=[]):
1515+
#
1516+
# ips_by_env = IPConfig.get_by_environment(None, env_id)
1517+
# ids_conf_current = [ip_by_env.id for ip_by_env in ips_by_env]
1518+
#
1519+
# # Configs with ids
1520+
# ids_conf_receive = [cfg.get('id') for cfg in configs
1521+
# if cfg.get('id')]
1522+
#
1523+
# # Configs to update: configs with id
1524+
# cfg_upt = [cfg for cfg in configs if cfg.get('id') and
1525+
# cfg.get('id') in ids_conf_current]
1526+
#
1527+
# # Configs to create: configs without id
1528+
# cfg_ins = [cfg for cfg in configs if not cfg.get('id')]
1529+
#
1530+
# # Configs to delete: configs not received
1531+
# cfg_del = [id_conf for id_conf in ids_conf_current
1532+
# if id_conf not in ids_conf_receive]
1533+
#
1534+
# # Updates configs
1535+
# self.update_configs(cfg_upt, self.id)
1536+
# # Creates configs
1537+
# self.create_configs(cfg_ins, self.id)
1538+
# # Deletes configs
1539+
# self.delete_configs(cfg_del, self.id)
15411540

15421541
def check_cidr(self, env_id=None, configs=[]):
15431542
log.info("check_cidr")
@@ -1625,29 +1624,29 @@ def validate_v3(self):
16251624
else:
16261625
raise AmbienteDuplicatedError(None, u'Duplicate Environment.')
16271626

1628-
def update_configs(self, configs, env_id):
1629-
"""
1630-
Update configs of environment
1631-
1632-
:param configs: Configs of environment
1633-
:param env: Id of environment
1634-
"""
1635-
for config in configs:
1636-
try:
1637-
ip_config = IPConfig.objects.get(
1638-
id=config.get('id'),
1639-
configenvironment__environment=env_id
1640-
)
1641-
except ObjectDoesNotExist:
1642-
raise exceptions.ConfigIpDoesNotExistException()
1643-
1644-
ip_config.subnet = config.get('subnet')
1645-
ip_config.new_prefix = config.get('new_prefix')
1646-
ip_config.type = config.get('type')
1647-
ip_config.network_type_id = config.get('network_type')
1648-
1649-
ip_config.save()
1650-
delete_cached_searches_list(ENVIRONMENT_CACHE_ENTRY)
1627+
# def update_configs(self, configs, env_id):
1628+
# """
1629+
# Update configs of environment
1630+
#
1631+
# :param configs: Configs of environment
1632+
# :param env: Id of environment
1633+
# """
1634+
# for config in configs:
1635+
# try:
1636+
# ip_config = IPConfig.objects.get(
1637+
# id=config.get('id'),
1638+
# configenvironment__environment=env_id
1639+
# )
1640+
# except ObjectDoesNotExist:
1641+
# raise exceptions.ConfigIpDoesNotExistException()
1642+
#
1643+
# ip_config.subnet = config.get('subnet')
1644+
# ip_config.new_prefix = config.get('new_prefix')
1645+
# ip_config.type = config.get('type')
1646+
# ip_config.network_type_id = config.get('network_type')
1647+
#
1648+
# ip_config.save()
1649+
# delete_cached_searches_list(ENVIRONMENT_CACHE_ENTRY)
16511650

16521651
def update_cidr(self, configs):
16531652
log.debug("Update config on cidr tables")
@@ -1657,42 +1656,45 @@ def update_cidr(self, configs):
16571656
for config in configs:
16581657
update_cidr(config)
16591658

1660-
def create_configs(self, configs, env_id):
1661-
log.debug("Save config on ipconfig tables")
1662-
1663-
"""
1664-
Create configs of environment
1665-
1666-
:param configs: Configs of environment
1667-
:param env: Id of environment
1668-
"""
1669-
for config in configs:
1670-
config_id = IPConfig.create(env_id, config)
1671-
config['config_id'] = config_id.id
1672-
1673-
delete_cached_searches_list(ENVIRONMENT_CACHE_ENTRY)
1674-
1675-
return configs
1676-
1677-
def create_cidr(self, configs=None):
1678-
log.debug("Save config on cidr tables")
1659+
# def create_configs(self, configs, env_id):
1660+
# log.debug("Save config on ipconfig tables")
1661+
#
1662+
# """
1663+
# Create configs of environment
1664+
#
1665+
# :param configs: Configs of environment
1666+
# :param env: Id of environment
1667+
# """
1668+
# for config in configs:
1669+
# config_id = IPConfig.create(env_id, config)
1670+
# config['config_id'] = config_id.id
1671+
#
1672+
# delete_cached_searches_list(ENVIRONMENT_CACHE_ENTRY)
1673+
#
1674+
# return configs
1675+
1676+
def create_cidr(self, configs=None, env_id=None):
1677+
log.debug("create_cidr")
16791678

16801679
from networkapi.api_environment.facade import post_cidr
1680+
from networkapi.api_environment.facade import post_cidr_auto
16811681

16821682
for config in configs:
1683-
post_cidr(config)
1684-
1685-
def delete_configs(self, configs_ids, env_id):
1686-
"""
1687-
Delete configs of environment
1688-
1689-
:param configs_ids: Id of Configs of environment
1690-
:param env_id: Id of environment
1691-
"""
1692-
1693-
for config_id in configs_ids:
1694-
IPConfig.remove(None, None, env_id, config_id)
1695-
delete_cached_searches_list(ENVIRONMENT_CACHE_ENTRY)
1683+
if env_id:
1684+
config["environment"] = env_id
1685+
post_cidr(config) if config.get("network") else post_cidr_auto(config)
1686+
1687+
# def delete_configs(self, configs_ids, env_id):
1688+
# """
1689+
# Delete configs of environment
1690+
#
1691+
# :param configs_ids: Id of Configs of environment
1692+
# :param env_id: Id of environment
1693+
# """
1694+
#
1695+
# for config_id in configs_ids:
1696+
# IPConfig.remove(None, None, env_id, config_id)
1697+
# delete_cached_searches_list(ENVIRONMENT_CACHE_ENTRY)
16961698

16971699
def delete_cidr(self, configs_ids=[]):
16981700
"""
@@ -1956,70 +1958,91 @@ def check_duplicated_cidr(self, environment, network):
19561958

19571959
return environments
19581960

1959-
def searchNextAvailableCIDR(self, subnets):
1961+
def searchNextAvailableCIDR(self, subnets, network_mask=None):
19601962
"""
19611963
Method that search next availacle cidr.
19621964
:param subnets: all subnets of environment.
19631965
:return: available subnet
19641966
"""
19651967
log.debug("searchNextAvailableCIDR")
19661968

1967-
for idx, _ in enumerate(subnets):
1968-
if int(subnets[idx].network_last_ip) + 1 is not int(subnets[idx+1].network_first_ip):
1969-
subnet = subnets[idx].network
1970-
new_subnet = NETADDR(subnet).next()
1971-
if not ipaddr.IPNetwork(new_subnet).overlaps(ipaddr.IPNetwork(subnets[idx+1].network)):
1972-
return str(new_subnet)
1969+
for idx in range(len(subnets)-1):
1970+
step = int(subnets[idx+1].network_first_ip) - int(subnets[idx].network_last_ip) - 1
1971+
if step >= 2 ** (32-int(network_mask)):
1972+
subnet = NETADDR(str(NETADDR(subnets[idx].network).next().ip) + "/" + network_mask)
1973+
if subnet.ip == subnet.network and \
1974+
not ipaddr.IPNetwork(subnet).overlaps(ipaddr.IPNetwork(subnets[idx+1].network)):
1975+
return str(subnet)
1976+
19731977
return ""
19741978

1975-
def nextAvailableCIDR(self, subnets, network):
1976-
"""
1977-
Try to aloccate
1978-
:param subnets:
1979-
:param network:
1980-
:return:
1981-
"""
1979+
def nextAvailableCIDR(self, subnets, network, network_mask=None):
1980+
""""""
1981+
log.debug("nextAvailableCIDR")
19821982

19831983
if not subnets:
1984-
subnet = list(NETADDR(network.network).subnet(int(network.subnet_mask)))[0]
1984+
subnet = list(NETADDR(network.network).subnet(int(network_mask)))[0]
19851985
return str(subnet)
19861986

1987-
subnet = NETADDR(subnets.latest('id').network).next()
1987+
last_subnet = NETADDR(subnets.latest("network_last_ip").network)
1988+
log.debug("Last Subnet: %s" % last_subnet)
1989+
log.debug("Subnet mask: %s" % network_mask)
1990+
1991+
if int(network_mask) > last_subnet.prefixlen:
1992+
subnet = list(last_subnet.next().subnet(int(network_mask)))[0]
1993+
elif int(network_mask) == last_subnet.prefixlen:
1994+
subnet = last_subnet.next()
1995+
else:
1996+
subnet = NETADDR(str(last_subnet.next().ip) + "/" + network_mask)
1997+
if not subnet.ip == subnet.network:
1998+
subnet = subnet.next()
1999+
19882000
if ipaddr.IPNetwork(subnet).overlaps(ipaddr.IPNetwork(network.network)):
19892001
return str(subnet)
19902002

1991-
return self.searchNextAvailableCIDR(subnets)
2003+
return self.searchNextAvailableCIDR(subnets, network_mask)
19922004

1993-
def checkAvailableCIDR(self, environment_id, ip_version=None):
2005+
def checkAvailableCIDR(self, environment_id, ip_version=None, network_mask=None):
19942006
""""""
2007+
log.debug("checkAvailableCIDR")
19952008

19962009
environment = Ambiente.get_by_pk(environment_id)
19972010

1998-
env_father_cidrs = EnvCIDR.objects.filter(id_env=environment.father_environment.id,
2011+
try:
2012+
father_environment = environment.father_environment.id
2013+
except Exception as e:
2014+
raise ValidationAPIException(
2015+
"The environment doesn't have an Environment Father. Error: %s" % e)
2016+
2017+
env_father_cidrs = EnvCIDR.objects.filter(id_env=father_environment,
19992018
ip_version=ip_version)
20002019

2001-
msg = ""
2002-
next_available_cidr = ""
2020+
if not env_father_cidrs:
2021+
raise ValidationAPIException(
2022+
"The Environment Father doesnt have an allocated CIDR block")
20032023

20042024
for cidr in env_father_cidrs:
2025+
mask = cidr.subnet_mask if not network_mask else network_mask
2026+
20052027
env_subnets = EnvCIDR.objects.filter(
20062028
network_first_ip__gte=cidr.network_first_ip,
20072029
network_last_ip__lte=cidr.network_last_ip,
20082030
id_env__father_environment__id=cidr.id_env.id).exclude(
20092031
id=cidr.id).order_by(
20102032
"network_first_ip")
2011-
log.debug("CIDR: %s" % cidr.network)
2012-
log.debug("Number of Subnets: %s" % len(env_subnets))
20132033

2014-
if len(env_subnets) == 2**(int(cidr.subnet_mask) - int(cidr.network_mask)):
2015-
msg += "There's no available network in this environment. CIDR: %s" % cidr.network
2016-
log.info(msg)
2017-
else:
2018-
next_available_cidr = self.nextAvailableCIDR(env_subnets, cidr)
2019-
msg = "Next available subnet: %s." % next_available_cidr
2020-
log.info(msg)
2034+
log.debug("Father`s CIDR: %s" % cidr.network)
2035+
log.debug("Subnets: %s" % len(env_subnets))
2036+
2037+
next_available_cidr = self.nextAvailableCIDR(env_subnets, cidr, mask)
2038+
if next_available_cidr:
2039+
msg = "Subnet available: %s." % next_available_cidr
2040+
return next_available_cidr, msg
20212041

2022-
return next_available_cidr, msg
2042+
raise CIDRErrorV3("Out of address space. It was not possible to allocate the subnet with "
2043+
"prefix length %s for the environment %s. "
2044+
"Please register a new CIDR on the father environment."
2045+
% (network_mask, environment.name))
20232046

20242047
def post(self, env_cidr):
20252048

@@ -2043,13 +2066,10 @@ def post(self, env_cidr):
20432066

20442067
environment = Ambiente().get_by_pk(int(env_cidr.get('environment')))
20452068
self.id_env = environment
2046-
20472069
self.id_network_type = TipoRede().get_by_pk(int(env_cidr.get('network_type')))
2048-
20492070
self.save()
20502071
except Exception as e:
20512072
raise CIDRErrorV3(e)
2052-
20532073
return self.id
20542074

20552075
def put(self, env_cidr):

0 commit comments

Comments
 (0)