Add delete peer method (#217)

* Add delete peer method

* Include error details when peer resource not found
This commit is contained in:
Jonathan Zernik 2020-08-04 03:07:32 -07:00 committed by GitHub
parent 089e0d8fbf
commit e7a515fae3
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 64 additions and 9 deletions

View file

@ -437,11 +437,11 @@ def test_delete_squeak(server_stub, admin_stub, saved_squeak_hash):
)
# Try to get the squeak and fail
with pytest.raises(Exception):
get_response = server_stub.GetSqueak(
with pytest.raises(Exception) as excinfo:
server_stub.GetSqueak(
squeak_server_pb2.GetSqueakRequest(hash=saved_squeak_hash)
)
assert "Squeak not found." in str(excinfo.value)
def test_create_peer(server_stub, admin_stub):
# Add a new peer
@ -516,3 +516,20 @@ def test_set_peer_uploading(server_stub, admin_stub, peer_id):
)
)
assert get_peer_response.squeak_peer.uploading == True
def test_delete_peer(server_stub, admin_stub, peer_id):
# Delete the peer
admin_stub.DeletePeer(
squeak_admin_pb2.DeletePeerRequest(
peer_id=peer_id,
)
)
# Try to get the peer and fail
with pytest.raises(Exception) as excinfo:
admin_stub.GetPeer(
squeak_admin_pb2.GetPeerRequest(
peer_id=peer_id,
)
)
assert "Peer not found." in str(excinfo.value)

View file

@ -102,6 +102,10 @@ service SqueakAdmin {
*/
rpc SetPeerUploading (SetPeerUploadingRequest) returns (SetPeerUploadingReply) {}
/** sqkadmin: `deletepeer`
*/
rpc DeletePeer (DeletePeerRequest) returns (DeletePeerReply) {}
}
message HelloRequest {
@ -396,3 +400,11 @@ message SetPeerUploadingRequest {
message SetPeerUploadingReply {
}
message DeletePeerRequest {
/// The peer id
int32 peer_id = 1;
}
message DeletePeerReply {
}

View file

@ -191,3 +191,7 @@ class SqueakAdminServerHandler(object):
)
)
self.squeak_node.set_peer_uploading(peer_id, uploading)
def handle_delete_squeak_peer(self, peer_id):
logger.info("Handle delete squeak peer with id: {}".format(peer_id))
self.squeak_node.delete_peer(peer_id)

View file

@ -168,6 +168,10 @@ class SqueakAdminServerServicer(squeak_admin_pb2_grpc.SqueakAdminServicer):
def GetPeer(self, request, context):
peer_id = request.peer_id
squeak_peer = self.handler.handle_get_squeak_peer(peer_id)
if squeak_peer is None:
context.set_code(grpc.StatusCode.NOT_FOUND)
context.set_details('Peer not found.')
return squeak_admin_pb2.GetPeerReply()
squeak_peer_msg = self._squeak_peer_to_message(squeak_peer)
return squeak_admin_pb2.GetPeerReply(
squeak_peer=squeak_peer_msg,
@ -201,6 +205,11 @@ class SqueakAdminServerServicer(squeak_admin_pb2_grpc.SqueakAdminServicer):
)
return squeak_admin_pb2.SetPeerUploadingReply()
def DeletePeer(self, request, context):
peer_id = request.peer_id
self.handler.handle_delete_squeak_peer(peer_id)
return squeak_admin_pb2.DeletePeerReply()
def _squeak_entry_to_message(self, squeak_entry_with_profile):
if squeak_entry_with_profile is None:
return None

View file

@ -228,3 +228,6 @@ class SqueakNode:
def set_peer_uploading(self, peer_id, uploading):
self.postgres_db.set_peer_uploading(peer_id, uploading)
def delete_peer(self, peer_id):
self.postgres_db.delete_peer(peer_id)

View file

@ -39,6 +39,8 @@ class SqueakStore:
def get_public_squeak(self, squeak_hash):
squeak_entry = self.postgres_db.get_squeak_entry(squeak_hash)
if squeak_entry is None:
return None
squeak = squeak_entry.squeak
# Remove the decryption key before returning.
squeak.ClearDecryptionKey()

View file

@ -458,7 +458,17 @@ class PostgresDb:
with self.get_cursor() as curs:
curs.execute(sql, (uploading, peer_id,))
def delete_peer(self, peer_id):
""" Delete a peer. """
sql = """
DELETE FROM peer WHERE peer_id=%s;
"""
with self.get_cursor() as curs:
curs.execute(sql, (peer_id,))
def _parse_squeak_entry(self, row):
if row is None:
return None
vch_decryption_key_column = row["vch_decryption_key"]
vch_decryption_key = (
bytes(vch_decryption_key_column) if vch_decryption_key_column else b''

View file

@ -43,7 +43,8 @@ class SqueakServerServicer(squeak_server_pb2_grpc.SqueakServerServicer):
squeak = self.handler.handle_get_squeak(squeak_hash)
if squeak == None:
context.set_code(grpc.StatusCode.INVALID_ARGUMENT)
context.set_code(grpc.StatusCode.NOT_FOUND)
context.set_details('Squeak not found.')
return squeak_server_pb2.GetSqueakReply(squeak=None,)
return squeak_server_pb2.GetSqueakReply(
@ -67,16 +68,13 @@ class SqueakServerServicer(squeak_server_pb2_grpc.SqueakServerServicer):
buy_response = self.handler.handle_buy_squeak(squeak_hash, challenge)
if buy_response == None:
context.set_code(grpc.StatusCode.INVALID_ARGUMENT)
context.set_code(grpc.StatusCode.NOT_FOUND)
context.set_details('Squeak not found.')
return squeak_server_pb2.BuySqueakReply(offer=None,)
offer_squeak_hash = buy_response.squeak_hash
amount = buy_response.amount
if offer_squeak_hash != squeak_hash:
context.set_code(grpc.StatusCode.INVALID_ARGUMENT)
return squeak_server_pb2.BuySqueakReply(offer=None,)
return squeak_server_pb2.BuySqueakReply(
offer=squeak_server_pb2.SqueakBuyOffer(
squeak_hash=offer_squeak_hash,