63 lines
1.8 KiB
Go
63 lines
1.8 KiB
Go
package tasks
|
|
|
|
import (
|
|
"context"
|
|
|
|
"forgejo.digital-droplets.de/philschlo/proxui/platform/cluster"
|
|
"forgejo.digital-droplets.de/philschlo/proxui/platform/proxmox"
|
|
)
|
|
|
|
type ProxmoxReconcileClientFactory func(cluster cluster.Cluster) (PlatformProxmoxReconcileClient, error)
|
|
|
|
type PlatformProxmoxReconcileClient interface {
|
|
GetVMStatus(ctx context.Context, node string, vmid int) (proxmox.VMStatus, error)
|
|
}
|
|
|
|
type ClusterReconcileClientResolver struct {
|
|
clusters ClusterRepository
|
|
factory ProxmoxReconcileClientFactory
|
|
}
|
|
|
|
func NewClusterReconcileClientResolver(clusters ClusterRepository, factory ProxmoxReconcileClientFactory) ClusterReconcileClientResolver {
|
|
return ClusterReconcileClientResolver{
|
|
clusters: clusters,
|
|
factory: factory,
|
|
}
|
|
}
|
|
|
|
func NewDefaultClusterReconcileClientResolver(clusters ClusterRepository) ClusterReconcileClientResolver {
|
|
return NewClusterReconcileClientResolver(clusters, func(cluster cluster.Cluster) (PlatformProxmoxReconcileClient, error) {
|
|
return proxmox.NewClient(cluster)
|
|
})
|
|
}
|
|
|
|
func (r ClusterReconcileClientResolver) ResolveReconcileClient(ctx context.Context, clusterID string) (ProxmoxReconcileClient, error) {
|
|
cluster, found, err := r.clusters.GetCluster(ctx, clusterID)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if !found {
|
|
return nil, ErrClusterNotFound
|
|
}
|
|
|
|
client, err := r.factory(cluster)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return platformReconcileClientAdapter{client: client}, nil
|
|
}
|
|
|
|
type platformReconcileClientAdapter struct {
|
|
client PlatformProxmoxReconcileClient
|
|
}
|
|
|
|
func (a platformReconcileClientAdapter) GetVMStatus(ctx context.Context, node string, vmid int) (ProxmoxVMStatus, error) {
|
|
status, err := a.client.GetVMStatus(ctx, node, vmid)
|
|
if err != nil {
|
|
return ProxmoxVMStatus{}, err
|
|
}
|
|
|
|
return ProxmoxVMStatus{Status: status.Status}, nil
|
|
}
|