Skip to content

Commit

Permalink
delete network devices when deleting a network
Browse files Browse the repository at this point in the history
  • Loading branch information
t-aleksander committed Dec 20, 2024
1 parent 36dfa47 commit 7a9fb14
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 2 deletions.
22 changes: 22 additions & 0 deletions src/db/models/wireguard.rs
Original file line number Diff line number Diff line change
Expand Up @@ -942,6 +942,28 @@ impl WireguardNetwork<Id> {
transfer_series,
})
}

pub async fn get_devices_by_type<'e, E>(
&self,
executor: E,
device_type: DeviceType,
) -> Result<Vec<Device<Id>>, SqlxError>
where
E: PgExecutor<'e>,
{
query_as!(
Device,
"SELECT \
id, name, wireguard_pubkey, user_id, created, description, device_type \"device_type: DeviceType\", \
configured \
FROM device WHERE id in (SELECT device_id FROM wireguard_network_device WHERE wireguard_network_id = $1) \
AND device_type = $2",
self.id,
device_type as DeviceType
)
.fetch_all(executor)
.await
}
}

// [`IpNetwork`] does not implement [`Default`]
Expand Down
2 changes: 1 addition & 1 deletion src/handlers/network_devices.rs
Original file line number Diff line number Diff line change
Expand Up @@ -642,7 +642,7 @@ pub async fn modify_network_device(
);
}

let network_device_info = NetworkDeviceInfo::from_device(device, &mut *transaction).await?;
let network_device_info = NetworkDeviceInfo::from_device(device, &mut transaction).await?;
transaction.commit().await?;

Ok(ApiResponse {
Expand Down
10 changes: 9 additions & 1 deletion src/handlers/wireguard.rs
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,15 @@ pub async fn delete_network(
);
let network = find_network(network_id, &appstate.pool).await?;
let network_name = network.name.clone();
network.delete(&appstate.pool).await?;
let mut transaction = appstate.pool.begin().await?;
let network_devices = network
.get_devices_by_type(&mut *transaction, DeviceType::Network)
.await?;
for device in network_devices {
device.delete(&mut *transaction).await?;
}
network.delete(&mut *transaction).await?;
transaction.commit().await?;
appstate.send_wireguard_event(GatewayEvent::NetworkDeleted(network_id, network_name));
info!(
"User {} deleted WireGuard network {network_id}",
Expand Down

0 comments on commit 7a9fb14

Please sign in to comment.