georgs9 #42

Closed
Ghost wants to merge 7 commits from georgs9 into main
6 changed files with 239 additions and 7 deletions
Showing only changes of commit 56c62fe3f7 - Show all commits

View File

@@ -1,4 +1,4 @@
# Service Aware Network Project Repo # 1Service Aware Network Project Repo
Welcome to our website repository! This repo is designed to help you and your team build high-quality websites efficiently. We've carefully chosen the technologies to make development smooth and enjoyable. Here's what you can expect from this template: Welcome to our website repository! This repo is designed to help you and your team build high-quality websites efficiently. We've carefully chosen the technologies to make development smooth and enjoyable. Here's what you can expect from this template:

View File

@@ -33,6 +33,8 @@ async def lifespan(app: FastAPI) -> Any:
def setup_app() -> FastAPI: def setup_app() -> FastAPI:
# bind sql engine # bind sql engine
# TODO comment aut and add flag to run with pupulated data rm *.sql run pytest with marked then start clan webui
# https://docs.pytest.org/en/7.1.x/example/markers.html
sql_models.Base.metadata.drop_all(engine) sql_models.Base.metadata.drop_all(engine)
sql_models.Base.metadata.create_all(bind=engine) sql_models.Base.metadata.create_all(bind=engine)

View File

@@ -1,6 +1,8 @@
import time
from typing import List, Optional from typing import List, Optional
from fastapi import APIRouter, Depends import httpx
from fastapi import APIRouter, BackgroundTasks, Depends
from sqlalchemy.orm import Session from sqlalchemy.orm import Session
from .. import sql_crud, sql_db, sql_models from .. import sql_crud, sql_db, sql_models
@@ -139,8 +141,11 @@ def get_repository(
@router.post("/api/v1/create_entity", response_model=Entity, tags=[Tags.entities]) @router.post("/api/v1/create_entity", response_model=Entity, tags=[Tags.entities])
def create_entity( def create_entity(
entity: EntityCreate, db: Session = Depends(sql_db.get_db) entity: EntityCreate, db: Session = Depends(sql_db.get_db)
) -> EntityCreate: ) -> EntityCreate | str:
# todo checken ob schon da ... # todo checken ob schon da ...
if sql_crud.get_entity_by_did(db, did=entity.did):
print("did already exsists")
return "Error did already exsists in db"
return sql_crud.create_entity(db, entity) return sql_crud.create_entity(db, entity)
@@ -155,9 +160,67 @@ def get_entities(
@router.get("/api/v1/get_entity", response_model=Optional[Entity], tags=[Tags.entities]) @router.get("/api/v1/get_entity", response_model=Optional[Entity], tags=[Tags.entities])
def get_entity( def get_entity(
entity_did: str = "did:sov:test:1234", entity_did: str = "did:sov:test:1234",
skip: int = 0,
limit: int = 100,
db: Session = Depends(sql_db.get_db), db: Session = Depends(sql_db.get_db),
) -> Optional[sql_models.Entity]: ) -> Optional[sql_models.Entity]:
entity = sql_crud.get_entity_by_did(db, did=entity_did) entity = sql_crud.get_entity_by_did(db, did=entity_did)
return entity return entity
@router.get(
"/api/v1/get_attached_entities",
response_model=List[Entity],
tags=[Tags.entities],
)
def get_attached_entities(
skip: int = 0, limit: int = 100, db: Session = Depends(sql_db.get_db)
) -> List[sql_models.Entity]:
entities = sql_crud.get_attached_entities(db, skip=skip, limit=limit)
return entities
@router.get("/api/v1/detach")
async def detach(
background_tasks: BackgroundTasks,
entity_did: str = "did:sov:test:1234",
skip: int = 0,
limit: int = 100,
db: Session = Depends(sql_db.get_db),
) -> dict[str, str]:
background_tasks.add_task(
sql_crud.set_attached_by_entity_did, db, entity_did, False
)
return {"message": "Detaching in the background"}
@router.get("/api/v1/attach")
async def attach(
background_tasks: BackgroundTasks,
entity_did: str = "did:sov:test:1234",
skip: int = 0,
limit: int = 100,
db: Session = Depends(sql_db.get_db),
) -> dict[str, str]:
background_tasks.add_task(attach_entity, db, entity_did)
return {"message": "Attaching in the background"}
# TODO
def attach_entity(db: Session, entity_did: str) -> None:
db_entity = sql_crud.set_attached_by_entity_did(db, entity_did, True)
try:
if db_entity is not None:
while db_entity.attached:
# query status endpoint
# https://www.python-httpx.org/
response = httpx.get(f"http://{db_entity.ip}", timeout=2)
print(response)
# test with:
# while true ; do printf 'HTTP/1.1 200 OK\r\n\r\ncool, thanks' | nc -l -N localhost 5555 ; done
# client test (apt install python3-httpx):
# httpx http://localhost:5555
# except not reached set false
time.sleep(1)
except Exception as e:
print(e)
if db_entity is not None:
db_entity = sql_crud.set_attached_by_entity_did(db, entity_did, False)

View File

@@ -1,6 +1,7 @@
from typing import List, Optional from typing import List, Optional
from sqlalchemy.orm import Session from sqlalchemy.orm import Session
from sqlalchemy.sql.expression import true
from . import schemas, sql_models from . import schemas, sql_models
@@ -136,3 +137,37 @@ def get_entities(
def get_entity_by_did(db: Session, did: str) -> Optional[sql_models.Entity]: def get_entity_by_did(db: Session, did: str) -> Optional[sql_models.Entity]:
return db.query(sql_models.Entity).filter(sql_models.Entity.did == did).first() return db.query(sql_models.Entity).filter(sql_models.Entity.did == did).first()
# get attached
def get_attached_entities(
db: Session, skip: int = 0, limit: int = 100
) -> List[sql_models.Entity]:
return (
db.query(sql_models.Entity)
.filter(sql_models.Entity.attached == true())
# https://stackoverflow.com/questions/18998010/flake8-complains-on-boolean-comparison-in-filter-clause
.offset(skip)
.limit(limit)
.all()
)
# set attached
# None if did not found
# Returns same entity if setting didnt changed something
def set_attached_by_entity_did(
db: Session, entity_did: str, value: bool
) -> Optional[sql_models.Entity]:
# ste attached to true
db_entity = get_entity_by_did(db, entity_did)
if db_entity is not None:
# db_entity.attached = Column(True)
setattr(db_entity, "attached", value)
# save changes in db
db.add(db_entity)
db.commit()
db.refresh(db_entity)
return db_entity
else:
return db_entity

View File

@@ -144,6 +144,66 @@ def test_producer5(api: TestClient) -> None:
def test_producer2(api: TestClient) -> None:
request_body = {
"uuid": "8e285c0c-4e40-430a-a477-26b3b81e30d1",
"service_name": "Luis'''s Fax",
"service_type": "Fax",
"endpoint_url": "http://127.0.0.1:8001",
"status": "unknown",
"other": {"faxen": "dicke"},
"entity_did": default_entity_did2,
}
paramter = "producer"
get_request = "entity_did=" + url.quote(default_entity_did2)
make_test_post_and_get(api, request_body, paramter, get_request)
def test_producer3(api: TestClient) -> None:
request_body = {
"uuid": "8e285c0c-4e40-430a-a477-26b3b81e30d2",
"service_name": "Erdem'''s VR-Stream",
"service_type": "VR-Stream",
"endpoint_url": "http://127.0.0.1:8002",
"status": "unknown",
"other": {"oculos": "rift"},
"entity_did": default_entity_did3,
}
paramter = "producer"
get_request = "entity_did=" + url.quote(default_entity_did3)
make_test_post_and_get(api, request_body, paramter, get_request)
def test_producer4(api: TestClient) -> None:
request_body = {
"uuid": "8e285c0c-4e40-430a-a477-26b3b81e30d3",
"service_name": "Onur'''s gallary",
"service_type": "gallary",
"endpoint_url": "http://127.0.0.1:8003",
"status": "unknown",
"other": {"nice": "pics"},
"entity_did": default_entity_did4,
}
paramter = "producer"
get_request = "entity_did=" + url.quote(default_entity_did4)
make_test_post_and_get(api, request_body, paramter, get_request)
def test_producer5(api: TestClient) -> None:
request_body = {
"uuid": "8e285c0c-4e40-430a-a477-26b3b81e30d4",
"service_name": "Sara'''s Game-Shop",
"service_type": "Game-Shop",
"endpoint_url": "http://127.0.0.1:8004",
"status": "unknown",
"other": {"war": "games"},
"entity_did": default_entity_did5,
}
paramter = "producer"
get_request = "entity_did=" + url.quote(default_entity_did5)
make_test_post_and_get(api, request_body, paramter, get_request)
######################### #########################
# # # #
# Consumer # # Consumer #
@@ -169,6 +229,17 @@ def test_consumer2(api: TestClient) -> None:
get_request = "entity_did="+url.quote(default_entity_did2) get_request = "entity_did="+url.quote(default_entity_did2)
make_test_post_and_get(api, request_body, paramter, get_request) make_test_post_and_get(api, request_body, paramter, get_request)
def test_consumer2(api: TestClient) -> None:
request_body = {
"entity_did": default_entity_did2,
"producer_uuid": "8e285c0c-4e40-430a-a477-26b3b81e30d4",
"other": {"war": "games"},
}
paramter = "consumer"
get_request = "entity_did=" + url.quote(default_entity_did2)
make_test_post_and_get(api, request_body, paramter, get_request)
######################### #########################
# # # #
# REPOSITORY # # REPOSITORY #
@@ -247,6 +318,66 @@ def test_repository5(api: TestClient) -> None:
def test_repository2(api: TestClient) -> None:
request_body = {
"uuid": "8e285c0c-4e40-430a-a477-26b3b81e30d1",
"service_name": "Luis'''s Fax",
"service_type": "Fax",
"endpoint_url": "http://127.0.0.1:8001",
"status": "unknown",
"other": {"faxen": "dicke"},
"entity_did": default_entity_did2,
}
paramter = "repository"
get_request = "entity_did=" + url.quote(default_entity_did2)
make_test_post_and_get(api, request_body, paramter, get_request)
def test_repository3(api: TestClient) -> None:
request_body = {
"uuid": "8e285c0c-4e40-430a-a477-26b3b81e30d2",
"service_name": "Erdem'''s VR-Stream",
"service_type": "VR-Stream",
"endpoint_url": "http://127.0.0.1:8002",
"status": "unknown",
"other": {"oculos": "rift"},
"entity_did": default_entity_did3,
}
paramter = "repository"
get_request = "entity_did=" + url.quote(default_entity_did3)
make_test_post_and_get(api, request_body, paramter, get_request)
def test_repository4(api: TestClient) -> None:
request_body = {
"uuid": "8e285c0c-4e40-430a-a477-26b3b81e30d3",
"service_name": "Onur'''s gallary",
"service_type": "gallary",
"endpoint_url": "http://127.0.0.1:8003",
"status": "unknown",
"other": {"nice": "pics"},
"entity_did": default_entity_did4,
}
paramter = "repository"
get_request = "entity_did=" + url.quote(default_entity_did4)
make_test_post_and_get(api, request_body, paramter, get_request)
def test_repository5(api: TestClient) -> None:
request_body = {
"uuid": "8e285c0c-4e40-430a-a477-26b3b81e30d4",
"service_name": "Sara'''s Game-Shop",
"service_type": "Game-Shop",
"endpoint_url": "http://127.0.0.2:8004",
"status": "unknown",
"other": {"war": "games"},
"entity_did": default_entity_did5,
}
paramter = "repository"
get_request = "entity_did=" + url.quote(default_entity_did5)
make_test_post_and_get(api, request_body, paramter, get_request)
######################### #########################
# # # #
# Entity # # Entity #
@@ -264,6 +395,7 @@ def test_entity(api: TestClient) -> None:
# get_request = "entity_did=did%3Asov%3Atest%3A1234" # get_request = "entity_did=did%3Asov%3Atest%3A1234"
make_test_post_and_get(api, request_body, paramter) make_test_post_and_get(api, request_body, paramter)
def test_entity2(api: TestClient) -> None: def test_entity2(api: TestClient) -> None:
request_body = { request_body = {
"did": default_entity_did2, "did": default_entity_did2,