瀏覽代碼

Client to make use of REST API

Signed-off-by: Madhu Venugopal <madhu@docker.com>
Madhu Venugopal 10 年之前
父節點
當前提交
977fcdd952
共有 4 個文件被更改,包括 122 次插入122 次删除
  1. 6 64
      libnetwork/api/api.go
  2. 68 0
      libnetwork/api/types.go
  3. 14 58
      libnetwork/client/network.go
  4. 34 0
      libnetwork/client/types.go

+ 6 - 64
libnetwork/api/api.go

@@ -7,7 +7,6 @@ import (
 	"net/http"
 	"net/http"
 
 
 	"github.com/docker/libnetwork"
 	"github.com/docker/libnetwork"
-	"github.com/docker/libnetwork/netutils"
 	"github.com/gorilla/mux"
 	"github.com/gorilla/mux"
 )
 )
 
 
@@ -125,24 +124,9 @@ func makeHandler(ctrl libnetwork.NetworkController, fct processor) http.HandlerF
 	}
 	}
 }
 }
 
 
-/***********
- Resources
-************/
-
-// networkResource is the body of the "get network" http response message
-type networkResource struct {
-	Name      string
-	ID        string
-	Type      string
-	Endpoints []*endpointResource
-}
-
-// endpointResource is the body of the "get endpoint" http response message
-type endpointResource struct {
-	Name    string
-	ID      string
-	Network string
-}
+/*****************
+ Resource Builders
+******************/
 
 
 func buildNetworkResource(nw libnetwork.Network) *networkResource {
 func buildNetworkResource(nw libnetwork.Network) *networkResource {
 	r := &networkResource{}
 	r := &networkResource{}
@@ -170,51 +154,9 @@ func buildEndpointResource(ep libnetwork.Endpoint) *endpointResource {
 	return r
 	return r
 }
 }
 
 
-/***********
- Body types
-************/
-
-// networkCreate is the expected body of the "create network" http request message
-type networkCreate struct {
-	Name        string
-	NetworkType string
-	Options     map[string]interface{}
-}
-
-// endpointCreate represents the body of the "create endpoint" http request message
-type endpointCreate struct {
-	Name         string
-	NetworkID    string
-	ExposedPorts []netutils.TransportPort
-	PortMapping  []netutils.PortBinding
-}
-
-// endpointJoin represents the expected body of the "join endpoint" or "leave endpoint" http request messages
-type endpointJoin struct {
-	ContainerID       string
-	HostName          string
-	DomainName        string
-	HostsPath         string
-	ResolvConfPath    string
-	DNS               []string
-	ExtraHosts        []endpointExtraHost
-	ParentUpdates     []endpointParentUpdate
-	UseDefaultSandbox bool
-}
-
-// EndpointExtraHost represents the extra host object
-type endpointExtraHost struct {
-	Name    string
-	Address string
-}
-
-// EndpointParentUpdate is the object carrying the information about the
-// endpoint parent that needs to be updated
-type endpointParentUpdate struct {
-	EndpointID string
-	Name       string
-	Address    string
-}
+/**************
+ Options Parser
+***************/
 
 
 func (ej *endpointJoin) parseOptions() []libnetwork.EndpointOption {
 func (ej *endpointJoin) parseOptions() []libnetwork.EndpointOption {
 	var setFctList []libnetwork.EndpointOption
 	var setFctList []libnetwork.EndpointOption

+ 68 - 0
libnetwork/api/types.go

@@ -0,0 +1,68 @@
+package api
+
+import "github.com/docker/libnetwork/netutils"
+
+/***********
+ Resources
+************/
+
+// networkResource is the body of the "get network" http response message
+type networkResource struct {
+	Name      string
+	ID        string
+	Type      string
+	Endpoints []*endpointResource
+}
+
+// endpointResource is the body of the "get endpoint" http response message
+type endpointResource struct {
+	Name    string
+	ID      string
+	Network string
+}
+
+/***********
+  Body types
+  ************/
+
+// networkCreate is the expected body of the "create network" http request message
+type networkCreate struct {
+	Name        string
+	NetworkType string
+	Options     map[string]interface{}
+}
+
+// endpointCreate represents the body of the "create endpoint" http request message
+type endpointCreate struct {
+	Name         string
+	NetworkID    string
+	ExposedPorts []netutils.TransportPort
+	PortMapping  []netutils.PortBinding
+}
+
+// endpointJoin represents the expected body of the "join endpoint" or "leave endpoint" http request messages
+type endpointJoin struct {
+	ContainerID       string
+	HostName          string
+	DomainName        string
+	HostsPath         string
+	ResolvConfPath    string
+	DNS               []string
+	ExtraHosts        []endpointExtraHost
+	ParentUpdates     []endpointParentUpdate
+	UseDefaultSandbox bool
+}
+
+// EndpointExtraHost represents the extra host object
+type endpointExtraHost struct {
+	Name    string
+	Address string
+}
+
+// EndpointParentUpdate is the object carrying the information about the
+// endpoint parent that needs to be updated
+type endpointParentUpdate struct {
+	EndpointID string
+	Name       string
+	Address    string
+}

+ 14 - 58
libnetwork/client/network.go

@@ -26,6 +26,7 @@ var (
 	}
 	}
 )
 )
 
 
+// CmdNetwork handles the root Network UI
 func (cli *NetworkCli) CmdNetwork(chain string, args ...string) error {
 func (cli *NetworkCli) CmdNetwork(chain string, args ...string) error {
 	cmd := cli.Subcmd(chain, "network", "COMMAND [OPTIONS] [arg...]", networkUsage(chain), false)
 	cmd := cli.Subcmd(chain, "network", "COMMAND [OPTIONS] [arg...]", networkUsage(chain), false)
 	cmd.Require(flag.Min, 1)
 	cmd.Require(flag.Min, 1)
@@ -37,17 +38,6 @@ func (cli *NetworkCli) CmdNetwork(chain string, args ...string) error {
 	return err
 	return err
 }
 }
 
 
-func networkUsage(chain string) string {
-	help := "Commands:\n"
-
-	for _, cmd := range networkCommands {
-		help += fmt.Sprintf("    %-10.10s%s\n", cmd.name, cmd.description)
-	}
-
-	help += fmt.Sprintf("\nRun '%s network COMMAND --help' for more information on a command.", chain)
-	return help
-}
-
 // CmdNetworkCreate handles Network Create UI
 // CmdNetworkCreate handles Network Create UI
 func (cli *NetworkCli) CmdNetworkCreate(chain string, args ...string) error {
 func (cli *NetworkCli) CmdNetworkCreate(chain string, args ...string) error {
 	cmd := cli.Subcmd(chain, "create", "NETWORK-NAME", "Creates a new network with a name specified by the user", false)
 	cmd := cli.Subcmd(chain, "create", "NETWORK-NAME", "Creates a new network with a name specified by the user", false)
@@ -60,8 +50,10 @@ func (cli *NetworkCli) CmdNetworkCreate(chain string, args ...string) error {
 	if *flDriver == "" {
 	if *flDriver == "" {
 		*flDriver = nullNetType
 		*flDriver = nullNetType
 	}
 	}
-	// TODO : Proper Backend handling
-	obj, _, err := readBody(cli.call("POST", "/networks/"+args[0], nil, nil))
+
+	nc := networkCreate{Name: cmd.Arg(0), NetworkType: *flDriver}
+
+	obj, _, err := readBody(cli.call("POST", "/networks/name/"+cmd.Arg(0), nc, nil))
 	if err != nil {
 	if err != nil {
 		fmt.Fprintf(cli.err, "%s", err.Error())
 		fmt.Fprintf(cli.err, "%s", err.Error())
 		return err
 		return err
@@ -80,8 +72,7 @@ func (cli *NetworkCli) CmdNetworkRm(chain string, args ...string) error {
 	if err != nil {
 	if err != nil {
 		return err
 		return err
 	}
 	}
-	// TODO : Proper Backend handling
-	obj, _, err := readBody(cli.call("DELETE", "/networks/"+args[0], nil, nil))
+	obj, _, err := readBody(cli.call("DELETE", "/networks/name/"+cmd.Arg(0), nil, nil))
 	if err != nil {
 	if err != nil {
 		fmt.Fprintf(cli.err, "%s", err.Error())
 		fmt.Fprintf(cli.err, "%s", err.Error())
 		return err
 		return err
@@ -99,7 +90,6 @@ func (cli *NetworkCli) CmdNetworkLs(chain string, args ...string) error {
 	if err != nil {
 	if err != nil {
 		return err
 		return err
 	}
 	}
-	// TODO : Proper Backend handling
 	obj, _, err := readBody(cli.call("GET", "/networks", nil, nil))
 	obj, _, err := readBody(cli.call("GET", "/networks", nil, nil))
 	if err != nil {
 	if err != nil {
 		fmt.Fprintf(cli.err, "%s", err.Error())
 		fmt.Fprintf(cli.err, "%s", err.Error())
@@ -119,8 +109,7 @@ func (cli *NetworkCli) CmdNetworkInfo(chain string, args ...string) error {
 	if err != nil {
 	if err != nil {
 		return err
 		return err
 	}
 	}
-	// TODO : Proper Backend handling
-	obj, _, err := readBody(cli.call("GET", "/networks/"+args[0], nil, nil))
+	obj, _, err := readBody(cli.call("GET", "/networks/name/"+cmd.Arg(0), nil, nil))
 	if err != nil {
 	if err != nil {
 		fmt.Fprintf(cli.err, "%s", err.Error())
 		fmt.Fprintf(cli.err, "%s", err.Error())
 		return err
 		return err
@@ -131,46 +120,13 @@ func (cli *NetworkCli) CmdNetworkInfo(chain string, args ...string) error {
 	return nil
 	return nil
 }
 }
 
 
-// CmdNetworkJoin handles the UI to let a Container join a Network via an endpoint
-// Sample UI : <chain> network join <container-name/id> <network-name/id> [<endpoint-name>]
-func (cli *NetworkCli) CmdNetworkJoin(chain string, args ...string) error {
-	cmd := cli.Subcmd(chain, "join", "CONTAINER-NAME/ID NETWORK-NAME/ID [ENDPOINT-NAME]",
-		chain+" join", false)
-	cmd.Require(flag.Min, 2)
-	err := cmd.ParseFlags(args, true)
-	if err != nil {
-		return err
-	}
-	// TODO : Proper Backend handling
-	obj, _, err := readBody(cli.call("POST", "/endpoints/", nil, nil))
-	if err != nil {
-		fmt.Fprintf(cli.err, "%s", err.Error())
-		return err
-	}
-	if _, err := io.Copy(cli.out, bytes.NewReader(obj)); err != nil {
-		return err
-	}
-	return nil
-}
+func networkUsage(chain string) string {
+	help := "Commands:\n"
 
 
-// CmdNetworkLeave handles the UI to let a Container disconnect from a Network
-// Sample UI : <chain> network leave <container-name/id> <network-name/id>
-func (cli *NetworkCli) CmdNetworkLeave(chain string, args ...string) error {
-	cmd := cli.Subcmd(chain, "leave", "CONTAINER-NAME/ID NETWORK-NAME/ID",
-		chain+" leave", false)
-	cmd.Require(flag.Min, 2)
-	err := cmd.ParseFlags(args, true)
-	if err != nil {
-		return err
-	}
-	// TODO : Proper Backend handling
-	obj, _, err := readBody(cli.call("PUT", "/endpoints/", nil, nil))
-	if err != nil {
-		fmt.Fprintf(cli.err, "%s", err.Error())
-		return err
-	}
-	if _, err := io.Copy(cli.out, bytes.NewReader(obj)); err != nil {
-		return err
+	for _, cmd := range networkCommands {
+		help += fmt.Sprintf("    %-10.10s%s\n", cmd.name, cmd.description)
 	}
 	}
-	return nil
+
+	help += fmt.Sprintf("\nRun '%s network COMMAND --help' for more information on a command.", chain)
+	return help
 }
 }

+ 34 - 0
libnetwork/client/types.go

@@ -0,0 +1,34 @@
+package client
+
+import "github.com/docker/libnetwork/sandbox"
+
+/***********
+ Resources
+************/
+
+// networkResource is the body of the "get network" http response message
+type networkResource struct {
+	Name      string
+	ID        string
+	Type      string
+	Endpoints []*endpointResource
+}
+
+// endpointResource is the body of the "get endpoint" http response message
+type endpointResource struct {
+	Name    string
+	ID      string
+	Network string
+	Info    sandbox.Info
+}
+
+/***********
+  Body types
+  ************/
+
+// networkCreate is the expected body of the "create network" http request message
+type networkCreate struct {
+	Name        string
+	NetworkType string
+	Options     map[string]interface{}
+}