mirror of https://github.com/docker/docs.git
Merge pull request #1309 from vieux/improve_networks
small improvment on network UX
This commit is contained in:
commit
e64c5ee209
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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.
|
||||
|
|
|
|||
|
|
@ -176,7 +176,9 @@ func (c *Cluster) RemoveContainer(container *cluster.Container, force, volumes b
|
|||
|
||||
// RemoveNetwork removes a network from the cluster
|
||||
func (c *Cluster) RemoveNetwork(network *cluster.Network) error {
|
||||
return network.Engine.RemoveNetwork(network)
|
||||
err := network.Engine.RemoveNetwork(network)
|
||||
c.refreshNetworks()
|
||||
return err
|
||||
}
|
||||
|
||||
func (c *Cluster) getEngineByAddr(addr string) *cluster.Engine {
|
||||
|
|
@ -335,6 +337,18 @@ func (c *Cluster) RemoveImages(name string, force bool) ([]*dockerclient.ImageDe
|
|||
return out, err
|
||||
}
|
||||
|
||||
func (c *Cluster) refreshNetworks() {
|
||||
var wg sync.WaitGroup
|
||||
for _, e := range c.engines {
|
||||
wg.Add(1)
|
||||
go func(e *cluster.Engine) {
|
||||
e.RefreshNetworks()
|
||||
wg.Done()
|
||||
}(e)
|
||||
}
|
||||
wg.Wait()
|
||||
}
|
||||
|
||||
// CreateNetwork creates a network in the cluster
|
||||
func (c *Cluster) CreateNetwork(request *dockerclient.NetworkCreate) (response *dockerclient.NetworkCreateResponse, err error) {
|
||||
var (
|
||||
|
|
@ -353,7 +367,9 @@ func (c *Cluster) CreateNetwork(request *dockerclient.NetworkCreate) (response *
|
|||
return nil, err
|
||||
}
|
||||
if nodes != nil {
|
||||
return c.engines[nodes[0].ID].CreateNetwork(request)
|
||||
resp, err := c.engines[nodes[0].ID].CreateNetwork(request)
|
||||
c.refreshNetworks()
|
||||
return resp, err
|
||||
}
|
||||
return nil, nil
|
||||
}
|
||||
|
|
|
|||
|
|
@ -26,7 +26,7 @@ function teardown() {
|
|||
[ "$status" -ne 0 ]
|
||||
|
||||
run docker_swarm network inspect node-0/bridge
|
||||
[ "${#lines[@]}" -eq 23 ]
|
||||
[[ "${output}" != *"\"containers\": {}"* ]]
|
||||
}
|
||||
|
||||
@test "docker network create" {
|
||||
|
|
@ -75,20 +75,20 @@ function teardown() {
|
|||
docker_swarm run -d --name test_container -e constraint:node==node-0 busybox sleep 100
|
||||
|
||||
run docker_swarm network inspect node-0/bridge
|
||||
[ "${#lines[@]}" -eq 23 ]
|
||||
[[ "${output}" != *"\"containers\": {}"* ]]
|
||||
|
||||
docker_swarm network disconnect node-0/bridge test_container
|
||||
|
||||
run docker_swarm network inspect node-0/bridge
|
||||
[ "${#lines[@]}" -eq 16 ]
|
||||
[[ "${output}" == *"\"containers\": {}"* ]]
|
||||
|
||||
docker_swarm network connect node-0/bridge test_container
|
||||
|
||||
run docker_swarm network inspect node-0/bridge
|
||||
[ "${#lines[@]}" -eq 23 ]
|
||||
[[ "${output}" != *"\"containers\": {}"* ]]
|
||||
|
||||
docker_swarm rm -f test_container
|
||||
|
||||
run docker_swarm network inspect node-0/bridge
|
||||
[ "${#lines[@]}" -eq 16 ]
|
||||
[[ "${output}" == *"\"containers\": {}"* ]]
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue