feat:
--Added link nodes abstraction
This commit is contained in:
1 parent
254f8f1816
commit
96be1fc94e
1 file changed
+98
-4
@@ -15,6 +15,7 @@ from sqlalchemy.orm import Session
|
||||
from models import NodeInterface
|
||||
import os
|
||||
from dotenv import load_dotenv
|
||||
from sqlalchemy.exc import SQLAlchemyError
|
||||
|
||||
load_dotenv()
|
||||
|
||||
@@ -59,6 +60,10 @@ class LinkNodeRequest(BaseModel):
|
||||
password: str
|
||||
|
||||
|
||||
class NodeLinkRequest(BaseModel):
|
||||
from_node: str
|
||||
to_node: str
|
||||
|
||||
#POST /register-node
|
||||
# {
|
||||
# "node_name": "node2",
|
||||
@@ -304,8 +309,9 @@ def register_node(data: RegisterNodeRequest, token_data: dict = Depends(verify_t
|
||||
# "user": "postgres",
|
||||
# "password": "postgres"
|
||||
# }
|
||||
@app.post("/link-nodes")
|
||||
def link_nodes(data: LinkNodeRequest,token_data: dict = Depends(verify_token)):
|
||||
#!!!!!!!!!!!!INIT synchronize_structure := false, synchronize_data := false !!!!!!!!!!!
|
||||
# @app.post("/link-nodes")
|
||||
def link_nodes(data: LinkNodeRequest):
|
||||
|
||||
conn = None
|
||||
cur = None
|
||||
@@ -363,8 +369,6 @@ def link_nodes(data: LinkNodeRequest,token_data: dict = Depends(verify_token)):
|
||||
"subscription_name": data.subscription_name,
|
||||
"subscriber": data.subscriber_host,
|
||||
"provider": data.provider_host,
|
||||
"authenticated_as": token_data["sub"],
|
||||
|
||||
}
|
||||
|
||||
except Exception as e:
|
||||
@@ -596,3 +600,93 @@ def delete_node_interface(
|
||||
if conn:
|
||||
conn.close()
|
||||
|
||||
|
||||
@app.post("/spock/link-nodes-abstract")
|
||||
def link_nodes_abstract(data: NodeLinkRequest,db: Session = Depends(get_db),token_data: dict = Depends(verify_token)):
|
||||
|
||||
|
||||
try:
|
||||
from_node = data.from_node
|
||||
to_node = data.to_node
|
||||
|
||||
print("From Node:", from_node)
|
||||
from_node_data = (
|
||||
db.query(NodeInterface)
|
||||
.filter(NodeInterface.if_name == from_node)
|
||||
.first()
|
||||
)
|
||||
if not from_node_data:
|
||||
raise HTTPException(
|
||||
status_code=404,
|
||||
detail=f"Node '{from_node}' not found"
|
||||
)
|
||||
print("fromFound:", from_node_data.if_name)
|
||||
print("fromHost:", from_node_data.host)
|
||||
print("fromPort:", from_node_data.port)
|
||||
print("fromDbname:", from_node_data.dbname)
|
||||
print("fromUser:", from_node_data.user)
|
||||
|
||||
#================================================
|
||||
print("===============================================")
|
||||
print("To Node:", to_node)
|
||||
to_node_data = (
|
||||
db.query(NodeInterface)
|
||||
.filter(NodeInterface.if_name == to_node)
|
||||
.first()
|
||||
)
|
||||
if not to_node_data:
|
||||
raise HTTPException(
|
||||
status_code=404,
|
||||
detail=f"Node '{to_node}' not found"
|
||||
)
|
||||
print("toFound:", to_node_data.if_name)
|
||||
print("toHost:", to_node_data.host)
|
||||
print("toPort:", to_node_data.port)
|
||||
print("toDbname:", to_node_data.dbname)
|
||||
print("toUser:", to_node_data.user)
|
||||
|
||||
#=============================================================================
|
||||
# Build internal function input
|
||||
link_data = LinkNodeRequest(
|
||||
subscription_name=f"{to_node}_from_{from_node}",
|
||||
|
||||
subscriber_host=to_node_data.host,
|
||||
subscriber_port=to_node_data.port,
|
||||
|
||||
provider_host=from_node_data.host,
|
||||
provider_port=from_node_data.port,
|
||||
|
||||
dbname=to_node_data.dbname,
|
||||
user=to_node_data.user,
|
||||
password="postgres"
|
||||
)
|
||||
|
||||
# Call internal function
|
||||
result = link_nodes(link_data)
|
||||
|
||||
#==============================================================================
|
||||
|
||||
|
||||
return {
|
||||
"from_node": from_node,
|
||||
"to_node": to_node
|
||||
}
|
||||
|
||||
except HTTPException:
|
||||
raise
|
||||
|
||||
except SQLAlchemyError as e:
|
||||
print("Database error:", str(e))
|
||||
|
||||
raise HTTPException(
|
||||
status_code=500,
|
||||
detail="Management database error"
|
||||
)
|
||||
|
||||
except Exception as e:
|
||||
print("Unexpected error:", str(e))
|
||||
|
||||
raise HTTPException(
|
||||
status_code=500,
|
||||
detail="Internal server error"
|
||||
)
|
||||
Reference in new issue
Block a user