Browse Source

* 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
pull/29/head
Per-Arne Andersen 5 years ago
parent
commit
f301675646
  1. 3
      wg_dashboard_backend/db/wireguard.py
  2. 13
      wg_dashboard_backend/routers/v1/peer.py
  3. 4
      wg_dashboard_backend/schemas.py

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

13
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

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

Loading…
Cancel
Save