def get_port_mab(self, ctx, switch: Switch = None, port: Port = None) -> bool: """ Retrieve whether MAB is active on a port. :raise PortNotFound """ LOG.debug("switch_network_manager_get_port_mab_called", extra=log_extra(port)) if switch is None: raise NetworkManagerReadError( "SNMP read error: switch object was None") if port is None: raise NetworkManagerReadError( "SNMP read error: port object was None") try: return get_SNMP_value(switch.community, switch.ip_v4, 'CISCO-MAC-AUTH-BYPASS-MIB', 'cmabIfAuthEnabled', port.switch_info.oid) except NetworkManagerReadError: raise
def create_product(self, ctx, name=None, selling_price=None, buying_price=None): """ Create a product . :raise ProductTypeNotFound ? """ s = ctx.get(CTX_SQL_SESSION) LOG.debug("sql_product_repository_create_product_called", extra=log_extra(ctx, name=name)) product = SQLProduct( name=name, buying_price=buying_price, selling_price=selling_price, ) with track_modifications(ctx, s, product): s.add(product) return product
def search_transaction_by(self, ctx, limit=DEFAULT_LIMIT, offset=DEFAULT_OFFSET, transaction_id=None, account_id=None, terms=None) \ -> (List[Transaction], int): LOG.debug("sql_transaction_repository_search_called", extra=log_extra(ctx)) s = ctx.get(CTX_SQL_SESSION) account_source = aliased(Account) account_destination = aliased(Account) q = s.query(SQLTransaction) q = q.join(account_source, account_source.id == SQLTransaction.dst) q = q.join(account_destination, account_destination.id == SQLTransaction.src) if transaction_id: q = q.filter(SQLTransaction.id == transaction_id) if terms: q = q.filter((SQLTransaction.name.contains(terms))) if account_id: q = q.filter((SQLTransaction.src == account_id) | (SQLTransaction.dst == account_id)) count = q.count() q = q.order_by(SQLTransaction.timestamp.asc()) q = q.offset(offset) q = q.limit(limit) r = q.all() return list(map(_map_transaction_sql_to_entity, r)), count
def create_device(self, ctx, mac_address=None, owner_username=None, connection_type=None, ip_v4_address=None, ip_v6_address=None): LOG.debug("sql_device_repository_create_device_called", extra=log_extra(ctx, mac=mac_address)) s = ctx.get(CTX_SQL_SESSION) all_devices = get_all_devices(s) device = s.query(all_devices).filter( all_devices.columns.mac == mac_address).one_or_none() if device is not None: raise DeviceAlreadyExist() # If the user do not change the connection type, we just need to update... if connection_type == DeviceType.Wired: create_wired_device( ctx, s=s, mac_address=mac_address, ip_v4_address=ip_v4_address or 'En Attente', ip_v6_address=ip_v6_address or 'En Attente', username=owner_username, ) else: create_wireless_device( ctx, s=s, mac_address=mac_address, username=owner_username, )
def search_room_by(self, ctx, limit=DEFAULT_LIMIT, offset=DEFAULT_OFFSET, room_number=None, owner_username=None, terms=None) -> ( List[Room], int): LOG.debug("sql_room_repository_search_room_by_called", extra=log_extra(ctx, username=owner_username, terms=terms)) s = ctx.get(CTX_SQL_SESSION) q = s.query(Chambre) if room_number: q = q.filter(Chambre.numero == room_number) if terms: q = q.filter(or_( Chambre.telephone.contains(terms), Chambre.description.contains(terms), )) if owner_username: q = q.join(Adherent) q = q.filter(Adherent.login == owner_username) count = q.count() q = q.order_by(Chambre.numero.asc()) q = q.offset(offset) q = q.limit(limit) r = q.all() r = list(map(_map_room_sql_to_entity, r)) return r, count
def create_room(self, ctx, room_number=None, description=None, phone_number=None, vlan_number=None) -> None: LOG.debug("sql_room_repository_create_room_called", extra=log_extra(ctx, room_number=room_number, description=description, phone_number=phone_number, vlan_number=vlan_number)) s = ctx.get(CTX_SQL_SESSION) now = datetime.now() result = s.query(Chambre).filter(Chambre.numero == room_number).one_or_none() if result is not None: raise RoomAlreadyExists() vlan = s.query(Vlan).filter(Vlan.numero == vlan_number).one_or_none() if vlan is None: raise VLANNotFoundError(vlan_number) room = Chambre( numero=int(room_number), description=description, telephone=phone_number, created_at=now, updated_at=now, vlan=vlan, ) s.add(room)
def search_account_by(self, ctx, limit=None, offset=None, account_id=None, terms=None) -> (List[Account], int): """ Search for an account. """ LOG.debug("sql_account_repository_search_called", extra=log_extra(ctx, account_id=account_id, terms=terms)) s = ctx.get(CTX_SQL_SESSION) q = s.query(SQLAccount) if account_id: q = q.filter(SQLAccount.id == account_id) if terms: q = q.filter(SQLAccount.name.contains(terms)) count = q.count() q = q.order_by(SQLAccount.creation_date.asc()) q = q.offset(offset) q = q.limit(limit) r = q.all() return list(map(_map_account_sql_to_entity, r)), count
def put(self, ctx, mac_address, body): """ Put (update or create) a new device in the database """ LOG.debug("http_device_put_called", extra=log_extra(ctx, mac=mac_address, request=body)) try: created = self.device_manager.update_or_create( ctx, mac_address=mac_address, req=MutationRequest( owner_username=body.get('username'), mac_address=body.get('mac'), connection_type=body.get('connection_type'), ip_v4_address=body.get('ip_address'), ip_v6_address=body.get('ipv6_address'), ), ) if created: return NoContent, 201 # 201 Created else: return NoContent, 204 # 204 No Content except NoMoreIPAvailableException: return "IP allocation failed.", 503 except UserInputError as e: return bad_request(e), 400
def search(self, ctx, limit=DEFAULT_LIMIT, offset=DEFAULT_OFFSET, switch_id=None, room_number=None, terms=None): """ Filter the port list according to some criteria """ LOG.debug("http_port_search_called", extra=log_extra(ctx, switch_id=switch_id, room_number=room_number, terms=terms)) try: result, count = self.port_manager.search(ctx, limit=limit, offset=offset, switch_id=switch_id, room_number=room_number, terms=terms) headers = { 'access-control-expose-headers': 'X-Total-Count', 'X-Total-Count': str(count) } return list(map(_map_port_to_http_response, result)), 200, headers except UserInputError as e: return bad_request(e), 400
def add_member_payment_record(self, ctx, amount_in_cents: int, title: str, member_username: str, payment_method: str) -> None: LOG.debug("sql_money_repository_add_payment_record", extra=log_extra(ctx, amount=amount_in_cents / 100, title=title, username=member_username, payment_method=payment_method)) now = datetime.now() s = ctx.get(CTX_SQL_SESSION) admin = ctx.get(CTX_ADMIN) admin_sql = s.query(Utilisateur).filter(Utilisateur.login == admin.login).one_or_none() if admin_sql is None: raise InvalidAdmin() member_sql = s.query(Adherent).filter(Adherent.login == member_username).one_or_none() if member_sql is None: raise MemberNotFoundError(member_username) payment_method_db = PAYMENT_METHOD_TO_DB.get(payment_method) if payment_method_db is None: raise UnknownPaymentMethod() e = Ecriture( intitule=title, montant=amount_in_cents / 100, moyen=payment_method, date=now, compte_id=1, # Compte pour les paiement des adherents. created_at=now, updated_at=now, utilisateur=admin_sql, adherent=member_sql, ) s.add(e)
def health(self, ctx): LOG.debug("http_health_called", extra=log_extra(ctx)) if self.health_manager.is_healthy(ctx): return "OK", 200 else: return "FAILURE", 503
def search_switches_by(self, ctx, limit=DEFAULT_LIMIT, offset=DEFAULT_OFFSET, switch_id: str = None, terms: str = None) -> (List[Switch], int): """ Search for a switch. """ LOG.debug("sql_network_object_repository_search_switch_by", extra=log_extra(ctx, switch_id=switch_id, terms=terms)) s = ctx.get(CTX_SQL_SESSION) q = s.query(SwitchSQL) if switch_id is not None: q = q.filter(SwitchSQL.id == switch_id) if terms is not None: q = q.filter( or_( SwitchSQL.description.contains(terms), SwitchSQL.ip.contains(terms), )) count = q.count() q = q.order_by(SwitchSQL.description.asc()) q = q.offset(offset) q = q.limit(limit) result = q.all() result = map(_map_switch_sql_to_entity, result) result = list(result) return result, count
def create_account(self, ctx, name=None, actif=None, type=None, creation_date=None): """ Create an account. :raise AccountTypeNotFound ? """ s = ctx.get(CTX_SQL_SESSION) LOG.debug("sql_account_repository_create_account_called", extra=log_extra(ctx, name=name, type=type)) now = datetime.now() account = SQLAccount( name=name, actif=actif, type=type, creation_date=now, ) with track_modifications(ctx, s, account): s.add(account) return account
def search(self, ctx, limit=DEFAULT_LIMIT, offset=DEFAULT_OFFSET, username=None, terms=None): """ Filter the list of the devices according to some criterias """ LOG.debug("http_device_search_called", extra=log_extra(ctx, limit=limit, offset=offset, username=username, terms=terms)) try: result, count = self.device_manager.search(ctx, limit, offset, username, terms) except UserInputError as e: return bad_request(e), 400 headers = { "X-Total-Count": count, "access-control-expose-headers": "X-Total-Count" } return list(map(_map_device_to_http_response, result)), 200, headers
def search_device_by(self, ctx, limit=DEFAULT_LIMIT, offset=DEFAULT_OFFSET, mac_address=None, username=None, terms=None) \ -> (List[Device], int): LOG.debug("sql_device_repository_search_called", extra=log_extra(ctx)) s = ctx.get(CTX_SQL_SESSION) # Return a subquery with all devices (wired & wireless) # The fields, ip, ipv6, dns, etc, are set to None for wireless devices # There is also a field "type" wich is wired and wireless all_devices = get_all_devices(s) # Query all devices and their owner's unsername q = s.query(all_devices, Adherent.login.label("login")) q = q.join(Adherent, Adherent.id == all_devices.columns.adherent_id) if mac_address: q = q.filter(all_devices.columns.mac == mac_address) if username: q = q.filter(Adherent.login == username) if terms: q = q.filter((all_devices.columns.mac.contains(terms)) | (all_devices.columns.ip.contains(terms)) | (all_devices.columns.ipv6.contains(terms)) | (Adherent.login.contains(terms))) count = q.count() q = q.order_by(all_devices.columns.mac.asc()) q = q.offset(offset) q = q.limit(limit) r = q.all() results = list(map(_map_device_sql_to_entity, r)) return results, count
def get(self, ctx, username): """ Get a specific member. """ LOG.debug("http_member_get_called", extra=log_extra(ctx, username=username)) try: return _map_member_to_http_response(self.member_manager.get_by_username(ctx, username)), 200 # 200 OK except MemberNotFoundError: return NoContent, 404 # 404 Not Found
def logs_search(self, ctx, username): """ Get logs from a member. """ LOG.debug("http_member_get_logs_called", extra=log_extra(ctx, username=username)) try: return self.member_manager.get_logs(ctx, username), 200 except MemberNotFoundError: return NoContent, 404
def get(self, ctx, transaction_id): """ Get a specific transaction. """ LOG.debug("http_transaction_get_called", extra=log_extra(ctx, transaction_id=transaction_id)) try: return _map_transaction_to_http_response( self.transaction_manager.get_by_id(ctx, transaction_id)), 200 # 200 OK except TransactionNotFoundError: return NoContent, 404 # 404 Not Found
def delete(self, ctx, username): """ Delete the specified User from the database """ LOG.debug("http_member_delete_called", extra=log_extra(ctx, username=username)) try: self.member_manager.delete(ctx, username) return NoContent, 204 # 204 No Content except MemberNotFoundError: return NoContent, 404 # 404 Not Found
def get(self, ctx, product_id): """ Get a specific account. """ LOG.debug("http_product_get_called", extra=log_extra(ctx, product_id=product_id)) try: result = self.product_manager.get_by_id(ctx, product_id) return _map_product_to_http_response(result), 200 # 200 OK except ProductNotFoundError: return NoContent, 404 # 404 Not Found
def delete_room(self, ctx, room_number) -> None: LOG.debug("sql_room_repository_delete_room_called", extra=log_extra(ctx, room_number=room_number)) s = ctx.get(CTX_SQL_SESSION) room = s.query(Chambre).filter(Chambre.numero == room_number).one_or_none() if room is None: raise RoomNotFoundError(room_number) s.delete(room)
def get(self, ctx, account_type_id): """ Return the account type specified by the id """ LOG.debug("http_account_type_get_called", extra=log_extra(ctx, account_type_id=account_type_id)) try: result = self.account_type_manager.get_by_id(ctx, account_type_id=account_type_id) return _map_account_type_to_http_response(result), 200 # OK except AccountTypeNotFoundError: return NoContent, 404 # 404 Not Found
def delete(self, ctx, switch_id): """ Delete the specified switch from the database """ LOG.debug("http_switch_delete_called", extra=log_extra(ctx, switch_id=switch_id)) try: self.switch_manager.delete(ctx, switch_id) return NoContent, 204 except SwitchNotFoundError: return NoContent, 404
def delete(self, ctx, room_number): """ Delete room from the database """ LOG.debug("http_room_delete_called", extra=log_extra(ctx, room_number=room_number)) try: self.room_manager.delete(ctx, room_number) return NoContent, 204 except RoomNotFoundError: return NoContent, 404
def delete(self, ctx, mac_address): """ Delete the specified device from the database """ LOG.debug("http_device_delete_called", extra=log_extra(ctx, mac=mac_address)) try: self.device_manager.delete(ctx, mac_address) return NoContent, 204 except DeviceNotFoundError: return NoContent, 404
def get(self, ctx, room_number): """ Get the room specified """ LOG.debug("http_room_get_called", extra=log_extra(ctx, room_number=room_number)) try: result = self.room_manager.get_by_number(ctx, room_number) return _map_room_to_http_response(result), 200 except RoomNotFoundError: return NoContent, 404
def delete(self, ctx, name): """ Delete the specified Product from the database """ LOG.debug("http_product_delete_called", extra=log_extra(ctx, name=name)) try: self.product_manager.delete(ctx, name) return NoContent, 204 # 204 No Content except ProductNotFoundError: return NoContent, 404 # 404 Not Found
def get(self, ctx, port_id): """ Get a port from the database """ LOG.debug("http_port_get_called", extra=log_extra(ctx, port_id=port_id)) try: result = self.port_manager.get_by_id(ctx, port_id=port_id) return _map_port_to_http_response(result), 200 except PortNotFoundError: return NoContent, 404
def patch(self, ctx, username, body): """ Partially update a member from the database """ LOG.debug("http_member_patch_called", extra=log_extra(ctx, username=username, request=body)) try: mutation_request = _map_http_request_to_partial_mutation_request(body) self.member_manager.update_partially(ctx, username, mutation_request) return NoContent, 204 # 204 No Content except MemberNotFoundError: return NoContent, 404 # 404 Not Found
def allocate_ip_v6(self, ctx, ip_range: str) -> str: s = ctx.get(CTX_SQL_SESSION) LOG.debug("sql_device_repository_allocate_ip_v6_called", extra=log_extra(ctx)) result = get_available_ip(ip_range, get_all_used_ipv6(s)) if result is None: raise NoMoreIPAvailableException() return result