mirror of
https://github.com/containers/podman.git
synced 2025-05-21 09:05:56 +08:00
APIv2 Add network list filtering
Add the filter option to the libpod endpoint. Add support for the name filter on the docker endpoint. Add apiv2 tests for the network list endpoints. Enable podman network integration tests for remote. Signed-off-by: Paul Holzinger <paul.holzinger@web.de>
This commit is contained in:
@ -5,6 +5,7 @@ import (
|
|||||||
"net"
|
"net"
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
|
"strings"
|
||||||
"syscall"
|
"syscall"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
@ -177,9 +178,11 @@ func ListNetworks(w http.ResponseWriter, r *http.Request) {
|
|||||||
utils.InternalServerError(w, err)
|
utils.InternalServerError(w, err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
filterNames, nameFilterExists := query.Filters["name"]
|
||||||
// TODO remove when filters are implemented
|
// TODO remove when filters are implemented
|
||||||
if len(query.Filters) > 0 {
|
if (!nameFilterExists && len(query.Filters) > 0) || len(query.Filters) > 1 {
|
||||||
utils.InternalServerError(w, errors.New("filters for listing networks is not implemented"))
|
utils.InternalServerError(w, errors.New("only the name filter for listing networks is implemented"))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
netNames, err := network.GetNetworkNamesFromFileSystem(config)
|
netNames, err := network.GetNetworkNamesFromFileSystem(config)
|
||||||
@ -187,6 +190,21 @@ func ListNetworks(w http.ResponseWriter, r *http.Request) {
|
|||||||
utils.InternalServerError(w, err)
|
utils.InternalServerError(w, err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// filter by name
|
||||||
|
if nameFilterExists {
|
||||||
|
names := []string{}
|
||||||
|
for _, name := range netNames {
|
||||||
|
for _, filter := range filterNames {
|
||||||
|
if strings.Contains(name, filter) {
|
||||||
|
names = append(names, name)
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
netNames = names
|
||||||
|
}
|
||||||
|
|
||||||
reports := make([]*types.NetworkResource, 0, len(netNames))
|
reports := make([]*types.NetworkResource, 0, len(netNames))
|
||||||
for _, name := range netNames {
|
for _, name := range netNames {
|
||||||
report, err := getNetworkResourceByName(name, runtime)
|
report, err := getNetworkResourceByName(name, runtime)
|
||||||
|
@ -42,7 +42,21 @@ func CreateNetwork(w http.ResponseWriter, r *http.Request) {
|
|||||||
}
|
}
|
||||||
func ListNetworks(w http.ResponseWriter, r *http.Request) {
|
func ListNetworks(w http.ResponseWriter, r *http.Request) {
|
||||||
runtime := r.Context().Value("runtime").(*libpod.Runtime)
|
runtime := r.Context().Value("runtime").(*libpod.Runtime)
|
||||||
options := entities.NetworkListOptions{}
|
decoder := r.Context().Value("decoder").(*schema.Decoder)
|
||||||
|
query := struct {
|
||||||
|
Filter string `schema:"filter"`
|
||||||
|
}{
|
||||||
|
// override any golang type defaults
|
||||||
|
}
|
||||||
|
if err := decoder.Decode(&query, r.URL.Query()); err != nil {
|
||||||
|
utils.Error(w, http.StatusText(http.StatusBadRequest), http.StatusBadRequest,
|
||||||
|
errors.Wrapf(err, "Failed to parse parameters for %s", r.URL.String()))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
options := entities.NetworkListOptions{
|
||||||
|
Filter: query.Filter,
|
||||||
|
}
|
||||||
ic := abi.ContainerEngine{Libpod: runtime}
|
ic := abi.ContainerEngine{Libpod: runtime}
|
||||||
reports, err := ic.NetworkList(r.Context(), options)
|
reports, err := ic.NetworkList(r.Context(), options)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -61,6 +61,11 @@ func (s *APIServer) registerNetworkHandlers(r *mux.Router) error {
|
|||||||
// - networks (compat)
|
// - networks (compat)
|
||||||
// summary: List networks
|
// summary: List networks
|
||||||
// description: Display summary of network configurations
|
// description: Display summary of network configurations
|
||||||
|
// parameters:
|
||||||
|
// - in: query
|
||||||
|
// name: filters
|
||||||
|
// type: string
|
||||||
|
// description: JSON encoded value of the filters (a map[string][]string) to process on the networks list. Only the name filter is supported.
|
||||||
// produces:
|
// produces:
|
||||||
// - application/json
|
// - application/json
|
||||||
// responses:
|
// responses:
|
||||||
@ -152,6 +157,11 @@ func (s *APIServer) registerNetworkHandlers(r *mux.Router) error {
|
|||||||
// - networks
|
// - networks
|
||||||
// summary: List networks
|
// summary: List networks
|
||||||
// description: Display summary of network configurations
|
// description: Display summary of network configurations
|
||||||
|
// parameters:
|
||||||
|
// - in: query
|
||||||
|
// name: filter
|
||||||
|
// type: string
|
||||||
|
// description: Provide filter values (e.g. 'name=podman')
|
||||||
// produces:
|
// produces:
|
||||||
// - application/json
|
// - application/json
|
||||||
// responses:
|
// responses:
|
||||||
|
@ -70,7 +70,7 @@ func Remove(ctx context.Context, nameOrID string, force *bool) ([]*entities.Netw
|
|||||||
}
|
}
|
||||||
|
|
||||||
// List returns a summary of all CNI network configurations
|
// List returns a summary of all CNI network configurations
|
||||||
func List(ctx context.Context) ([]*entities.NetworkListReport, error) {
|
func List(ctx context.Context, options entities.NetworkListOptions) ([]*entities.NetworkListReport, error) {
|
||||||
var (
|
var (
|
||||||
netList []*entities.NetworkListReport
|
netList []*entities.NetworkListReport
|
||||||
)
|
)
|
||||||
@ -78,7 +78,11 @@ func List(ctx context.Context) ([]*entities.NetworkListReport, error) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
response, err := conn.DoRequest(nil, http.MethodGet, "/networks/json", nil, nil)
|
params := url.Values{}
|
||||||
|
if options.Filter != "" {
|
||||||
|
params.Set("filter", options.Filter)
|
||||||
|
}
|
||||||
|
response, err := conn.DoRequest(nil, http.MethodGet, "/networks/json", params, nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return netList, err
|
return netList, err
|
||||||
}
|
}
|
||||||
|
@ -8,7 +8,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func (ic *ContainerEngine) NetworkList(ctx context.Context, options entities.NetworkListOptions) ([]*entities.NetworkListReport, error) {
|
func (ic *ContainerEngine) NetworkList(ctx context.Context, options entities.NetworkListOptions) ([]*entities.NetworkListReport, error) {
|
||||||
return network.List(ic.ClientCxt)
|
return network.List(ic.ClientCxt, options)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (ic *ContainerEngine) NetworkInspect(ctx context.Context, namesOrIds []string, options entities.NetworkInspectOptions) ([]entities.NetworkInspectReport, error) {
|
func (ic *ContainerEngine) NetworkInspect(ctx context.Context, namesOrIds []string, options entities.NetworkInspectOptions) ([]entities.NetworkInspectReport, error) {
|
||||||
|
@ -21,6 +21,27 @@ if root; then
|
|||||||
t POST libpod/networks/create '"Subnet":{"IP":"10.10.1.0","Mask":[0,255,255,0]}' 500 \
|
t POST libpod/networks/create '"Subnet":{"IP":"10.10.1.0","Mask":[0,255,255,0]}' 500 \
|
||||||
.cause~'.*mask is invalid'
|
.cause~'.*mask is invalid'
|
||||||
|
|
||||||
|
# network list
|
||||||
|
t GET libpod/networks/json 200
|
||||||
|
t GET libpod/networks/json?filter=name=network1 200 \
|
||||||
|
length=1 \
|
||||||
|
.[0].Name=network1
|
||||||
|
t GET networks 200
|
||||||
|
|
||||||
|
#network list docker endpoint
|
||||||
|
#filters={"name":["network1","network2"]}
|
||||||
|
t GET networks?filters=%7B%22name%22%3A%5B%22network1%22%2C%22network2%22%5D%7D 200 \
|
||||||
|
length=2
|
||||||
|
#filters={"name":["network"]}
|
||||||
|
t GET networks?filters=%7B%22name%22%3A%5B%22network%22%5D%7D 200 \
|
||||||
|
length=2
|
||||||
|
# invalid filter filters={"label":"abc"}
|
||||||
|
t GET networks?filters=%7B%22label%22%3A%5B%22abc%22%5D%7D 500 \
|
||||||
|
.cause="only the name filter for listing networks is implemented"
|
||||||
|
# invalid filter filters={"label":"abc","name":["network"]}
|
||||||
|
t GET networks?filters=%7B%22label%22%3A%22abc%22%2C%22name%22%3A%5B%22network%22%5D%7D 500 \
|
||||||
|
.cause="only the name filter for listing networks is implemented"
|
||||||
|
|
||||||
# clean the network
|
# clean the network
|
||||||
t DELETE libpod/networks/network1 200 \
|
t DELETE libpod/networks/network1 200 \
|
||||||
.[0].Name~network1 \
|
.[0].Name~network1 \
|
||||||
|
@ -1,5 +1,3 @@
|
|||||||
// +build !remote
|
|
||||||
|
|
||||||
package integration
|
package integration
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
Reference in New Issue
Block a user