Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/SK-933 | Add delete combiner/client to Api #693

Merged
merged 3 commits into from
Sep 4, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
43 changes: 43 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,46 @@
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 as e:
# return jsonify({"message": "An unexpected error occurred"}), 500
return jsonify({"message": e}), 500
Fixed Show fixed Hide fixed
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
Loading