Browse Source

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 1 year ago
parent
commit
40adb4317b
2 changed files with 4 additions and 19 deletions
  1. 2 17
      api/server/router/network/network_routes.go
  2. 2 2
      libnetwork/error.go

+ 2 - 17
api/server/router/network/network_routes.go

@@ -83,10 +83,6 @@ func (e ambigousResultsError) Error() string {
 
 
 func (ambigousResultsError) InvalidParameter() {}
 func (ambigousResultsError) InvalidParameter() {}
 
 
-func nameConflict(name string) error {
-	return errdefs.Conflict(libnetwork.NetworkNameError(name))
-}
-
 func (n *networkRouter) getNetwork(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
 func (n *networkRouter) getNetwork(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
 	if err := httputils.ParseForm(r); err != nil {
 	if err := httputils.ParseForm(r); err != nil {
 		return err
 		return err
@@ -213,21 +209,11 @@ func (n *networkRouter) postNetworkCreate(ctx context.Context, w http.ResponseWr
 	}
 	}
 
 
 	if nws, err := n.cluster.GetNetworksByName(create.Name); err == nil && len(nws) > 0 {
 	if nws, err := n.cluster.GetNetworksByName(create.Name); err == nil && len(nws) > 0 {
-		return nameConflict(create.Name)
+		return libnetwork.NetworkNameError(create.Name)
 	}
 	}
 
 
 	nw, err := n.backend.CreateNetwork(create)
 	nw, err := n.backend.CreateNetwork(create)
 	if err != nil {
 	if err != nil {
-		var warning string
-		if _, ok := err.(libnetwork.NetworkNameError); ok {
-			// check if user defined CheckDuplicate, if set true, return err
-			// otherwise prepare a warning message
-			if create.CheckDuplicate {
-				return nameConflict(create.Name)
-			}
-			warning = libnetwork.NetworkNameError(create.Name).Error()
-		}
-
 		if _, ok := err.(libnetwork.ManagerRedirectError); !ok {
 		if _, ok := err.(libnetwork.ManagerRedirectError); !ok {
 			return err
 			return err
 		}
 		}
@@ -236,8 +222,7 @@ func (n *networkRouter) postNetworkCreate(ctx context.Context, w http.ResponseWr
 			return err
 			return err
 		}
 		}
 		nw = &types.NetworkCreateResponse{
 		nw = &types.NetworkCreateResponse{
-			ID:      id,
-			Warning: warning,
+			ID: id,
 		}
 		}
 	}
 	}
 
 

+ 2 - 2
libnetwork/error.go

@@ -53,8 +53,8 @@ func (nnr NetworkNameError) Error() string {
 	return fmt.Sprintf("network with name %s already exists", string(nnr))
 	return fmt.Sprintf("network with name %s already exists", string(nnr))
 }
 }
 
 
-// Forbidden denotes the type of this error
-func (nnr NetworkNameError) Forbidden() {}
+// Conflict denotes the type of this error
+func (nnr NetworkNameError) Conflict() {}
 
 
 // UnknownNetworkError is returned when libnetwork could not find in its database
 // UnknownNetworkError is returned when libnetwork could not find in its database
 // a network with the same name and id.
 // a network with the same name and id.