38
38
from networkapi .exception import EnvironmentVipError
39
39
from networkapi .exception import EnvironmentVipNotFoundError
40
40
from networkapi .exception import InvalidValueError
41
+ from networkapi .api_rest .exceptions import ValidationAPIException
41
42
from networkapi .filter .models import CannotDissociateFilterError
42
43
from networkapi .filter .models import Filter
43
44
from networkapi .filter .models import FilterNotFoundError
55
56
56
57
from netaddr import IPNetwork as NETADDR
57
58
59
+
58
60
log = logging .getLogger (__name__ )
59
61
60
62
@@ -1424,14 +1426,11 @@ def create_v3(self, env_map):
1424
1426
1425
1427
configs = env_map .get ('configs' , [])
1426
1428
1427
- for conf in configs :
1428
- conf ["environment" ] = self .id
1429
-
1430
1429
# # save network on IPConfig tables
1431
1430
# configs = self.create_configs(configs, self.id)
1432
1431
1433
1432
# save network on CIDR tables
1434
- self .create_cidr (configs )
1433
+ self .create_cidr (configs = configs , env_id = self . id )
1435
1434
1436
1435
delete_cached_searches_list (ENVIRONMENT_CACHE_ENTRY )
1437
1436
@@ -1512,32 +1511,32 @@ def update_v3(self, env_map):
1512
1511
delete_cached_searches_list (ENVIRONMENT_CACHE_ENTRY )
1513
1512
destroy_lock (locks_list )
1514
1513
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)
1541
1540
1542
1541
def check_cidr (self , env_id = None , configs = []):
1543
1542
log .info ("check_cidr" )
@@ -1625,29 +1624,29 @@ def validate_v3(self):
1625
1624
else :
1626
1625
raise AmbienteDuplicatedError (None , u'Duplicate Environment.' )
1627
1626
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)
1651
1650
1652
1651
def update_cidr (self , configs ):
1653
1652
log .debug ("Update config on cidr tables" )
@@ -1657,42 +1656,45 @@ def update_cidr(self, configs):
1657
1656
for config in configs :
1658
1657
update_cidr (config )
1659
1658
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 " )
1679
1678
1680
1679
from networkapi .api_environment .facade import post_cidr
1680
+ from networkapi .api_environment .facade import post_cidr_auto
1681
1681
1682
1682
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)
1696
1698
1697
1699
def delete_cidr (self , configs_ids = []):
1698
1700
"""
@@ -1956,70 +1958,91 @@ def check_duplicated_cidr(self, environment, network):
1956
1958
1957
1959
return environments
1958
1960
1959
- def searchNextAvailableCIDR (self , subnets ):
1961
+ def searchNextAvailableCIDR (self , subnets , network_mask = None ):
1960
1962
"""
1961
1963
Method that search next availacle cidr.
1962
1964
:param subnets: all subnets of environment.
1963
1965
:return: available subnet
1964
1966
"""
1965
1967
log .debug ("searchNextAvailableCIDR" )
1966
1968
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
+
1973
1977
return ""
1974
1978
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" )
1982
1982
1983
1983
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 ]
1985
1985
return str (subnet )
1986
1986
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
+
1988
2000
if ipaddr .IPNetwork (subnet ).overlaps (ipaddr .IPNetwork (network .network )):
1989
2001
return str (subnet )
1990
2002
1991
- return self .searchNextAvailableCIDR (subnets )
2003
+ return self .searchNextAvailableCIDR (subnets , network_mask )
1992
2004
1993
- def checkAvailableCIDR (self , environment_id , ip_version = None ):
2005
+ def checkAvailableCIDR (self , environment_id , ip_version = None , network_mask = None ):
1994
2006
""""""
2007
+ log .debug ("checkAvailableCIDR" )
1995
2008
1996
2009
environment = Ambiente .get_by_pk (environment_id )
1997
2010
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 ,
1999
2018
ip_version = ip_version )
2000
2019
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" )
2003
2023
2004
2024
for cidr in env_father_cidrs :
2025
+ mask = cidr .subnet_mask if not network_mask else network_mask
2026
+
2005
2027
env_subnets = EnvCIDR .objects .filter (
2006
2028
network_first_ip__gte = cidr .network_first_ip ,
2007
2029
network_last_ip__lte = cidr .network_last_ip ,
2008
2030
id_env__father_environment__id = cidr .id_env .id ).exclude (
2009
2031
id = cidr .id ).order_by (
2010
2032
"network_first_ip" )
2011
- log .debug ("CIDR: %s" % cidr .network )
2012
- log .debug ("Number of Subnets: %s" % len (env_subnets ))
2013
2033
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
2021
2041
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 ))
2023
2046
2024
2047
def post (self , env_cidr ):
2025
2048
@@ -2043,13 +2066,10 @@ def post(self, env_cidr):
2043
2066
2044
2067
environment = Ambiente ().get_by_pk (int (env_cidr .get ('environment' )))
2045
2068
self .id_env = environment
2046
-
2047
2069
self .id_network_type = TipoRede ().get_by_pk (int (env_cidr .get ('network_type' )))
2048
-
2049
2070
self .save ()
2050
2071
except Exception as e :
2051
2072
raise CIDRErrorV3 (e )
2052
-
2053
2073
return self .id
2054
2074
2055
2075
def put (self , env_cidr ):
0 commit comments