From 12d3e3e64416e1f0f5add0fa30d82e45fe6f4bd8 Mon Sep 17 00:00:00 2001 From: Michael Crosby Date: Tue, 17 Sep 2013 23:36:49 +0000 Subject: [PATCH] Add view all links for the engine --- api.go | 24 +++++++++++++++++------- commands.go | 6 ++++++ links.go | 11 +++++++++++ virtual-containers.go | 11 ----------- 4 files changed, 34 insertions(+), 18 deletions(-) delete mode 100644 virtual-containers.go diff --git a/api.go b/api.go index 36a853e6c..d602715c9 100644 --- a/api.go +++ b/api.go @@ -961,11 +961,16 @@ func getLinksJSON(srv *Server, version float64, w http.ResponseWriter, r *http.R out := []APILink{} name := r.FormValue("name") rawRm := r.FormValue("rm") + rawAll := r.FormValue("all") rm, err := getBoolParam(rawRm) if err != nil { return err } + all, err := getBoolParam(rawAll) + if err != nil { + return err + } if rm { link := srv.runtime.links.GetById(name) @@ -979,15 +984,20 @@ func getLinksJSON(srv *Server, version float64, w http.ResponseWriter, r *http.R w.WriteHeader(http.StatusNotFound) return nil } - if name == "" { - return fmt.Errorf("Name cannot be empty for link") + var links []*Link + if all { + links = srv.runtime.links.GetAll() + } else { + if name == "" { + return fmt.Errorf("Name cannot be empty for link") + } + container := srv.runtime.Get(name) + if container == nil { + return fmt.Errorf("Container not found %s", name) + } + links = srv.runtime.links.Get(container) } - container := srv.runtime.Get(name) - if container == nil { - return fmt.Errorf("Container not found %s", name) - } - links := srv.runtime.links.Get(container) for _, l := range links { out = append(out, APILink{ ID: l.ID(), diff --git a/commands.go b/commands.go index 8e96ab3b3..c802e8096 100644 --- a/commands.go +++ b/commands.go @@ -96,6 +96,7 @@ func (cli *DockerCli) CmdHelp(args ...string) error { {"insert", "Insert a file in an image"}, {"inspect", "Return low-level information on a container"}, {"kill", "Kill a running container"}, + {"links", "View and modify links to running containers"}, {"login", "Register or Login to the docker registry server"}, {"logs", "Fetch the logs of a container"}, {"port", "Lookup the public-facing port which is NAT-ed to PRIVATE_PORT"}, @@ -1115,6 +1116,7 @@ func (cli *DockerCli) CmdPs(args ...string) error { func (cli *DockerCli) CmdLinks(args ...string) error { cmd := Subcmd("links", "[OPTIONS] CONTAINER", "Get the links for a container") flRm := cmd.Bool("rm", false, "Remove an existing link by the link ID") + flAll := cmd.Bool("a", false, "Display all registered and active links") if err := cmd.Parse(args); err != nil { return err @@ -1127,6 +1129,10 @@ func (cli *DockerCli) CmdLinks(args ...string) error { v.Set("rm", "1") } + if *flAll { + v.Set("all", "1") + } + body, statusCode, err := cli.call("GET", "/links/json?"+v.Encode(), nil) if err != nil { return err diff --git a/links.go b/links.go index ec455526d..cc66d4280 100644 --- a/links.go +++ b/links.go @@ -132,6 +132,17 @@ func (l *LinkRepository) Get(c *Container) []*Link { return out } +// Return all links in the repository +func (l *LinkRepository) GetAll() []*Link { + out := make([]*Link, len(l.links)) + var i int + for _, link := range l.links { + out[i] = link + i++ + } + return out +} + // Get a link based on the link's ID func (l *LinkRepository) GetById(id string) *Link { return l.links[id] diff --git a/virtual-containers.go b/virtual-containers.go deleted file mode 100644 index 3b5b7d781..000000000 --- a/virtual-containers.go +++ /dev/null @@ -1,11 +0,0 @@ -package docker - -// Returns a new virtual container for interfacing with the host interfaces -func NewHostContainer() (*Container, error) { - return nil, nil -} - -// Returns a new virutal container for interfacing with the docker daemon -func NewDockerContainer() (*Container, error) { - return nil, nil -}