|
59 | 59 | UserGroup, |
60 | 60 | ) |
61 | 61 | from .metrics import Metrics |
| 62 | +from pydantic import BaseModel |
62 | 63 |
|
63 | 64 |
|
64 | 65 | @asynccontextmanager |
@@ -771,6 +772,55 @@ async def put_node(node_id: str, node: Node, |
771 | 772 | return obj |
772 | 773 |
|
773 | 774 |
|
| 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 | + |
774 | 824 | async def _set_node_ownership_recursively(user: User, hierarchy: Hierarchy, |
775 | 825 | submitter: str, treeid: str): |
776 | 826 | """Set node ownership information for a hierarchy of nodes""" |
|
0 commit comments