From 140acaee49cd73dcc0446db34ed9bee9403a4d60 Mon Sep 17 00:00:00 2001 From: NaiJi Date: Wed, 7 Jun 2023 03:22:27 -0300 Subject: [PATCH] chore: Implement basic Digital Ocean server models --- .../digital_ocean/digital_ocean_api.dart | 35 ++++++++++------ .../json/digital_ocean_server_info.dart | 39 +++++++++++++++++ .../json/digital_ocean_server_info.g.dart | 37 ++++++++++++++++ .../models/json/hetzner_server_info.g.dart | 8 ++++ .../server_providers/digital_ocean.dart | 42 +++++++++---------- 5 files changed, 128 insertions(+), 33 deletions(-) create mode 100644 lib/logic/models/json/digital_ocean_server_info.dart create mode 100644 lib/logic/models/json/digital_ocean_server_info.g.dart diff --git a/lib/logic/api_maps/rest_maps/server_providers/digital_ocean/digital_ocean_api.dart b/lib/logic/api_maps/rest_maps/server_providers/digital_ocean/digital_ocean_api.dart index 2a4153c1..19d99a6c 100644 --- a/lib/logic/api_maps/rest_maps/server_providers/digital_ocean/digital_ocean_api.dart +++ b/lib/logic/api_maps/rest_maps/server_providers/digital_ocean/digital_ocean_api.dart @@ -7,7 +7,7 @@ import 'package:selfprivacy/logic/api_maps/rest_maps/server_providers/server_pro import 'package:selfprivacy/logic/api_maps/staging_options.dart'; import 'package:selfprivacy/logic/models/disk_size.dart'; import 'package:selfprivacy/logic/models/hive/user.dart'; -import 'package:selfprivacy/logic/models/server_provider_location.dart'; +import 'package:selfprivacy/logic/models/json/digital_ocean_server_info.dart'; import 'package:selfprivacy/utils/password_generator.dart'; class DigitalOceanApi extends ServerProviderApi with VolumeProviderApi { @@ -98,7 +98,8 @@ class DigitalOceanApi extends ServerProviderApi with VolumeProviderApi { ); } - Future createVolume() async { + Future> createVolume() async { + DigitalOceanVolume? volume; Response? createVolumeResponse; final Dio client = await getClient(); try { @@ -114,6 +115,7 @@ class DigitalOceanApi extends ServerProviderApi with VolumeProviderApi { 'filesystem_type': 'ext4', }, ); + volume = DigitalOceanVolume.fromJson(createVolumeResponse.data['volume']); } catch (e) { print(e); return GenericResult( @@ -126,15 +128,17 @@ class DigitalOceanApi extends ServerProviderApi with VolumeProviderApi { } return GenericResult( - data: createVolumeResponse.data, + data: volume, success: true, code: createVolumeResponse.statusCode, message: createVolumeResponse.statusMessage, ); } - Future> getVolumes({final String? status}) async { - List volumes = []; + Future>> getVolumes({ + final String? status, + }) async { + final List volumes = []; Response? getVolumesResponse; final Dio client = await getClient(); @@ -145,7 +149,9 @@ class DigitalOceanApi extends ServerProviderApi with VolumeProviderApi { 'status': status, }, ); - volumes = getVolumesResponse.data['volumes']; + for (final volume in getVolumesResponse.data['volumes']) { + volumes.add(DigitalOceanVolume.fromJson(volume)); + } } catch (e) { print(e); return GenericResult( @@ -159,7 +165,7 @@ class DigitalOceanApi extends ServerProviderApi with VolumeProviderApi { return GenericResult( data: volumes, - success: false, + success: true, ); } @@ -297,7 +303,7 @@ class DigitalOceanApi extends ServerProviderApi with VolumeProviderApi { ); } - Future createServer({ + Future> createServer({ required final String dnsApiToken, required final String dnsProviderType, required final String serverApiToken, @@ -310,6 +316,7 @@ class DigitalOceanApi extends ServerProviderApi with VolumeProviderApi { }) async { final String stagingAcme = StagingOptions.stagingAcme ? 'true' : 'false'; + int? dropletId; Response? serverCreateResponse; final Dio client = await getClient(); try { @@ -331,6 +338,7 @@ class DigitalOceanApi extends ServerProviderApi with VolumeProviderApi { '/droplets', data: data, ); + dropletId = serverCreateResponse.data['droplet']['id']; } catch (e) { print(e); return GenericResult( @@ -343,7 +351,7 @@ class DigitalOceanApi extends ServerProviderApi with VolumeProviderApi { } return GenericResult( - data: serverCreateResponse, + data: dropletId, success: true, code: serverCreateResponse.statusCode, message: serverCreateResponse.statusMessage, @@ -502,8 +510,9 @@ class DigitalOceanApi extends ServerProviderApi with VolumeProviderApi { return GenericResult(success: true, data: servers); } - Future> getAvailableLocations() async { - List locations = []; + Future>> + getAvailableLocations() async { + final List locations = []; final Dio client = await getClient(); try { @@ -511,7 +520,9 @@ class DigitalOceanApi extends ServerProviderApi with VolumeProviderApi { '/regions', ); - locations = response.data!['regions']; + for (final region in response.data!['regions']) { + locations.add(DigitalOceanLocation.fromJson(region)); + } } catch (e) { print(e); return GenericResult( diff --git a/lib/logic/models/json/digital_ocean_server_info.dart b/lib/logic/models/json/digital_ocean_server_info.dart new file mode 100644 index 00000000..ee9e9162 --- /dev/null +++ b/lib/logic/models/json/digital_ocean_server_info.dart @@ -0,0 +1,39 @@ +import 'package:json_annotation/json_annotation.dart'; + +part 'digital_ocean_server_info.g.dart'; + +@JsonSerializable() +class DigitalOceanVolume { + DigitalOceanVolume( + this.id, + this.name, + this.sizeGigabytes, + this.dropletIds, + ); + + final String id; + final String name; + + @JsonKey(name: 'droplet_ids') + final List dropletIds; + + @JsonKey(name: 'size_gigabytes') + final int sizeGigabytes; + + static DigitalOceanVolume fromJson(final Map json) => + _$DigitalOceanVolumeFromJson(json); +} + +@JsonSerializable() +class DigitalOceanLocation { + DigitalOceanLocation( + this.slug, + this.name, + ); + + final String slug; + final String name; + + static DigitalOceanLocation fromJson(final Map json) => + _$DigitalOceanLocationFromJson(json); +} diff --git a/lib/logic/models/json/digital_ocean_server_info.g.dart b/lib/logic/models/json/digital_ocean_server_info.g.dart new file mode 100644 index 00000000..6a6a9b45 --- /dev/null +++ b/lib/logic/models/json/digital_ocean_server_info.g.dart @@ -0,0 +1,37 @@ +// GENERATED CODE - DO NOT MODIFY BY HAND + +part of 'digital_ocean_server_info.dart'; + +// ************************************************************************** +// JsonSerializableGenerator +// ************************************************************************** + +DigitalOceanVolume _$DigitalOceanVolumeFromJson(Map json) => + DigitalOceanVolume( + json['id'] as String, + json['name'] as String, + json['size_gigabytes'] as int, + (json['droplet_ids'] as List).map((e) => e as int).toList(), + ); + +Map _$DigitalOceanVolumeToJson(DigitalOceanVolume instance) => + { + 'id': instance.id, + 'name': instance.name, + 'droplet_ids': instance.dropletIds, + 'size_gigabytes': instance.sizeGigabytes, + }; + +DigitalOceanLocation _$DigitalOceanLocationFromJson( + Map json) => + DigitalOceanLocation( + json['slug'] as String, + json['name'] as String, + ); + +Map _$DigitalOceanLocationToJson( + DigitalOceanLocation instance) => + { + 'slug': instance.slug, + 'name': instance.name, + }; diff --git a/lib/logic/models/json/hetzner_server_info.g.dart b/lib/logic/models/json/hetzner_server_info.g.dart index 7cc08abc..74607bad 100644 --- a/lib/logic/models/json/hetzner_server_info.g.dart +++ b/lib/logic/models/json/hetzner_server_info.g.dart @@ -81,6 +81,8 @@ HetznerServerTypeInfo _$HetznerServerTypeInfoFromJson( (json['prices'] as List) .map((e) => HetznerPriceInfo.fromJson(e as Map)) .toList(), + json['name'] as String, + json['description'] as String, ); Map _$HetznerServerTypeInfoToJson( @@ -89,6 +91,8 @@ Map _$HetznerServerTypeInfoToJson( 'cores': instance.cores, 'memory': instance.memory, 'disk': instance.disk, + 'name': instance.name, + 'description': instance.description, 'prices': instance.prices, }; @@ -96,12 +100,14 @@ HetznerPriceInfo _$HetznerPriceInfoFromJson(Map json) => HetznerPriceInfo( HetznerPriceInfo.getPrice(json['price_hourly'] as Map), HetznerPriceInfo.getPrice(json['price_monthly'] as Map), + json['location'] as String, ); Map _$HetznerPriceInfoToJson(HetznerPriceInfo instance) => { 'price_hourly': instance.hourly, 'price_monthly': instance.monthly, + 'location': instance.location, }; HetznerLocation _$HetznerLocationFromJson(Map json) => @@ -110,10 +116,12 @@ HetznerLocation _$HetznerLocationFromJson(Map json) => json['city'] as String, json['description'] as String, json['network_zone'] as String, + json['name'] as String, ); Map _$HetznerLocationToJson(HetznerLocation instance) => { + 'name': instance.name, 'country': instance.country, 'city': instance.city, 'description': instance.description, diff --git a/lib/logic/providers/server_providers/digital_ocean.dart b/lib/logic/providers/server_providers/digital_ocean.dart index 67808d6e..53c24df7 100644 --- a/lib/logic/providers/server_providers/digital_ocean.dart +++ b/lib/logic/providers/server_providers/digital_ocean.dart @@ -6,6 +6,7 @@ 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/hive/server_domain.dart'; +import 'package:selfprivacy/logic/models/json/digital_ocean_server_info.dart'; import 'package:selfprivacy/logic/models/metrics.dart'; import 'package:selfprivacy/logic/models/price.dart'; import 'package:selfprivacy/logic/models/server_basic_info.dart'; @@ -179,10 +180,13 @@ class DigitalOceanServerProvider extends ServerProvider { } try { - final int dropletId = serverResult.data['droplet']['id']; - final ServerVolume? newVolume = (await createVolume()).data; - final bool attachedVolume = - (await attachVolume(newVolume!, dropletId)).data; + final int dropletId = serverResult.data!; + final newVolume = (await createVolume()).data; + final bool attachedVolume = (await _adapter.api().attachVolume( + newVolume!.name, + dropletId, + )) + .data; String? ipv4; int attempts = 0; @@ -253,15 +257,15 @@ class DigitalOceanServerProvider extends ServerProvider { ); } - final List rawLocations = result.data; + final List rawLocations = result.data; for (final rawLocation in rawLocations) { ServerProviderLocation? location; try { location = ServerProviderLocation( - title: rawLocation['slug'], - description: rawLocation['name'], - flag: getEmojiFlag(rawLocation['slug']), - identifier: rawLocation['slug'], + title: rawLocation.slug, + description: rawLocation.name, + flag: getEmojiFlag(rawLocation.slug), + identifier: rawLocation.slug, ); } catch (e) { continue; @@ -638,17 +642,15 @@ class DigitalOceanServerProvider extends ServerProvider { try { int id = 0; for (final rawVolume in result.data) { - final volumeId = rawVolume['id']; - final int volumeSize = rawVolume['size_gigabytes'] * 1024 * 1024 * 1024; - final volumeDropletIds = rawVolume['droplet_ids']; - final String volumeName = rawVolume['name']; + final String volumeName = rawVolume.name; final volume = ServerVolume( id: id++, name: volumeName, - sizeByte: volumeSize, - serverId: volumeDropletIds.isNotEmpty ? volumeDropletIds[0] : null, + sizeByte: rawVolume.sizeGigabytes * 1024 * 1024 * 1024, + serverId: + rawVolume.dropletIds.isNotEmpty ? rawVolume.dropletIds[0] : null, linuxDevice: 'scsi-0DO_Volume_$volumeName', - uuid: volumeId, + uuid: rawVolume.id, ); volumes.add(volume); } @@ -693,16 +695,14 @@ class DigitalOceanServerProvider extends ServerProvider { ); } - final volumeId = result.data['volume']['id']; - final volumeSize = result.data['volume']['size_gigabytes']; - final volumeName = result.data['volume']['name']; + final String volumeName = result.data!.name; volume = ServerVolume( id: getVolumesResult.data.length, name: volumeName, - sizeByte: volumeSize, + sizeByte: result.data!.sizeGigabytes, serverId: null, linuxDevice: '/dev/disk/by-id/scsi-0DO_Volume_$volumeName', - uuid: volumeId, + uuid: result.data!.id, ); return GenericResult(