1
0
Fork 0
mirror of https://github.com/moby/moby.git synced 2022-11-09 12:21:53 -05:00

Merge pull request #158 from aboch/idt

Network and Endpoint query methods to return error on not found
This commit is contained in:
Madhu Venugopal 2015-05-15 16:26:17 -07:00
commit 8d4460a208
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 {
return nil, &responseStatus{Status: err.Error(), StatusCode: http.StatusBadRequest}
}
if nw == 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 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 {
return nil, &responseStatus{Status: err.Error(), StatusCode: http.StatusBadRequest}
}
if ep == 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 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
}