Prechádzať zdrojové kódy

Merge pull request #66 from aboch/net

Add Network method to return list of endpoints
Madhu Venugopal 10 rokov pred
rodič
commit
2e10d9197e
2 zmenil súbory, kde vykonal 27 pridanie a 1 odobranie
  1. 8 0
      libnetwork/libnetwork_test.go
  2. 19 1
      libnetwork/network.go

+ 8 - 0
libnetwork/libnetwork_test.go

@@ -72,6 +72,14 @@ func TestSimplebridge(t *testing.T) {
 		t.Fatal(err)
 		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 {
 	if err := ep.Delete(); err != nil {
 		t.Fatal(err)
 		t.Fatal(err)
 	}
 	}

+ 19 - 1
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
     // For each new container: allocate IP and interfaces. The returned network
     // settings will be used for container infos (inspect and such), as well as
     // settings will be used for container infos (inspect and such), as well as
     // iptables rules for port publishing.
     // iptables rules for port publishing.
-    _, sinfo, err := network.CreateEndpoint("Endpoint1", networkNamespace.Key(), "")
+    ep, err := network.CreateEndpoint("Endpoint1", networkNamespace.Key(), "")
     if err != nil {
     if err != nil {
 	    return
 	    return
     }
     }
@@ -72,6 +72,9 @@ type Network interface {
 	// Labels support will be added in the near future.
 	// Labels support will be added in the near future.
 	CreateEndpoint(name string, sboxKey string, options interface{}) (Endpoint, error)
 	CreateEndpoint(name string, sboxKey string, options interface{}) (Endpoint, error)
 
 
+	// Endpoints returns the list of Endpoint in this network.
+	Endpoints() []Endpoint
+
 	// Delete the network.
 	// Delete the network.
 	Delete() error
 	Delete() error
 }
 }
@@ -250,6 +253,21 @@ func (n *network) CreateEndpoint(name string, sboxKey string, options interface{
 	return ep, nil
 	return ep, 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 {
 func (ep *endpoint) ID() string {
 	return string(ep.id)
 	return string(ep.id)
 }
 }