improve docker network ls and rm

Signed-off-by: Victor Vieux <victorvieux@gmail.com>
This commit is contained in:
Victor Vieux 2015-10-16 17:33:19 -07:00
parent e9c486b046
commit 4e1ae773e2
2 changed files with 18 additions and 3 deletions

View File

@ -172,9 +172,11 @@ func getImagesJSON(c *context, w http.ResponseWriter, r *http.Request) {
// GET /networks
func getNetworks(c *context, w http.ResponseWriter, r *http.Request) {
out := []*dockerclient.NetworkResource{}
for _, network := range c.cluster.Networks() {
for _, network := range c.cluster.Networks().Uniq() {
tmp := (*network).NetworkResource
tmp.Name = network.Engine.Name + "/" + network.Name
if tmp.Scope == "local" {
tmp.Name = network.Engine.Name + "/" + network.Name
}
out = append(out, &tmp)
}
w.Header().Set("Content-Type", "application/json")
@ -687,7 +689,7 @@ func deleteNetworks(c *context, w http.ResponseWriter, r *http.Request) {
var id = mux.Vars(r)["networkid"]
if network := c.cluster.Networks().Get(id); network != nil {
if network := c.cluster.Networks().Uniq().Get(id); network != nil {
if err := c.cluster.RemoveNetwork(network); err != nil {
httpError(w, err.Error(), http.StatusInternalServerError)
return

View File

@ -17,6 +17,19 @@ type Network struct {
// Networks represents a map of networks
type Networks []*Network
// Uniq returns all uniq networks
func (networks Networks) Uniq() Networks {
tmp := make(map[string]*Network)
for _, network := range networks {
tmp[network.ID] = network
}
uniq := Networks{}
for _, network := range tmp {
uniq = append(uniq, network)
}
return uniq
}
// Get returns a network using it's ID or Name
func (networks Networks) Get(IDOrName string) *Network {
// Abort immediately if the name is empty.