Network and Endpoint query methods to return error on not found

- As requested by Docker committers

Signed-off-by: Alessandro Boch <aboch@docker.com>
This commit is contained in:
Alessandro Boch 2015-05-15 16:04:09 -07:00
parent 1e84379b1e
commit 5e044b5b5f
5 changed files with 36 additions and 18 deletions

View File

@ -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))
}
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}
}
if nw == nil {
return nil, &responseStatus{Status: "Resource not found: Network", StatusCode: http.StatusNotFound}
}
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))
}
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}
}
if ep == nil {
return nil, &responseStatus{Status: "Resource not found: Endpoint", StatusCode: http.StatusNotFound}
}
return ep, &successResponse
}

View File

@ -70,10 +70,10 @@ type NetworkController interface {
// WalkNetworks uses the provided function to walk the Network(s) managed by this controller.
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)
// 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)
}
@ -212,6 +212,10 @@ func (c *controller) NetworkByName(name string) (Network, error) {
c.WalkNetworks(s)
if n == nil {
return nil, ErrNoSuchNetwork
}
return n, nil
}
@ -224,7 +228,7 @@ func (c *controller) NetworkByID(id string) (Network, error) {
if n, ok := c.networks[types.UUID(id)]; ok {
return n, nil
}
return nil, nil
return nil, ErrNoSuchNetwork
}
func (c *controller) sandboxAdd(key string, create bool) (sandbox.Sandbox, error) {

View File

@ -6,6 +6,10 @@ import (
)
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
// is passed to NewNetwork api.
ErrNilNetworkDriver = errors.New("nil NetworkDriver instance")

View File

@ -591,11 +591,11 @@ func TestControllerQuery(t *testing.T) {
}
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")
@ -665,7 +665,10 @@ func TestNetworkQuery(t *testing.T) {
}
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)
}
if e != nil {
@ -673,6 +676,9 @@ func TestNetworkQuery(t *testing.T) {
}
e, err = net1.EndpointByID(ep12.ID())
if err != nil {
t.Fatal(err)
}
if ep12 != e {
t.Fatalf("EndpointByID() returned %v instead of %v", e, ep12)
}
@ -1273,7 +1279,7 @@ func runParallelTests(t *testing.T, thrNumber int) {
t.Fatal(err)
}
if ep == nil {
t.Fatal("Could not find ep1")
t.Fatal("Got nil ep with no error")
}
for i := 0; i < iterCnt; i++ {

View File

@ -36,10 +36,10 @@ type Network interface {
// WalkEndpoints uses the provided function to walk the Endpoints
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)
// 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)
}
@ -188,6 +188,10 @@ func (n *network) EndpointByName(name string) (Endpoint, error) {
n.WalkEndpoints(s)
if e == nil {
return nil, ErrNoSuchEndpoint
}
return e, nil
}
@ -200,5 +204,5 @@ func (n *network) EndpointByID(id string) (Endpoint, error) {
if e, ok := n.endpoints[types.UUID(id)]; ok {
return e, nil
}
return nil, nil
return nil, ErrNoSuchEndpoint
}