From 448771cd88d9462500a94fcf78515e8a89344ac1 Mon Sep 17 00:00:00 2001 From: George Date: Wed, 16 Sep 2026 12:42:24 +0530 Subject: [PATCH] feat: --Cluster management table api added to create cluster --- instructions.txt | 6 ++++++ main.py | 35 +++++++++++++++++++++++++++++++---- models.py | 14 +++++++++++++- 3 files changed, 50 insertions(+), 5 deletions(-) diff --git a/instructions.txt b/instructions.txt index 180cb7b..2a0ce56 100644 --- a/instructions.txt +++ b/instructions.txt @@ -176,6 +176,12 @@ SELECT spock.sub_create( provider_dsn := 'host=172.16.10.123 port=5444 dbname=postgres user=postgres password=postgres' ); +|================| +|DELETE Node LINK| +|================| +SELECT spock.sub_drop('node1_from_node2'); + + |======| |RESULT| |======| diff --git a/main.py b/main.py index 99e03bf..de2ac4e 100644 --- a/main.py +++ b/main.py @@ -12,7 +12,7 @@ from database import Base, engine,SessionLocal,get_db from uuid import UUID # import models from sqlalchemy.orm import Session -from models import NodeInterface +from models import NodeInterface, Cluster import os from dotenv import load_dotenv from sqlalchemy.exc import SQLAlchemyError @@ -714,6 +714,7 @@ def create_cluster(data: ClusterRequest,db: Session = Depends(get_db),token_data print("Cluster Name:", data.cluster_name) print("Links:", data.links) results = [] + node_names = set() for from_node, to_node in data.links: print(f"{from_node} -> {to_node}") #================= @@ -731,30 +732,56 @@ def create_cluster(data: ClusterRequest,db: Session = Depends(get_db),token_data f"Successfully linked {from_node} -> {to_node}" ) + node_names.add(from_node) + node_names.add(to_node) + results.append({ "from_node": from_node, "to_node": to_node, "status": "success" }) + cluster = Cluster( + cluster_name=data.cluster_name + ) + db.add(cluster) + db.flush() + + nodes = ( + db.query(NodeInterface) + .filter(NodeInterface.if_name.in_(node_names)) + .all() + ) + + if len(nodes) != len(node_names): + found_nodes = {node.if_name for node in nodes} + missing_nodes = node_names - found_nodes + + raise HTTPException( + status_code=404, + detail=f"Nodes not found: {list(missing_nodes)}" + ) + + for node in nodes: + node.cluster = cluster + db.commit() return { "cluster_name": data.cluster_name, "links": data.links, - "cluster_name": data.cluster_name, "links": results } except HTTPException: + db.rollback() raise except Exception as e: - + db.rollback() print("Cluster creation failed:", str(e)) raise HTTPException( status_code=500, detail="Cluster creation failed" ) - diff --git a/models.py b/models.py index 8a184a9..6d17867 100644 --- a/models.py +++ b/models.py @@ -115,4 +115,16 @@ def add_cluster_id_column(): REFERENCES "fastAPI_cluster"(uuid); """)) -# add_cluster_id_column() \ No newline at end of file +# add_cluster_id_column() + + +# ALTER TABLE "fastAPI_node_interface" +# DROP CONSTRAINT fk_node_interface_cluster; + +# ALTER TABLE "fastAPI_node_interface" +# ADD CONSTRAINT fk_node_interface_cluster +# FOREIGN KEY (cluster_id) +# REFERENCES "fastAPI_cluster"(uuid) +# ON DELETE SET NULL; + +#\d "fastAPI_node_interface" \ No newline at end of file