mirror of
https://github.com/moby/moby.git
synced 2022-11-09 12:21:53 -05:00
Client to make use of REST API
Signed-off-by: Madhu Venugopal <madhu@docker.com>
This commit is contained in:
parent
2d3758619b
commit
977fcdd952
4 changed files with 122 additions and 122 deletions
|
@ -7,7 +7,6 @@ import (
|
|||
"net/http"
|
||||
|
||||
"github.com/docker/libnetwork"
|
||||
"github.com/docker/libnetwork/netutils"
|
||||
"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 {
|
||||
r := &networkResource{}
|
||||
|
@ -170,51 +154,9 @@ func buildEndpointResource(ep libnetwork.Endpoint) *endpointResource {
|
|||
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 {
|
||||
var setFctList []libnetwork.EndpointOption
|
||||
|
|
68
libnetwork/api/types.go
Normal file
68
libnetwork/api/types.go
Normal file
|
@ -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
|
||||
}
|
|
@ -26,6 +26,7 @@ var (
|
|||
}
|
||||
)
|
||||
|
||||
// CmdNetwork handles the root Network UI
|
||||
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)
|
||||
|
@ -37,17 +38,6 @@ func (cli *NetworkCli) CmdNetwork(chain string, args ...string) error {
|
|||
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
|
||||
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)
|
||||
|
@ -60,8 +50,10 @@ func (cli *NetworkCli) CmdNetworkCreate(chain string, args ...string) error {
|
|||
if *flDriver == "" {
|
||||
*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 {
|
||||
fmt.Fprintf(cli.err, "%s", err.Error())
|
||||
return err
|
||||
|
@ -80,8 +72,7 @@ func (cli *NetworkCli) CmdNetworkRm(chain string, args ...string) error {
|
|||
if err != nil {
|
||||
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 {
|
||||
fmt.Fprintf(cli.err, "%s", err.Error())
|
||||
return err
|
||||
|
@ -99,7 +90,6 @@ func (cli *NetworkCli) CmdNetworkLs(chain string, args ...string) error {
|
|||
if err != nil {
|
||||
return err
|
||||
}
|
||||
// TODO : Proper Backend handling
|
||||
obj, _, err := readBody(cli.call("GET", "/networks", nil, nil))
|
||||
if err != nil {
|
||||
fmt.Fprintf(cli.err, "%s", err.Error())
|
||||
|
@ -119,8 +109,7 @@ func (cli *NetworkCli) CmdNetworkInfo(chain string, args ...string) error {
|
|||
if err != nil {
|
||||
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 {
|
||||
fmt.Fprintf(cli.err, "%s", err.Error())
|
||||
return err
|
||||
|
@ -131,46 +120,13 @@ func (cli *NetworkCli) CmdNetworkInfo(chain string, args ...string) error {
|
|||
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
|
||||
for _, cmd := range networkCommands {
|
||||
help += fmt.Sprintf(" %-10.10s%s\n", cmd.name, cmd.description)
|
||||
}
|
||||
// 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
|
||||
}
|
||||
return nil
|
||||
|
||||
help += fmt.Sprintf("\nRun '%s network COMMAND --help' for more information on a command.", chain)
|
||||
return help
|
||||
}
|
||||
|
|
34
libnetwork/client/types.go
Normal file
34
libnetwork/client/types.go
Normal file
|
@ -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{}
|
||||
}
|
Loading…
Add table
Reference in a new issue