chore: Implement HetznerVolume model and use it instead dynamic json

This commit is contained in:
NaiJi 2023-06-06 23:35:57 -03:00
parent 4da4ed6afd
commit ffa13aee04
4 changed files with 57 additions and 21 deletions

View file

@ -131,8 +131,9 @@ class HetznerApi extends ServerProviderApi with VolumeProviderApi {
);
}
Future<GenericResult> createVolume() async {
Future<GenericResult<HetznerVolume?>> createVolume() async {
Response? createVolumeResponse;
HetznerVolume? volume;
final Dio client = await getClient();
try {
createVolumeResponse = await client.post(
@ -146,6 +147,7 @@ class HetznerApi extends ServerProviderApi with VolumeProviderApi {
'format': 'ext4'
},
);
volume = HetznerVolume.fromJson(createVolumeResponse.data);
} catch (e) {
print(e);
return GenericResult(
@ -158,7 +160,7 @@ class HetznerApi extends ServerProviderApi with VolumeProviderApi {
}
return GenericResult(
data: createVolumeResponse.data,
data: volume,
success: true,
code: createVolumeResponse.statusCode,
message: createVolumeResponse.statusMessage,

View file

@ -113,3 +113,24 @@ class HetznerLocation {
static HetznerLocation fromJson(final Map<String, dynamic> json) =>
_$HetznerLocationFromJson(json);
}
@JsonSerializable()
class HetznerVolume {
HetznerVolume(
this.id,
this.sizeByte,
this.serverId,
this.name,
this.linuxDevice,
);
final int id;
final int sizeByte;
final int? serverId;
final String name;
@JsonKey(name: 'linux_device')
final String? linuxDevice;
static HetznerVolume fromJson(final Map<String, dynamic> json) =>
_$HetznerVolumeFromJson(json);
}

View file

@ -119,3 +119,21 @@ Map<String, dynamic> _$HetznerLocationToJson(HetznerLocation instance) =>
'description': instance.description,
'network_zone': instance.zone,
};
HetznerVolume _$HetznerVolumeFromJson(Map<String, dynamic> json) =>
HetznerVolume(
json['id'] as int,
json['sizeByte'] as int,
json['serverId'] as int?,
json['name'] as String,
json['linux_device'] as String?,
);
Map<String, dynamic> _$HetznerVolumeToJson(HetznerVolume instance) =>
<String, dynamic>{
'id': instance.id,
'sizeByte': instance.sizeByte,
'serverId': instance.serverId,
'name': instance.name,
'linux_device': instance.linuxDevice,
};

View file

@ -441,7 +441,7 @@ class HetznerServerProvider extends ServerProvider {
);
}
final volume = volumeResult.data['volume'];
final volume = volumeResult.data!;
final serverApiToken = StringGenerators.apiToken();
final hostname = getHostnameFromDomain(
installationData.serverDomain.domainName,
@ -455,7 +455,7 @@ class HetznerServerProvider extends ServerProvider {
dnsProviderType:
dnsProviderToInfectName(installationData.dnsProviderType),
hostName: hostname,
volumeId: volume['id'],
volumeId: volume.id,
base64Password: base64.encode(
utf8.encode(installationData.rootUser.password ?? 'PASS'),
),
@ -464,7 +464,7 @@ class HetznerServerProvider extends ServerProvider {
);
if (!serverResult.success || serverResult.data == null) {
await _adapter.api().deleteVolume(volume['id']);
await _adapter.api().deleteVolume(volume.id);
await Future.delayed(const Duration(seconds: 5));
if (serverResult.message != null &&
serverResult.message == 'uniqueness_error') {
@ -530,11 +530,11 @@ class HetznerServerProvider extends ServerProvider {
ip4: serverResult.data['server']['public_net']['ipv4']['ip'],
createTime: DateTime.now(),
volume: ServerVolume(
id: volume['id'],
name: volume['name'],
sizeByte: volume['size'],
serverId: volume['server'],
linuxDevice: volume['linux_device'],
id: volume.id,
name: volume.name,
sizeByte: volume.sizeByte,
serverId: volume.serverId,
linuxDevice: volume.linuxDevice,
),
apiToken: serverApiToken,
provider: ServerProviderType.hetzner,
@ -564,7 +564,7 @@ class HetznerServerProvider extends ServerProvider {
CallbackDialogueChoice(
title: 'modals.try_again'.tr(),
callback: () async {
await _adapter.api().deleteVolume(volume['id']);
await _adapter.api().deleteVolume(volume.id);
await Future.delayed(const Duration(seconds: 5));
final deletion = await deleteServer(hostname);
if (deletion.success) {
@ -673,17 +673,12 @@ class HetznerServerProvider extends ServerProvider {
}
try {
final volumeId = result.data['volume']['id'];
final volumeSize = result.data['volume']['size'];
final volumeServer = result.data['volume']['server'];
final volumeName = result.data['volume']['name'];
final volumeDevice = result.data['volume']['linux_device'];
volume = ServerVolume(
id: volumeId,
name: volumeName,
sizeByte: volumeSize,
serverId: volumeServer,
linuxDevice: volumeDevice,
id: result.data!.id,
name: result.data!.name,
sizeByte: result.data!.sizeByte,
serverId: result.data!.serverId,
linuxDevice: result.data!.linuxDevice,
);
} catch (e) {
print(e);