From f3016756466f2f3efce27c33b1de4c8025a60220 Mon Sep 17 00:00:00 2001 From: Per-Arne Andersen Date: Sun, 19 Jul 2020 21:06:55 +0200 Subject: [PATCH] * Moving away from silly .sync/.from_db scheme and use "raw" sqlalchemy. * Fixed bug where server interface were queried using ID and not the name. Fixes #22 --- wg_dashboard_backend/db/wireguard.py | 3 ++- wg_dashboard_backend/routers/v1/peer.py | 13 +++++++------ wg_dashboard_backend/schemas.py | 4 ++-- 3 files changed, 11 insertions(+), 9 deletions(-) diff --git a/wg_dashboard_backend/db/wireguard.py b/wg_dashboard_backend/db/wireguard.py index 4ee50c2..bb4270c 100644 --- a/wg_dashboard_backend/db/wireguard.py +++ b/wg_dashboard_backend/db/wireguard.py @@ -43,7 +43,8 @@ def peer_dns_set(sess: Session, peer: schemas.WGPeer) -> schemas.WGPeer: def peer_remove(sess: Session, peer: schemas.WGPeer) -> bool: - db_peers = peer.filter_query(sess).all() + + db_peers = sess.query(models.WGPeer).filter_by(id=peer.id).all() for db_peer in db_peers: sess.delete(db_peer) diff --git a/wg_dashboard_backend/routers/v1/peer.py b/wg_dashboard_backend/routers/v1/peer.py index c299521..e57e1a0 100644 --- a/wg_dashboard_backend/routers/v1/peer.py +++ b/wg_dashboard_backend/routers/v1/peer.py @@ -61,14 +61,15 @@ def add_peer( server=server )) - peer.sync(sess) - peer.from_db(sess) + db_peer = models.WGPeer(**peer.dict()) + sess.add(db_peer) + sess.commit() # If server is running. Add peer if script.wireguard.is_running(server): script.wireguard.add_peer(server, peer) - return peer + return schemas.WGPeer.from_orm(db_peer) @router.post("/delete", response_model=schemas.WGPeer) @@ -76,13 +77,13 @@ def delete_peer( peer: schemas.WGPeer, sess: Session = Depends(middleware.get_db) ): - peer.from_db(sess) # Sync full object + + server = sess.query(models.WGServer).filter_by(id=peer.server_id).one() if not db.wireguard.peer_remove(sess, peer): raise HTTPException(400, detail="Were not able to delete peer %s (%s)" % (peer.name, peer.public_key)) - server = schemas.WGServer(interface=peer.server_id) - if script.wireguard.is_running(server): + if script.wireguard.is_running(schemas.WGServer(interface=server.interface)): script.wireguard.remove_peer(server, peer) return peer diff --git a/wg_dashboard_backend/schemas.py b/wg_dashboard_backend/schemas.py index d98c08a..f7ebf54 100644 --- a/wg_dashboard_backend/schemas.py +++ b/wg_dashboard_backend/schemas.py @@ -124,8 +124,8 @@ class WGPeer(GenericModel): class Meta: model = models.WGPeer - key = "address" - excludes = {"id"} + key = "id" + excludes = {} class WGPeerConfig(GenericModel):