From f151cc23ab8d6032ea575990ef76f64367225361 Mon Sep 17 00:00:00 2001 From: Alessandro Boch Date: Fri, 17 Apr 2015 23:13:29 -0700 Subject: [PATCH] Add Network method to return list of endpoints Signed-off-by: Alessandro Boch --- libnetwork/libnetwork_test.go | 8 ++++++++ libnetwork/network.go | 20 +++++++++++++++++++- 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/libnetwork/libnetwork_test.go b/libnetwork/libnetwork_test.go index 2f6a1006e5..f6f9cc0556 100644 --- a/libnetwork/libnetwork_test.go +++ b/libnetwork/libnetwork_test.go @@ -70,6 +70,14 @@ func TestSimplebridge(t *testing.T) { t.Fatal(err) } + epList := network.Endpoints() + if len(epList) != 1 { + t.Fatal(err) + } + if ep != epList[0] { + t.Fatal(err) + } + if err := ep.Delete(); err != nil { t.Fatal(err) } diff --git a/libnetwork/network.go b/libnetwork/network.go index d12a13544d..377157bb88 100644 --- a/libnetwork/network.go +++ b/libnetwork/network.go @@ -31,7 +31,7 @@ create network namespaces and allocate interfaces for containers to use. // For each new container: allocate IP and interfaces. The returned network // settings will be used for container infos (inspect and such), as well as // iptables rules for port publishing. - _, sinfo, err := network.CreateEndpoint("Endpoint1", networkNamespace.Key(), "") + ep, err := network.CreateEndpoint("Endpoint1", networkNamespace.Key(), "") if err != nil { return } @@ -71,6 +71,9 @@ type Network interface { // Labels support will be added in the near future. CreateEndpoint(name string, sboxKey string, options interface{}) (Endpoint, *driverapi.SandboxInfo, error) + // Endpoints returns the list of Endpoint in this network. + Endpoints() []Endpoint + // Delete the network. Delete() error } @@ -240,6 +243,21 @@ func (n *network) CreateEndpoint(name string, sboxKey string, options interface{ return ep, sinfo, nil } +func (n *network) Endpoints() []Endpoint { + n.Lock() + defer n.Unlock() + + list := make([]Endpoint, len(n.endpoints)) + + idx := 0 + for _, e := range n.endpoints { + list[idx] = e + idx++ + } + + return list +} + func (ep *endpoint) ID() string { return string(ep.id) }