feat:
--Added new Link and register API
This commit is contained in:
1 parent
a92d68fa35
commit
ce55d6f591
3 files changed
+272
-37
No files matched your search
@@ -1,47 +1,229 @@
|
||||
from fastapi import FastAPI, HTTPException
|
||||
from pydantic import BaseModel
|
||||
import psycopg2
|
||||
|
||||
# ===== TEST DATA =====
|
||||
# id = 600
|
||||
source_node = "node1"
|
||||
message = "from python with love "
|
||||
|
||||
conn = None
|
||||
|
||||
try:
|
||||
conn = psycopg2.connect(
|
||||
host="172.16.10.123",
|
||||
port=5444,
|
||||
database="postgres",
|
||||
user="postgres",
|
||||
password="postgres"
|
||||
)
|
||||
app = FastAPI()
|
||||
|
||||
|
||||
cursor = conn.cursor()
|
||||
class RegisterNodeRequest(BaseModel):
|
||||
node_name: str
|
||||
host: str
|
||||
port: int = 5444
|
||||
dbname: str = "postgres"
|
||||
user: str = "postgres"
|
||||
password: str
|
||||
|
||||
cursor.execute("""
|
||||
INSERT INTO test_replication (source_node, message)
|
||||
VALUES (%s, %s)
|
||||
RETURNING id
|
||||
""", (source_node, message))
|
||||
|
||||
# Get the ID generated by PostgreSQL
|
||||
generated_id = cursor.fetchone()[0]
|
||||
class LinkNodeRequest(BaseModel):
|
||||
subscription_name: str
|
||||
|
||||
conn.commit()
|
||||
# The node where sub_create() must run
|
||||
subscriber_host: str
|
||||
subscriber_port: int = 5444
|
||||
|
||||
print("LOCAL COMMIT: SUCCESS")
|
||||
print("Generated ID:", generated_id)
|
||||
print("Source:", source_node)
|
||||
print("Message:", message)
|
||||
# The provider node
|
||||
provider_host: str
|
||||
provider_port: int = 5444
|
||||
|
||||
except Exception as e:
|
||||
if conn:
|
||||
conn.rollback()
|
||||
dbname: str = "postgres"
|
||||
user: str = "postgres"
|
||||
password: str
|
||||
|
||||
print("LOCAL COMMIT: FAILED")
|
||||
print("Error:", e)
|
||||
|
||||
finally:
|
||||
if conn:
|
||||
conn.close()
|
||||
#POST /register-node
|
||||
# {
|
||||
# "node_name": "node2",
|
||||
# "host": "172.16.10.110",
|
||||
# "port": 5444,
|
||||
# "dbname": "postgres",
|
||||
# "user": "postgres",
|
||||
# "password": "postgres"
|
||||
# }
|
||||
|
||||
# postgres=# SELECT spock.node_add_interface(
|
||||
# 'node2',
|
||||
# 'node2_external',
|
||||
# 'host=172.16.10.110 port=5444 dbname=postgres user=postgres password=postgres'
|
||||
# );
|
||||
|
||||
|
||||
|
||||
@app.post("/register-node")
|
||||
def register_node(data: RegisterNodeRequest):
|
||||
|
||||
conn = None
|
||||
cur = None
|
||||
|
||||
try:
|
||||
# Connect to the node we want to register
|
||||
conn = psycopg2.connect(
|
||||
host=data.host,
|
||||
port=data.port,
|
||||
dbname=data.dbname,
|
||||
user=data.user,
|
||||
password=data.password
|
||||
)
|
||||
|
||||
cur = conn.cursor()
|
||||
|
||||
# 1. Create Spock extension
|
||||
cur.execute("""
|
||||
CREATE EXTENSION IF NOT EXISTS spock;
|
||||
""")
|
||||
|
||||
# 2. Check Spock version
|
||||
cur.execute("""
|
||||
SELECT extname, extversion
|
||||
FROM pg_extension
|
||||
WHERE extname = 'spock';
|
||||
""")
|
||||
|
||||
spock = cur.fetchone()
|
||||
|
||||
# 3. Build node DSN
|
||||
dsn = (
|
||||
f"host={data.host} "
|
||||
f"port={data.port} "
|
||||
f"dbname={data.dbname} "
|
||||
f"user={data.user} "
|
||||
f"password={data.password}"
|
||||
)
|
||||
|
||||
# 4. Create Spock node
|
||||
cur.execute("""
|
||||
SELECT spock.node_create(
|
||||
node_name := %s,
|
||||
dsn := %s
|
||||
);
|
||||
""", (
|
||||
data.node_name,
|
||||
dsn
|
||||
))
|
||||
|
||||
node_id = cur.fetchone()[0]
|
||||
|
||||
conn.commit()
|
||||
|
||||
# 5. Show registered nodes
|
||||
cur.execute("""
|
||||
SELECT node_id, node_name
|
||||
FROM spock.node;
|
||||
""")
|
||||
|
||||
rows = cur.fetchall()
|
||||
|
||||
nodes = [
|
||||
{
|
||||
"node_id": row[0],
|
||||
"node_name": row[1]
|
||||
}
|
||||
for row in rows
|
||||
]
|
||||
|
||||
return {
|
||||
"status": "success",
|
||||
"message": "Node registered",
|
||||
"node_id": node_id,
|
||||
"spock_version": spock[1],
|
||||
"nodes": nodes
|
||||
}
|
||||
|
||||
except Exception as e:
|
||||
if conn:
|
||||
conn.rollback()
|
||||
|
||||
raise HTTPException(
|
||||
status_code=500,
|
||||
detail=str(e)
|
||||
)
|
||||
|
||||
finally:
|
||||
if cur:
|
||||
cur.close()
|
||||
|
||||
if conn:
|
||||
conn.close()
|
||||
|
||||
|
||||
|
||||
# {
|
||||
# "subscription_name": "node2_from_node1",
|
||||
# "subscriber_host": "172.16.10.110",
|
||||
# "subscriber_port": 5444,
|
||||
# "provider_host": "172.16.10.123",
|
||||
# "provider_port": 5444,
|
||||
# "dbname": "postgres",
|
||||
# "user": "postgres",
|
||||
# "password": "postgres"
|
||||
# }
|
||||
@app.post("/link-nodes")
|
||||
def link_nodes(data: LinkNodeRequest):
|
||||
|
||||
conn = None
|
||||
cur = None
|
||||
|
||||
try:
|
||||
# IMPORTANT:
|
||||
# Connect to the subscriber node.
|
||||
conn = psycopg2.connect(
|
||||
host=data.subscriber_host,
|
||||
port=data.subscriber_port,
|
||||
dbname=data.dbname,
|
||||
user=data.user,
|
||||
password=data.password
|
||||
)
|
||||
|
||||
cur = conn.cursor()
|
||||
|
||||
# Provider DSN
|
||||
provider_dsn = (
|
||||
f"host={data.provider_host} "
|
||||
f"port={data.provider_port} "
|
||||
f"dbname={data.dbname} "
|
||||
f"user={data.user} "
|
||||
f"password={data.password}"
|
||||
)
|
||||
|
||||
# Create subscription
|
||||
# Create subscription
|
||||
cur.execute("""
|
||||
SELECT spock.sub_create(
|
||||
subscription_name := %s,
|
||||
provider_dsn := %s,
|
||||
synchronize_structure := true,
|
||||
synchronize_data := true
|
||||
);
|
||||
""", (
|
||||
data.subscription_name,
|
||||
provider_dsn
|
||||
))
|
||||
|
||||
|
||||
subscription_id = cur.fetchone()[0]
|
||||
|
||||
conn.commit()
|
||||
|
||||
return {
|
||||
"status": "success",
|
||||
"message": "Nodes linked successfully",
|
||||
"subscription_id": subscription_id,
|
||||
"subscription_name": data.subscription_name,
|
||||
"subscriber": data.subscriber_host,
|
||||
"provider": data.provider_host
|
||||
}
|
||||
|
||||
except Exception as e:
|
||||
if conn:
|
||||
conn.rollback()
|
||||
|
||||
raise HTTPException(
|
||||
status_code=500,
|
||||
detail=str(e)
|
||||
)
|
||||
|
||||
finally:
|
||||
if cur:
|
||||
cur.close()
|
||||
|
||||
if conn:
|
||||
conn.close()
|
||||
Reference in new issue
Block a user