mirror of
https://git.selfprivacy.org/kherel/selfprivacy.org.app.git
synced 2025-01-23 09:16:54 +00:00
chore: Implement server deletion for digital ocean on provider level
This commit is contained in:
parent
bc9ab447f0
commit
7b543f9030
|
@ -402,64 +402,22 @@ class DigitalOceanApi extends ServerProviderApi with VolumeProviderApi {
|
|||
);
|
||||
}
|
||||
|
||||
@override
|
||||
Future<GenericResult<bool>> deleteServer({
|
||||
required final String domainName,
|
||||
}) async {
|
||||
Future<GenericResult<void>> deleteServer(final int serverId) async {
|
||||
final Dio client = await getClient();
|
||||
|
||||
final String hostname = getHostnameFromDomain(domainName);
|
||||
final servers = await getServers();
|
||||
final ServerBasicInfo serverToRemove;
|
||||
try {
|
||||
serverToRemove = servers.firstWhere(
|
||||
(final el) => el.name == hostname,
|
||||
);
|
||||
} catch (e) {
|
||||
print(e);
|
||||
return GenericResult(
|
||||
data: false,
|
||||
success: false,
|
||||
message: e.toString(),
|
||||
);
|
||||
}
|
||||
|
||||
final volumes = await getVolumes();
|
||||
final ServerVolume volumeToRemove;
|
||||
try {
|
||||
volumeToRemove = volumes.firstWhere(
|
||||
(final el) => el.serverId == serverToRemove.id,
|
||||
);
|
||||
} catch (e) {
|
||||
print(e);
|
||||
return GenericResult(
|
||||
data: false,
|
||||
success: false,
|
||||
message: e.toString(),
|
||||
);
|
||||
}
|
||||
|
||||
final List<Future> laterFutures = <Future>[];
|
||||
|
||||
await detachVolume(volumeToRemove);
|
||||
await Future.delayed(const Duration(seconds: 10));
|
||||
|
||||
try {
|
||||
laterFutures.add(deleteVolume(volumeToRemove));
|
||||
laterFutures.add(client.delete('/droplets/${serverToRemove.id}'));
|
||||
await Future.wait(laterFutures);
|
||||
await client.delete('/droplets/$serverId');
|
||||
} catch (e) {
|
||||
print(e);
|
||||
return GenericResult(
|
||||
success: false,
|
||||
data: false,
|
||||
data: null,
|
||||
message: e.toString(),
|
||||
);
|
||||
} finally {
|
||||
close(client);
|
||||
}
|
||||
|
||||
return GenericResult(success: true, data: true);
|
||||
return GenericResult(success: true, data: null);
|
||||
}
|
||||
|
||||
Future<GenericResult<void>> restart(final int serverId) async {
|
||||
|
|
|
@ -1,5 +1,8 @@
|
|||
import 'package:easy_localization/easy_localization.dart';
|
||||
import 'package:selfprivacy/logic/api_maps/rest_maps/server_providers/digital_ocean/digital_ocean_api.dart';
|
||||
import 'package:selfprivacy/logic/models/callback_dialogue_branching.dart';
|
||||
import 'package:selfprivacy/logic/models/disk_size.dart';
|
||||
import 'package:selfprivacy/logic/models/hive/server_details.dart';
|
||||
import 'package:selfprivacy/logic/models/metrics.dart';
|
||||
import 'package:selfprivacy/logic/models/price.dart';
|
||||
import 'package:selfprivacy/logic/models/server_basic_info.dart';
|
||||
|
@ -8,6 +11,7 @@ import 'package:selfprivacy/logic/models/server_provider_location.dart';
|
|||
import 'package:selfprivacy/logic/models/server_type.dart';
|
||||
import 'package:selfprivacy/logic/providers/server_provider.dart';
|
||||
import 'package:selfprivacy/utils/extensions/string_extensions.dart';
|
||||
import 'package:selfprivacy/utils/network_utils.dart';
|
||||
|
||||
class ApiAdapter {
|
||||
ApiAdapter({final String? region, final bool isWithToken = true})
|
||||
|
@ -419,4 +423,64 @@ class DigitalOceanServerProvider extends ServerProvider {
|
|||
data: timestamp,
|
||||
);
|
||||
}
|
||||
|
||||
Future<GenericResult<CallbackDialogueBranching?>> deleteServer(
|
||||
final String hostname,
|
||||
) async {
|
||||
final String deletionName = getHostnameFromDomain(hostname);
|
||||
final serversResult = await getServers();
|
||||
try {
|
||||
final servers = serversResult.data;
|
||||
ServerBasicInfo? foundServer;
|
||||
for (final server in servers) {
|
||||
if (server.name == deletionName) {
|
||||
foundServer = server;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
final volumes = await _adapter.api().getVolumes();
|
||||
final ServerVolume volumeToRemove;
|
||||
volumeToRemove = volumes.firstWhere(
|
||||
(final el) => el.serverId == foundServer!.id,
|
||||
);
|
||||
|
||||
await _adapter.api().detachVolume(volumeToRemove);
|
||||
|
||||
await Future.delayed(const Duration(seconds: 10));
|
||||
final List<Future> laterFutures = <Future>[];
|
||||
laterFutures.add(_adapter.api().deleteVolume(volumeToRemove));
|
||||
laterFutures.add(_adapter.api().deleteServer(foundServer!.id));
|
||||
|
||||
await Future.wait(laterFutures);
|
||||
} catch (e) {
|
||||
print(e);
|
||||
return GenericResult(
|
||||
success: false,
|
||||
data: CallbackDialogueBranching(
|
||||
choices: [
|
||||
CallbackDialogueChoice(
|
||||
title: 'basis.cancel'.tr(),
|
||||
callback: null,
|
||||
),
|
||||
CallbackDialogueChoice(
|
||||
title: 'basis.try_again'.tr(),
|
||||
callback: () async {
|
||||
await Future.delayed(const Duration(seconds: 5));
|
||||
return deleteServer(hostname);
|
||||
},
|
||||
),
|
||||
],
|
||||
description: 'modals.try_again'.tr(),
|
||||
title: 'modals.server_deletion_error'.tr(),
|
||||
),
|
||||
message: e.toString(),
|
||||
);
|
||||
}
|
||||
|
||||
return GenericResult(
|
||||
success: true,
|
||||
data: null,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -598,7 +598,7 @@ class HetznerServerProvider extends ServerProvider {
|
|||
for (final volumeId in foundServer.volumes) {
|
||||
laterFutures.add(_adapter.api().deleteVolume(volumeId));
|
||||
}
|
||||
laterFutures.add(_adapter.api().deleteVolume(foundServer.id));
|
||||
laterFutures.add(_adapter.api().deleteServer(serverId: foundServer.id));
|
||||
|
||||
await Future.wait(laterFutures);
|
||||
} catch (e) {
|
||||
|
|
Loading…
Reference in a new issue