2022-10-11 20:11:13 +00:00
|
|
|
import 'dart:io';
|
|
|
|
|
|
|
|
import 'package:dio/dio.dart';
|
|
|
|
import 'package:selfprivacy/config/get_it_config.dart';
|
2023-06-19 18:17:15 +00:00
|
|
|
import 'package:selfprivacy/logic/api_maps/generic_result.dart';
|
|
|
|
import 'package:selfprivacy/logic/api_maps/rest_maps/rest_api_map.dart';
|
2023-06-16 02:59:48 +00:00
|
|
|
import 'package:selfprivacy/logic/api_maps/tls_options.dart';
|
2022-10-11 20:11:13 +00:00
|
|
|
import 'package:selfprivacy/logic/models/hive/user.dart';
|
2023-06-07 06:22:27 +00:00
|
|
|
import 'package:selfprivacy/logic/models/json/digital_ocean_server_info.dart';
|
2022-10-11 20:11:13 +00:00
|
|
|
import 'package:selfprivacy/utils/password_generator.dart';
|
|
|
|
|
2023-06-19 18:17:15 +00:00
|
|
|
class DigitalOceanApi extends RestApiMap {
|
2022-10-12 01:42:45 +00:00
|
|
|
DigitalOceanApi({
|
2022-10-13 21:15:42 +00:00
|
|
|
required this.region,
|
2022-12-15 11:42:21 +00:00
|
|
|
this.hasLogger = true,
|
2022-10-12 01:42:45 +00:00
|
|
|
this.isWithToken = true,
|
|
|
|
});
|
2022-10-11 20:11:13 +00:00
|
|
|
@override
|
|
|
|
bool hasLogger;
|
|
|
|
@override
|
|
|
|
bool isWithToken;
|
|
|
|
|
2022-10-16 00:51:10 +00:00
|
|
|
final String? region;
|
2022-10-12 04:55:30 +00:00
|
|
|
|
2022-10-11 20:11:13 +00:00
|
|
|
@override
|
|
|
|
BaseOptions get options {
|
2023-06-02 04:36:33 +00:00
|
|
|
final BaseOptions options = BaseOptions(
|
|
|
|
baseUrl: rootAddress,
|
|
|
|
contentType: Headers.jsonContentType,
|
|
|
|
responseType: ResponseType.json,
|
|
|
|
);
|
2022-10-11 20:11:13 +00:00
|
|
|
if (isWithToken) {
|
|
|
|
final String? token = getIt<ApiConfigModel>().serverProviderKey;
|
|
|
|
assert(token != null);
|
|
|
|
options.headers = {'Authorization': 'Bearer $token'};
|
|
|
|
}
|
|
|
|
|
|
|
|
if (validateStatus != null) {
|
|
|
|
options.validateStatus = validateStatus!;
|
|
|
|
}
|
|
|
|
|
|
|
|
return options;
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
2022-11-14 05:48:36 +00:00
|
|
|
String get rootAddress => 'https://api.digitalocean.com/v2';
|
2023-12-28 19:35:39 +00:00
|
|
|
String get infectProviderName => 'DIGITALOCEAN';
|
2022-10-11 20:11:13 +00:00
|
|
|
|
2023-07-13 09:22:39 +00:00
|
|
|
Future<GenericResult<List>> getServers() async {
|
|
|
|
List servers = [];
|
|
|
|
|
|
|
|
final Dio client = await getClient();
|
|
|
|
try {
|
|
|
|
final Response response = await client.get('/droplets');
|
|
|
|
servers = response.data['droplets'];
|
|
|
|
} catch (e) {
|
|
|
|
print(e);
|
|
|
|
return GenericResult(
|
|
|
|
success: false,
|
|
|
|
data: servers,
|
|
|
|
message: e.toString(),
|
|
|
|
);
|
|
|
|
} finally {
|
|
|
|
close(client);
|
|
|
|
}
|
|
|
|
|
|
|
|
return GenericResult(success: true, data: servers);
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<GenericResult<int?>> createServer({
|
|
|
|
required final String dnsApiToken,
|
|
|
|
required final String dnsProviderType,
|
|
|
|
required final String serverApiToken,
|
|
|
|
required final User rootUser,
|
|
|
|
required final String base64Password,
|
|
|
|
required final String databasePassword,
|
|
|
|
required final String domainName,
|
|
|
|
required final String hostName,
|
|
|
|
required final String serverType,
|
2023-12-28 19:35:39 +00:00
|
|
|
required final String? customSshKey,
|
2023-07-13 09:22:39 +00:00
|
|
|
}) async {
|
|
|
|
final String stagingAcme = TlsOptions.stagingAcme ? 'true' : 'false';
|
|
|
|
|
|
|
|
int? dropletId;
|
|
|
|
Response? serverCreateResponse;
|
|
|
|
final Dio client = await getClient();
|
|
|
|
try {
|
|
|
|
final Map<String, Object> data = {
|
|
|
|
'name': hostName,
|
|
|
|
'size': serverType,
|
|
|
|
'image': 'ubuntu-20-04-x64',
|
|
|
|
'user_data': '#cloud-config\n'
|
|
|
|
'runcmd:\n'
|
2023-12-28 19:35:39 +00:00
|
|
|
'- curl https://git.selfprivacy.org/SelfPrivacy/selfprivacy-nixos-infect/raw/branch/master/nixos-infect | '
|
|
|
|
"API_TOKEN=$serverApiToken CONFIG_URL='https://git.selfprivacy.org/SelfPrivacy/selfprivacy-nixos-template/archive/master.tar.gz' "
|
|
|
|
"DNS_PROVIDER_TOKEN=$dnsApiToken DNS_PROVIDER_TYPE=$dnsProviderType DOMAIN='$domainName' ENCODED_PASSWORD='$base64Password' "
|
|
|
|
"HOSTNAME=$hostName LUSER='${rootUser.login}' NIX_VERSION=2.18.1 PROVIDER=$infectProviderName STAGING_ACME='$stagingAcme' "
|
|
|
|
"${customSshKey != null ? "SSH_AUTHORIZED_KEY='$customSshKey'" : ""} "
|
|
|
|
'bash 2>&1 | tee /root/nixos-infect.log',
|
2023-07-13 09:22:39 +00:00
|
|
|
'region': region!,
|
|
|
|
};
|
|
|
|
print('Decoded data: $data');
|
|
|
|
|
|
|
|
serverCreateResponse = await client.post(
|
|
|
|
'/droplets',
|
|
|
|
data: data,
|
|
|
|
);
|
|
|
|
dropletId = serverCreateResponse.data['droplet']['id'];
|
|
|
|
} catch (e) {
|
|
|
|
print(e);
|
|
|
|
return GenericResult(
|
|
|
|
success: false,
|
|
|
|
data: null,
|
|
|
|
message: e.toString(),
|
|
|
|
);
|
|
|
|
} finally {
|
|
|
|
close(client);
|
|
|
|
}
|
|
|
|
|
|
|
|
return GenericResult(
|
|
|
|
data: dropletId,
|
|
|
|
success: true,
|
|
|
|
code: serverCreateResponse.statusCode,
|
|
|
|
message: serverCreateResponse.statusMessage,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<GenericResult<void>> deleteServer(final int serverId) async {
|
|
|
|
final Dio client = await getClient();
|
|
|
|
try {
|
|
|
|
await client.delete('/droplets/$serverId');
|
|
|
|
} catch (e) {
|
|
|
|
print(e);
|
|
|
|
return GenericResult(
|
|
|
|
success: false,
|
|
|
|
data: null,
|
|
|
|
message: e.toString(),
|
|
|
|
);
|
|
|
|
} finally {
|
|
|
|
close(client);
|
|
|
|
}
|
|
|
|
|
|
|
|
return GenericResult(success: true, data: null);
|
|
|
|
}
|
|
|
|
|
2023-02-13 14:13:32 +00:00
|
|
|
Future<GenericResult<bool>> isApiTokenValid(final String token) async {
|
2022-10-11 20:11:13 +00:00
|
|
|
bool isValid = false;
|
|
|
|
Response? response;
|
2022-11-28 18:51:37 +00:00
|
|
|
String message = '';
|
2022-10-11 20:11:13 +00:00
|
|
|
final Dio client = await getClient();
|
|
|
|
try {
|
|
|
|
response = await client.get(
|
2022-10-12 01:42:45 +00:00
|
|
|
'/account',
|
2022-10-11 20:11:13 +00:00
|
|
|
options: Options(
|
2022-11-28 18:51:37 +00:00
|
|
|
followRedirects: false,
|
|
|
|
validateStatus: (final status) =>
|
|
|
|
status != null && (status >= 200 || status == 401),
|
2022-10-11 20:11:13 +00:00
|
|
|
headers: {'Authorization': 'Bearer $token'},
|
|
|
|
),
|
|
|
|
);
|
|
|
|
} catch (e) {
|
|
|
|
print(e);
|
|
|
|
isValid = false;
|
2022-11-28 18:51:37 +00:00
|
|
|
message = e.toString();
|
2022-10-11 20:11:13 +00:00
|
|
|
} finally {
|
|
|
|
close(client);
|
|
|
|
}
|
|
|
|
|
2022-11-28 18:51:37 +00:00
|
|
|
if (response == null) {
|
2023-02-13 14:13:32 +00:00
|
|
|
return GenericResult(
|
2022-11-28 18:51:37 +00:00
|
|
|
data: isValid,
|
|
|
|
success: false,
|
|
|
|
message: message,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (response.statusCode == HttpStatus.ok) {
|
|
|
|
isValid = true;
|
|
|
|
} else if (response.statusCode == HttpStatus.unauthorized) {
|
|
|
|
isValid = false;
|
|
|
|
} else {
|
|
|
|
throw Exception('code: ${response.statusCode}');
|
2022-10-11 20:11:13 +00:00
|
|
|
}
|
|
|
|
|
2023-02-13 14:13:32 +00:00
|
|
|
return GenericResult(
|
2022-11-28 18:51:37 +00:00
|
|
|
data: isValid,
|
|
|
|
success: true,
|
|
|
|
message: response.statusMessage,
|
|
|
|
);
|
2022-10-11 20:11:13 +00:00
|
|
|
}
|
|
|
|
|
2023-07-13 09:22:39 +00:00
|
|
|
Future<GenericResult<List<DigitalOceanLocation>>>
|
|
|
|
getAvailableLocations() async {
|
|
|
|
final List<DigitalOceanLocation> locations = [];
|
|
|
|
|
2022-10-11 20:11:13 +00:00
|
|
|
final Dio client = await getClient();
|
|
|
|
try {
|
2023-07-13 09:22:39 +00:00
|
|
|
final Response response = await client.get(
|
|
|
|
'/regions',
|
|
|
|
);
|
2022-10-17 17:42:23 +00:00
|
|
|
|
2023-07-13 09:22:39 +00:00
|
|
|
for (final region in response.data!['regions']) {
|
|
|
|
locations.add(DigitalOceanLocation.fromJson(region));
|
|
|
|
}
|
|
|
|
} catch (e) {
|
|
|
|
print(e);
|
|
|
|
return GenericResult(
|
|
|
|
data: [],
|
|
|
|
success: false,
|
|
|
|
message: e.toString(),
|
|
|
|
);
|
|
|
|
} finally {
|
|
|
|
close(client);
|
|
|
|
}
|
|
|
|
|
|
|
|
return GenericResult(data: locations, success: true);
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<GenericResult<List<DigitalOceanServerType>>>
|
|
|
|
getAvailableServerTypes() async {
|
|
|
|
final List<DigitalOceanServerType> types = [];
|
|
|
|
|
|
|
|
final Dio client = await getClient();
|
|
|
|
try {
|
|
|
|
final Response response = await client.get(
|
|
|
|
'/sizes',
|
|
|
|
);
|
|
|
|
for (final size in response.data!['sizes']) {
|
|
|
|
types.add(DigitalOceanServerType.fromJson(size));
|
|
|
|
}
|
|
|
|
} catch (e) {
|
|
|
|
print(e);
|
|
|
|
return GenericResult(
|
|
|
|
data: [],
|
|
|
|
success: false,
|
|
|
|
message: e.toString(),
|
|
|
|
);
|
|
|
|
} finally {
|
|
|
|
close(client);
|
|
|
|
}
|
|
|
|
|
|
|
|
return GenericResult(data: types, success: true);
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<GenericResult<void>> powerOn(final int serverId) async {
|
|
|
|
final Dio client = await getClient();
|
|
|
|
try {
|
|
|
|
await client.post(
|
|
|
|
'/droplets/$serverId/actions',
|
2022-10-11 20:11:13 +00:00
|
|
|
data: {
|
2023-07-13 09:22:39 +00:00
|
|
|
'type': 'power_on',
|
2022-10-11 20:11:13 +00:00
|
|
|
},
|
|
|
|
);
|
|
|
|
} catch (e) {
|
|
|
|
print(e);
|
2023-02-13 14:13:32 +00:00
|
|
|
return GenericResult(
|
2023-07-13 09:22:39 +00:00
|
|
|
success: false,
|
2022-12-02 18:40:08 +00:00
|
|
|
data: null,
|
2023-07-13 09:22:39 +00:00
|
|
|
message: e.toString(),
|
|
|
|
);
|
|
|
|
} finally {
|
|
|
|
close(client);
|
|
|
|
}
|
|
|
|
|
|
|
|
return GenericResult(success: true, data: null);
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<GenericResult<void>> restart(final int serverId) async {
|
|
|
|
final Dio client = await getClient();
|
|
|
|
try {
|
|
|
|
await client.post(
|
|
|
|
'/droplets/$serverId/actions',
|
|
|
|
data: {
|
|
|
|
'type': 'reboot',
|
|
|
|
},
|
|
|
|
);
|
|
|
|
} catch (e) {
|
|
|
|
print(e);
|
|
|
|
return GenericResult(
|
2022-12-02 18:40:08 +00:00
|
|
|
success: false,
|
2023-07-13 09:22:39 +00:00
|
|
|
data: null,
|
2022-12-02 18:40:08 +00:00
|
|
|
message: e.toString(),
|
|
|
|
);
|
2022-10-11 20:11:13 +00:00
|
|
|
} finally {
|
2023-07-13 09:22:39 +00:00
|
|
|
close(client);
|
2022-10-11 20:11:13 +00:00
|
|
|
}
|
|
|
|
|
2023-07-13 09:22:39 +00:00
|
|
|
return GenericResult(success: true, data: null);
|
2022-10-11 20:11:13 +00:00
|
|
|
}
|
|
|
|
|
2023-06-07 06:22:27 +00:00
|
|
|
Future<GenericResult<List<DigitalOceanVolume>>> getVolumes({
|
|
|
|
final String? status,
|
|
|
|
}) async {
|
|
|
|
final List<DigitalOceanVolume> volumes = [];
|
2022-10-11 20:11:13 +00:00
|
|
|
|
2023-03-21 16:08:46 +00:00
|
|
|
Response? getVolumesResponse;
|
2022-10-11 20:11:13 +00:00
|
|
|
final Dio client = await getClient();
|
|
|
|
try {
|
2022-11-23 07:55:28 +00:00
|
|
|
getVolumesResponse = await client.get(
|
2022-10-11 20:11:13 +00:00
|
|
|
'/volumes',
|
|
|
|
queryParameters: {
|
|
|
|
'status': status,
|
|
|
|
},
|
|
|
|
);
|
2023-06-07 06:22:27 +00:00
|
|
|
for (final volume in getVolumesResponse.data['volumes']) {
|
|
|
|
volumes.add(DigitalOceanVolume.fromJson(volume));
|
|
|
|
}
|
2022-10-11 20:11:13 +00:00
|
|
|
} catch (e) {
|
|
|
|
print(e);
|
2023-03-21 16:08:46 +00:00
|
|
|
return GenericResult(
|
|
|
|
data: [],
|
|
|
|
success: false,
|
|
|
|
message: e.toString(),
|
|
|
|
);
|
2022-10-11 20:11:13 +00:00
|
|
|
} finally {
|
|
|
|
client.close();
|
|
|
|
}
|
|
|
|
|
2023-03-21 16:08:46 +00:00
|
|
|
return GenericResult(
|
|
|
|
data: volumes,
|
2023-06-07 06:22:27 +00:00
|
|
|
success: true,
|
2023-03-21 16:08:46 +00:00
|
|
|
);
|
2022-10-11 20:11:13 +00:00
|
|
|
}
|
|
|
|
|
2023-08-06 23:28:02 +00:00
|
|
|
Future<GenericResult<DigitalOceanVolume?>> createVolume(final int gb) async {
|
2023-07-13 09:22:39 +00:00
|
|
|
DigitalOceanVolume? volume;
|
|
|
|
Response? createVolumeResponse;
|
2022-10-11 20:11:13 +00:00
|
|
|
final Dio client = await getClient();
|
|
|
|
try {
|
2023-07-13 09:22:39 +00:00
|
|
|
await Future.delayed(const Duration(seconds: 6));
|
|
|
|
|
|
|
|
createVolumeResponse = await client.post(
|
|
|
|
'/volumes',
|
|
|
|
data: {
|
2023-08-06 23:28:02 +00:00
|
|
|
'size_gigabytes': gb,
|
2023-07-13 09:22:39 +00:00
|
|
|
'name': 'volume${StringGenerators.storageName()}',
|
|
|
|
'labels': {'labelkey': 'value'},
|
|
|
|
'region': region,
|
|
|
|
'filesystem_type': 'ext4',
|
|
|
|
},
|
|
|
|
);
|
|
|
|
volume = DigitalOceanVolume.fromJson(createVolumeResponse.data['volume']);
|
2022-10-11 20:11:13 +00:00
|
|
|
} catch (e) {
|
|
|
|
print(e);
|
2023-03-21 16:08:46 +00:00
|
|
|
return GenericResult(
|
|
|
|
data: null,
|
|
|
|
success: false,
|
|
|
|
message: e.toString(),
|
|
|
|
);
|
2022-10-11 20:11:13 +00:00
|
|
|
} finally {
|
|
|
|
client.close();
|
|
|
|
}
|
2023-03-21 16:08:46 +00:00
|
|
|
|
|
|
|
return GenericResult(
|
2023-07-13 09:22:39 +00:00
|
|
|
data: volume,
|
2023-03-21 16:08:46 +00:00
|
|
|
success: true,
|
2023-07-13 09:22:39 +00:00
|
|
|
code: createVolumeResponse.statusCode,
|
|
|
|
message: createVolumeResponse.statusMessage,
|
2023-03-21 16:08:46 +00:00
|
|
|
);
|
2022-10-11 20:11:13 +00:00
|
|
|
}
|
|
|
|
|
2023-02-13 14:13:32 +00:00
|
|
|
Future<GenericResult<bool>> attachVolume(
|
2023-03-21 16:08:46 +00:00
|
|
|
final String name,
|
2022-10-26 16:07:35 +00:00
|
|
|
final int serverId,
|
|
|
|
) async {
|
2022-10-11 20:11:13 +00:00
|
|
|
bool success = false;
|
|
|
|
|
2022-12-02 18:40:08 +00:00
|
|
|
Response? attachVolumeResponse;
|
2022-10-11 20:11:13 +00:00
|
|
|
final Dio client = await getClient();
|
|
|
|
try {
|
2022-11-23 07:55:28 +00:00
|
|
|
attachVolumeResponse = await client.post(
|
2022-10-12 04:55:30 +00:00
|
|
|
'/volumes/actions',
|
2022-10-11 20:11:13 +00:00
|
|
|
data: {
|
2022-10-12 04:55:30 +00:00
|
|
|
'type': 'attach',
|
2023-03-21 16:08:46 +00:00
|
|
|
'volume_name': name,
|
2022-10-12 04:55:30 +00:00
|
|
|
'region': region,
|
|
|
|
'droplet_id': serverId,
|
2022-10-11 20:11:13 +00:00
|
|
|
},
|
|
|
|
);
|
2022-11-23 07:55:28 +00:00
|
|
|
success =
|
|
|
|
attachVolumeResponse.data['action']['status'].toString() != 'error';
|
2022-10-11 20:11:13 +00:00
|
|
|
} catch (e) {
|
|
|
|
print(e);
|
2023-02-13 14:13:32 +00:00
|
|
|
return GenericResult(
|
2022-12-02 18:40:08 +00:00
|
|
|
data: false,
|
|
|
|
success: false,
|
|
|
|
message: e.toString(),
|
|
|
|
);
|
2022-10-11 20:11:13 +00:00
|
|
|
} finally {
|
2022-10-27 16:01:22 +00:00
|
|
|
close(client);
|
2022-10-11 20:11:13 +00:00
|
|
|
}
|
|
|
|
|
2023-02-13 14:13:32 +00:00
|
|
|
return GenericResult(
|
2022-12-02 18:40:08 +00:00
|
|
|
data: success,
|
|
|
|
success: true,
|
|
|
|
code: attachVolumeResponse.statusCode,
|
|
|
|
message: attachVolumeResponse.statusMessage,
|
|
|
|
);
|
2022-10-11 20:11:13 +00:00
|
|
|
}
|
|
|
|
|
2023-03-21 16:08:46 +00:00
|
|
|
Future<GenericResult<bool>> detachVolume(
|
|
|
|
final String name,
|
|
|
|
final int serverId,
|
|
|
|
) async {
|
2022-10-11 20:11:13 +00:00
|
|
|
bool success = false;
|
|
|
|
|
2022-11-23 07:55:28 +00:00
|
|
|
final Response detachVolumeResponse;
|
2022-10-11 20:11:13 +00:00
|
|
|
final Dio client = await getClient();
|
|
|
|
try {
|
2022-11-23 07:55:28 +00:00
|
|
|
detachVolumeResponse = await client.post(
|
2022-10-12 04:55:30 +00:00
|
|
|
'/volumes/actions',
|
|
|
|
data: {
|
|
|
|
'type': 'detach',
|
2023-03-21 16:08:46 +00:00
|
|
|
'volume_name': name,
|
|
|
|
'droplet_id': serverId,
|
2022-10-12 04:55:30 +00:00
|
|
|
'region': region,
|
|
|
|
},
|
|
|
|
);
|
2022-11-23 07:55:28 +00:00
|
|
|
success =
|
|
|
|
detachVolumeResponse.data['action']['status'].toString() != 'error';
|
2022-10-11 20:11:13 +00:00
|
|
|
} catch (e) {
|
|
|
|
print(e);
|
2023-03-21 16:08:46 +00:00
|
|
|
return GenericResult(
|
|
|
|
data: false,
|
|
|
|
success: false,
|
|
|
|
message: e.toString(),
|
|
|
|
);
|
2022-10-11 20:11:13 +00:00
|
|
|
} finally {
|
|
|
|
client.close();
|
|
|
|
}
|
|
|
|
|
2023-03-21 16:08:46 +00:00
|
|
|
return GenericResult(
|
|
|
|
data: success,
|
|
|
|
success: true,
|
|
|
|
);
|
2022-10-11 20:11:13 +00:00
|
|
|
}
|
|
|
|
|
2023-07-13 09:22:39 +00:00
|
|
|
Future<GenericResult<void>> deleteVolume(final String uuid) async {
|
|
|
|
final Dio client = await getClient();
|
|
|
|
try {
|
|
|
|
await client.delete('/volumes/$uuid');
|
|
|
|
} catch (e) {
|
|
|
|
print(e);
|
|
|
|
return GenericResult(
|
|
|
|
data: null,
|
|
|
|
success: false,
|
|
|
|
message: e.toString(),
|
|
|
|
);
|
|
|
|
} finally {
|
|
|
|
client.close();
|
|
|
|
}
|
|
|
|
|
|
|
|
return GenericResult(
|
|
|
|
data: null,
|
|
|
|
success: true,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2023-03-21 16:08:46 +00:00
|
|
|
Future<GenericResult<bool>> resizeVolume(
|
2023-10-01 00:02:53 +00:00
|
|
|
final String uuid,
|
2023-10-01 00:35:58 +00:00
|
|
|
final int gb,
|
2022-11-04 12:18:35 +00:00
|
|
|
) async {
|
2022-10-11 20:11:13 +00:00
|
|
|
bool success = false;
|
|
|
|
|
2022-11-23 07:55:28 +00:00
|
|
|
final Response resizeVolumeResponse;
|
2022-10-11 20:11:13 +00:00
|
|
|
final Dio client = await getClient();
|
|
|
|
try {
|
2022-11-23 07:55:28 +00:00
|
|
|
resizeVolumeResponse = await client.post(
|
2023-10-01 00:02:53 +00:00
|
|
|
'/volumes/$uuid/actions',
|
2022-10-11 20:11:13 +00:00
|
|
|
data: {
|
2022-10-12 04:55:30 +00:00
|
|
|
'type': 'resize',
|
2023-10-01 00:35:58 +00:00
|
|
|
'size_gigabytes': gb,
|
2022-10-12 04:55:30 +00:00
|
|
|
'region': region,
|
2022-10-11 20:11:13 +00:00
|
|
|
},
|
|
|
|
);
|
2022-11-23 07:55:28 +00:00
|
|
|
success =
|
|
|
|
resizeVolumeResponse.data['action']['status'].toString() != 'error';
|
2022-10-11 20:11:13 +00:00
|
|
|
} catch (e) {
|
|
|
|
print(e);
|
2023-03-21 16:08:46 +00:00
|
|
|
return GenericResult(
|
|
|
|
data: false,
|
|
|
|
success: false,
|
|
|
|
message: e.toString(),
|
|
|
|
);
|
2022-10-11 20:11:13 +00:00
|
|
|
} finally {
|
|
|
|
client.close();
|
|
|
|
}
|
|
|
|
|
2023-03-21 16:08:46 +00:00
|
|
|
return GenericResult(
|
|
|
|
data: success,
|
|
|
|
success: true,
|
|
|
|
);
|
2022-10-11 20:11:13 +00:00
|
|
|
}
|
|
|
|
|
2023-03-02 23:01:09 +00:00
|
|
|
Future<GenericResult<List>> getMetricsCpu(
|
2022-11-12 17:29:06 +00:00
|
|
|
final int serverId,
|
2022-10-11 20:11:13 +00:00
|
|
|
final DateTime start,
|
|
|
|
final DateTime end,
|
|
|
|
) async {
|
2023-03-02 23:01:09 +00:00
|
|
|
List metrics = [];
|
2022-11-14 16:45:05 +00:00
|
|
|
|
|
|
|
final Dio client = await getClient();
|
2022-11-23 07:55:28 +00:00
|
|
|
try {
|
2023-03-02 23:01:09 +00:00
|
|
|
final Response response = await client.get(
|
2022-11-23 07:55:28 +00:00
|
|
|
'/monitoring/metrics/droplet/cpu',
|
|
|
|
queryParameters: {
|
|
|
|
'start': '${(start.microsecondsSinceEpoch / 1000000).round()}',
|
|
|
|
'end': '${(end.microsecondsSinceEpoch / 1000000).round()}',
|
|
|
|
'host_id': '$serverId',
|
|
|
|
},
|
|
|
|
);
|
2023-03-02 23:01:09 +00:00
|
|
|
metrics = response.data['data']['result'];
|
2022-11-23 07:55:28 +00:00
|
|
|
} catch (e) {
|
2022-11-14 16:45:05 +00:00
|
|
|
print(e);
|
2023-03-02 23:01:09 +00:00
|
|
|
return GenericResult(
|
|
|
|
success: false,
|
|
|
|
data: [],
|
|
|
|
message: e.toString(),
|
|
|
|
);
|
2022-11-14 16:45:05 +00:00
|
|
|
} finally {
|
|
|
|
close(client);
|
2022-11-23 07:55:28 +00:00
|
|
|
}
|
2022-11-14 16:45:05 +00:00
|
|
|
|
2023-03-02 23:01:09 +00:00
|
|
|
return GenericResult(success: true, data: metrics);
|
2022-10-11 20:11:13 +00:00
|
|
|
}
|
|
|
|
|
2023-03-02 23:01:09 +00:00
|
|
|
Future<GenericResult<List>> getMetricsBandwidth(
|
|
|
|
final int serverId,
|
|
|
|
final DateTime start,
|
|
|
|
final DateTime end,
|
|
|
|
final bool isInbound,
|
|
|
|
) async {
|
|
|
|
List metrics = [];
|
2022-11-11 03:32:01 +00:00
|
|
|
|
2022-10-11 20:11:13 +00:00
|
|
|
final Dio client = await getClient();
|
2022-11-11 03:32:01 +00:00
|
|
|
try {
|
2023-03-02 23:01:09 +00:00
|
|
|
final Response response = await client.get(
|
|
|
|
'/monitoring/metrics/droplet/bandwidth',
|
|
|
|
queryParameters: {
|
|
|
|
'start': '${(start.microsecondsSinceEpoch / 1000000).round()}',
|
|
|
|
'end': '${(end.microsecondsSinceEpoch / 1000000).round()}',
|
|
|
|
'host_id': '$serverId',
|
|
|
|
'interface': 'public',
|
|
|
|
'direction': isInbound ? 'inbound' : 'outbound',
|
|
|
|
},
|
|
|
|
);
|
|
|
|
metrics = response.data['data']['result'][0]['values'];
|
2022-11-11 03:32:01 +00:00
|
|
|
} catch (e) {
|
|
|
|
print(e);
|
2023-03-02 23:01:09 +00:00
|
|
|
return GenericResult(
|
|
|
|
success: false,
|
|
|
|
data: [],
|
|
|
|
message: e.toString(),
|
|
|
|
);
|
2022-11-11 03:32:01 +00:00
|
|
|
} finally {
|
|
|
|
close(client);
|
|
|
|
}
|
2022-10-11 20:11:13 +00:00
|
|
|
|
2023-03-02 23:01:09 +00:00
|
|
|
return GenericResult(success: true, data: metrics);
|
2022-10-11 20:11:13 +00:00
|
|
|
}
|
|
|
|
}
|