Skip to content

Commit

Permalink
Merge pull request #1 from treviza153/master
Browse files Browse the repository at this point in the history
Merge Branchs
  • Loading branch information
treviza153 committed Oct 31, 2019
2 parents 199e487 + 50444f2 commit 3abf474
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 1 deletion.
4 changes: 3 additions & 1 deletion networkapi/error_message_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@
290: u'Option vip is already associated with the environment vip',
291: u'Option vip is not associated with the environment vip',
292: u'IPv6 %s not registered for the environment %s',
293: u'Dont Deallocate all relationships between Vlan be because is active.',
293: u'Vlan is currently in active/deployed state. It needs to be undeployed before deletion.',
294: u'Invalid Environment Configuration or not registered',
295: u'Unavailable address to create a NetworkIPv4',
296: u'Unavailable address to create a NetworkIPv6',
Expand Down Expand Up @@ -327,6 +327,8 @@
407: u'Erro ao remover a variável.',
410: u'Channel não pode ser deletado. %s',
413: u'Interface não pode ser desconectada. Remova o Port Channel primeiro.',
414: u'A rede a ser cadastrada não pertence a rede do ambiente. Cadastre o range desejado no ambiente',
415: u'O ambiente não consta com rede cadastrada. É necessário cadastrar uma rede ao ambiente.'
}


Expand Down
26 changes: 26 additions & 0 deletions networkapi/ip/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,32 @@ def __str__(self):
return msg.encode('utf-8', 'replace')


class NetworkSubnetRange(NetworkIPvXError):

"""Exception for a network that does not be a subnet of de environment network."""

def __init__(self, cause, message=None):
self.cause = cause
self.message = message

def __str__(self):
msg = u'Caused by: %s, Message: %s' % (self.cause, self.message)
return msg.encode('utf-8', 'replace')


class NetworkEnvironmentError(NetworkIPvXError):

"""Exception for a environment that does not have a network."""

def __init__(self, cause, message=None):
self.cause = cause
self.message = message

def __str__(self):
msg = u'Caused by: %s, Message: %s' % (self.cause, self.message)
return msg.encode('utf-8', 'replace')


class IpErrorV3(Exception):

"""Representa um erro ocorrido durante acesso à tabelas relacionadas com IP."""
Expand Down
47 changes: 47 additions & 0 deletions networkapi/ip/resource/NetworkAddResource.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

from networkapi.admin_permission import AdminPermission
from networkapi.ambiente.models import ConfigEnvironmentInvalidError
from networkapi.ambiente.models import ConfigEnvironment
from networkapi.ambiente.models import EnvironmentVip
from networkapi.ambiente.models import IP_VERSION
from networkapi.auth import has_perm
Expand All @@ -43,6 +44,8 @@
from networkapi.ip.models import NetworkIPv4
from networkapi.ip.models import NetworkIPv4AddressNotAvailableError
from networkapi.ip.models import NetworkIPv4Error
from networkapi.ip.models import NetworkEnvironmentError
from networkapi.ip.models import NetworkSubnetRange
from networkapi.ip.models import NetworkIPv6
from networkapi.ip.models import NetworkIPv6AddressNotAvailableError
from networkapi.ip.models import NetworkIPv6Error
Expand Down Expand Up @@ -172,6 +175,50 @@ def handle_post(self, request, user, *args, **kwargs):
u'Network cannot be allocated. %s already in use '
u'in this environment VIP.' % network_aux)

# Check if the new network is in the range of the Environment Network
try:
vlan = Vlan().get_by_pk(id_vlan)
vlan_env_id = vlan.ambiente

config_env = ConfigEnvironment()
environment_conf = config_env.get_by_environment(vlan_env_id)

for x in environment_conf:

ipconfig = x.ip_config
subnet = ipconfig.subnet

try:
if subnet:
env_net = IPNetwork(subnet)

try:
if net in env_net:
self.log.debug('Network "%s" can be allocated because is in the '
'environment network(%s) subnets.' % (net, subnet))

else:
raise NetworkSubnetRange(None, 'A rede a ser cadastrada (%s) não pertence às '
'subredes do ambiente (rede ambiente: %s). '
'Cadastre o range desejado no '
'ambiente.' % (net, subnet))

except NetworkSubnetRange:
self.log.error('Network "%s" can not be allocated because is not in the '
'environment network(%s) subnets.' % (net, subnet))
return self.response_error(414)

else:
raise NetworkEnvironmentError(None, 'O ambiente não consta com rede cadastrada. '
'É necessário cadastrar uma rede ao ambiente.')

except NetworkEnvironmentError:
self.log.error('The environment does not have a registered network')
return self.response_error(415)

except Exception as ERROR:
self.log.error(ERROR)

# # Filter case 1 - Adding new network with same ip range to another network on other environment ##
# Get environments with networks with the same ip range
nets = NetworkIPv4.objects.filter(oct1=expl[0], oct2=expl[1], oct3=expl[2],
Expand Down

0 comments on commit 3abf474

Please sign in to comment.