--Cluster management table api added to create cluster
This commit is contained in:
george committed 2026-09-16 12:42:24 +05:30
1 parent d0e1d31093
commit 448771cd88
3 files changed
+50 -5

No files matched your search

+6
View File
@@ -176,6 +176,12 @@ SELECT spock.sub_create(
provider_dsn := 'host=172.16.10.123 port=5444 dbname=postgres user=postgres password=postgres' 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| |RESULT|
|======| |======|
+31 -4
View File
@@ -12,7 +12,7 @@ from database import Base, engine,SessionLocal,get_db
from uuid import UUID from uuid import UUID
# import models # import models
from sqlalchemy.orm import Session from sqlalchemy.orm import Session
from models import NodeInterface from models import NodeInterface, Cluster
import os import os
from dotenv import load_dotenv from dotenv import load_dotenv
from sqlalchemy.exc import SQLAlchemyError 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("Cluster Name:", data.cluster_name)
print("Links:", data.links) print("Links:", data.links)
results = [] results = []
node_names = set()
for from_node, to_node in data.links: for from_node, to_node in data.links:
print(f"{from_node} -> {to_node}") 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}" f"Successfully linked {from_node} -> {to_node}"
) )
node_names.add(from_node)
node_names.add(to_node)
results.append({ results.append({
"from_node": from_node, "from_node": from_node,
"to_node": to_node, "to_node": to_node,
"status": "success" "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 { return {
"cluster_name": data.cluster_name, "cluster_name": data.cluster_name,
"links": data.links, "links": data.links,
"cluster_name": data.cluster_name,
"links": results "links": results
} }
except HTTPException: except HTTPException:
db.rollback()
raise raise
except Exception as e: except Exception as e:
db.rollback()
print("Cluster creation failed:", str(e)) print("Cluster creation failed:", str(e))
raise HTTPException( raise HTTPException(
status_code=500, status_code=500,
detail="Cluster creation failed" detail="Cluster creation failed"
) )
+13 -1
View File
@@ -115,4 +115,16 @@ def add_cluster_id_column():
REFERENCES "fastAPI_cluster"(uuid); REFERENCES "fastAPI_cluster"(uuid);
""")) """))
# add_cluster_id_column() # 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"