本文整理汇总了Python中networkapi.ip.models.Ip类的典型用法代码示例。如果您正苦于以下问题:Python Ip类的具体用法?Python Ip怎么用?Python Ip使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Ip类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: handle_put
def handle_put(self, request, user, *args, **kwargs):
'''Trata as requisições de PUT para inserir o relacionamento entre IP e Equipamento.
URL: ip/<id_ip>/equipamento/<id_equipamento>/$
'''
try:
ip_id = kwargs.get('id_ip')
equip_id = kwargs.get('id_equipamento')
if not is_valid_int_greater_zero_param(ip_id):
self.log.error(
u'The ip_id parameter is not a valid value: %s.', ip_id)
raise InvalidValueError(None, 'ip_id', ip_id)
if not is_valid_int_greater_zero_param(equip_id):
self.log.error(
u'The equip_id parameter is not a valid value: %s.', equip_id)
raise InvalidValueError(None, 'equip_id', equip_id)
Ip.get_by_pk(ip_id)
with distributedlock(LOCK_IP_EQUIPMENT % (ip_id, equip_id)):
ip_equipment = insert_ip_equipment(ip_id, equip_id, user)
ipequipamento_map = dict()
ipequipamento_map['id'] = ip_equipment.id
map = dict()
map['ip_equipamento'] = ipequipamento_map
return self.response(dumps_networkapi(map))
except InvalidValueError, e:
return self.response_error(269, e.param, e.value)
示例2: handle_delete
def handle_delete(self, request, user, *args, **kwargs):
"""Treat DELETE requests to remove IP and Equipment relationship.
URL: ip/<id_ip>/equipamento/<id_equipamento>/$
"""
try:
ip_id = kwargs.get('id_ip')
equip_id = kwargs.get('id_equipamento')
if not is_valid_int_greater_zero_param(ip_id):
self.log.error(
u'The ip_id parameter is not a valid value: %s.', ip_id)
raise InvalidValueError(None, 'ip_id', ip_id)
if not is_valid_int_greater_zero_param(equip_id):
self.log.error(
u'The equip_id parameter is not a valid value: %s.', equip_id)
raise InvalidValueError(None, 'equip_id', equip_id)
Ip.get_by_pk(ip_id)
Equipamento.get_by_pk(equip_id)
with distributedlock(LOCK_IP_EQUIPMENT % (ip_id, equip_id)):
ipv4 = Ip.get_by_pk(ip_id)
equipament = Equipamento.get_by_pk(equip_id)
# Delete vlan's cache
destroy_cache_function([ipv4])
# delete equipment's cache
destroy_cache_function([equip_id], True)
server_pool_member_list = ServerPoolMember.objects.filter(
ip=ipv4)
if server_pool_member_list.count() != 0:
# IP associated with Server Pool
server_pool_name_list = set()
for member in server_pool_member_list:
item = '{}: {}'.format(
member.server_pool.id, member.server_pool.identifier)
server_pool_name_list.add(item)
server_pool_name_list = list(server_pool_name_list)
server_pool_identifiers = ', '.join(server_pool_name_list)
raise IpCantRemoveFromServerPool({'ip': mount_ipv4_string(ipv4), 'equip_name': equipament.nome, 'server_pool_identifiers': server_pool_identifiers},
'Ipv4 não pode ser disassociado do equipamento %s porque ele está sendo utilizando nos Server Pools (id:identifier) %s' % (equipament.nome, server_pool_identifiers))
remove_ip_equipment(ip_id, equip_id, user)
return self.response(dumps_networkapi({}))
except IpCantRemoveFromServerPool, e:
return self.response_error(385, e.cause.get('ip'), e.cause.get('equip_name'), e.cause.get('server_pool_identifiers'))
示例3: create_ipv4
def create_ipv4(ipv4, user=None):
"""Creates a Ipv4."""
try:
ipv4_obj = Ip()
ipv4_obj.create_v3(ipv4)
except ObjectDoesNotExistException, e:
raise ObjectDoesNotExistException(e.detail)
示例4: get_ipv4_by_id
def get_ipv4_by_id(ip_id):
"""Get Ipv4."""
try:
network = Ip.get_by_pk(ip_id)
except IpNotFoundError, e:
raise ObjectDoesNotExistException(e.message)
示例5: handle_get
def handle_get(self, request, user, *args, **kwargs):
"""Handles GET requests to get a ipv4 by id.
URLs: ip/get-ipv4/id_ip
"""
try:
# Commons Validations
# User permission
if not has_perm(user, AdminPermission.IPS, AdminPermission.READ_OPERATION):
self.log.error(
u'User does not have permission to perform the operation.')
return self.not_authorized()
# Business Validations
# Valid id access
id_ip = kwargs.get('id_ip')
if not is_valid_int_greater_zero_param(id_ip):
raise InvalidValueError(None, 'id_ip', id_ip)
# Business Rules
ip = Ip()
ip = ip.get_by_pk(id_ip)
ip_map = dict()
equip_list = []
for ipequip in ip.ipequipamento_set.all():
equip_list.append(ipequip.equipamento.nome)
# IP map
ip_map = model_to_dict(ip)
ip_map['equipamentos'] = equip_list if len(
equip_list) > 0 else None
# Return XML
return self.response(dumps_networkapi({'ipv4': ip_map}))
except InvalidValueError, e:
self.log.error(
u'Parameter %s is invalid. Value: %s.', e.param, e.value)
return self.response_error(269, e.param, e.value)
示例6: get_ip_objects
def get_ip_objects(dic):
ip_object = None
ipv6_object = None
if len(dic['ip']) <= 15:
ip_object = Ip.get_by_pk(dic['id'])
else:
ipv6_object = Ipv6.get_by_pk(dic['id'])
return ip_object, ipv6_object
示例7: create
def create(self, ipv4_id, networkipv4_id):
ipv4 = Ip.get_by_pk(ipv4_id)
networkipv4 = NetworkIPv4.get_by_pk(networkipv4_id)
if len(DHCPRelayIPv4.objects.filter(ipv4=ipv4, networkipv4=networkipv4)) > 0:
raise exceptions.DHCPRelayAlreadyExistsError(ipv4_id, networkipv4_id)
self.ipv4 = ipv4
self.networkipv4 = networkipv4
示例8: inserir_equip
def inserir_equip(user, variablestochangecore, rede_id):
ip = Ip()
ip.descricao = None
ip.oct1, ip.oct2, ip.oct3, ip.oct4 = str(variablestochangecore["IPCORE"]).split('.')
equip = Equipamento.get_by_name(variablestochangecore["EQUIP_NAME"])
rede = NetworkIPv4.get_by_pk(rede_id)
ip.save_ipv4(equip.id, user, rede)
if ip.id is None:
raise RackAplError (None, None, "Erro ao inserir os equipamentos")
# Delete vlan's cache
destroy_cache_function([rede.vlan_id])
list_id_equip = []
list_id_equip.append(equip.id)
destroy_cache_function(list_id_equip, True)
return 0
示例9: handle_get
def handle_get(self, request, user, *args, **kwargs):
"""Treat requests GET to verify that the IP belongs to environment.
URLs: /ip/x1.x2.x3.x4/ambiente/<id_amb>
URLs: /ip/<ip>/ambiente/<id_amb>
"""
self.log.info("GET to verify that the IP belongs to environment")
try:
# User permission
if not has_perm(user, AdminPermission.IPS, AdminPermission.READ_OPERATION):
self.log.error(
u'User does not have permission to perform the operation.')
return self.not_authorized()
environment_id = kwargs.get('id_amb')
# Valid Environment ID
if not is_valid_int_greater_zero_param(environment_id):
self.log.error(
u'The id_environment parameter is not a valid value: %s.', environment_id)
raise InvalidValueError(None, 'id_environment', environment_id)
ip = kwargs.get('ip')
# Valid IP
if not is_valid_ipv4(ip):
self.log.error(u'Parameter ip is invalid. Value: %s.', ip)
raise InvalidValueError(None, 'ip', ip)
# Find Environment by ID to check if it exist
Ambiente.get_by_pk(environment_id)
# Existing IP
octs = str(IPAddress(ip, 4).exploded).split('.')
ip = Ip.get_by_octs_and_environment(
octs[0], octs[1], octs[2], octs[3], environment_id)
# Build dictionary return
ip_map = dict()
ip_map['id'] = ip.id
ip_map['id_vlan'] = ip.networkipv4.vlan.id
ip_map['oct4'] = ip.oct4
ip_map['oct3'] = ip.oct3
ip_map['oct2'] = ip.oct2
ip_map['oct1'] = ip.oct1
ip_map['descricao'] = ip.descricao
return self.response(dumps_networkapi({'ip': ip_map}))
except InvalidValueError, e:
return self.response_error(269, e.param, e.value)
示例10: buscar_ip
def buscar_ip(id_sw):
ip_sw=None
ips = IpEquipamento()
ips = ips.list_by_equip(id_sw)
for ip in ips:
ip_sw = Ip.get_by_pk(ip.ip.id)
if not ip_sw==None:
ip_sw = str(ip_sw.oct1)+'.'+str(ip_sw.oct2)+'.'+str(ip_sw.oct3)+'.'+str(ip_sw.oct4)
return ip_sw
示例11: handle_get
def handle_get(self, request, user, *args, **kwargs):
'''Handles GET requests get an IP4 available.
URL: ip/availableip4/ip_rede
'''
self.log.info('Get an IP4 available')
try:
# User permission
if not has_perm(user, AdminPermission.IPS, AdminPermission.READ_OPERATION):
self.log.error(
u'User does not have permission to perform the operation.')
return self.not_authorized()
# Business Validations
# Valid id access
id_network = kwargs.get('id_rede')
if not is_valid_int_greater_zero_param(id_network):
self.log.error(
u'Parameter id_rede is invalid. Value: %s.', id_network)
raise InvalidValueError(None, 'id_rede', id_network)
# Business Rules
ip = Ip.get_available_ip(id_network)
list_ip = []
list_ip.append(ip)
network_map = dict()
map_aux = dict()
map_aux['ip'] = list_ip
network_map['ip'] = map_aux
# Business Rules
return self.response(dumps_networkapi(network_map))
except InvalidValueError, e:
return self.response_error(269, e.param, e.value)
示例12: handle_get
def handle_get(self, request, user, *args, **kwargs):
'''Handles GET requests for delete an IP4
URL: ip4/delete/id_ip4
'''
self.log.info('Delete an IP4')
try:
# User permission
if not has_perm(user, AdminPermission.IPS, AdminPermission.WRITE_OPERATION):
self.log.error(
u'User does not have permission to perform the operation.')
return self.not_authorized()
# Business Validations
# Valid id access
id_ip = kwargs.get('id_ipv4')
if not is_valid_int_greater_zero_param(id_ip):
self.log.error(
u'Parameter id_ip is invalid. Value: %s.', id_ip)
raise InvalidValueError(None, 'id_rede', id_ip)
ip = Ip.get_by_pk(id_ip)
with distributedlock(LOCK_IPV4 % id_ip):
# Business Rules
ip.delete()
# Business Rules
return self.response(dumps_networkapi({}))
except IpCantBeRemovedFromVip, e:
return self.response_error(319, "ip", 'ipv4', id_ip)
示例13: handle_post
def handle_post(self, request, user, *args, **kwargs):
"""Handles POST requests to edit an IP.
URL: ipv4/edit/
"""
self.log.info('Edit an IP')
try:
# Load XML data
xml_map, attrs_map = loads(request.raw_post_data)
# XML data format
networkapi_map = xml_map.get('networkapi')
if networkapi_map is None:
msg = u'There is no value to the networkapi tag of XML request.'
self.log.error(msg)
return self.response_error(3, msg)
ip_map = networkapi_map.get('ip_map')
if ip_map is None:
msg = u'There is no value to the ip tag of XML request.'
self.log.error(msg)
return self.response_error(3, msg)
# Get XML data
id_ip = ip_map.get('id_ip')
description = ip_map.get('descricao')
ip4 = ip_map.get('ip4')
# Valid id_ip
if not is_valid_int_greater_zero_param(id_ip):
self.log.error(
u'Parameter id_ip is invalid. Value: %s.', id_ip)
raise InvalidValueError(None, 'id_ip', id_ip)
if not is_valid_string_maxsize(ip4, 15):
self.log.error(u'Parameter ip4 is invalid. Value: %s.', ip4)
raise InvalidValueError(None, 'ip4', description)
# Valid description
if description is not None:
if not is_valid_string_maxsize(description, 100) or not is_valid_string_minsize(description, 3):
self.log.error(
u'Parameter description is invalid. Value: %s.', description)
raise InvalidValueError(None, 'description', description)
# User permission
if not has_perm(user, AdminPermission.IPS, AdminPermission.WRITE_OPERATION):
raise UserNotAuthorizedError(
None, u'User does not have permission to perform the operation.')
# Business Rules
# New IP
ip = Ip()
ip = ip.get_by_pk(id_ip)
with distributedlock(LOCK_IPV4 % id_ip):
# se Houver erro no ip informado para retorna-lo na mensagem
ip_error = ip4
# verificação se foi passado algo errado no ip
ip4 = ip4.split('.')
for oct in ip4:
if not is_valid_int_param(oct):
raise InvalidValueError(None, 'ip4', ip_error)
# Ip passado de forma invalida
if len(ip4) is not 4:
raise IndexError
ip.descricao = description
ip.oct1 = ip4[0]
ip.oct2 = ip4[1]
ip.oct3 = ip4[2]
ip.oct4 = ip4[3]
# Persist
ip.edit_ipv4(user)
return self.response(dumps_networkapi({}))
except UserNotAuthorizedError:
return self.not_authorized()
except IndexError:
msg = 'Invalid IP %s' % ip_error
return self.response_error(150, msg)
except IpNotFoundError, e:
return self.response_error(150, e.message)
示例14: handle_get
def handle_get(self, request, user, *args, **kwargs):
"""Handles GET requests to get a ipv4.
URLs: ip/get/id_ip
"""
try:
# Commons Validations
# User permission
if not has_perm(user, AdminPermission.IPS, AdminPermission.READ_OPERATION):
self.log.error(
u'User does not have permission to perform the operation.')
return self.not_authorized()
# Business Validations
# Valid id access
id_ip = kwargs.get('id_ip')
if not is_valid_int_greater_zero_param(id_ip):
raise InvalidValueError(None, 'id_ip', id_ip)
# Business Rules
ip = Ip()
ip = ip.get_by_pk(id_ip)
EquipIps = []
mapa = dict()
#lista = []
try:
EquipIps = []
equipsIp = IpEquipamento.list_by_ip(ip.id)
for eIp in equipsIp:
EquipIps.append(eIp)
mapa[ip.id] = EquipIps
# lista.append(mapa)
except IpEquipmentNotFoundError:
EquipIps.append(None)
except IpError:
EquipIps.append(None)
network_map = dict()
list_ips = []
lequips = []
lequips = []
ip_maps = dict()
ip_maps['id'] = ip.id
ip_maps['oct1'] = ip.oct1
ip_maps['oct2'] = ip.oct2
ip_maps['oct3'] = ip.oct3
ip_maps['oct4'] = ip.oct4
ip_maps['descricao'] = ip.descricao
for equip in mapa.get(ip.id):
equip = Equipamento.get_by_pk(equip.equipamento.id)
lequips.append(model_to_dict(equip))
ip_maps['equipamento'] = lequips
list_ips.append(ip_maps)
list_ips
network_map['ips'] = list_ips
# Return XML
return self.response(dumps_networkapi(network_map))
except InvalidValueError, e:
self.log.error(
u'Parameter %s is invalid. Value: %s.', e.param, e.value)
return self.response_error(269, e.param, e.value)
示例15: handle_post
#.........这里部分代码省略.........
if blocks != expl:
raise InvalidValueError(None, 'rede', network)
# Business Rules
if version == IP_VERSION.IPv4[0]:
# Find all networks related to environment
nets = NetworkIPv4.objects.select_related().filter(
vlan__ambiente__id=vlan.ambiente.id)
# Cast to API class
networks = set([IPv4Network(
'%d.%d.%d.%d/%d' % (net_ip.oct1, net_ip.oct2, net_ip.oct3, net_ip.oct4, net_ip.block)) for net_ip in nets])
# If network selected not in use
if net in networks:
raise NetworkIPv4AddressNotAvailableError(
None, u'Unavailable address to create a NetworkIPv4.')
if env_vip is not None:
# Find all networks related to environment vip
nets = NetworkIPv4.objects.select_related().filter(
ambient_vip__id=env_vip.id)
# Cast to API class
networks = set([IPv4Network(
'%d.%d.%d.%d/%d' % (net_ip.oct1, net_ip.oct2, net_ip.oct3, net_ip.oct4, net_ip.block)) for net_ip in nets])
# If there is already a network with the same range ip as
# related the environment vip
if net in networks:
raise NetworkIpAddressNotAvailableError(
None, u'Unavailable address to create a NetworkIPv4.')
# # 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], oct4=expl[3], block=expl[4])
env_ids = list()
for net_ip in nets:
env_ids.append(net_ip.vlan.ambiente.id)
# If other network with same ip range exists
if len(env_ids) > 0:
# Get equipments related to this network's environment
env_equips = EquipamentoAmbiente.objects.filter(
ambiente=vlan.ambiente.id)
# Verify equipments related with all other environments
# that contains networks with same ip range
for env_id in env_ids:
# Equipments related to other environments
other_env_equips = EquipamentoAmbiente.objects.filter(
ambiente=env_id)
# Adjust to equipments
equip_list = list()
for equip_env in other_env_equips:
equip_list.append(equip_env.equipamento.id)
for env_equip in env_equips:
if env_equip.equipamento.id in equip_list:
# Filter testing