feat:
--ADDED create cluster api
This commit is contained in:
1 parent
96be1fc94e
commit
d0e1d31093
3 files changed
+146
-7
No files matched your search
@@ -176,6 +176,11 @@ 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'
|
||||||
);
|
);
|
||||||
|
|
||||||
|
|======|
|
||||||
|
|RESULT|
|
||||||
|
|======|
|
||||||
|
SELECT * FROM spock.sub_show_status();
|
||||||
|
|
||||||
|=============|
|
|=============|
|
||||||
|Register Node|
|
|Register Node|
|
||||||
|=============|
|
|=============|
|
||||||
|
|||||||
@@ -64,6 +64,12 @@ class NodeLinkRequest(BaseModel):
|
|||||||
from_node: str
|
from_node: str
|
||||||
to_node: str
|
to_node: str
|
||||||
|
|
||||||
|
|
||||||
|
class ClusterRequest(BaseModel):
|
||||||
|
cluster_name: str
|
||||||
|
links: list[tuple[str, str]]
|
||||||
|
|
||||||
|
|
||||||
#POST /register-node
|
#POST /register-node
|
||||||
# {
|
# {
|
||||||
# "node_name": "node2",
|
# "node_name": "node2",
|
||||||
@@ -601,8 +607,8 @@ def delete_node_interface(
|
|||||||
conn.close()
|
conn.close()
|
||||||
|
|
||||||
|
|
||||||
@app.post("/spock/link-nodes-abstract")
|
# @app.post("/spock/link-nodes-abstract")
|
||||||
def link_nodes_abstract(data: NodeLinkRequest,db: Session = Depends(get_db),token_data: dict = Depends(verify_token)):
|
def link_nodes_abstract(data: NodeLinkRequest,db: Session):
|
||||||
|
|
||||||
|
|
||||||
try:
|
try:
|
||||||
@@ -663,7 +669,7 @@ def link_nodes_abstract(data: NodeLinkRequest,db: Session = Depends(get_db),toke
|
|||||||
|
|
||||||
# Call internal function
|
# Call internal function
|
||||||
result = link_nodes(link_data)
|
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(
|
raise HTTPException(
|
||||||
status_code=500,
|
status_code=500,
|
||||||
detail="Internal server error"
|
detail="Internal server error"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
@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"
|
||||||
|
)
|
||||||
|
|
||||||
@@ -1,8 +1,10 @@
|
|||||||
from sqlalchemy import Column, BigInteger, Integer, String
|
from sqlalchemy import Column, BigInteger, Integer, String
|
||||||
from database import Base
|
from database import Base, engine
|
||||||
import uuid
|
import uuid
|
||||||
from sqlalchemy.dialects.postgresql import UUID
|
from sqlalchemy.dialects.postgresql import UUID
|
||||||
|
from sqlalchemy.orm import relationship
|
||||||
|
from sqlalchemy import ForeignKey
|
||||||
|
from sqlalchemy import text
|
||||||
class NodeInterface(Base):
|
class NodeInterface(Base):
|
||||||
__tablename__ = "fastAPI_node_interface"
|
__tablename__ = "fastAPI_node_interface"
|
||||||
|
|
||||||
@@ -49,4 +51,68 @@ class NodeInterface(Base):
|
|||||||
user = Column(
|
user = Column(
|
||||||
String,
|
String,
|
||||||
nullable=False
|
nullable=False
|
||||||
)
|
)
|
||||||
|
|
||||||
|
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()
|
||||||
Reference in new issue
Block a user