Skip to content

Commit d982b4a

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 d982b4a

File tree

1 file changed

+48
-0
lines changed

1 file changed

+48
-0
lines changed

api/main.py

Lines changed: 48 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
@@ -770,6 +771,53 @@ async def put_node(node_id: str, node: Node,
770771
await db.create(evhist)
771772
return obj
772773

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

774822
async def _set_node_ownership_recursively(user: User, hierarchy: Hierarchy,
775823
submitter: str, treeid: str):

0 commit comments

Comments
 (0)