softplayer-web/lib/api/grpc/environments.dart

107 lines
3.3 KiB
Dart
Raw Permalink Normal View History

2024-04-19 11:19:44 +00:00
import 'package:softplayer_dart_proto/main.dart';
2024-04-29 10:42:16 +00:00
import 'package:softplayer_web/api/grpc/creds.dart';
2024-04-19 11:19:44 +00:00
class EnvironmentLocalData {
2024-04-30 15:31:53 +00:00
EnvironmentLocalData(
{required this.serverType,
required this.serverLocation,
required this.provider,
required this.name,
2024-05-02 17:09:44 +00:00
required this.description,
this.uuid});
2024-04-29 10:42:16 +00:00
2024-04-30 15:31:53 +00:00
final String name;
final String description;
final String provider;
final String serverType;
final String serverLocation;
2024-05-02 17:09:44 +00:00
String? uuid;
2024-04-19 11:19:44 +00:00
}
class EnvironmentsGrpc {
2024-04-29 10:42:16 +00:00
// final GrpcWebClientChannel channel;
final EnvironmentsClient envStub;
// Init the grpc channel for environments
EnvironmentsGrpc(channel) : envStub = EnvironmentsClient(channel);
// Get environments from the API
2024-05-02 17:09:44 +00:00
Future<EnvironmentLocalData> get(String uuid, SoftplayerCreds creds) async {
2024-04-29 10:42:16 +00:00
final request = GetOptions(
2024-05-02 17:09:44 +00:00
id: EnvironmentId(uuid: uuid),
2024-04-29 10:42:16 +00:00
ownerId: OwnerId(uuid: creds.uuid),
token: Token(token: creds.token),
);
2024-04-19 11:19:44 +00:00
2024-04-29 10:42:16 +00:00
try {
final response = await envStub.get(request);
2024-04-30 15:31:53 +00:00
return EnvironmentLocalData(
2024-05-02 17:09:44 +00:00
uuid: uuid,
2024-04-30 15:31:53 +00:00
serverType: response.spec.serverType.toString(),
serverLocation: response.spec.serverLocation.toString(),
provider: response.spec.provider.toString(),
name: response.metadata.name,
description: response.metadata.description);
2024-04-29 10:42:16 +00:00
} catch (e) {
rethrow;
}
2024-04-19 11:19:44 +00:00
}
2024-04-30 15:31:53 +00:00
Future<EnvironmentLocalData> create(
EnvironmentLocalData data, SoftplayerCreds creds) async {
2024-05-02 17:09:44 +00:00
print(data);
2024-04-30 15:31:53 +00:00
final request = CreateOptions(
metadata:
EnvironmentMetadata(description: data.description, name: data.name),
spec: EnvironmentSpec(
// Currently we do not support other kinds
kubernetes: Kubernetes.KUBERNETES_K3S,
// Currently we do not support other providers
provider: Provider.PROVIDER_HETZNER,
serverLocation: Location.values
.firstWhere((e) => e.toString() == data.serverLocation),
serverType: ServerType.values
.firstWhere((e) => e.toString() == data.serverType),
),
ownerId: OwnerId(uuid: creds.uuid),
token: Token(token: creds.token),
);
try {
final response = await envStub.create(request);
return EnvironmentLocalData(
2024-05-02 17:09:44 +00:00
uuid: response.id.uuid,
2024-04-30 15:31:53 +00:00
serverType: response.spec.serverType.toString(),
serverLocation: response.spec.serverLocation.toString(),
provider: response.spec.provider.toString(),
name: response.metadata.name,
description: response.metadata.description);
} catch (e) {
rethrow;
}
}
Stream<List<EnvironmentLocalData>> list(SoftplayerCreds creds) async* {
List<EnvironmentLocalData> envs = [];
2024-04-19 11:19:44 +00:00
try {
2024-04-29 10:42:16 +00:00
await for (var feature in envStub.list(
ListOptions(
ownerId: OwnerId(uuid: creds.uuid),
token: Token(token: creds.token),
),
)) {
2024-04-30 15:31:53 +00:00
envs.add(EnvironmentLocalData(
2024-05-02 17:09:44 +00:00
uuid: feature.id.uuid,
2024-04-30 15:31:53 +00:00
serverType: feature.spec.serverType.toString(),
serverLocation: feature.spec.serverLocation.toString(),
provider: feature.spec.provider.toString(),
name: feature.metadata.name,
description: feature.metadata.description,
2024-04-29 10:42:16 +00:00
));
2024-04-19 11:19:44 +00:00
}
} catch (e) {
rethrow;
}
yield envs;
}
}