Browse Source

Merge pull request #158 from aboch/idt

Network and Endpoint query methods to return error on not found
Madhu Venugopal 10 years ago
parent
commit
8d4460a208
5 changed files with 36 additions and 18 deletions
  1. 6 6
      libnetwork/api/api.go
  2. 7 3
      libnetwork/controller.go
  3. 4 0
      libnetwork/error.go
  4. 12 6
      libnetwork/libnetwork_test.go
  5. 7 3
      libnetwork/network.go

+ 6 - 6
libnetwork/api/api.go

@@ -499,11 +499,11 @@ func findNetwork(c libnetwork.NetworkController, s string, by int) (libnetwork.N
 		panic(fmt.Sprintf("unexpected selector for network search: %d", by))
 		panic(fmt.Sprintf("unexpected selector for network search: %d", by))
 	}
 	}
 	if err != nil {
 	if err != nil {
+		if err == libnetwork.ErrNoSuchNetwork {
+			return nil, &responseStatus{Status: "Resource not found: Network", StatusCode: http.StatusNotFound}
+		}
 		return nil, &responseStatus{Status: err.Error(), StatusCode: http.StatusBadRequest}
 		return nil, &responseStatus{Status: err.Error(), StatusCode: http.StatusBadRequest}
 	}
 	}
-	if nw == nil {
-		return nil, &responseStatus{Status: "Resource not found: Network", StatusCode: http.StatusNotFound}
-	}
 	return nw, &successResponse
 	return nw, &successResponse
 }
 }
 
 
@@ -525,11 +525,11 @@ func findEndpoint(c libnetwork.NetworkController, ns, es string, nwBy, epBy int)
 		panic(fmt.Sprintf("unexpected selector for endpoint search: %d", epBy))
 		panic(fmt.Sprintf("unexpected selector for endpoint search: %d", epBy))
 	}
 	}
 	if err != nil {
 	if err != nil {
+		if err == libnetwork.ErrNoSuchEndpoint {
+			return nil, &responseStatus{Status: "Resource not found: Endpoint", StatusCode: http.StatusNotFound}
+		}
 		return nil, &responseStatus{Status: err.Error(), StatusCode: http.StatusBadRequest}
 		return nil, &responseStatus{Status: err.Error(), StatusCode: http.StatusBadRequest}
 	}
 	}
-	if ep == nil {
-		return nil, &responseStatus{Status: "Resource not found: Endpoint", StatusCode: http.StatusNotFound}
-	}
 	return ep, &successResponse
 	return ep, &successResponse
 }
 }
 
 

+ 7 - 3
libnetwork/controller.go

@@ -70,10 +70,10 @@ type NetworkController interface {
 	// WalkNetworks uses the provided function to walk the Network(s) managed by this controller.
 	// WalkNetworks uses the provided function to walk the Network(s) managed by this controller.
 	WalkNetworks(walker NetworkWalker)
 	WalkNetworks(walker NetworkWalker)
 
 
-	// NetworkByName returns the Network which has the passed name, if it exists otherwise nil is returned
+	// NetworkByName returns the Network which has the passed name. If not found, the error ErrNoSuchNetwork is returned.
 	NetworkByName(name string) (Network, error)
 	NetworkByName(name string) (Network, error)
 
 
-	// NetworkByID returns the Network which has the passed id, if it exists otherwise nil is returned
+	// NetworkByID returns the Network which has the passed id. If not found, the error ErrNoSuchNetwork is returned.
 	NetworkByID(id string) (Network, error)
 	NetworkByID(id string) (Network, error)
 }
 }
 
 
@@ -212,6 +212,10 @@ func (c *controller) NetworkByName(name string) (Network, error) {
 
 
 	c.WalkNetworks(s)
 	c.WalkNetworks(s)
 
 
+	if n == nil {
+		return nil, ErrNoSuchNetwork
+	}
+
 	return n, nil
 	return n, nil
 }
 }
 
 
@@ -224,7 +228,7 @@ func (c *controller) NetworkByID(id string) (Network, error) {
 	if n, ok := c.networks[types.UUID(id)]; ok {
 	if n, ok := c.networks[types.UUID(id)]; ok {
 		return n, nil
 		return n, nil
 	}
 	}
-	return nil, nil
+	return nil, ErrNoSuchNetwork
 }
 }
 
 
 func (c *controller) sandboxAdd(key string, create bool) (sandbox.Sandbox, error) {
 func (c *controller) sandboxAdd(key string, create bool) (sandbox.Sandbox, error) {

+ 4 - 0
libnetwork/error.go

@@ -6,6 +6,10 @@ import (
 )
 )
 
 
 var (
 var (
+	// ErrNoSuchNetwork is returned when a network query finds no result
+	ErrNoSuchNetwork = errors.New("network not found")
+	// ErrNoSuchEndpoint is returned when a endpoint query finds no result
+	ErrNoSuchEndpoint = errors.New("endpoint not found")
 	// ErrNilNetworkDriver is returned if a nil network driver
 	// ErrNilNetworkDriver is returned if a nil network driver
 	// is passed to NewNetwork api.
 	// is passed to NewNetwork api.
 	ErrNilNetworkDriver = errors.New("nil NetworkDriver instance")
 	ErrNilNetworkDriver = errors.New("nil NetworkDriver instance")

+ 12 - 6
libnetwork/libnetwork_test.go

@@ -591,11 +591,11 @@ func TestControllerQuery(t *testing.T) {
 	}
 	}
 
 
 	g, err := controller.NetworkByID("network1")
 	g, err := controller.NetworkByID("network1")
-	if err != nil {
-		t.Fatalf("Unexpected failure for NetworkByID(): %v", err)
+	if err == nil {
+		t.Fatalf("Unexpected success for NetworkByID(): %g", g)
 	}
 	}
-	if g != nil {
-		t.Fatalf("NetworkByID() succeeded with unknown target id")
+	if err != libnetwork.ErrNoSuchNetwork {
+		t.Fatalf("NetworkByID() failed with unexpected error: %v", err)
 	}
 	}
 
 
 	g, err = controller.NetworkByName("network1")
 	g, err = controller.NetworkByName("network1")
@@ -665,7 +665,10 @@ func TestNetworkQuery(t *testing.T) {
 	}
 	}
 
 
 	e, err = net1.EndpointByName("IamNotAnEndpoint")
 	e, err = net1.EndpointByName("IamNotAnEndpoint")
-	if err != nil {
+	if err == nil {
+		t.Fatalf("EndpointByName() succeeded with unknown target name")
+	}
+	if err != libnetwork.ErrNoSuchEndpoint {
 		t.Fatal(err)
 		t.Fatal(err)
 	}
 	}
 	if e != nil {
 	if e != nil {
@@ -673,6 +676,9 @@ func TestNetworkQuery(t *testing.T) {
 	}
 	}
 
 
 	e, err = net1.EndpointByID(ep12.ID())
 	e, err = net1.EndpointByID(ep12.ID())
+	if err != nil {
+		t.Fatal(err)
+	}
 	if ep12 != e {
 	if ep12 != e {
 		t.Fatalf("EndpointByID() returned %v instead of %v", e, ep12)
 		t.Fatalf("EndpointByID() returned %v instead of %v", e, ep12)
 	}
 	}
@@ -1273,7 +1279,7 @@ func runParallelTests(t *testing.T, thrNumber int) {
 		t.Fatal(err)
 		t.Fatal(err)
 	}
 	}
 	if ep == nil {
 	if ep == nil {
-		t.Fatal("Could not find ep1")
+		t.Fatal("Got nil ep with no error")
 	}
 	}
 
 
 	for i := 0; i < iterCnt; i++ {
 	for i := 0; i < iterCnt; i++ {

+ 7 - 3
libnetwork/network.go

@@ -36,10 +36,10 @@ type Network interface {
 	// WalkEndpoints uses the provided function to walk the Endpoints
 	// WalkEndpoints uses the provided function to walk the Endpoints
 	WalkEndpoints(walker EndpointWalker)
 	WalkEndpoints(walker EndpointWalker)
 
 
-	// EndpointByName returns the Endpoint which has the passed name, if it exists otherwise nil is returned
+	// EndpointByName returns the Endpoint which has the passed name. If not found, the error ErrNoSuchEndpoint is returned.
 	EndpointByName(name string) (Endpoint, error)
 	EndpointByName(name string) (Endpoint, error)
 
 
-	// EndpointByID returns the Endpoint which has the passed id, if it exists otherwise nil is returned
+	// EndpointByID returns the Endpoint which has the passed id. If not found, the error ErrNoSuchEndpoint is returned.
 	EndpointByID(id string) (Endpoint, error)
 	EndpointByID(id string) (Endpoint, error)
 }
 }
 
 
@@ -188,6 +188,10 @@ func (n *network) EndpointByName(name string) (Endpoint, error) {
 
 
 	n.WalkEndpoints(s)
 	n.WalkEndpoints(s)
 
 
+	if e == nil {
+		return nil, ErrNoSuchEndpoint
+	}
+
 	return e, nil
 	return e, nil
 }
 }
 
 
@@ -200,5 +204,5 @@ func (n *network) EndpointByID(id string) (Endpoint, error) {
 	if e, ok := n.endpoints[types.UUID(id)]; ok {
 	if e, ok := n.endpoints[types.UUID(id)]; ok {
 		return e, nil
 		return e, nil
 	}
 	}
-	return nil, nil
+	return nil, ErrNoSuchEndpoint
 }
 }