def deploy_networkipv4(network_id, user): """Loads template for creating Network IPv4 equipment configuration, creates file and apply config. :param network_id: NetworkIPv4 Id Returns: List with status of equipments output """ try: netv4_obj = get_networkipv4_by_id(network_id) routers = netv4_obj.vlan.ambiente.routers if not routers: raise exceptions.NoEnvironmentRoutersFoundException() if facade_eqpt.all_equipments_are_in_maintenance(routers): raise exceptions_eqpt.AllEquipmentsAreInMaintenanceException() if user: if not facade_eqpt.all_equipments_can_update_config(routers, user): raise exceptions_eqpt.UserDoesNotHavePermInAllEqptException( 'User does not have permission to update conf in eqpt. ' 'Verify the permissions of user group with equipment group' '.Network:{}'.format(netv4_obj.id)) except ObjectDoesNotExistException, e: raise ObjectDoesNotExistException(e.detail)
def deploy_networkipv6(network_id, user): """Loads template for creating Network IPv6 equipment configuration, creates file and apply config. Args: NetworkIPv6 object Equipamento objects list Returns: List with status of equipments output """ netv6_obj = get_networkipv6_by_id(network_id) routers = netv6_obj.vlan.ambiente.routers if not routers: raise exceptions.NoEnvironmentRoutersFoundException() if facade_eqpt.all_equipments_are_in_maintenance(routers): raise exceptions_eqpt.AllEquipmentsAreInMaintenanceException() if user: if not facade_eqpt.all_equipments_can_update_config(routers, user): raise exceptions_eqpt.UserDoesNotHavePermInAllEqptException( 'User does not have permission to update conf in eqpt. ' 'Verify the permissions of user group with equipment group. ' 'Network:{}'.format(netv6_obj.id)) # lock network id to prevent multiple requests to same id with distributedlock(LOCK_NETWORK_IPV6 % netv6_obj.id): with distributedlock(LOCK_VLAN % netv6_obj.vlan.id): if netv6_obj.active == 1: return 'Network already active. Nothing to do.' # load dict with all equipment attributes dict_ips = get_dict_v6_to_use_in_configuration_deploy( user, netv6_obj, routers) status_deploy = dict() # TODO implement threads for equipment in routers: # generate config file file_to_deploy = _generate_config_file( dict_ips, equipment, TEMPLATE_NETWORKv6_ACTIVATE) # deploy config file in equipments lockvar = LOCK_EQUIPMENT_DEPLOY_CONFIG_NETWORK_SCRIPT % ( equipment.id) status_deploy[ equipment.id] = deploy_config_in_equipment_synchronous( file_to_deploy, equipment, lockvar) netv6_obj.activate_v3() # transaction.commit() if netv6_obj.vlan.ativada == 0: netv6_obj.vlan.activate_v3() return status_deploy
def undeploy_networkipv4(network_id, user): """Loads template for removing Network IPv4 equipment configuration, creates file and apply config. :param network_id: NetworkIPv4 Id Returns: List with status of equipments output """ netv4_obj = get_networkipv4_by_id(network_id) routers = netv4_obj.vlan.ambiente.routers if not routers: raise exceptions.NoEnvironmentRoutersFoundException() if facade_eqpt.all_equipments_are_in_maintenance(routers): raise exceptions_eqpt.AllEquipmentsAreInMaintenanceException() if user: if not facade_eqpt.all_equipments_can_update_config(routers, user): raise exceptions_eqpt.UserDoesNotHavePermInAllEqptException( 'User does not have permission to update conf in eqpt. ' 'Verify the permissions of user group with equipment group. ' 'Network:{}'.format(netv4_obj.id)) # lock network id to prevent multiple requests to same id with distributedlock(LOCK_NETWORK_IPV4 % netv4_obj.id): with distributedlock(LOCK_VLAN % netv4_obj.vlan.id): if netv4_obj.active == 0: return 'Network already not active. Nothing to do.' # load dict with all equipment attributes dict_ips = get_dict_v4_to_use_in_configuration_deploy( user, netv4_obj, routers) status_deploy = dict() # TODO implement threads for equipment in routers: # generate config file file_to_deploy = _generate_config_file( dict_ips, equipment, TEMPLATE_NETWORKv4_DEACTIVATE) lockvar = LOCK_EQUIPMENT_DEPLOY_CONFIG_NETWORK_SCRIPT % ( equipment.id) # deploy config file in equipments status_deploy[ equipment.id] = deploy_config_in_equipment_synchronous( file_to_deploy, equipment, lockvar) netv4_obj.deactivate_v3() # transaction.commit() if netv4_obj.vlan.ativada == 1: # if there are no other networks active in vlan, remove int # vlan if not _has_active_network_in_vlan(netv4_obj.vlan): # remove int vlan for equipment in routers: if equipment.maintenance is not True: pass # Delete SVI status_deploy[equipment.id] += _remove_svi( equipment, netv4_obj.vlan.num_vlan) # Need verify this call netv4_obj.vlan.deactivate_v3() return status_deploy
def networkIPv6_deploy(request, network_id): """Deploy network L3 configuration in the environment routers for network ipv6 Receives optional parameter equipments to specify what equipment should receive network configuration """ networkipv6 = NetworkIPv6.get_by_pk(int(network_id)) environment = networkipv6.vlan.ambiente equipments_id_list = None if request.DATA is not None: equipments_id_list = request.DATA.get('equipments', None) equipment_list = [] if equipments_id_list is not None: if type(equipments_id_list) is not list: raise api_exceptions.ValidationException('equipments') for equip in equipments_id_list: try: int(equip) except ValueError: raise api_exceptions.ValidationException('equipments') # Check that equipments received as parameters are in correct vlan # environment equipment_list = Equipamento.objects.filter( equipamentoambiente__ambiente=environment, id__in=equipments_id_list) log.info('list = %s' % equipment_list) if len(equipment_list) != len(equipments_id_list): log.error( 'Error: equipments %s are not part of network environment.' % equipments_id_list) raise exceptions.EquipmentIDNotInCorrectEnvException() else: # TODO GET network routers equipment_list = Equipamento.objects.filter( ipv6equipament__ip__networkipv6=networkipv6, equipamentoambiente__ambiente=networkipv6.vlan.ambiente, equipamentoambiente__is_router=1).distinct() if len(equipment_list) == 0: raise exceptions.NoEnvironmentRoutersFoundException() # Check permission to configure equipments for equip in equipment_list: # User permission if not has_perm(request.user, AdminPermission.EQUIPMENT_MANAGEMENT, AdminPermission.WRITE_OPERATION, None, equip.id, AdminPermission.EQUIP_WRITE_OPERATION): log.error( u'User does not have permission to perform the operation.') raise PermissionDenied( 'No permission to configure equipment %s-%s' % (equip.id, equip.nome)) if all_equipments_are_in_maintenance(equipment_list): raise AllEquipmentsAreInMaintenanceException() try: # deploy network configuration if request.method == 'POST': returned_data = facade.deploy_networkIPv6_configuration( request.user, networkipv6, equipment_list) elif request.method == 'DELETE': returned_data = facade.remove_deploy_networkIPv6_configuration( request.user, networkipv6, equipment_list) return Response(returned_data) except Exception, exception: log.error(exception) raise api_exceptions.NetworkAPIException()