Browse code

api: Remove duplicated check on CheckDuplicate

Partially revert commit 94b880f.

The CheckDuplicate field has been introduced in commit 2ab94e1. At that
time, this check was done in the network router. It was then moved to
the daemon package in commit 3ca2982. However, commit 94b880f duplicated
the logic into the network router for no apparent reason. Finally,
commit ab18718 made sure a 409 would be returned instead of a 500.

As this logic is first done by the daemon, the error -> warning
conversion can't happen because CheckDuplicate has to be true for the
daemon package to return an error. If it's false, the daemon proceed
with the network creation, set the Warning field of its return value and
return no error.

Thus, the CheckDuplicate logic in the api is removed and
libnetwork.NetworkNameError now implements the ErrConflict interface.

Signed-off-by: Albin Kerouanton <albinker@gmail.com>

Albin Kerouanton authored on 2023/08/08 06:08:10
Showing 2 changed files
... ...
@@ -83,10 +83,6 @@ func (e ambigousResultsError) Error() string {
83 83
 
84 84
 func (ambigousResultsError) InvalidParameter() {}
85 85
 
86
-func nameConflict(name string) error {
87
-	return errdefs.Conflict(libnetwork.NetworkNameError(name))
88
-}
89
-
90 86
 func (n *networkRouter) getNetwork(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
91 87
 	if err := httputils.ParseForm(r); err != nil {
92 88
 		return err
... ...
@@ -213,21 +209,11 @@ func (n *networkRouter) postNetworkCreate(ctx context.Context, w http.ResponseWr
213 213
 	}
214 214
 
215 215
 	if nws, err := n.cluster.GetNetworksByName(create.Name); err == nil && len(nws) > 0 {
216
-		return nameConflict(create.Name)
216
+		return libnetwork.NetworkNameError(create.Name)
217 217
 	}
218 218
 
219 219
 	nw, err := n.backend.CreateNetwork(create)
220 220
 	if err != nil {
221
-		var warning string
222
-		if _, ok := err.(libnetwork.NetworkNameError); ok {
223
-			// check if user defined CheckDuplicate, if set true, return err
224
-			// otherwise prepare a warning message
225
-			if create.CheckDuplicate {
226
-				return nameConflict(create.Name)
227
-			}
228
-			warning = libnetwork.NetworkNameError(create.Name).Error()
229
-		}
230
-
231 221
 		if _, ok := err.(libnetwork.ManagerRedirectError); !ok {
232 222
 			return err
233 223
 		}
... ...
@@ -236,8 +222,7 @@ func (n *networkRouter) postNetworkCreate(ctx context.Context, w http.ResponseWr
236 236
 			return err
237 237
 		}
238 238
 		nw = &types.NetworkCreateResponse{
239
-			ID:      id,
240
-			Warning: warning,
239
+			ID: id,
241 240
 		}
242 241
 	}
243 242
 
... ...
@@ -53,8 +53,8 @@ func (nnr NetworkNameError) Error() string {
53 53
 	return fmt.Sprintf("network with name %s already exists", string(nnr))
54 54
 }
55 55
 
56
-// Forbidden denotes the type of this error
57
-func (nnr NetworkNameError) Forbidden() {}
56
+// Conflict denotes the type of this error
57
+func (nnr NetworkNameError) Conflict() {}
58 58
 
59 59
 // UnknownNetworkError is returned when libnetwork could not find in its database
60 60
 // a network with the same name and id.