From 7fdc5467146f3243e073d565956dbf1322408bb2 Mon Sep 17 00:00:00 2001 From: NaiJi Date: Mon, 14 Nov 2022 09:48:36 +0400 Subject: [PATCH] refactor(server-api): Make appearance provider name polymorphic and required for metadata --- lib/logic/api_maps/rest_maps/api_map.dart | 3 ++- .../digital_ocean/digital_ocean.dart | 11 +++++++---- .../rest_maps/server_providers/hetzner/hetzner.dart | 12 ++++++++---- .../rest_maps/server_providers/server_provider.dart | 8 +++++++- 4 files changed, 24 insertions(+), 10 deletions(-) diff --git a/lib/logic/api_maps/rest_maps/api_map.dart b/lib/logic/api_maps/rest_maps/api_map.dart index 007bfd98..4c22c461 100644 --- a/lib/logic/api_maps/rest_maps/api_map.dart +++ b/lib/logic/api_maps/rest_maps/api_map.dart @@ -41,7 +41,8 @@ abstract class ApiMap { FutureOr get options; - abstract final String rootAddress; + String get rootAddress; + abstract final bool hasLogger; abstract final bool isWithToken; diff --git a/lib/logic/api_maps/rest_maps/server_providers/digital_ocean/digital_ocean.dart b/lib/logic/api_maps/rest_maps/server_providers/digital_ocean/digital_ocean.dart index 527a8ed2..1c1f342c 100644 --- a/lib/logic/api_maps/rest_maps/server_providers/digital_ocean/digital_ocean.dart +++ b/lib/logic/api_maps/rest_maps/server_providers/digital_ocean/digital_ocean.dart @@ -49,10 +49,13 @@ class DigitalOceanApi extends ServerProviderApi with VolumeProviderApi { } @override - final String rootAddress = 'https://api.digitalocean.com/v2'; + String get rootAddress => 'https://api.digitalocean.com/v2'; @override - final String infectProviderName = 'digitalocean'; + String get infectProviderName => 'digitalocean'; + + @override + String get appearanceProviderName => 'Digital Ocean'; @override Future isApiTokenValid(final String token) async { @@ -452,7 +455,7 @@ class DigitalOceanApi extends ServerProviderApi with VolumeProviderApi { final DateTime end, ) async { ServerMetrics? metrics; - return metrics!; + return metrics; } @override @@ -498,7 +501,7 @@ class DigitalOceanApi extends ServerProviderApi with VolumeProviderApi { ServerMetadataEntity( type: MetadataType.other, name: 'server.provider'.tr(), - value: 'Digital Ocean', + value: appearanceProviderName, ), ]; } catch (e) { diff --git a/lib/logic/api_maps/rest_maps/server_providers/hetzner/hetzner.dart b/lib/logic/api_maps/rest_maps/server_providers/hetzner/hetzner.dart index f2bd0a9e..7e623c69 100644 --- a/lib/logic/api_maps/rest_maps/server_providers/hetzner/hetzner.dart +++ b/lib/logic/api_maps/rest_maps/server_providers/hetzner/hetzner.dart @@ -50,10 +50,13 @@ class HetznerApi extends ServerProviderApi with VolumeProviderApi { } @override - final String rootAddress = 'https://api.hetzner.cloud/v1'; + String get rootAddress => 'https://api.hetzner.cloud/v1'; @override - final String infectProviderName = 'hetzner'; + String get infectProviderName => 'hetzner'; + + @override + String get appearanceProviderName => 'Hetzner'; @override Future isApiTokenValid(final String token) async { @@ -349,12 +352,13 @@ class HetznerApi extends ServerProviderApi with VolumeProviderApi { final String apiToken = StringGenerators.apiToken(); final String hostname = getHostnameFromDomain(domainName); + const String infectBranch = 'providers/hetzner'; final String base64Password = base64.encode(utf8.encode(rootUser.password ?? 'PASS')); final String userdataString = - "#cloud-config\nruncmd:\n- curl https://git.selfprivacy.org/SelfPrivacy/selfprivacy-nixos-infect/raw/branch/master/nixos-infect | PROVIDER=$infectProviderName NIX_CHANNEL=nixos-21.05 DOMAIN='$domainName' LUSER='${rootUser.login}' ENCODED_PASSWORD='$base64Password' CF_TOKEN=$dnsApiToken DB_PASSWORD=$dbPassword API_TOKEN=$apiToken HOSTNAME=$hostname bash 2>&1 | tee /tmp/infect.log"; + "#cloud-config\nruncmd:\n- curl https://git.selfprivacy.org/SelfPrivacy/selfprivacy-nixos-infect/raw/branch/$infectBranch/nixos-infect | PROVIDER=$infectProviderName NIX_CHANNEL=nixos-21.05 DOMAIN='$domainName' LUSER='${rootUser.login}' ENCODED_PASSWORD='$base64Password' CF_TOKEN=$dnsApiToken DB_PASSWORD=$dbPassword API_TOKEN=$apiToken HOSTNAME=$hostname bash 2>&1 | tee /tmp/infect.log"; ServerHostingDetails? serverDetails; DioError? hetznerError; @@ -614,7 +618,7 @@ class HetznerApi extends ServerProviderApi with VolumeProviderApi { ServerMetadataEntity( type: MetadataType.other, name: 'server.provider'.tr(), - value: 'Hetzner', + value: appearanceProviderName, ), ]; } catch (e) { diff --git a/lib/logic/api_maps/rest_maps/server_providers/server_provider.dart b/lib/logic/api_maps/rest_maps/server_providers/server_provider.dart index 42004e70..21c6ddd0 100644 --- a/lib/logic/api_maps/rest_maps/server_providers/server_provider.dart +++ b/lib/logic/api_maps/rest_maps/server_providers/server_provider.dart @@ -48,5 +48,11 @@ abstract class ServerProviderApi extends ApiMap { final DateTime end, ); - abstract final String infectProviderName; + /// Provider name key which lets infect understand what kind of installation + /// it requires, for example 'digitaloceal' for Digital Ocean + String get infectProviderName; + + /// Actual provider name to render on information page for user, + /// for example 'Digital Ocean' for Digital Ocean + String get appearanceProviderName; }