Skip to content

Commit 4fadfe3

Browse files
committed
feat(main.py): Implement /nodes/set endpoint
Implement batch "field to value" bulk set to improve speed of some batch operations. This way we can update field on multiple nodes to particular value at once. Signed-off-by: Denys Fedoryshchenko <denys.f@collabora.com>
1 parent 22febf3 commit 4fadfe3

File tree

1 file changed

+50
-0
lines changed

1 file changed

+50
-0
lines changed

api/main.py

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@
5959
UserGroup,
6060
)
6161
from .metrics import Metrics
62+
from pydantic import BaseModel
6263

6364

6465
@asynccontextmanager
@@ -771,6 +772,55 @@ async def put_node(node_id: str, node: Node,
771772
return obj
772773

773774

775+
class NodeUpdateRequest(BaseModel):
776+
nodes: List[str]
777+
field: str
778+
value: str
779+
780+
781+
@app.put('/batch/nodeset', response_model=int)
782+
async def put_batch_nodeset(data: NodeUpdateRequest,
783+
user: str = Depends(get_current_user)):
784+
"""
785+
Set a field to a value for multiple nodes
786+
TBD: Make db.bulkupdate to update multiple nodes in one go
787+
"""
788+
metrics.add('http_requests_total', 1)
789+
updated = 0
790+
nodes = data.nodes
791+
field = data.field
792+
value = data.value
793+
for node_id in nodes:
794+
node_from_id = await db.find_by_id(Node, node_id)
795+
if not node_from_id:
796+
raise HTTPException(
797+
status_code=status.HTTP_404_NOT_FOUND,
798+
detail=f"Node not found with id: {node_id}"
799+
)
800+
# verify ownership
801+
if not user.username == node_from_id.owner:
802+
raise HTTPException(
803+
status_code=status.HTTP_401_UNAUTHORIZED,
804+
detail="Unauthorized to complete the operation"
805+
)
806+
# right now we support only field:
807+
# processed_by_kcidb_bridge, also value should be boolean
808+
if field == 'processed_by_kcidb_bridge':
809+
if value == 'true' or value == 'True':
810+
value = True
811+
elif value == 'false' or value == 'False':
812+
value = False
813+
setattr(node_from_id, field, value)
814+
await db.update(node_from_id)
815+
updated += 1
816+
else:
817+
raise HTTPException(
818+
status_code=status.HTTP_400_BAD_REQUEST,
819+
detail="Field not supported"
820+
)
821+
return updated
822+
823+
774824
async def _set_node_ownership_recursively(user: User, hierarchy: Hierarchy,
775825
submitter: str, treeid: str):
776826
"""Set node ownership information for a hierarchy of nodes"""

0 commit comments

Comments
 (0)