From 8644d00d3cf98505590ce2488cc06079f77da2f4 Mon Sep 17 00:00:00 2001 From: Madhu Venugopal Date: Sat, 16 May 2015 08:23:59 -0700 Subject: [PATCH] Client to make use of REST API Signed-off-by: Madhu Venugopal --- api/api.go | 70 ++++----------------------------------------- api/types.go | 68 ++++++++++++++++++++++++++++++++++++++++++++ client/network.go | 72 +++++++++-------------------------------------- client/types.go | 34 ++++++++++++++++++++++ 4 files changed, 122 insertions(+), 122 deletions(-) create mode 100644 api/types.go create mode 100644 client/types.go diff --git a/api/api.go b/api/api.go index 0b0c0b4..0eebf4d 100644 --- a/api/api.go +++ b/api/api.go @@ -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 diff --git a/api/types.go b/api/types.go new file mode 100644 index 0000000..1a0ac85 --- /dev/null +++ b/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 +} diff --git a/client/network.go b/client/network.go index 52bd9c8..d9effab 100644 --- a/client/network.go +++ b/client/network.go @@ -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 : network join [] -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 : network leave -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 } diff --git a/client/types.go b/client/types.go new file mode 100644 index 0000000..27d17b9 --- /dev/null +++ b/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{} +}