github.com/containers/libpod@v1.9.4-0.20220419124438-4284fd425507/pkg/api/handlers/libpod/networks.go (about)

     1  package libpod
     2  
     3  import (
     4  	"net/http"
     5  
     6  	"github.com/containers/libpod/libpod"
     7  	"github.com/containers/libpod/pkg/api/handlers/utils"
     8  	"github.com/containers/libpod/pkg/network"
     9  	"github.com/gorilla/schema"
    10  	"github.com/pkg/errors"
    11  )
    12  
    13  func CreateNetwork(w http.ResponseWriter, r *http.Request) {}
    14  func ListNetworks(w http.ResponseWriter, r *http.Request) {
    15  	runtime := r.Context().Value("runtime").(*libpod.Runtime)
    16  	config, err := runtime.GetConfig()
    17  	if err != nil {
    18  		utils.InternalServerError(w, err)
    19  		return
    20  	}
    21  	configDir := config.Network.NetworkConfigDir
    22  	if len(configDir) < 1 {
    23  		configDir = network.CNIConfigDir
    24  	}
    25  	networks, err := network.LoadCNIConfsFromDir(configDir)
    26  	if err != nil {
    27  		utils.InternalServerError(w, err)
    28  		return
    29  	}
    30  	utils.WriteResponse(w, http.StatusOK, networks)
    31  }
    32  
    33  func RemoveNetwork(w http.ResponseWriter, r *http.Request) {
    34  	// 200 ok
    35  	// 404 no such
    36  	// 500 internal
    37  	decoder := r.Context().Value("decoder").(*schema.Decoder)
    38  	query := struct {
    39  		Force bool `schema:"force"`
    40  	}{
    41  		// override any golang type defaults
    42  	}
    43  	if err := decoder.Decode(&query, r.URL.Query()); err != nil {
    44  		utils.Error(w, http.StatusText(http.StatusBadRequest), http.StatusBadRequest,
    45  			errors.Wrapf(err, "Failed to parse parameters for %s", r.URL.String()))
    46  		return
    47  	}
    48  	name := utils.GetName(r)
    49  	if err := network.RemoveNetwork(name); err != nil {
    50  		// If the network cannot be found, we return a 404.
    51  		if errors.Cause(err) == network.ErrNetworkNotFound {
    52  			utils.Error(w, "Something went wrong", http.StatusNotFound, err)
    53  			return
    54  		}
    55  		utils.InternalServerError(w, err)
    56  		return
    57  	}
    58  	utils.WriteResponse(w, http.StatusOK, "")
    59  }
    60  
    61  func InspectNetwork(w http.ResponseWriter, r *http.Request) {
    62  	decoder := r.Context().Value("decoder").(*schema.Decoder)
    63  	query := struct {
    64  		Force bool `schema:"force"`
    65  	}{
    66  		// override any golang type defaults
    67  	}
    68  	if err := decoder.Decode(&query, r.URL.Query()); err != nil {
    69  		utils.Error(w, http.StatusText(http.StatusBadRequest), http.StatusBadRequest,
    70  			errors.Wrapf(err, "Failed to parse parameters for %s", r.URL.String()))
    71  		return
    72  	}
    73  	name := utils.GetName(r)
    74  	n, err := network.InspectNetwork(name)
    75  	if err != nil {
    76  		// If the network cannot be found, we return a 404.
    77  		if errors.Cause(err) == network.ErrNetworkNotFound {
    78  			utils.Error(w, "Something went wrong", http.StatusNotFound, err)
    79  			return
    80  		}
    81  		utils.InternalServerError(w, err)
    82  		return
    83  	}
    84  	utils.WriteResponse(w, http.StatusOK, n)
    85  }