mirror of
https://github.com/psviderski/uncloud.git
synced 2026-08-26 11:03:34 +00:00
chore: update ListVolumes to accept a filter
This commit is contained in:
+8
-4
@@ -48,20 +48,24 @@ type ServiceClient interface {
|
||||
|
||||
// ProxyMachinesContext returns a new context that proxies gRPC requests to the specified machines.
|
||||
// If namesOrIDs is nil, all machines are included.
|
||||
func ProxyMachinesContext(ctx context.Context, cli MachineClient, namesOrIDs []string) (context.Context, error) {
|
||||
func ProxyMachinesContext(
|
||||
ctx context.Context, cli MachineClient, namesOrIDs []string,
|
||||
) (context.Context, MachineMembersList, error) {
|
||||
machines, err := cli.ListMachines(ctx)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("list machines: %w", err)
|
||||
return nil, nil, fmt.Errorf("list machines: %w", err)
|
||||
}
|
||||
|
||||
var proxiedMachines MachineMembersList
|
||||
md := metadata.New(nil)
|
||||
for _, m := range machines {
|
||||
if namesOrIDs == nil ||
|
||||
if len(namesOrIDs) == 0 ||
|
||||
slices.Contains(namesOrIDs, m.Machine.Name) || slices.Contains(namesOrIDs, m.Machine.Id) {
|
||||
proxiedMachines = append(proxiedMachines, m)
|
||||
machineIP, _ := m.Machine.Network.ManagementIp.ToAddr()
|
||||
md.Append("machines", machineIP.String())
|
||||
}
|
||||
}
|
||||
|
||||
return metadata.NewOutgoingContext(ctx, md), nil
|
||||
return metadata.NewOutgoingContext(ctx, md), proxiedMachines, nil
|
||||
}
|
||||
|
||||
@@ -18,3 +18,13 @@ func (m MachineMembersList) FindByManagementIP(ip string) *pb.MachineMember {
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (m MachineMembersList) FindByNameOrID(nameOrID string) *pb.MachineMember {
|
||||
for _, machine := range m {
|
||||
if machine.Machine.Id == nameOrID || machine.Machine.Name == nameOrID {
|
||||
return machine
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -3,6 +3,7 @@ package api
|
||||
import (
|
||||
"fmt"
|
||||
"reflect"
|
||||
"slices"
|
||||
"sort"
|
||||
"strings"
|
||||
|
||||
@@ -195,3 +196,47 @@ type MachineVolume struct {
|
||||
// Volume is the Docker volume model.
|
||||
Volume volume.Volume
|
||||
}
|
||||
|
||||
// VolumeFilter defines criteria to filter volumes in ListVolumes.
|
||||
type VolumeFilter struct {
|
||||
// Driver filters volumes by storage driver name.
|
||||
Driver string
|
||||
// Labels filters volumes by label key-value pairs. Volumes must match all labels.
|
||||
Labels map[string]string
|
||||
// MachineIDs filters volumes to those on the specified machines (names or IDs).
|
||||
Machines []string
|
||||
// Names filters volumes by name. Volumes must match one of the names.
|
||||
Names []string
|
||||
}
|
||||
|
||||
// MatchesFilter checks if a volume matches the specified filter criteria.
|
||||
func (v *MachineVolume) MatchesFilter(filter *VolumeFilter) bool {
|
||||
if filter == nil {
|
||||
return true
|
||||
}
|
||||
// Filter by name.
|
||||
if len(filter.Names) > 0 && !slices.Contains(filter.Names, v.Volume.Name) {
|
||||
return false
|
||||
}
|
||||
// Filter by driver.
|
||||
if filter.Driver != "" && v.Volume.Driver != filter.Driver {
|
||||
return false
|
||||
}
|
||||
// Filter by labels.
|
||||
for key, value := range filter.Labels {
|
||||
labelValue, exists := v.Volume.Labels[key]
|
||||
if !exists || labelValue != value {
|
||||
return false
|
||||
}
|
||||
}
|
||||
// Filter by machines.
|
||||
if len(filter.Machines) > 0 {
|
||||
if !slices.ContainsFunc(filter.Machines, func(nameOrID string) bool {
|
||||
return v.MachineName == nameOrID || v.MachineID == nameOrID
|
||||
}) {
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user