Implement hetzner volumes cubit

pull/97/head
NaiJi ✨ 2022-06-27 10:07:11 +03:00
parent 352351663f
commit 0a919907c8
7 changed files with 127 additions and 7 deletions

View File

@ -74,10 +74,14 @@ class HetznerApi extends ApiMap {
},
);
final dbId = dbCreateResponse.data['volume']['id'];
final dbSize = dbCreateResponse.data['volume']['size'];
final dbServer = dbCreateResponse.data['volume']['server'];
final dbName = dbCreateResponse.data['volume']['name'];
volume = ServerVolume(
id: dbId,
name: dbName,
sizeGb: dbSize,
serverId: dbServer,
);
} catch (e) {
print(e);
@ -88,7 +92,7 @@ class HetznerApi extends ApiMap {
return volume;
}
Future<List<ServerVolume>> getVolumes(final String? status) async {
Future<List<ServerVolume>> getVolumes({final String? status}) async {
final List<ServerVolume> volumes = [];
final Response dbGetResponse;
@ -103,10 +107,14 @@ class HetznerApi extends ApiMap {
final List<dynamic> rawVolumes = dbGetResponse.data['volumes'];
for (final rawVolume in rawVolumes) {
final int dbId = rawVolume['id'];
final int dbSize = rawVolume['size'];
final dbServer = rawVolume['server'];
final String dbName = rawVolume['name'];
final volume = ServerVolume(
id: dbId,
name: dbName,
sizeGb: dbSize,
serverId: dbServer,
);
volumes.add(volume);
}
@ -127,10 +135,14 @@ class HetznerApi extends ApiMap {
try {
dbGetResponse = await client.get('/volumes/$id');
final int dbId = dbGetResponse.data['volume']['id'];
final int dbSize = dbGetResponse.data['volume']['size'];
final int dbServer = dbGetResponse.data['volume']['server'];
final String dbName = dbGetResponse.data['volume']['name'];
volume = ServerVolume(
id: dbId,
name: dbName,
sizeGb: dbSize,
serverId: dbServer,
);
} catch (e) {
print(e);

View File

@ -16,17 +16,16 @@ class ApiDevicesCubit
@override
void load() async {
if (serverInstallationCubit.state is ServerInstallationFinished) {
final List<ApiToken>? devices = await _getApiTokens();
if (devices != null) {
emit(ApiDevicesState(devices, LoadingStatus.success));
} else {
emit(const ApiDevicesState([], LoadingStatus.error));
}
_refetch();
}
}
Future<void> refresh() async {
emit(const ApiDevicesState([], LoadingStatus.refreshing));
_refetch();
}
void _refetch() async {
final List<ApiToken>? devices = await _getApiTokens();
if (devices != null) {
emit(ApiDevicesState(devices, LoadingStatus.success));

View File

@ -491,6 +491,8 @@ class ServerInstallationCubit extends Cubit<ServerInstallationState> {
volume: ServerVolume(
id: server.volumeId,
name: 'recovered_volume',
sizeGb: 10, // ?????
serverId: server.id,
),
apiToken: dataState.serverDetails!.apiToken,
provider: ServerProvider.hetzner,

View File

@ -451,6 +451,8 @@ class ServerInstallationRepository {
volume: ServerVolume(
id: 0,
name: '',
sizeGb: 10,
serverId: 0,
),
provider: ServerProvider.unknown,
id: 0,
@ -485,6 +487,8 @@ class ServerInstallationRepository {
volume: ServerVolume(
id: 0,
name: '',
sizeGb: 10,
serverId: 0,
),
provider: ServerProvider.unknown,
id: 0,
@ -519,6 +523,8 @@ class ServerInstallationRepository {
volume: ServerVolume(
id: 0,
name: '',
serverId: 0,
sizeGb: 10,
),
provider: ServerProvider.unknown,
id: 0,
@ -544,6 +550,8 @@ class ServerInstallationRepository {
volume: ServerVolume(
id: 0,
name: '',
sizeGb: 10,
serverId: 0,
),
provider: ServerProvider.unknown,
id: 0,

View File

@ -0,0 +1,70 @@
import 'package:selfprivacy/config/get_it_config.dart';
import 'package:selfprivacy/logic/api_maps/hetzner.dart';
import 'package:selfprivacy/logic/api_maps/server.dart';
import 'package:selfprivacy/logic/common_enum/common_enum.dart';
import 'package:selfprivacy/logic/cubit/app_config_dependent/authentication_dependend_cubit.dart';
import 'package:selfprivacy/logic/models/hive/server_details.dart';
part 'volumes_state.dart';
class ApiVolumesCubit
extends ServerInstallationDependendCubit<ApiVolumesState> {
ApiVolumesCubit(final ServerInstallationCubit serverInstallationCubit)
: super(serverInstallationCubit, const ApiVolumesState.initial());
final ServerApi api = ServerApi();
@override
void load() async {
if (serverInstallationCubit.state is ServerInstallationFinished) {
_refetch();
}
}
void refresh() async {
emit(const ApiVolumesState([], LoadingStatus.refreshing));
_refetch();
}
void _refetch() async {
final List<ServerVolume> volumes = await HetznerApi().getVolumes();
if (volumes.isNotEmpty) {
emit(ApiVolumesState(volumes, LoadingStatus.success));
} else {
emit(const ApiVolumesState([], LoadingStatus.error));
}
}
void attachVolume(final ServerVolume volume) async {
final ServerHostingDetails server = getIt<ApiConfigModel>().serverDetails!;
HetznerApi().attachVolume(volume.id, server.id);
refresh();
}
void detachVolume(final ServerVolume volume) async {
HetznerApi().detachVolume(volume.id);
refresh();
}
void resizeVolume(final ServerVolume volume, final int newSizeGb) {
if (volume.sizeGb < newSizeGb) {
HetznerApi().resizeVolume(volume.id, newSizeGb);
refresh();
}
}
void createVolume() async {
HetznerApi().createVolume();
refresh();
}
void deleteVolume(final ServerVolume volume) async {
HetznerApi().deleteVolume(volume.id);
refresh();
}
@override
void clear() {
emit(const ApiVolumesState.initial());
}
}

View File

@ -0,0 +1,23 @@
part of 'volumes_cubit.dart';
class ApiVolumesState extends ServerInstallationDependendState {
const ApiVolumesState(this._volumes, this.status);
const ApiVolumesState.initial() : this(const [], LoadingStatus.uninitialized);
final List<ServerVolume> _volumes;
final LoadingStatus status;
List<ServerVolume> get volumes => _volumes;
ApiVolumesState copyWith({
final List<ServerVolume>? volumes,
final LoadingStatus? status,
}) =>
ApiVolumesState(
volumes ?? _volumes,
status ?? this.status,
);
@override
List<Object?> get props => [_volumes];
}

View File

@ -55,12 +55,18 @@ class ServerVolume {
ServerVolume({
required this.id,
required this.name,
required this.sizeGb,
required this.serverId,
});
@HiveField(1)
int id;
@HiveField(2)
String name;
@HiveField(3, defaultValue: 10)
int sizeGb;
@HiveField(4, defaultValue: null)
int? serverId;
}
@HiveType(typeId: 101)