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

feat(main.py): Implement /nodes/set endpoint #584

Merged
merged 1 commit into from
Dec 5, 2024
Merged
Changes from all 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
50 changes: 50 additions & 0 deletions api/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
from pymongo.errors import DuplicateKeyError
from fastapi_users import FastAPIUsers
from beanie import PydanticObjectId
from kernelci.api.models import (

Check failure on line 37 in api/main.py

View workflow job for this annotation

GitHub Actions / Lint

Unable to import 'kernelci.api.models'
Node,
Hierarchy,
PublishEvent,
Expand All @@ -59,6 +59,7 @@
UserGroup,
)
from .metrics import Metrics
from pydantic import BaseModel

Check warning on line 62 in api/main.py

View workflow job for this annotation

GitHub Actions / Lint

third party import "from pydantic import BaseModel" should be placed before "from .auth import Authentication"


@asynccontextmanager
Expand Down Expand Up @@ -713,7 +714,7 @@


@app.put('/node/{node_id}', response_model=Node, response_model_by_alias=False)
async def put_node(node_id: str, node: Node,

Check warning on line 717 in api/main.py

View workflow job for this annotation

GitHub Actions / Lint

Too many local variables (16/15)
user: str = Depends(authorize_user),
noevent: Optional[bool] = Query(None)):
"""Update an already added node"""
Expand Down Expand Up @@ -771,6 +772,55 @@
return obj


class NodeUpdateRequest(BaseModel):

Check warning on line 775 in api/main.py

View workflow job for this annotation

GitHub Actions / Lint

Missing class docstring
nodes: List[str]
field: str
value: str


@app.put('/batch/nodeset', response_model=int)
async def put_batch_nodeset(data: NodeUpdateRequest,
user: str = Depends(get_current_user)):
"""
Set a field to a value for multiple nodes
TBD: Make db.bulkupdate to update multiple nodes in one go
"""
metrics.add('http_requests_total', 1)
updated = 0
nodes = data.nodes
field = data.field
value = data.value
for node_id in nodes:
node_from_id = await db.find_by_id(Node, node_id)
if not node_from_id:
raise HTTPException(
status_code=status.HTTP_404_NOT_FOUND,
detail=f"Node not found with id: {node_id}"
)
# verify ownership
if not user.username == node_from_id.owner:
raise HTTPException(
status_code=status.HTTP_401_UNAUTHORIZED,
detail="Unauthorized to complete the operation"
)
# right now we support only field:
# processed_by_kcidb_bridge, also value should be boolean
if field == 'processed_by_kcidb_bridge':
if value == 'true' or value == 'True':

Check warning on line 809 in api/main.py

View workflow job for this annotation

GitHub Actions / Lint

Consider merging these comparisons with "in" to "value in ('true', 'True')"
value = True
elif value == 'false' or value == 'False':

Check warning on line 811 in api/main.py

View workflow job for this annotation

GitHub Actions / Lint

Consider merging these comparisons with "in" to "value in ('false', 'False')"
value = False
setattr(node_from_id, field, value)
await db.update(node_from_id)
updated += 1
else:
raise HTTPException(
status_code=status.HTTP_400_BAD_REQUEST,
detail="Field not supported"
)
return updated


async def _set_node_ownership_recursively(user: User, hierarchy: Hierarchy,
submitter: str, treeid: str):
"""Set node ownership information for a hierarchy of nodes"""
Expand Down
Loading