feat: share proxmox cluster client with worker
This commit is contained in:
@@ -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
|
||||
}
|
||||
Reference in New Issue
Block a user