mirror of
https://github.com/containers/podman.git
synced 2025-06-23 18:59:30 +08:00
Merge pull request #13189 from jwhonce/wip/network_version
Add version guard to libpod API endpoints
This commit is contained in:
@ -17,22 +17,37 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func CreateNetwork(w http.ResponseWriter, r *http.Request) {
|
func CreateNetwork(w http.ResponseWriter, r *http.Request) {
|
||||||
|
if v, err := utils.SupportedVersion(r, ">=4.0.0"); err != nil {
|
||||||
|
utils.BadRequest(w, "version", v.String(), err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
runtime := r.Context().Value(api.RuntimeKey).(*libpod.Runtime)
|
runtime := r.Context().Value(api.RuntimeKey).(*libpod.Runtime)
|
||||||
network := types.Network{}
|
network := types.Network{}
|
||||||
if err := json.NewDecoder(r.Body).Decode(&network); err != nil {
|
if err := json.NewDecoder(r.Body).Decode(&network); err != nil {
|
||||||
utils.Error(w, http.StatusInternalServerError, errors.Wrap(err, "decode body"))
|
utils.Error(w, http.StatusInternalServerError, errors.Wrap(err, "failed to decode request JSON payload"))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
ic := abi.ContainerEngine{Libpod: runtime}
|
ic := abi.ContainerEngine{Libpod: runtime}
|
||||||
report, err := ic.NetworkCreate(r.Context(), network)
|
report, err := ic.NetworkCreate(r.Context(), network)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
utils.InternalServerError(w, err)
|
if errors.Is(err, types.ErrNetworkExists) {
|
||||||
|
utils.Error(w, http.StatusConflict, err)
|
||||||
|
} else {
|
||||||
|
utils.InternalServerError(w, err)
|
||||||
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
utils.WriteResponse(w, http.StatusOK, report)
|
utils.WriteResponse(w, http.StatusOK, report)
|
||||||
}
|
}
|
||||||
|
|
||||||
func ListNetworks(w http.ResponseWriter, r *http.Request) {
|
func ListNetworks(w http.ResponseWriter, r *http.Request) {
|
||||||
|
if v, err := utils.SupportedVersion(r, ">=4.0.0"); err != nil {
|
||||||
|
utils.BadRequest(w, "version", v.String(), err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
runtime := r.Context().Value(api.RuntimeKey).(*libpod.Runtime)
|
runtime := r.Context().Value(api.RuntimeKey).(*libpod.Runtime)
|
||||||
filterMap, err := util.PrepareFilters(r)
|
filterMap, err := util.PrepareFilters(r)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -54,6 +69,11 @@ func ListNetworks(w http.ResponseWriter, r *http.Request) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func RemoveNetwork(w http.ResponseWriter, r *http.Request) {
|
func RemoveNetwork(w http.ResponseWriter, r *http.Request) {
|
||||||
|
if v, err := utils.SupportedVersion(r, ">=4.0.0"); err != nil {
|
||||||
|
utils.BadRequest(w, "version", v.String(), err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
runtime := r.Context().Value(api.RuntimeKey).(*libpod.Runtime)
|
runtime := r.Context().Value(api.RuntimeKey).(*libpod.Runtime)
|
||||||
decoder := r.Context().Value(api.DecoderKey).(*schema.Decoder)
|
decoder := r.Context().Value(api.DecoderKey).(*schema.Decoder)
|
||||||
query := struct {
|
query := struct {
|
||||||
@ -87,21 +107,18 @@ func RemoveNetwork(w http.ResponseWriter, r *http.Request) {
|
|||||||
utils.WriteResponse(w, http.StatusOK, reports)
|
utils.WriteResponse(w, http.StatusOK, reports)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// InspectNetwork reports on given network's details
|
||||||
func InspectNetwork(w http.ResponseWriter, r *http.Request) {
|
func InspectNetwork(w http.ResponseWriter, r *http.Request) {
|
||||||
runtime := r.Context().Value(api.RuntimeKey).(*libpod.Runtime)
|
if v, err := utils.SupportedVersion(r, ">=4.0.0"); err != nil {
|
||||||
decoder := r.Context().Value(api.DecoderKey).(*schema.Decoder)
|
utils.BadRequest(w, "version", v.String(), err)
|
||||||
query := struct {
|
|
||||||
}{
|
|
||||||
// override any golang type defaults
|
|
||||||
}
|
|
||||||
if err := decoder.Decode(&query, r.URL.Query()); err != nil {
|
|
||||||
utils.Error(w, http.StatusInternalServerError,
|
|
||||||
errors.Wrapf(err, "failed to parse parameters for %s", r.URL.String()))
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
runtime := r.Context().Value(api.RuntimeKey).(*libpod.Runtime)
|
||||||
|
ic := abi.ContainerEngine{Libpod: runtime}
|
||||||
|
|
||||||
name := utils.GetName(r)
|
name := utils.GetName(r)
|
||||||
options := entities.InspectOptions{}
|
options := entities.InspectOptions{}
|
||||||
ic := abi.ContainerEngine{Libpod: runtime}
|
|
||||||
reports, errs, err := ic.NetworkInspect(r.Context(), []string{name}, options)
|
reports, errs, err := ic.NetworkInspect(r.Context(), []string{name}, options)
|
||||||
// If the network cannot be found, we return a 404.
|
// If the network cannot be found, we return a 404.
|
||||||
if len(errs) > 0 {
|
if len(errs) > 0 {
|
||||||
@ -117,14 +134,19 @@ func InspectNetwork(w http.ResponseWriter, r *http.Request) {
|
|||||||
|
|
||||||
// Connect adds a container to a network
|
// Connect adds a container to a network
|
||||||
func Connect(w http.ResponseWriter, r *http.Request) {
|
func Connect(w http.ResponseWriter, r *http.Request) {
|
||||||
runtime := r.Context().Value(api.RuntimeKey).(*libpod.Runtime)
|
if v, err := utils.SupportedVersion(r, ">=4.0.0"); err != nil {
|
||||||
|
utils.BadRequest(w, "version", v.String(), err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
runtime := r.Context().Value(api.RuntimeKey).(*libpod.Runtime)
|
||||||
var netConnect entities.NetworkConnectOptions
|
var netConnect entities.NetworkConnectOptions
|
||||||
if err := json.NewDecoder(r.Body).Decode(&netConnect); err != nil {
|
if err := json.NewDecoder(r.Body).Decode(&netConnect); err != nil {
|
||||||
utils.Error(w, http.StatusInternalServerError, errors.Wrap(err, "Decode()"))
|
utils.Error(w, http.StatusInternalServerError, errors.Wrap(err, "failed to decode request JSON payload"))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
name := utils.GetName(r)
|
name := utils.GetName(r)
|
||||||
|
|
||||||
err := runtime.ConnectContainerToNetwork(netConnect.Container, name, netConnect.PerNetworkOptions)
|
err := runtime.ConnectContainerToNetwork(netConnect.Container, name, netConnect.PerNetworkOptions)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if errors.Cause(err) == define.ErrNoSuchCtr {
|
if errors.Cause(err) == define.ErrNoSuchCtr {
|
||||||
@ -143,10 +165,15 @@ func Connect(w http.ResponseWriter, r *http.Request) {
|
|||||||
|
|
||||||
// ExistsNetwork check if a network exists
|
// ExistsNetwork check if a network exists
|
||||||
func ExistsNetwork(w http.ResponseWriter, r *http.Request) {
|
func ExistsNetwork(w http.ResponseWriter, r *http.Request) {
|
||||||
runtime := r.Context().Value(api.RuntimeKey).(*libpod.Runtime)
|
if v, err := utils.SupportedVersion(r, ">=4.0.0"); err != nil {
|
||||||
name := utils.GetName(r)
|
utils.BadRequest(w, "version", v.String(), err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
runtime := r.Context().Value(api.RuntimeKey).(*libpod.Runtime)
|
||||||
ic := abi.ContainerEngine{Libpod: runtime}
|
ic := abi.ContainerEngine{Libpod: runtime}
|
||||||
|
|
||||||
|
name := utils.GetName(r)
|
||||||
report, err := ic.NetworkExists(r.Context(), name)
|
report, err := ic.NetworkExists(r.Context(), name)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
utils.Error(w, http.StatusInternalServerError, err)
|
utils.Error(w, http.StatusInternalServerError, err)
|
||||||
@ -161,7 +188,13 @@ func ExistsNetwork(w http.ResponseWriter, r *http.Request) {
|
|||||||
|
|
||||||
// Prune removes unused networks
|
// Prune removes unused networks
|
||||||
func Prune(w http.ResponseWriter, r *http.Request) {
|
func Prune(w http.ResponseWriter, r *http.Request) {
|
||||||
|
if v, err := utils.SupportedVersion(r, ">=4.0.0"); err != nil {
|
||||||
|
utils.BadRequest(w, "version", v.String(), err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
runtime := r.Context().Value(api.RuntimeKey).(*libpod.Runtime)
|
runtime := r.Context().Value(api.RuntimeKey).(*libpod.Runtime)
|
||||||
|
ic := abi.ContainerEngine{Libpod: runtime}
|
||||||
|
|
||||||
filterMap, err := util.PrepareFilters(r)
|
filterMap, err := util.PrepareFilters(r)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -172,7 +205,6 @@ func Prune(w http.ResponseWriter, r *http.Request) {
|
|||||||
pruneOptions := entities.NetworkPruneOptions{
|
pruneOptions := entities.NetworkPruneOptions{
|
||||||
Filters: *filterMap,
|
Filters: *filterMap,
|
||||||
}
|
}
|
||||||
ic := abi.ContainerEngine{Libpod: runtime}
|
|
||||||
pruneReports, err := ic.NetworkPrune(r.Context(), pruneOptions)
|
pruneReports, err := ic.NetworkPrune(r.Context(), pruneOptions)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
utils.Error(w, http.StatusInternalServerError, err)
|
utils.Error(w, http.StatusInternalServerError, err)
|
||||||
|
@ -80,7 +80,7 @@ var _ = Describe("Podman networks", func() {
|
|||||||
|
|
||||||
// Valid filter params => network should be pruned now.
|
// Valid filter params => network should be pruned now.
|
||||||
filters = map[string][]string{
|
filters = map[string][]string{
|
||||||
"until": {"5000000000"}, //June 11, 2128
|
"until": {"5000000000"}, // June 11, 2128
|
||||||
}
|
}
|
||||||
pruneResponse, err = network.Prune(connText, new(network.PruneOptions).WithFilters(filters))
|
pruneResponse, err = network.Prune(connText, new(network.PruneOptions).WithFilters(filters))
|
||||||
Expect(err).To(BeNil())
|
Expect(err).To(BeNil())
|
||||||
@ -105,7 +105,7 @@ var _ = Describe("Podman networks", func() {
|
|||||||
_, err = network.Create(connText, &net)
|
_, err = network.Create(connText, &net)
|
||||||
Expect(err).ToNot(BeNil())
|
Expect(err).ToNot(BeNil())
|
||||||
code, _ := bindings.CheckResponseCode(err)
|
code, _ := bindings.CheckResponseCode(err)
|
||||||
Expect(code).To(BeNumerically("==", http.StatusInternalServerError))
|
Expect(code).To(BeNumerically("==", http.StatusConflict))
|
||||||
})
|
})
|
||||||
|
|
||||||
It("inspect network", func() {
|
It("inspect network", func() {
|
||||||
|
@ -8,7 +8,10 @@ t GET networks/non-existing-network 404 \
|
|||||||
|
|
||||||
t POST libpod/networks/create name='"network1"' 200 \
|
t POST libpod/networks/create name='"network1"' 200 \
|
||||||
.name=network1 \
|
.name=network1 \
|
||||||
.created~[0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\}.* \
|
.created~[0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\}.*
|
||||||
|
|
||||||
|
t POST /v3.4.0/libpod/networks/create name='"bad_version"' 400 \
|
||||||
|
.cause='given version is not supported'
|
||||||
|
|
||||||
# --data '{"name":"network2","subnets":[{"subnet":"10.10.254.0/24"}],"Labels":{"abc":"val"}}'
|
# --data '{"name":"network2","subnets":[{"subnet":"10.10.254.0/24"}],"Labels":{"abc":"val"}}'
|
||||||
t POST libpod/networks/create name='"network2"' \
|
t POST libpod/networks/create name='"network2"' \
|
||||||
|
@ -256,11 +256,11 @@ function t() {
|
|||||||
|
|
||||||
# If given path begins with /, use it as-is; otherwise prepend /version/
|
# If given path begins with /, use it as-is; otherwise prepend /version/
|
||||||
local url=http://$HOST:$PORT
|
local url=http://$HOST:$PORT
|
||||||
if expr "$path" : "/" >/dev/null; then
|
case "$path" in
|
||||||
url="$url$path"
|
/*) url="$url$path" ;;
|
||||||
else
|
libpod/*) url="$url/v4.0.0/$path" ;;
|
||||||
url="$url/v1.40/$path"
|
*) url="$url/v1.41/$path" ;;
|
||||||
fi
|
esac
|
||||||
|
|
||||||
# Log every action we do
|
# Log every action we do
|
||||||
echo "-------------------------------------------------------------" >>$LOG
|
echo "-------------------------------------------------------------" >>$LOG
|
||||||
|
Reference in New Issue
Block a user