diff --git a/lib/logic/cubit/server_installation/server_installation_cubit.dart b/lib/logic/cubit/server_installation/server_installation_cubit.dart index 968444e8..c669cf75 100644 --- a/lib/logic/cubit/server_installation/server_installation_cubit.dart +++ b/lib/logic/cubit/server_installation/server_installation_cubit.dart @@ -721,15 +721,18 @@ class ServerInstallationCubit extends Cubit { await repository.saveIsServerResetedSecondTime(true); await repository.saveHasFinalChecked(true); await repository.saveIsRecoveringServer(false); - final serverInfo = await ProvidersController.currentServerProvider! - .getServerInfo(state.serverDetails!.id); + final serverType = await ProvidersController.currentServerProvider! + .getServerType(state.serverDetails!.id); + await repository.saveServerType(serverType.data!); + await ProvidersController.currentServerProvider! + .trySetServerLocation(serverType.data!.location.identifier); final User mainUser = await repository.getMainUser(); await repository.saveRootUser(mainUser); final ServerInstallationRecovery updatedState = (state as ServerInstallationRecovery).copyWith( backblazeCredential: backblazeCredential, rootUser: mainUser, - serverTypeIdentificator: serverInfo.data?.serverTypeId ?? '', + serverTypeIdentificator: serverType.data!.identifier, ); emit(updatedState.finish()); } diff --git a/lib/logic/providers/server_providers/digital_ocean.dart b/lib/logic/providers/server_providers/digital_ocean.dart index 7b613698..4082abe5 100644 --- a/lib/logic/providers/server_providers/digital_ocean.dart +++ b/lib/logic/providers/server_providers/digital_ocean.dart @@ -90,54 +90,90 @@ class DigitalOceanServerProvider extends ServerProvider { } @override - Future> getServerInfo( - final int serverId, - ) async { - ServerBasicInfo? server; + Future> getServerType(final int serverId) async { + ServerType? serverType; + dynamic server; final result = await _adapter.api().getServers(); if (result.data.isEmpty || !result.success) { return GenericResult( success: result.success, - data: server, + data: serverType, code: result.code, message: result.message, ); } - final rawServers = result.data; + final List rawServers = result.data; for (final rawServer in rawServers) { - String? ipv4; if (rawServer['networks']['v4'].isNotEmpty) { for (final v4 in rawServer['networks']['v4']) { if (v4['type'].toString() == 'public') { - ipv4 = v4['ip_address'].toString(); + server = rawServer; } } } - - try { - server = ServerBasicInfo( - id: rawServer['id'], - reverseDns: rawServer['name'], - created: DateTime.now(), - ip: ipv4!, - name: rawServer['name'], - serverTypeId: rawServer['region']['slug'], - ); - } catch (e) { - print(e); - continue; - } } if (server == null) { + const String msg = 'getServerType: no server!'; + print(msg); return GenericResult( success: false, - data: server, + data: serverType, + message: msg, ); } - return GenericResult(success: true, data: server); + final rawLocationsResult = await getAvailableLocations(); + if (rawLocationsResult.data.isEmpty || !rawLocationsResult.success) { + return GenericResult( + success: rawLocationsResult.success, + data: serverType, + code: rawLocationsResult.code, + message: rawLocationsResult.message, + ); + } + + ServerProviderLocation? location; + for (final rawLocation in rawLocationsResult.data) { + if (rawLocation.identifier == server['region']['slug']) { + location = rawLocation; + } + } + + if (location == null) { + const String msg = 'getServerType: no location!'; + print(msg); + return GenericResult( + success: false, + data: serverType, + message: msg, + ); + } + + ServerType? type; + final rawSize = DigitalOceanServerType.fromJson(server['size']); + for (final rawRegion in rawSize.regions) { + if (rawRegion == server['region']['slug']) { + type = ServerType( + title: rawSize.description, + identifier: rawSize.slug, + ram: rawSize.memory / 1024, + cores: rawSize.vcpus, + disk: DiskSize(byte: rawSize.disk * 1024 * 1024 * 1024), + price: Price( + value: rawSize.priceMonthly, + currency: currency, + ), + location: location, + ); + } + } + + return GenericResult( + success: true, + data: type, + ); } @override diff --git a/lib/logic/providers/server_providers/hetzner.dart b/lib/logic/providers/server_providers/hetzner.dart index db9ddc03..e902a788 100644 --- a/lib/logic/providers/server_providers/hetzner.dart +++ b/lib/logic/providers/server_providers/hetzner.dart @@ -90,15 +90,14 @@ class HetznerServerProvider extends ServerProvider { } @override - Future> getServerInfo( - final int serverId, - ) async { - ServerBasicInfo? server; + Future> getServerType(final int serverId) async { + ServerType? serverType; + HetznerServerInfo? server; final result = await _adapter.api().getServers(); if (result.data.isEmpty || !result.success) { return GenericResult( success: result.success, - data: server, + data: serverType, code: result.code, message: result.message, ); @@ -106,34 +105,60 @@ class HetznerServerProvider extends ServerProvider { final List hetznerServers = result.data; for (final hetznerServer in hetznerServers) { - if (hetznerServer.publicNet.ipv4 == null || - hetznerServer.id != serverId) { - continue; - } - - try { - server = ServerBasicInfo( - id: hetznerServer.id, - name: hetznerServer.name, - ip: hetznerServer.publicNet.ipv4!.ip, - reverseDns: hetznerServer.publicNet.ipv4!.reverseDns, - created: hetznerServer.created, - serverTypeId: hetznerServer.serverType.name, - ); - } catch (e) { - print(e); - continue; + if (hetznerServer.publicNet.ipv4 != null || + hetznerServer.id == serverId) { + server = hetznerServer; + break; } } if (server == null) { + const String msg = 'getServerType: no server!'; + print(msg); return GenericResult( success: false, - data: server, + data: serverType, + message: msg, ); } - return GenericResult(success: true, data: server); + double? priceValue; + for (final price in server.serverType.prices) { + if (price.location == server.location.name) { + priceValue = price.monthly; + } + } + + if (priceValue == null) { + const String msg = 'getServerType: no price!'; + print(msg); + return GenericResult( + success: false, + data: serverType, + message: msg, + ); + } + + return GenericResult( + success: true, + data: ServerType( + title: server.serverType.description, + identifier: server.serverType.name, + ram: server.serverType.memory.toDouble(), + cores: server.serverType.cores, + disk: DiskSize(byte: server.serverType.disk * 1024 * 1024 * 1024), + price: Price( + value: priceValue, + currency: currency, + ), + location: ServerProviderLocation( + title: server.location.city, + description: server.location.description, + flag: server.location.flag, + identifier: server.location.name, + ), + ), + ); } @override diff --git a/lib/logic/providers/server_providers/server_provider.dart b/lib/logic/providers/server_providers/server_provider.dart index 4182d9fc..cfcf3f5e 100644 --- a/lib/logic/providers/server_providers/server_provider.dart +++ b/lib/logic/providers/server_providers/server_provider.dart @@ -24,12 +24,10 @@ abstract class ServerProvider { /// Only with public IPv4 addresses. Future>> getServers(); - /// Returns actual [ServerBasicInfo] of the + /// Returns actual [ServerType] of the /// requested server entry assigned /// to the authorized user. - /// - /// Only with public IPv4 address. - Future> getServerInfo(final int serverId); + Future> getServerType(final int serverId); /// Tries to launch installation of SelfPrivacy on /// the requested server entry for the authorized account.