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'))
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'))
def handle_delete(self, request, user, *args, **kwargs): """Handles DELETE requests to deallocate all relationships between NetworkIPv4. URL: network/ipv4/<id_network_ipv4>/deallocate/ """ self.log.info('Deallocate all relationships between NetworkIPv4.') try: # Commons Validations # User permission if not has_perm(user, AdminPermission.VLAN_MANAGEMENT, AdminPermission.WRITE_OPERATION): self.log.error( u'User does not have permission to perform the operation.') return self.not_authorized() # Business Validations # Load URL param network_ipv4_id = kwargs.get('id_network_ipv4') # Valid NetworkIpv4 ID if not is_valid_int_greater_zero_param(network_ipv4_id): self.log.error( u'Parameter id_network_ipv4 is invalid. Value: %s.', network_ipv4_id) raise InvalidValueError( None, 'id_network_ipv4', network_ipv4_id) # Existing NetworkIpv4 ID network_ipv4 = NetworkIPv4().get_by_pk(network_ipv4_id) for ipv4 in network_ipv4.ip_set.all(): 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) ip_formated = mount_ipv4_string(ipv4) network_ipv4_ip = mount_ipv4_string(network_ipv4) raise IpCantRemoveFromServerPool({'ip': ip_formated, 'network_ip': network_ipv4_ip, 'server_pool_identifiers': server_pool_identifiers}, 'Não foi possível excluir a rede %s pois o ip %s contido nela esta sendo usado nos Server Pools (id:identifier) %s' % (network_ipv4_ip, ip_formated, server_pool_identifiers)) with distributedlock(LOCK_NETWORK_IPV4 % network_ipv4_id): destroy_cache_function([network_ipv4.vlan_id]) key_list_eqs = Equipamento.objects.filter( ipequipamento__ip__networkipv4=network_ipv4).values_list('id', flat=True) destroy_cache_function(key_list_eqs, True) # Business Rules # Remove NetworkIPv4 (will remove all relationships by cascade) locks_used = [LOCK_NETWORK_IPV4 % network_ipv4_id] network_ipv4.delete_v3(locks_used) # Return nothing return self.response(dumps_networkapi({})) except IpCantRemoveFromServerPool, e: return self.response_error(386, e.cause.get('network_ip'), e.cause.get('ip'), e.cause.get('server_pool_identifiers'))
def handle_delete(self, request, user, *args, **kwargs): """Treat requests DELETE to deallocate all relationships between Vlan. URL: vlan/<id_vlan>/deallocate/ """ self.log.info("Deallocate all relationships between Vlan.") try: # User permission if not has_perm(user, AdminPermission.VLAN_MANAGEMENT, AdminPermission.WRITE_OPERATION): self.log.error( u'User does not have permission to perform the operation.') raise UserNotAuthorizedError(None) # Load URL param id_vlan = kwargs.get('id_vlan') # Valid vlan id if not is_valid_int_greater_zero_param(id_vlan): self.log.error( u'The id_vlan parameter is not a valid value: %s.', id_vlan) raise InvalidValueError(None, 'id_vlan', id_vlan) # Find Vlan by id to check if it exist vlan = Vlan().get_by_pk(id_vlan) # Delete vlan's cache destroy_cache_function([id_vlan]) # Delete equipment's cache equip_id_list = [] for netv4 in vlan.networkipv4_set.all(): for ipv4 in netv4.ip_set.all(): 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) ip_formated = mount_ipv4_string(ipv4) vlan_name = vlan.nome network_ip = mount_ipv4_string(netv4) raise IpCantRemoveFromServerPool({'ip': ip_formated, 'vlan_name': vlan_name, 'network_ip': network_ip, 'server_pool_identifiers': server_pool_identifiers}, "Não foi possível excluir a vlan %s pois ela possui a rede %s e essa rede possui o ip %s contido nela, e esse ip esta sendo usado nos Server Pools (id:identifier) %s" % (vlan_name, network_ip, ip_formated, server_pool_identifiers)) for ip_equip in ipv4.ipequipamento_set.all(): equip_id_list.append(ip_equip.equipamento_id) for netv6 in vlan.networkipv6_set.all(): for ip in netv6.ipv6_set.all(): server_pool_member_list = ServerPoolMember.objects.filter(ipv6=ip) 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) ip_formated = mount_ipv6_string(ip) vlan_name = vlan.nome network_ip = mount_ipv6_string(netv6) raise IpCantRemoveFromServerPool({'ip': ip_formated, 'vlan_name': vlan_name, 'network_ip': network_ip, 'server_pool_identifiers': server_pool_identifiers}, "Não foi possível excluir a vlan %s pois ela possui a rede %s e essa rede possui o ip %s contido nela, e esse ip esta sendo usado nos Server Pools (id:identifier) %s" % (vlan_name, network_ip, ip_formated, server_pool_identifiers)) for ip_equip in ip.ipv6equipament_set.all(): equip_id_list.append(ip_equip.equipamento_id) destroy_cache_function(equip_id_list, True) with distributedlock(LOCK_VLAN % id_vlan): # Remove Vlan vlan.delete(user) return self.response(dumps_networkapi({})) except IpCantRemoveFromServerPool, e: return self.response_error(387, e.cause.get('vlan_name'), e.cause.get('network_ip'), e.cause.get('ip'), e.cause.get('server_pool_identifiers'))
def handle_get(self, request, user, *args, **kwargs): """Handles GET requests to list all real related equipment. URLs: equipamento/get_real_related/<id_equip> """ try: # User permission if not has_perm(user, AdminPermission.VIPS_REQUEST, AdminPermission.READ_OPERATION): self.log.error( u'User does not have permission to perform the operation.') return self.not_authorized() id_equip = kwargs.get('id_equip') # Valid equipment ID if not is_valid_int_greater_zero_param(id_equip): self.log.error( u'The id_equip parameter is not a valid value: %s.', id_equip) raise InvalidValueError(None, 'id_equip', id_equip) equipment = Equipamento.get_by_pk(id_equip) map_dicts = [] # IPV4 for ip_equip in equipment.ipequipamento_set.all(): vip_dict = dict() ip = ip_equip.ip for server_pool_member in ip.serverpoolmember_set.all(): server_pool_id = server_pool_member.server_pool_id vip_port_to_pool = VipPortToPool.objects.filter( server_pool__id=server_pool_id) for vptp in vip_port_to_pool: vip = RequisicaoVips.get_by_pk( vptp.requisicao_vip.id) if vip.id not in vip_dict: vip_dict = {str(vip.id): list()} host_name = vip.variables_to_map()['host'] map_dicts.append({'server_pool_member_id': server_pool_member.id, 'id_vip': vip.id, 'host_name': host_name, 'port_vip': vptp.port_vip, 'port_real': server_pool_member.port_real, 'ip': mount_ipv4_string(ip)}) # IPV6 for ip_equip in equipment.ipv6equipament_set.all(): vip_dict = dict() ip = ip_equip.ip for server_pool_member in ip.serverpoolmember_set.all(): server_pool_id = server_pool_member.server_pool_id vip_port_to_pool = VipPortToPool.objects.filter( server_pool__id=server_pool_id) for vptp in vip_port_to_pool: vip = RequisicaoVips.get_by_pk( vptp.requisicao_vip.id) if vip.id not in vip_dict: vip_dict = {str(vip.id): list()} host_name = vip.variables_to_map()['host'] map_dicts.append({'server_pool_member_id': server_pool_member.id, 'id_vip': vip.id, 'host_name': host_name, 'port_vip': vptp.port_vip, 'port_real': server_pool_member.port_real, 'ip': mount_ipv6_string(ip)}) vip_map = dict() vip_map["vips"] = map_dicts vip_map["equip_name"] = equipment.nome # Return XML return self.response(dumps_networkapi(vip_map)) except EquipamentoNotFoundError, e: return self.response_error(117, id_equip)
def handle_delete(self, request, user, *args, **kwargs): """Treat requests DELETE to deallocate all relationships between Vlan. URL: vlan/<id_vlan>/deallocate/ """ self.log.info("Deallocate all relationships between Vlan.") try: # User permission if not has_perm(user, AdminPermission.VLAN_MANAGEMENT, AdminPermission.WRITE_OPERATION): self.log.error( u'User does not have permission to perform the operation.') raise UserNotAuthorizedError(None) # Load URL param id_vlan = kwargs.get('id_vlan') # Valid vlan id if not is_valid_int_greater_zero_param(id_vlan): self.log.error( u'The id_vlan parameter is not a valid value: %s.', id_vlan) raise InvalidValueError(None, 'id_vlan', id_vlan) # Find Vlan by id to check if it exist vlan = Vlan().get_by_pk(id_vlan) # Delete vlan's cache destroy_cache_function([id_vlan]) # Delete equipment's cache equip_id_list = [] for netv4 in vlan.networkipv4_set.all(): for ipv4 in netv4.ip_set.all(): 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) ip_formated = mount_ipv4_string(ipv4) vlan_name = vlan.nome network_ip = mount_ipv4_string(netv4) raise IpCantRemoveFromServerPool( { 'ip': ip_formated, 'vlan_name': vlan_name, 'network_ip': network_ip, 'server_pool_identifiers': server_pool_identifiers }, "Não foi possível excluir a vlan %s pois ela possui a rede %s e essa rede possui o ip %s contido nela, e esse ip esta sendo usado nos Server Pools (id:identifier) %s" % (vlan_name, network_ip, ip_formated, server_pool_identifiers)) for ip_equip in ipv4.ipequipamento_set.all(): equip_id_list.append(ip_equip.equipamento_id) for netv6 in vlan.networkipv6_set.all(): for ip in netv6.ipv6_set.all(): server_pool_member_list = ServerPoolMember.objects.filter( ipv6=ip) 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) ip_formated = mount_ipv6_string(ip) vlan_name = vlan.nome network_ip = mount_ipv6_string(netv6) raise IpCantRemoveFromServerPool( { 'ip': ip_formated, 'vlan_name': vlan_name, 'network_ip': network_ip, 'server_pool_identifiers': server_pool_identifiers }, "Não foi possível excluir a vlan %s pois ela possui a rede %s e essa rede possui o ip %s contido nela, e esse ip esta sendo usado nos Server Pools (id:identifier) %s" % (vlan_name, network_ip, ip_formated, server_pool_identifiers)) for ip_equip in ip.ipv6equipament_set.all(): equip_id_list.append(ip_equip.equipamento_id) destroy_cache_function(equip_id_list, True) with distributedlock(LOCK_VLAN % id_vlan): # Remove Vlan vlan.delete() return self.response(dumps_networkapi({})) except IpCantRemoveFromServerPool, e: return self.response_error(387, e.cause.get('vlan_name'), e.cause.get('network_ip'), e.cause.get('ip'), e.cause.get('server_pool_identifiers'))
def handle_delete(self, request, user, *args, **kwargs): """Handles DELETE requests to deallocate all relationships between NetworkIPv4. URL: network/ipv4/<id_network_ipv4>/deallocate/ """ self.log.info('Deallocate all relationships between NetworkIPv4.') try: # Commons Validations # User permission if not has_perm(user, AdminPermission.VLAN_MANAGEMENT, AdminPermission.WRITE_OPERATION): self.log.error( u'User does not have permission to perform the operation.') return self.not_authorized() # Business Validations # Load URL param network_ipv4_id = kwargs.get('id_network_ipv4') # Valid NetworkIpv4 ID if not is_valid_int_greater_zero_param(network_ipv4_id): self.log.error( u'Parameter id_network_ipv4 is invalid. Value: %s.', network_ipv4_id) raise InvalidValueError(None, 'id_network_ipv4', network_ipv4_id) # Existing NetworkIpv4 ID network_ipv4 = NetworkIPv4().get_by_pk(network_ipv4_id) for ipv4 in network_ipv4.ip_set.all(): 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) ip_formated = mount_ipv4_string(ipv4) network_ipv4_ip = mount_ipv4_string(network_ipv4) raise IpCantRemoveFromServerPool( { 'ip': ip_formated, 'network_ip': network_ipv4_ip, 'server_pool_identifiers': server_pool_identifiers }, 'Não foi possível excluir a rede %s pois o ip %s contido nela esta sendo usado nos Server Pools (id:identifier) %s' % (network_ipv4_ip, ip_formated, server_pool_identifiers)) with distributedlock(LOCK_NETWORK_IPV4 % network_ipv4_id): destroy_cache_function([network_ipv4.vlan_id]) key_list_eqs = Equipamento.objects.filter( ipequipamento__ip__networkipv4=network_ipv4).values_list( 'id', flat=True) destroy_cache_function(key_list_eqs, True) # Business Rules # Remove NetworkIPv4 (will remove all relationships by cascade) locks_used = [LOCK_NETWORK_IPV4 % network_ipv4_id] network_ipv4.delete_v3(locks_used) # Return nothing return self.response(dumps_networkapi({})) except IpCantRemoveFromServerPool, e: return self.response_error(386, e.cause.get('network_ip'), e.cause.get('ip'), e.cause.get('server_pool_identifiers'))