From d737a815a916946496409c9ccd1a96ac70ac16c2 Mon Sep 17 00:00:00 2001 From: Paul Holzinger Date: Tue, 21 Feb 2023 16:55:27 +0100 Subject: [PATCH] compat API: network create return 409 for duplicate If the name already exists and CheckDuplicate is set we need to return 409, if CheckDuplicate is not set we return the network without error. Fixes #17585 Signed-off-by: Paul Holzinger --- pkg/api/handlers/compat/networks.go | 11 +++++++++-- test/apiv2/35-networks.at | 3 +++ 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/pkg/api/handlers/compat/networks.go b/pkg/api/handlers/compat/networks.go index 704af4b0e4..bb9cda8875 100644 --- a/pkg/api/handlers/compat/networks.go +++ b/pkg/api/handlers/compat/networks.go @@ -277,10 +277,17 @@ func CreateNetwork(w http.ResponseWriter, r *http.Request) { // FIXME can we use the IPAM driver and options? } + opts := nettypes.NetworkCreateOptions{ + IgnoreIfExists: !networkCreate.CheckDuplicate, + } ic := abi.ContainerEngine{Libpod: runtime} - newNetwork, err := ic.NetworkCreate(r.Context(), network, nil) + newNetwork, err := ic.NetworkCreate(r.Context(), network, &opts) if err != nil { - utils.InternalServerError(w, err) + if errors.Is(err, nettypes.ErrNetworkExists) { + utils.Error(w, http.StatusConflict, err) + } else { + utils.InternalServerError(w, err) + } return } diff --git a/test/apiv2/35-networks.at b/test/apiv2/35-networks.at index 07ba45efb2..dfa5b583b7 100644 --- a/test/apiv2/35-networks.at +++ b/test/apiv2/35-networks.at @@ -104,6 +104,9 @@ t GET networks/podman 200 \ # network create docker t POST networks/create Name=net3\ IPAM='{"Config":[]}' 201 +# create with same name should not error unless CheckDuplicate is set +t POST networks/create Name=net3 201 +t POST networks/create Name=net3\ CheckDuplicate=true 409 # network delete docker t DELETE networks/net3 204