feat:
--Added Read Registered nodes API --Added DEL unregister Node
This commit is contained in:
1 parent
7a0e8c7cd6
commit
9977440c2d
4 files changed
+149
-18
No files matched your search
@@ -8,13 +8,17 @@ from psycopg2.extras import RealDictCursor
|
||||
import asyncio
|
||||
from fastapi.middleware.cors import CORSMiddleware
|
||||
|
||||
from database import Base, engine,SessionLocal
|
||||
from database import Base, engine,SessionLocal,get_db
|
||||
from uuid import UUID
|
||||
# import models
|
||||
from sqlalchemy.orm import Session
|
||||
from models import NodeInterface
|
||||
origins = [
|
||||
"http://localhost:5173",
|
||||
"http://127.0.0.1:5173",
|
||||
]
|
||||
import os
|
||||
from dotenv import load_dotenv
|
||||
|
||||
load_dotenv()
|
||||
|
||||
origins = os.getenv("CORS_ORIGINS", "").split(",")
|
||||
|
||||
|
||||
app = FastAPI()
|
||||
@@ -33,9 +37,9 @@ app.add_middleware(
|
||||
class RegisterNodeRequest(BaseModel):
|
||||
node_name: str
|
||||
host: str
|
||||
port: int = 5444
|
||||
dbname: str = "postgres"
|
||||
user: str = "postgres"
|
||||
port: int
|
||||
dbname: str
|
||||
user: str
|
||||
password: str
|
||||
|
||||
|
||||
@@ -44,14 +48,14 @@ class LinkNodeRequest(BaseModel):
|
||||
|
||||
# The node where sub_create() must run
|
||||
subscriber_host: str
|
||||
subscriber_port: int = 5444
|
||||
subscriber_port: int
|
||||
|
||||
# The provider node
|
||||
provider_host: str
|
||||
provider_port: int = 5444
|
||||
provider_port: int
|
||||
|
||||
dbname: str = "postgres"
|
||||
user: str = "postgres"
|
||||
dbname: str
|
||||
user: str
|
||||
password: str
|
||||
|
||||
|
||||
@@ -75,8 +79,9 @@ class LinkNodeRequest(BaseModel):
|
||||
# ============================================================
|
||||
# JWT TOKEN CREATION
|
||||
# ============================================================
|
||||
SECRET_KEY = "my-super-secret-key-change-this"
|
||||
ALGORITHM = "HS256"
|
||||
SECRET_KEY = os.getenv("SECRET_KEY")
|
||||
ALGORITHM = os.getenv("ALGORITHM", "HS256")
|
||||
# ALGORITHM = "HS256"
|
||||
|
||||
|
||||
@app.post("/get-token")
|
||||
@@ -378,6 +383,7 @@ def link_nodes(data: LinkNodeRequest,token_data: dict = Depends(verify_token)):
|
||||
if conn:
|
||||
conn.close()
|
||||
|
||||
#!!!!!!!!!!!!THIS IS A TEMPORARY FIXSS!!!!!!!!!!!!!
|
||||
|
||||
DB_CONFIG = {
|
||||
"host": "172.16.10.123",
|
||||
@@ -494,4 +500,59 @@ def get_spock_subscription_status(token_data: dict = Depends(verify_token),):
|
||||
|
||||
|
||||
|
||||
@app.get("/spock/node-interfaces")
|
||||
def get_node_interfaces(db: Session = Depends(get_db),token_data: dict = Depends(verify_token)):
|
||||
|
||||
try:
|
||||
|
||||
rows = db.query(NodeInterface).all()
|
||||
|
||||
return {
|
||||
"status": "success",
|
||||
"count": len(rows),
|
||||
"nodes": [
|
||||
{"uuid": str(row.uuid),
|
||||
"if_id": row.if_id,
|
||||
"if_name": row.if_name,
|
||||
"if_nodeid": row.if_nodeid,
|
||||
"host": row.host,
|
||||
"port": row.port,
|
||||
"dbname": row.dbname,
|
||||
"user": row.user
|
||||
}
|
||||
for row in rows
|
||||
]
|
||||
}
|
||||
|
||||
except Exception as e:
|
||||
raise HTTPException(
|
||||
status_code=500,
|
||||
detail=str(e)
|
||||
)
|
||||
|
||||
@app.delete("/spock/del-node-interfaces")
|
||||
def delete_node_interface(
|
||||
uuid: UUID,
|
||||
db: Session = Depends(get_db),
|
||||
token_data: dict = Depends(verify_token)
|
||||
):
|
||||
print("API called DEL API")
|
||||
print(f"uuid received: {uuid}")
|
||||
node = (
|
||||
db.query(NodeInterface)
|
||||
.filter(NodeInterface.uuid == uuid)
|
||||
.first()
|
||||
)
|
||||
if not node:
|
||||
raise HTTPException(
|
||||
status_code=404,
|
||||
detail="Node interface not found"
|
||||
)
|
||||
|
||||
# Print fetched record
|
||||
|
||||
return {
|
||||
"status": "success",
|
||||
"message": "API called DEL API",
|
||||
"uuid": str(uuid)
|
||||
}
|
||||
Reference in new issue
Block a user