From 0dcf7f875f11f03f1b4b32234fb2510d0a3cde4f Mon Sep 17 00:00:00 2001 From: Abdulrahman Alshahrani Date: Thu, 24 Apr 2025 19:27:46 -0500 Subject: [PATCH] client: Add server-side filtering for networks Signed-off-by: Abdulrahman Alshahrani --- client/incus_networks.go | 42 ++++++++++++++++++++++++++++++++++++++++ client/interfaces.go | 2 ++ 2 files changed, 44 insertions(+) diff --git a/client/incus_networks.go b/client/incus_networks.go index ed4a6e465..af75b3aae 100644 --- a/client/incus_networks.go +++ b/client/incus_networks.go @@ -42,6 +42,27 @@ func (r *ProtocolIncus) GetNetworks() ([]api.Network, error) { return networks, nil } +// GetNetworksWithFilter returns a list of filtered Network struct. +func (r *ProtocolIncus) GetNetworksWithFilter(filters []string) ([]api.Network, error) { + if !r.HasExtension("network") { + return nil, fmt.Errorf("The server is missing the required \"network\" API extension") + } + + networks := []api.Network{} + + v := url.Values{} + v.Set("recursion", "1") + v.Set("filter", parseFilters(filters)) + + // Fetch the raw value + _, err := r.queryStruct("GET", fmt.Sprintf("/networks?%s", v.Encode()), nil, "", &networks) + if err != nil { + return nil, err + } + + return networks, nil +} + // GetNetworksAllProjects gets all networks across all projects. func (r *ProtocolIncus) GetNetworksAllProjects() ([]api.Network, error) { if !r.HasExtension("networks_all_projects") { @@ -57,6 +78,27 @@ func (r *ProtocolIncus) GetNetworksAllProjects() ([]api.Network, error) { return networks, nil } +// GetNetworksAllProjectsWithFilter gets a filtered list of all networks across all projects. +func (r *ProtocolIncus) GetNetworksAllProjectsWithFilter(filters []string) ([]api.Network, error) { + if !r.HasExtension("networks_all_projects") { + return nil, fmt.Errorf(`The server is missing the required "networks_all_projects" API extension`) + } + + networks := []api.Network{} + + v := url.Values{} + v.Set("recursion", "1") + v.Set("all-projects", "true") + v.Set("filter", parseFilters(filters)) + + _, err := r.queryStruct("GET", fmt.Sprintf("/networks?%s", v.Encode()), nil, "", &networks) + if err != nil { + return nil, err + } + + return networks, nil +} + // GetNetwork returns a Network entry for the provided name. func (r *ProtocolIncus) GetNetwork(name string) (*api.Network, string, error) { if !r.HasExtension("network") { diff --git a/client/interfaces.go b/client/interfaces.go index d654d8120..a340fbbb8 100644 --- a/client/interfaces.go +++ b/client/interfaces.go @@ -194,7 +194,9 @@ type InstanceServer interface { // Network functions ("network" API extension) GetNetworkNames() (names []string, err error) GetNetworks() (networks []api.Network, err error) + GetNetworksWithFilter(filters []string) (networks []api.Network, err error) GetNetworksAllProjects() (networks []api.Network, err error) + GetNetworksAllProjectsWithFilter(filters []string) (networks []api.Network, err error) GetNetwork(name string) (network *api.Network, ETag string, err error) GetNetworkLeases(name string) (leases []api.NetworkLease, err error) GetNetworkState(name string) (state *api.NetworkState, err error)