2015-04-26 03:19:01 -04:00
|
|
|
package client
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2015-05-21 06:58:30 -04:00
|
|
|
"encoding/json"
|
2015-04-26 03:19:01 -04:00
|
|
|
"fmt"
|
2015-05-21 06:58:30 -04:00
|
|
|
"net/http"
|
2015-05-23 18:03:52 -04:00
|
|
|
"text/tabwriter"
|
2015-04-26 03:19:01 -04:00
|
|
|
|
|
|
|
flag "github.com/docker/docker/pkg/mflag"
|
2015-05-23 18:03:52 -04:00
|
|
|
"github.com/docker/docker/pkg/stringid"
|
2015-12-21 20:31:50 -05:00
|
|
|
"github.com/docker/libnetwork/netlabel"
|
2015-05-17 10:07:09 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
type command struct {
|
|
|
|
name string
|
|
|
|
description string
|
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
|
|
|
networkCommands = []command{
|
|
|
|
{"create", "Create a network"},
|
|
|
|
{"rm", "Remove a network"},
|
|
|
|
{"ls", "List all networks"},
|
|
|
|
{"info", "Display information of a network"},
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
2015-05-16 11:23:59 -04:00
|
|
|
// CmdNetwork handles the root Network UI
|
2015-05-17 10:07:09 -04:00
|
|
|
func (cli *NetworkCli) CmdNetwork(chain string, args ...string) error {
|
|
|
|
cmd := cli.Subcmd(chain, "network", "COMMAND [OPTIONS] [arg...]", networkUsage(chain), false)
|
|
|
|
cmd.Require(flag.Min, 1)
|
|
|
|
err := cmd.ParseFlags(args, true)
|
|
|
|
if err == nil {
|
|
|
|
cmd.Usage()
|
2015-05-21 06:58:30 -04:00
|
|
|
return fmt.Errorf("invalid command : %v", args)
|
2015-05-17 10:07:09 -04:00
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-04-26 03:19:01 -04:00
|
|
|
// CmdNetworkCreate handles Network Create UI
|
|
|
|
func (cli *NetworkCli) CmdNetworkCreate(chain string, args ...string) error {
|
2015-05-17 10:07:09 -04:00
|
|
|
cmd := cli.Subcmd(chain, "create", "NETWORK-NAME", "Creates a new network with a name specified by the user", false)
|
2015-06-14 12:00:27 -04:00
|
|
|
flDriver := cmd.String([]string{"d", "-driver"}, "", "Driver to manage the Network")
|
2015-12-21 20:31:50 -05:00
|
|
|
flInternal := cmd.Bool([]string{"-internal"}, false, "Config the network to be internal")
|
2016-01-29 19:54:57 -05:00
|
|
|
flIPv6 := cmd.Bool([]string{"-ipv6"}, false, "Enable IPv6 on the network")
|
2015-06-14 12:00:27 -04:00
|
|
|
cmd.Require(flag.Exact, 1)
|
2015-04-26 03:19:01 -04:00
|
|
|
err := cmd.ParseFlags(args, true)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-12-21 20:31:50 -05:00
|
|
|
networkOpts := make(map[string]string)
|
|
|
|
if *flInternal {
|
|
|
|
networkOpts[netlabel.Internal] = "true"
|
|
|
|
}
|
2016-01-29 19:54:57 -05:00
|
|
|
if *flIPv6 {
|
|
|
|
networkOpts[netlabel.EnableIPv6] = "true"
|
|
|
|
}
|
2015-05-22 13:56:36 -04:00
|
|
|
// Construct network create request body
|
2015-10-14 19:38:46 -04:00
|
|
|
var driverOpts []string
|
2015-12-21 20:31:50 -05:00
|
|
|
nc := networkCreate{Name: cmd.Arg(0), NetworkType: *flDriver, DriverOpts: driverOpts, NetworkOpts: networkOpts}
|
2015-05-19 16:04:16 -04:00
|
|
|
obj, _, err := readBody(cli.call("POST", "/networks", nc, nil))
|
2015-04-26 03:19:01 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-05-23 18:03:52 -04:00
|
|
|
var replyID string
|
|
|
|
err = json.Unmarshal(obj, &replyID)
|
|
|
|
if err != nil {
|
2015-04-26 03:19:01 -04:00
|
|
|
return err
|
|
|
|
}
|
2015-05-23 18:03:52 -04:00
|
|
|
fmt.Fprintf(cli.out, "%s\n", replyID)
|
2015-04-26 03:19:01 -04:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// CmdNetworkRm handles Network Delete UI
|
|
|
|
func (cli *NetworkCli) CmdNetworkRm(chain string, args ...string) error {
|
2015-05-21 06:58:30 -04:00
|
|
|
cmd := cli.Subcmd(chain, "rm", "NETWORK", "Deletes a network", false)
|
2015-06-14 12:00:27 -04:00
|
|
|
cmd.Require(flag.Exact, 1)
|
2015-04-26 03:19:01 -04:00
|
|
|
err := cmd.ParseFlags(args, true)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-05-21 06:58:30 -04:00
|
|
|
id, err := lookupNetworkID(cli, cmd.Arg(0))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-05-23 18:03:52 -04:00
|
|
|
_, _, err = readBody(cli.call("DELETE", "/networks/"+id, nil, nil))
|
2015-04-26 03:19:01 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// CmdNetworkLs handles Network List UI
|
|
|
|
func (cli *NetworkCli) CmdNetworkLs(chain string, args ...string) error {
|
2015-05-17 10:07:09 -04:00
|
|
|
cmd := cli.Subcmd(chain, "ls", "", "Lists all the networks created by the user", false)
|
2015-05-23 18:03:52 -04:00
|
|
|
quiet := cmd.Bool([]string{"q", "-quiet"}, false, "Only display numeric IDs")
|
|
|
|
noTrunc := cmd.Bool([]string{"#notrunc", "-no-trunc"}, false, "Do not truncate the output")
|
|
|
|
nLatest := cmd.Bool([]string{"l", "-latest"}, false, "Show the latest network created")
|
|
|
|
last := cmd.Int([]string{"n"}, -1, "Show n last created networks")
|
2015-04-26 03:19:01 -04:00
|
|
|
err := cmd.ParseFlags(args, true)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
obj, _, err := readBody(cli.call("GET", "/networks", nil, nil))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-05-23 18:03:52 -04:00
|
|
|
if *last == -1 && *nLatest {
|
|
|
|
*last = 1
|
|
|
|
}
|
|
|
|
|
|
|
|
var networkResources []networkResource
|
|
|
|
err = json.Unmarshal(obj, &networkResources)
|
|
|
|
if err != nil {
|
2015-04-26 03:19:01 -04:00
|
|
|
return err
|
|
|
|
}
|
2015-05-23 18:03:52 -04:00
|
|
|
|
|
|
|
wr := tabwriter.NewWriter(cli.out, 20, 1, 3, ' ', 0)
|
|
|
|
|
|
|
|
// unless quiet (-q) is specified, print field titles
|
|
|
|
if !*quiet {
|
|
|
|
fmt.Fprintln(wr, "NETWORK ID\tNAME\tTYPE")
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, networkResource := range networkResources {
|
|
|
|
ID := networkResource.ID
|
|
|
|
netName := networkResource.Name
|
|
|
|
if !*noTrunc {
|
|
|
|
ID = stringid.TruncateID(ID)
|
|
|
|
}
|
|
|
|
if *quiet {
|
|
|
|
fmt.Fprintln(wr, ID)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
netType := networkResource.Type
|
|
|
|
fmt.Fprintf(wr, "%s\t%s\t%s\t",
|
|
|
|
ID,
|
|
|
|
netName,
|
|
|
|
netType)
|
|
|
|
fmt.Fprint(wr, "\n")
|
|
|
|
}
|
|
|
|
wr.Flush()
|
2015-04-26 03:19:01 -04:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// CmdNetworkInfo handles Network Info UI
|
|
|
|
func (cli *NetworkCli) CmdNetworkInfo(chain string, args ...string) error {
|
2015-05-21 06:58:30 -04:00
|
|
|
cmd := cli.Subcmd(chain, "info", "NETWORK", "Displays detailed information on a network", false)
|
2015-06-14 12:00:27 -04:00
|
|
|
cmd.Require(flag.Exact, 1)
|
2015-04-26 03:19:01 -04:00
|
|
|
err := cmd.ParseFlags(args, true)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-05-21 06:58:30 -04:00
|
|
|
|
|
|
|
id, err := lookupNetworkID(cli, cmd.Arg(0))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
obj, _, err := readBody(cli.call("GET", "/networks/"+id, nil, nil))
|
2015-04-26 03:19:01 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-05-23 18:03:52 -04:00
|
|
|
networkResource := &networkResource{}
|
|
|
|
if err := json.NewDecoder(bytes.NewReader(obj)).Decode(networkResource); err != nil {
|
2015-04-26 03:19:01 -04:00
|
|
|
return err
|
|
|
|
}
|
2015-05-23 18:03:52 -04:00
|
|
|
fmt.Fprintf(cli.out, "Network Id: %s\n", networkResource.ID)
|
|
|
|
fmt.Fprintf(cli.out, "Name: %s\n", networkResource.Name)
|
|
|
|
fmt.Fprintf(cli.out, "Type: %s\n", networkResource.Type)
|
2015-06-11 11:30:45 -04:00
|
|
|
if networkResource.Services != nil {
|
|
|
|
for _, serviceResource := range networkResource.Services {
|
|
|
|
fmt.Fprintf(cli.out, " Service Id: %s\n", serviceResource.ID)
|
|
|
|
fmt.Fprintf(cli.out, "\tName: %s\n", serviceResource.Name)
|
2015-05-23 18:03:52 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-26 03:19:01 -04:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-05-21 06:58:30 -04:00
|
|
|
// Helper function to predict if a string is a name or id or partial-id
|
|
|
|
// This provides a best-effort mechanism to identify a id with the help of GET Filter APIs
|
|
|
|
// Being a UI, its most likely that name will be used by the user, which is used to lookup
|
|
|
|
// the corresponding ID. If ID is not found, this function will assume that the passed string
|
|
|
|
// is an ID by itself.
|
|
|
|
|
|
|
|
func lookupNetworkID(cli *NetworkCli, nameID string) (string, error) {
|
|
|
|
obj, statusCode, err := readBody(cli.call("GET", "/networks?name="+nameID, nil, nil))
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
if statusCode != http.StatusOK {
|
|
|
|
return "", fmt.Errorf("name query failed for %s due to : statuscode(%d) %v", nameID, statusCode, string(obj))
|
|
|
|
}
|
|
|
|
|
|
|
|
var list []*networkResource
|
|
|
|
err = json.Unmarshal(obj, &list)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
if len(list) > 0 {
|
|
|
|
// name query filter will always return a single-element collection
|
|
|
|
return list[0].ID, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check for Partial-id
|
|
|
|
obj, statusCode, err = readBody(cli.call("GET", "/networks?partial-id="+nameID, nil, nil))
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
if statusCode != http.StatusOK {
|
|
|
|
return "", fmt.Errorf("partial-id match query failed for %s due to : statuscode(%d) %v", nameID, statusCode, string(obj))
|
|
|
|
}
|
|
|
|
|
|
|
|
err = json.Unmarshal(obj, &list)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
if len(list) == 0 {
|
|
|
|
return "", fmt.Errorf("resource not found %s", nameID)
|
|
|
|
}
|
|
|
|
if len(list) > 1 {
|
|
|
|
return "", fmt.Errorf("multiple Networks matching the partial identifier (%s). Please use full identifier", nameID)
|
|
|
|
}
|
|
|
|
return list[0].ID, nil
|
|
|
|
}
|
|
|
|
|
2015-05-16 11:23:59 -04:00
|
|
|
func networkUsage(chain string) string {
|
|
|
|
help := "Commands:\n"
|
2015-04-26 03:19:01 -04:00
|
|
|
|
2015-05-16 11:23:59 -04:00
|
|
|
for _, cmd := range networkCommands {
|
2015-07-22 22:46:57 -04:00
|
|
|
help += fmt.Sprintf(" %-25.25s%s\n", cmd.name, cmd.description)
|
2015-04-26 03:19:01 -04:00
|
|
|
}
|
2015-05-16 11:23:59 -04:00
|
|
|
|
2015-05-23 16:05:48 -04:00
|
|
|
help += fmt.Sprintf("\nRun '%s network COMMAND --help' for more information on a command.", chain)
|
2015-05-22 10:54:23 -04:00
|
|
|
return help
|
|
|
|
}
|