1
0
mirror of https://github.com/lxc/incus.git synced 2026-02-05 09:46:19 +01:00

client: Add server-side filtering for networks

Signed-off-by: Abdulrahman Alshahrani <abdulrahmanalshahrani@utexas.edu>
This commit is contained in:
Abdulrahman Alshahrani
2025-04-24 19:27:46 -05:00
parent 11f8f9e6ae
commit 0dcf7f875f
2 changed files with 44 additions and 0 deletions

View File

@@ -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") {

View File

@@ -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)