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(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 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 membership_post(self, ctx, username, body): """ Add a membership record in the database """ LOG.debug("http_member_post_membership_called", extra=log_extra(ctx, username=username, request=body)) try: self.member_manager.new_membership(ctx, username, body.get('duration'), body.get('payment_method'), start_str=body.get('start')) return NoContent, 200 # 200 OK except MemberNotFoundError: return NoContent, 404 # 404 Not Found except UserInputError as e: return bad_request(e), 400 # 400 Bad Request
def put(self, ctx, username, body): """ Create/Update member from the database """ LOG.debug("http_member_put_called", extra=log_extra(ctx, username=username, request=body)) mutation_request = _map_http_request_to_full_mutation_request(body) try: created = self.member_manager.update_or_create(ctx, username, mutation_request) if created: return NoContent, 201 # 201 Created else: return NoContent, 204 # 204 No Content except UserInputError as e: return bad_request(e), 400 # 400 Bad Request
def post(self, ctx, body): """ Add a transaction record in the database """ LOG.debug("http_transaction_post_called", extra=log_extra(ctx, request=body)) mutation_request = _map_http_request_to_full_mutation_request(body) try: created = self.transaction_manager.update_or_create(ctx, mutation_request) if created: return NoContent, 201 # 201 Created else: return NoContent, 204 # 204 No Content except UserInputError as e: return bad_request(e), 400 # 400 Bad Request
def password_put(self, ctx, username, body): """ Set the password of a member. """ # Careful not to log the body here! LOG.debug("http_member_put_password_called", extra=log_extra(ctx, username=username, body=None)) try: self.member_manager.change_password(ctx, username, body.get('password')) return NoContent, 204 # 204 No Content except MemberNotFoundError: return NoContent, 404 # 404 Not Found except UserInputError as e: return bad_request(e), 400 # 400 Bad Request
def post(self, ctx, body): """ Create a switch in the database """ LOG.debug("http_switch_post_called", extra=log_extra(ctx, request=body)) try: switch_id = self.switch_manager.create( ctx, MutationRequest( ip_v4=body.get('ip'), description=body.get('description'), community=body.get('community'), )) return NoContent, 201, {'Location': f'/switch/{switch_id}'} except UserInputError as e: return bad_request(e), 400
def search(self, ctx, limit=DEFAULT_LIMIT, offset=DEFAULT_OFFSET, terms=None, room_number=None): """ Search all the member. """ LOG.debug("http_member_search_called", extra=log_extra(ctx, limit=limit, offset=offset, terms=terms, room_number=room_number)) try: result, total_count = self.member_manager.search(ctx, limit, offset, room_number, terms) headers = { "X-Total-Count": str(total_count), 'access-control-expose-headers': 'X-Total-Count' } result = list(map(_map_member_to_http_response, result)) return result, 200, headers # 200 OK except UserInputError as e: return bad_request(e), 400 # 400 Bad Request
def put(self, ctx, switch_id, body): """ Update the specified switch from the database """ LOG.debug("http_switch_put_called", extra=log_extra(ctx, switch_id=switch_id, request=body)) try: self.switch_manager.update( ctx, switch_id, MutationRequest( ip_v4=body.get('ip'), description=body.get('description'), community=body.get('community'), )) return NoContent, 204 except SwitchNotFoundError: return NoContent, 404 except UserInputError as e: return bad_request(e), 400
def post(self, ctx, body): """ Create a port in the database """ LOG.debug("http_port_post_called", extra=log_extra(ctx, request=body)) try: port_id = self.port_manager.create( ctx, MutationRequest( port_number=body.get('port_number'), room_number=body.get('room_number'), switch_id=body.get('switch_id'), rcom=0, # TODO: Add to spec. oid=None, # TODO: Add to spec. )) headers = {'Location': '/port/{}'.format(port_id)} return NoContent, 200, headers except UserInputError as e: return bad_request(e), 400
def post(self, ctx, body): """ Add a product record in the database """ LOG.debug("http_product_post_called", extra=log_extra(ctx, request=body)) try: created = self.product_manager.update_or_create( ctx, req=FullMutationRequest( name=body.get('name'), buying_price=body.get('buying_price'), selling_price=body.get('selling_price')), product_id=body.get('id_')) if created: return NoContent, 201 # 201 Created else: return NoContent, 204 # 204 No Content except UserInputError as e: return bad_request(e), 400 # 400 Bad Request
def put(self, ctx, room_number, body): """ Update/create a room in the database """ LOG.debug("http_room_put_called", extra=log_extra(ctx, room_number=room_number, request=body)) try: created = self.room_manager.update_or_create( ctx, room_number, MutationRequest( room_number=body.get('room_number'), description=body.get('description'), phone_number=body.get('phone'), vlan_number=body.get('vlan'), )) if created: return NoContent, 201 else: return NoContent, 204 except UserInputError as e: return bad_request(e), 400
def post(self, ctx, body): """ Add an account record in the database """ LOG.debug("http_account_post_called", extra=log_extra(ctx, request=body)) try: created = self.account_manager.update_or_create( ctx, req=FullMutationRequest( name=body.get('name'), type=body.get('type_'), actif=body.get('actif'), creation_date=body.get('creation_date')), account_id=body.get('account_id')) if created: return NoContent, 201 # 201 Created else: return NoContent, 204 # 204 No Content except UserInputError as e: return bad_request(e), 400 # 400 Bad Request
def search(self, ctx, limit=DEFAULT_LIMIT, offset=DEFAULT_OFFSET, terms=None): """ Filter the list of the rooms """ LOG.debug("http_room_search_called", extra=log_extra(ctx, terms=terms)) try: result, count = self.room_manager.search(ctx, limit=limit, offset=offset, terms=terms) result = map(_map_room_to_http_response, result) result = list(result) headers = { 'access-control-expose-headers': 'X-Total-Count', 'X-Total-Count': str(count) } return result, 200, headers except UserInputError as e: return bad_request(e), 400
def search(self, ctx, limit=DEFAULT_LIMIT, offset=DEFAULT_OFFSET, terms=None): LOG.debug("http_product_search_called", extra=log_extra(ctx, limit=limit, offset=offset, terms=terms)) try: result, count = self.product_manager.search(ctx, product_id=None, terms=terms) headers = { "X-Total-Count": count, 'access-control-expose-headers': 'X-Total-Count' } return list(map(_map_product_to_http_response, result)), 200, headers except UserInputError as e: return bad_request(e), 400 # 400 Bad Request
def put(self, ctx, port_id, body): """ Update a port in the database """ LOG.debug("http_port_put_called", extra=log_extra(ctx, port_id=port_id)) try: self.port_manager.update( ctx, port_id, MutationRequest( port_number=body.get('port_number'), room_number=body.get('room_number'), switch_id=body.get('switch_id'), rcom=0, # Add to spec. oid=None, # Add to spec. )) return NoContent, 204 except PortNotFoundError: return NoContent, 404 except UserInputError as e: return bad_request(e), 400