diff --git a/instructions.txt b/instructions.txt index e1a4f1d..180cb7b 100644 --- a/instructions.txt +++ b/instructions.txt @@ -176,6 +176,11 @@ SELECT spock.sub_create( provider_dsn := 'host=172.16.10.123 port=5444 dbname=postgres user=postgres password=postgres' ); +|======| +|RESULT| +|======| +SELECT * FROM spock.sub_show_status(); + |=============| |Register Node| |=============| diff --git a/main.py b/main.py index 4a5f4b2..99e03bf 100644 --- a/main.py +++ b/main.py @@ -64,6 +64,12 @@ class NodeLinkRequest(BaseModel): from_node: str to_node: str + +class ClusterRequest(BaseModel): + cluster_name: str + links: list[tuple[str, str]] + + #POST /register-node # { # "node_name": "node2", @@ -601,8 +607,8 @@ def delete_node_interface( 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)): +# @app.post("/spock/link-nodes-abstract") +def link_nodes_abstract(data: NodeLinkRequest,db: Session): try: @@ -663,7 +669,7 @@ def link_nodes_abstract(data: NodeLinkRequest,db: Session = Depends(get_db),toke # Call internal function result = link_nodes(link_data) - + print("Linking Result:", result["status"]) #============================================================================== @@ -689,4 +695,66 @@ def link_nodes_abstract(data: NodeLinkRequest,db: Session = Depends(get_db),toke raise HTTPException( status_code=500, detail="Internal server error" - ) \ No newline at end of file + ) + + + + +@app.post("/spock/create_cluster") +# INPUT EXAMPLE +# { +# "cluster_name": "cluster1", +# "links": [ +# ["node1", "node2"], +# ["node2", "node1"] +# ] +# } +def create_cluster(data: ClusterRequest,db: Session = Depends(get_db),token_data: dict = Depends(verify_token)): + try: + print("Cluster Name:", data.cluster_name) + print("Links:", data.links) + results = [] + for from_node, to_node in data.links: + print(f"{from_node} -> {to_node}") + #================= + link_request = NodeLinkRequest( + from_node=from_node, + to_node=to_node + ) + + result = link_nodes_abstract( + link_request, + db + ) + + print( + f"Successfully linked {from_node} -> {to_node}" + ) + + results.append({ + "from_node": from_node, + "to_node": to_node, + "status": "success" + }) + + + + return { + "cluster_name": data.cluster_name, + "links": data.links, + "cluster_name": data.cluster_name, + "links": results + } + except HTTPException: + raise + + + except Exception as e: + + print("Cluster creation failed:", str(e)) + + raise HTTPException( + status_code=500, + detail="Cluster creation failed" + ) + diff --git a/models.py b/models.py index 2bc0a84..8a184a9 100644 --- a/models.py +++ b/models.py @@ -1,8 +1,10 @@ from sqlalchemy import Column, BigInteger, Integer, String -from database import Base +from database import Base, engine import uuid from sqlalchemy.dialects.postgresql import UUID - +from sqlalchemy.orm import relationship +from sqlalchemy import ForeignKey +from sqlalchemy import text class NodeInterface(Base): __tablename__ = "fastAPI_node_interface" @@ -49,4 +51,68 @@ class NodeInterface(Base): user = Column( String, nullable=False - ) \ No newline at end of file + ) + + cluster_id = Column( + UUID(as_uuid=True), + ForeignKey("fastAPI_cluster.uuid"), + nullable=True + ) + + cluster = relationship( + "Cluster", + back_populates="nodes" + ) + + + + + + + + + + +class Cluster(Base): + __tablename__ = "fastAPI_cluster" + + uuid = Column( + UUID(as_uuid=True), + primary_key=True, + default=uuid.uuid4, + nullable=False + ) + + cluster_name = Column( + String, + unique=True, + nullable=False + ) + + nodes = relationship( + "NodeInterface", + back_populates="cluster" + ) + + + + +#EXPERIMENTAL + +def add_cluster_id_column(): + + with engine.begin() as conn: + + conn.execute(text(""" + ALTER TABLE "fastAPI_node_interface" + ADD COLUMN IF NOT EXISTS cluster_id UUID; + """)) + + conn.execute(text(""" + ALTER TABLE "fastAPI_node_interface" + ADD CONSTRAINT fk_node_interface_cluster + FOREIGN KEY (cluster_id) + REFERENCES "fastAPI_cluster"(uuid); + """)) + +# add_cluster_id_column() \ No newline at end of file