Skip to content

Commit

Permalink
Query servicepath by path and not just ID (#221)
Browse files Browse the repository at this point in the history
* Query servicepath by path and not just ID

* Update RELEASE_NOTES.md

* Some tests

* autopep8 action fixes (#222)

Co-authored-by: Cerfoglg <[email protected]>

* Test fixes

---------

Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
Co-authored-by: Cerfoglg <[email protected]>
  • Loading branch information
3 people authored Apr 24, 2023
1 parent 2e69fad commit aef6d9d
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 15 deletions.
1 change: 1 addition & 0 deletions RELEASE_NOTES.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
- Push model for policies, plus scheduler for pushing on the hour
- /me endpoint for policies shows foaf:Agent type policies when no token
provided
- Query servicepaths by path and not just ID

### Bug fixes

Expand Down
9 changes: 9 additions & 0 deletions anubis-management-api/anubis/tenants/operations.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,15 @@ def get_tenant_service_path_by_path(db: Session, tenant_id: str, path: str):
models.ServicePath.path.asc()).all()


def get_tenant_single_service_path_by_path(
db: Session, tenant_id: str, path: str):
return db.query(
models.ServicePath).filter(
models.ServicePath.tenant_id == tenant_id).filter(
models.ServicePath.path == path).order_by(
models.ServicePath.path.asc()).first()


def get_tenant_service_path(db: Session, service_path_id: str, tenant_id: str):
return db.query(
models.ServicePath).filter(
Expand Down
19 changes: 10 additions & 9 deletions anubis-management-api/anubis/tenants/routers.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ def read_service_paths(
summary="List all Tenants")
async def read_tenants(skip: int = 0, limit: int = 100, db: Session = Depends(get_db)):
services = operations.get_tenants(db, skip=skip, limit=limit)
# services = list(map(lambda s: {"name":s.name, "id":s.id}, services))
return services


Expand Down Expand Up @@ -332,7 +333,7 @@ def read_tenant_service_paths(
return service_paths


@router.get("/{tenant_id}/service_paths/{service_path_id}",
@router.get("/{tenant_id}/service_paths/{service_path_id:path}",
response_model=schemas.ServicePath,
summary="Get a Service Path inside a Tenant")
def read_service_path(
Expand All @@ -347,10 +348,11 @@ def read_service_path(
tenant_id = db_tenant.id
service_path = operations.get_tenant_service_path(
db, service_path_id=service_path_id, tenant_id=tenant_id)
# Federico commented these lines because he is not sure what's the purpose.
# if not service_path:
# service_path = operations.get_tenant_service_path_by_path(
# db, tenant_id=tenant_id, path="/" + service_path_id)
if not service_path:
service_path = operations.get_tenant_single_service_path_by_path(
db, tenant_id=tenant_id, path="/" + service_path_id)
if not service_path:
raise HTTPException(status_code=404, detail="Service Path not found")
return service_path

# TODO how to handle changes on the tree? either path is dynamically computed, or this is cumbersome
Expand All @@ -376,10 +378,9 @@ def delete_service_path(
tenant_id = db_tenant.id
db_service_path = operations.get_tenant_service_path(
db, service_path_id=service_path_id, tenant_id=tenant_id)
# Federico commented these lines because he is not sure what's the purpose.
# if not db_service_path:
# db_service_path = operations.get_tenant_service_path_by_path(
# db, tenant_id=tenant_id, path="/" + service_path_id)
if not db_service_path:
db_service_path = operations.get_tenant_single_service_path_by_path(
db, tenant_id=tenant_id, path="/" + service_path_id)
if not db_service_path:
raise HTTPException(status_code=404, detail="ServicePath not found")
if db_service_path.path == '/':
Expand Down
18 changes: 12 additions & 6 deletions anubis-management-api/anubis/tests/test_tenants.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ def test_service_paths(test_db):
response = client.get("/v1/tenants/" + tenant_id + "/service_paths/")
body = response.json()
assert response.status_code == 200
assert body[0]["path"] == "/"
assert body["path"] == "/"

response = client.post(
"/v1/tenants/" + tenant_id + "/service_paths",
Expand All @@ -59,26 +59,32 @@ def test_service_paths(test_db):
service_path_id = response.headers["Service-Path-ID"]
assert service_path_id

response = client.get("/v1/tenants/" + tenant_id + "/service_paths/")
response = client.get("/v1/tenants/" + tenant_id + "/service_paths")
body = response.json()
assert response.status_code == 200
assert len(body) == 3

response = client.get("/v1/tenants/test/service_paths/")
body = response.json()
assert response.status_code == 200
assert body["path"] == "/"
assert len(body["children"]) == 1

response = client.get(
"/v1/tenants/" +
tenant_id +
"/service_paths/?name=/foobar/barbar")
"/service_paths/foobar/barbar")
body = response.json()
assert response.status_code == 200
assert len(body) == 1
assert body["path"] == "/foobar/barbar"

response = client.get(
"/v1/tenants/" +
tenant_id +
"/service_paths/?name=/foobar")
"/service_paths/foobar")
body = response.json()
assert response.status_code == 200
assert len(body) == 2
assert body["path"] == "/foobar"

response = client.get(
"/v1/tenants/" +
Expand Down

0 comments on commit aef6d9d

Please sign in to comment.