feat: share proxmox cluster client with worker

This commit is contained in:
Philipp
2026-06-11 10:24:36 +02:00
parent 7c818cf92e
commit bf31a8db37
16 changed files with 240 additions and 14 deletions
+69
View File
@@ -0,0 +1,69 @@
package tasks
import (
"context"
"forgejo.digital-droplets.de/philschlo/proxui/platform/cluster"
"forgejo.digital-droplets.de/philschlo/proxui/platform/proxmox"
)
type ClusterRepository interface {
GetCluster(ctx context.Context, id string) (cluster.Cluster, bool, error)
}
type ProxmoxTaskClientFactory func(cluster cluster.Cluster) (PlatformProxmoxTaskClient, error)
type PlatformProxmoxTaskClient interface {
GetTaskStatus(ctx context.Context, node string, upid string) (proxmox.TaskStatus, error)
}
type ClusterTaskClientResolver struct {
clusters ClusterRepository
factory ProxmoxTaskClientFactory
}
func NewClusterTaskClientResolver(clusters ClusterRepository, factory ProxmoxTaskClientFactory) ClusterTaskClientResolver {
return ClusterTaskClientResolver{
clusters: clusters,
factory: factory,
}
}
func NewDefaultClusterTaskClientResolver(clusters ClusterRepository) ClusterTaskClientResolver {
return NewClusterTaskClientResolver(clusters, func(cluster cluster.Cluster) (PlatformProxmoxTaskClient, error) {
return proxmox.NewClient(cluster)
})
}
func (r ClusterTaskClientResolver) ResolveTaskClient(ctx context.Context, clusterID string) (ProxmoxTaskClient, 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 platformTaskClientAdapter{client: client}, nil
}
type platformTaskClientAdapter struct {
client PlatformProxmoxTaskClient
}
func (a platformTaskClientAdapter) GetTaskStatus(ctx context.Context, node string, upid string) (ProxmoxTaskStatus, error) {
status, err := a.client.GetTaskStatus(ctx, node, upid)
if err != nil {
return ProxmoxTaskStatus{}, err
}
return ProxmoxTaskStatus{
Status: status.Status,
ExitStatus: status.ExitStatus,
}, nil
}