summaryrefslogtreecommitdiff
path: root/pkg
diff options
context:
space:
mode:
authorOpenShift Merge Robot <openshift-merge-robot@users.noreply.github.com>2020-09-10 10:15:01 -0400
committerGitHub <noreply@github.com>2020-09-10 10:15:01 -0400
commit3d33923374ee39977166d9fff3ee53c58d884bbe (patch)
tree327b33269b94c9d1faec38eaedf5b78a12a26214 /pkg
parente1b47296daaf4148d9970b59757a86e71bf8bb10 (diff)
parentce7d2bba1f6fdc96ef7439135b3c1388b7bac297 (diff)
downloadpodman-3d33923374ee39977166d9fff3ee53c58d884bbe.tar.gz
podman-3d33923374ee39977166d9fff3ee53c58d884bbe.tar.bz2
podman-3d33923374ee39977166d9fff3ee53c58d884bbe.zip
Merge pull request #7536 from Luap99/api-network-filter
APIv2 Add network list filtering
Diffstat (limited to 'pkg')
-rw-r--r--pkg/api/handlers/compat/networks.go22
-rw-r--r--pkg/api/handlers/libpod/networks.go16
-rw-r--r--pkg/api/server/register_networks.go12
-rw-r--r--pkg/bindings/network/network.go8
-rw-r--r--pkg/domain/infra/tunnel/network.go2
5 files changed, 53 insertions, 7 deletions
diff --git a/pkg/api/handlers/compat/networks.go b/pkg/api/handlers/compat/networks.go
index 80b7505df..87b947549 100644
--- a/pkg/api/handlers/compat/networks.go
+++ b/pkg/api/handlers/compat/networks.go
@@ -5,6 +5,7 @@ import (
"net"
"net/http"
"os"
+ "strings"
"syscall"
"time"
@@ -177,9 +178,11 @@ func ListNetworks(w http.ResponseWriter, r *http.Request) {
utils.InternalServerError(w, err)
return
}
+
+ filterNames, nameFilterExists := query.Filters["name"]
// TODO remove when filters are implemented
- if len(query.Filters) > 0 {
- utils.InternalServerError(w, errors.New("filters for listing networks is not implemented"))
+ if (!nameFilterExists && len(query.Filters) > 0) || len(query.Filters) > 1 {
+ utils.InternalServerError(w, errors.New("only the name filter for listing networks is implemented"))
return
}
netNames, err := network.GetNetworkNamesFromFileSystem(config)
@@ -187,6 +190,21 @@ func ListNetworks(w http.ResponseWriter, r *http.Request) {
utils.InternalServerError(w, err)
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))
for _, name := range netNames {
report, err := getNetworkResourceByName(name, runtime)
diff --git a/pkg/api/handlers/libpod/networks.go b/pkg/api/handlers/libpod/networks.go
index 475522664..dfece2a4e 100644
--- a/pkg/api/handlers/libpod/networks.go
+++ b/pkg/api/handlers/libpod/networks.go
@@ -42,7 +42,21 @@ func CreateNetwork(w http.ResponseWriter, r *http.Request) {
}
func ListNetworks(w http.ResponseWriter, r *http.Request) {
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}
reports, err := ic.NetworkList(r.Context(), options)
if err != nil {
diff --git a/pkg/api/server/register_networks.go b/pkg/api/server/register_networks.go
index 7918ad4a2..61916eedf 100644
--- a/pkg/api/server/register_networks.go
+++ b/pkg/api/server/register_networks.go
@@ -61,6 +61,11 @@ func (s *APIServer) registerNetworkHandlers(r *mux.Router) error {
// - networks (compat)
// summary: List networks
// 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:
// - application/json
// responses:
@@ -106,7 +111,7 @@ func (s *APIServer) registerNetworkHandlers(r *mux.Router) error {
// required: true
// description: the name of the network
// - in: query
- // name: Force
+ // name: force
// type: boolean
// description: remove containers associated with network
// produces:
@@ -152,6 +157,11 @@ func (s *APIServer) registerNetworkHandlers(r *mux.Router) error {
// - networks
// summary: List networks
// description: Display summary of network configurations
+ // parameters:
+ // - in: query
+ // name: filter
+ // type: string
+ // description: Provide filter values (e.g. 'name=podman')
// produces:
// - application/json
// responses:
diff --git a/pkg/bindings/network/network.go b/pkg/bindings/network/network.go
index fd1111282..d8dc7e352 100644
--- a/pkg/bindings/network/network.go
+++ b/pkg/bindings/network/network.go
@@ -70,7 +70,7 @@ func Remove(ctx context.Context, nameOrID string, force *bool) ([]*entities.Netw
}
// 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 (
netList []*entities.NetworkListReport
)
@@ -78,7 +78,11 @@ func List(ctx context.Context) ([]*entities.NetworkListReport, error) {
if err != nil {
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 {
return netList, err
}
diff --git a/pkg/domain/infra/tunnel/network.go b/pkg/domain/infra/tunnel/network.go
index 2b197cac0..074425087 100644
--- a/pkg/domain/infra/tunnel/network.go
+++ b/pkg/domain/infra/tunnel/network.go
@@ -8,7 +8,7 @@ import (
)
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) {