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):