Skip to content

Commit

Permalink
compat API: network create return 409 for duplicate
Browse files Browse the repository at this point in the history
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 containers#17585

Signed-off-by: Paul Holzinger <[email protected]>
  • Loading branch information
Luap99 committed Feb 21, 2023
1 parent ea66c7b commit 4b4b423
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 2 deletions.
11 changes: 9 additions & 2 deletions pkg/api/handlers/compat/networks.go
Original file line number Diff line number Diff line change
Expand Up @@ -280,10 +280,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
}

Expand Down
3 changes: 3 additions & 0 deletions test/apiv2/35-networks.at
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down

0 comments on commit 4b4b423

Please sign in to comment.