Skip to content

Commit

Permalink
Feature/SK-933 | Add delete combiner/client to Api (#693)
Browse files Browse the repository at this point in the history
  • Loading branch information
niklastheman authored Sep 4, 2024
1 parent b351f42 commit 55c9da4
Show file tree
Hide file tree
Showing 5 changed files with 110 additions and 3 deletions.
44 changes: 44 additions & 0 deletions fedn/network/api/v1/client_routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -368,3 +368,47 @@ def get_client(id: str):
return jsonify({"message": f"Entity with id: {id} not found"}), 404
except Exception:
return jsonify({"message": "An unexpected error occurred"}), 500

# delete client
@bp.route("/<string:id>", methods=["DELETE"])
@jwt_auth_required(role="admin")
def delete_client(id: str):
"""Delete client
Deletes a client based on the provided id.
---
tags:
- Clients
parameters:
- name: id
in: path
required: true
type: string
description: The id of the client
responses:
200:
description: The client was deleted
404:
description: The client was not found
schema:
type: object
properties:
message:
type: string
500:
description: An error occurred
schema:
type: object
properties:
message:
type: string
"""
try:
result: bool = client_store.delete(id)

msg = "Client deleted" if result else "Client not deleted"

return jsonify({"message": msg}), 200
except EntityNotFound:
return jsonify({"message": f"Entity with id: {id} not found"}), 404
except Exception:
return jsonify({"message": "An unexpected error occurred"}), 500
42 changes: 42 additions & 0 deletions fedn/network/api/v1/combiner_routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -339,3 +339,45 @@ def get_combiner(id: str):
return jsonify({"message": f"Entity with id: {id} not found"}), 404
except Exception:
return jsonify({"message": "An unexpected error occurred"}), 500

@bp.route("/<string:id>", methods=["DELETE"])
@jwt_auth_required(role="admin")
def delete_combiner(id: str):
"""Delete combiner
Deletes a combiner based on the provided id.
---
tags:
- Combiners
parameters:
- name: id
in: path
required: true
type: string
description: The id of the combiner
responses:
200:
description: The combiner was deleted
404:
description: The combiner was not found
schema:
type: object
properties:
error:
type: string
500:
description: An error occurred
schema:
type: object
properties:
message:
type: string
"""
try:
result: bool = combiner_store.delete(id)
msg = "Combiner deleted" if result else "Combiner not deleted"

return jsonify({"message": msg}), 200
except EntityNotFound:
return jsonify({"message": f"Entity with id: {id} not found"}), 404
except Exception:
return jsonify({"message": "An unexpected error occurred"}), 500
12 changes: 11 additions & 1 deletion fedn/network/storage/statestore/stores/client_store.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,13 @@
from typing import Any, Dict, List, Tuple

import pymongo
from bson import ObjectId
from pymongo.database import Database

from fedn.network.storage.statestore.stores.store import Store

from .shared import EntityNotFound


class Client:
def __init__(self, id: str, name: str, combiner: str, combiner_preferred: str, ip: str, status: str, updated_at: str, last_seen: datetime):
Expand Down Expand Up @@ -53,7 +56,14 @@ def add(self, item: Client)-> Tuple[bool, Any]:
raise NotImplementedError("Add not implemented for ClientStore")

def delete(self, id: str) -> bool:
raise NotImplementedError("Delete not implemented for ClientStore")
kwargs = { "_id": ObjectId(id) } if ObjectId.is_valid(id) else { "client_id": id }

document = self.database[self.collection].find_one(kwargs)

if document is None:
raise EntityNotFound(f"Entity with (id | client_id) {id} not found")

return super().delete(document["_id"])

def list(self, limit: int, skip: int, sort_key: str, sort_order=pymongo.DESCENDING, use_typing: bool = False, **kwargs) -> Dict[int, List[Client]]:
"""List entities
Expand Down
12 changes: 11 additions & 1 deletion fedn/network/storage/statestore/stores/combiner_store.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,17 @@ def add(self, item: Combiner)-> Tuple[bool, Any]:
raise NotImplementedError("Add not implemented for CombinerStore")

def delete(self, id: str) -> bool:
raise NotImplementedError("Delete not implemented for CombinerStore")
if(ObjectId.is_valid(id)):
kwargs = { "_id": ObjectId(id)}
else:
return False

document = self.database[self.collection].find_one(kwargs)

if document is None:
raise EntityNotFound(f"Entity with (id) {id} not found")

return super().delete(document["_id"])

def list(self, limit: int, skip: int, sort_key: str, sort_order=pymongo.DESCENDING, use_typing: bool = False, **kwargs) -> Dict[int, List[Combiner]]:
"""List entities
Expand Down
3 changes: 2 additions & 1 deletion fedn/network/storage/statestore/stores/store.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,8 @@ def add(self, item: T) -> Tuple[bool, Any]:
return False, str(e)

def delete(self, id: str) -> bool:
pass
result = self.database[self.collection].delete_one({"_id": ObjectId(id)})
return result.deleted_count == 1

def list(self, limit: int, skip: int, sort_key: str, sort_order=pymongo.DESCENDING, use_typing: bool = False, **kwargs) -> Dict[int, List[T]]:
"""List entities
Expand Down

0 comments on commit 55c9da4

Please sign in to comment.