2015-04-22 19:17:03 -04:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"net/http"
|
2015-05-21 14:58:28 -04:00
|
|
|
"strings"
|
2015-04-22 19:17:03 -04:00
|
|
|
|
|
|
|
"github.com/docker/libnetwork"
|
2015-05-14 17:56:15 -04:00
|
|
|
"github.com/docker/libnetwork/types"
|
2015-04-22 19:17:03 -04:00
|
|
|
"github.com/gorilla/mux"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
successResponse = responseStatus{Status: "Success", StatusCode: http.StatusOK}
|
|
|
|
createdResponse = responseStatus{Status: "Created", StatusCode: http.StatusCreated}
|
|
|
|
mismatchResponse = responseStatus{Status: "Body/URI parameter mismatch", StatusCode: http.StatusBadRequest}
|
2015-05-22 13:56:36 -04:00
|
|
|
badQueryResponse = responseStatus{Status: "Unsupported query", StatusCode: http.StatusBadRequest}
|
2015-04-22 19:17:03 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2015-05-19 16:04:16 -04:00
|
|
|
// Resource name regex
|
2015-06-16 03:13:54 -04:00
|
|
|
// Gorilla mux encloses the passed pattern with '^' and '$'. So we need to do some tricks
|
|
|
|
// to have mux eventually build a query regex which matches empty or word string (`^$|[\w]+`)
|
2015-05-19 16:04:16 -04:00
|
|
|
regex = "[a-zA-Z_0-9-]+"
|
2015-06-16 03:13:54 -04:00
|
|
|
qregx = "$|" + regex
|
2015-05-19 16:04:16 -04:00
|
|
|
// Router URL variable definition
|
2015-06-16 03:13:54 -04:00
|
|
|
nwName = "{" + urlNwName + ":" + regex + "}"
|
|
|
|
nwNameQr = "{" + urlNwName + ":" + qregx + "}"
|
|
|
|
nwID = "{" + urlNwID + ":" + regex + "}"
|
|
|
|
nwPIDQr = "{" + urlNwPID + ":" + qregx + "}"
|
|
|
|
epName = "{" + urlEpName + ":" + regex + "}"
|
|
|
|
epNameQr = "{" + urlEpName + ":" + qregx + "}"
|
|
|
|
epID = "{" + urlEpID + ":" + regex + "}"
|
|
|
|
epPIDQr = "{" + urlEpPID + ":" + qregx + "}"
|
2015-07-02 01:00:48 -04:00
|
|
|
sbID = "{" + urlSbID + ":" + regex + "}"
|
|
|
|
sbPIDQr = "{" + urlSbPID + ":" + qregx + "}"
|
|
|
|
cnIDQr = "{" + urlCnID + ":" + qregx + "}"
|
|
|
|
cnPIDQr = "{" + urlCnPID + ":" + qregx + "}"
|
2015-06-16 03:13:54 -04:00
|
|
|
|
|
|
|
// Internal URL variable name.They can be anything as
|
|
|
|
// long as they do not collide with query fields.
|
|
|
|
urlNwName = "network-name"
|
2015-05-19 16:04:16 -04:00
|
|
|
urlNwID = "network-id"
|
2015-05-21 14:58:28 -04:00
|
|
|
urlNwPID = "network-partial-id"
|
2015-04-22 19:17:03 -04:00
|
|
|
urlEpName = "endpoint-name"
|
|
|
|
urlEpID = "endpoint-id"
|
2015-05-21 14:58:28 -04:00
|
|
|
urlEpPID = "endpoint-partial-id"
|
2015-07-02 01:00:48 -04:00
|
|
|
urlSbID = "sandbox-id"
|
|
|
|
urlSbPID = "sandbox-partial-id"
|
2015-04-22 19:17:03 -04:00
|
|
|
urlCnID = "container-id"
|
2015-07-02 01:00:48 -04:00
|
|
|
urlCnPID = "container-partial-id"
|
2015-04-22 19:17:03 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
// NewHTTPHandler creates and initialize the HTTP handler to serve the requests for libnetwork
|
|
|
|
func NewHTTPHandler(c libnetwork.NetworkController) func(w http.ResponseWriter, req *http.Request) {
|
|
|
|
h := &httpHandler{c: c}
|
|
|
|
h.initRouter()
|
|
|
|
return h.handleRequest
|
|
|
|
}
|
|
|
|
|
|
|
|
type responseStatus struct {
|
|
|
|
Status string
|
|
|
|
StatusCode int
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *responseStatus) isOK() bool {
|
|
|
|
return r.StatusCode == http.StatusOK || r.StatusCode == http.StatusCreated
|
|
|
|
}
|
|
|
|
|
|
|
|
type processor func(c libnetwork.NetworkController, vars map[string]string, body []byte) (interface{}, *responseStatus)
|
|
|
|
|
|
|
|
type httpHandler struct {
|
|
|
|
c libnetwork.NetworkController
|
|
|
|
r *mux.Router
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *httpHandler) handleRequest(w http.ResponseWriter, req *http.Request) {
|
|
|
|
// Make sure the service is there
|
|
|
|
if h.c == nil {
|
|
|
|
http.Error(w, "NetworkController is not available", http.StatusServiceUnavailable)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get handler from router and execute it
|
|
|
|
h.r.ServeHTTP(w, req)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *httpHandler) initRouter() {
|
2015-05-19 16:04:16 -04:00
|
|
|
m := map[string][]struct {
|
|
|
|
url string
|
|
|
|
qrs []string
|
|
|
|
fct processor
|
|
|
|
}{
|
2015-04-22 19:17:03 -04:00
|
|
|
"GET": {
|
2015-05-19 16:04:16 -04:00
|
|
|
// Order matters
|
2015-06-16 03:13:54 -04:00
|
|
|
{"/networks", []string{"name", nwNameQr}, procGetNetworks},
|
|
|
|
{"/networks", []string{"partial-id", nwPIDQr}, procGetNetworks},
|
2015-05-19 16:04:16 -04:00
|
|
|
{"/networks", nil, procGetNetworks},
|
|
|
|
{"/networks/" + nwID, nil, procGetNetwork},
|
2015-06-16 03:13:54 -04:00
|
|
|
{"/networks/" + nwID + "/endpoints", []string{"name", epNameQr}, procGetEndpoints},
|
|
|
|
{"/networks/" + nwID + "/endpoints", []string{"partial-id", epPIDQr}, procGetEndpoints},
|
2015-05-22 10:53:41 -04:00
|
|
|
{"/networks/" + nwID + "/endpoints", nil, procGetEndpoints},
|
2015-05-19 16:04:16 -04:00
|
|
|
{"/networks/" + nwID + "/endpoints/" + epID, nil, procGetEndpoint},
|
2015-06-16 03:13:54 -04:00
|
|
|
{"/services", []string{"network", nwNameQr}, procGetServices},
|
|
|
|
{"/services", []string{"name", epNameQr}, procGetServices},
|
|
|
|
{"/services", []string{"partial-id", epPIDQr}, procGetServices},
|
2015-06-09 19:38:11 -04:00
|
|
|
{"/services", nil, procGetServices},
|
|
|
|
{"/services/" + epID, nil, procGetService},
|
2015-07-02 01:00:48 -04:00
|
|
|
{"/services/" + epID + "/backend", nil, procGetSandbox},
|
|
|
|
{"/sandboxes", []string{"partial-container-id", cnPIDQr}, procGetSandboxes},
|
|
|
|
{"/sandboxes", []string{"container-id", cnIDQr}, procGetSandboxes},
|
|
|
|
{"/sandboxes", []string{"partial-id", sbPIDQr}, procGetSandboxes},
|
|
|
|
{"/sandboxes", nil, procGetSandboxes},
|
|
|
|
{"/sandboxes/" + sbID, nil, procGetSandbox},
|
2015-04-22 19:17:03 -04:00
|
|
|
},
|
|
|
|
"POST": {
|
2015-05-19 16:04:16 -04:00
|
|
|
{"/networks", nil, procCreateNetwork},
|
|
|
|
{"/networks/" + nwID + "/endpoints", nil, procCreateEndpoint},
|
2015-07-02 01:00:48 -04:00
|
|
|
{"/networks/" + nwID + "/endpoints/" + epID + "/sandboxes", nil, procJoinEndpoint},
|
2015-06-09 19:38:11 -04:00
|
|
|
{"/services", nil, procPublishService},
|
|
|
|
{"/services/" + epID + "/backend", nil, procAttachBackend},
|
2015-07-02 01:00:48 -04:00
|
|
|
{"/sandboxes", nil, procCreateSandbox},
|
2015-04-22 19:17:03 -04:00
|
|
|
},
|
|
|
|
"DELETE": {
|
2015-05-19 16:04:16 -04:00
|
|
|
{"/networks/" + nwID, nil, procDeleteNetwork},
|
|
|
|
{"/networks/" + nwID + "/endpoints/" + epID, nil, procDeleteEndpoint},
|
2015-07-02 01:00:48 -04:00
|
|
|
{"/networks/" + nwID + "/endpoints/" + epID + "/sandboxes/" + sbID, nil, procLeaveEndpoint},
|
2015-06-09 19:38:11 -04:00
|
|
|
{"/services/" + epID, nil, procUnpublishService},
|
2015-07-02 01:00:48 -04:00
|
|
|
{"/services/" + epID + "/backend/" + sbID, nil, procDetachBackend},
|
|
|
|
{"/sandboxes/" + sbID, nil, procDeleteSandbox},
|
2015-04-22 19:17:03 -04:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
h.r = mux.NewRouter()
|
|
|
|
for method, routes := range m {
|
2015-05-19 16:04:16 -04:00
|
|
|
for _, route := range routes {
|
2015-05-23 15:20:30 -04:00
|
|
|
r := h.r.Path("/{.*}" + route.url).Methods(method).HandlerFunc(makeHandler(h.c, route.fct))
|
2015-05-19 16:04:16 -04:00
|
|
|
if route.qrs != nil {
|
|
|
|
r.Queries(route.qrs...)
|
|
|
|
}
|
2015-05-24 12:30:41 -04:00
|
|
|
|
|
|
|
r = h.r.Path(route.url).Methods(method).HandlerFunc(makeHandler(h.c, route.fct))
|
|
|
|
if route.qrs != nil {
|
|
|
|
r.Queries(route.qrs...)
|
|
|
|
}
|
2015-04-22 19:17:03 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func makeHandler(ctrl libnetwork.NetworkController, fct processor) http.HandlerFunc {
|
|
|
|
return func(w http.ResponseWriter, req *http.Request) {
|
|
|
|
var (
|
|
|
|
body []byte
|
|
|
|
err error
|
|
|
|
)
|
|
|
|
if req.Body != nil {
|
|
|
|
body, err = ioutil.ReadAll(req.Body)
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, "Invalid body: "+err.Error(), http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-16 03:13:54 -04:00
|
|
|
res, rsp := fct(ctrl, mux.Vars(req), body)
|
2015-09-02 19:12:19 -04:00
|
|
|
if !rsp.isOK() {
|
|
|
|
http.Error(w, rsp.Status, rsp.StatusCode)
|
|
|
|
return
|
|
|
|
}
|
2015-04-22 19:17:03 -04:00
|
|
|
if res != nil {
|
|
|
|
writeJSON(w, rsp.StatusCode, res)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-16 11:23:59 -04:00
|
|
|
/*****************
|
|
|
|
Resource Builders
|
|
|
|
******************/
|
2015-04-22 19:17:03 -04:00
|
|
|
|
|
|
|
func buildNetworkResource(nw libnetwork.Network) *networkResource {
|
|
|
|
r := &networkResource{}
|
|
|
|
if nw != nil {
|
|
|
|
r.Name = nw.Name()
|
|
|
|
r.ID = nw.ID()
|
|
|
|
r.Type = nw.Type()
|
|
|
|
epl := nw.Endpoints()
|
|
|
|
r.Endpoints = make([]*endpointResource, 0, len(epl))
|
|
|
|
for _, e := range epl {
|
|
|
|
epr := buildEndpointResource(e)
|
|
|
|
r.Endpoints = append(r.Endpoints, epr)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return r
|
|
|
|
}
|
|
|
|
|
|
|
|
func buildEndpointResource(ep libnetwork.Endpoint) *endpointResource {
|
|
|
|
r := &endpointResource{}
|
|
|
|
if ep != nil {
|
|
|
|
r.Name = ep.Name()
|
|
|
|
r.ID = ep.ID()
|
|
|
|
r.Network = ep.Network()
|
|
|
|
}
|
|
|
|
return r
|
|
|
|
}
|
|
|
|
|
2015-07-02 01:00:48 -04:00
|
|
|
func buildSandboxResource(sb libnetwork.Sandbox) *sandboxResource {
|
|
|
|
r := &sandboxResource{}
|
|
|
|
if sb != nil {
|
|
|
|
r.ID = sb.ID()
|
|
|
|
r.Key = sb.Key()
|
|
|
|
r.ContainerID = sb.ContainerID()
|
2015-06-11 11:30:45 -04:00
|
|
|
}
|
|
|
|
return r
|
|
|
|
}
|
|
|
|
|
2015-05-22 13:56:36 -04:00
|
|
|
/****************
|
|
|
|
Options Parsers
|
|
|
|
*****************/
|
|
|
|
|
2015-07-02 01:00:48 -04:00
|
|
|
func (sc *sandboxCreate) parseOptions() []libnetwork.SandboxOption {
|
|
|
|
var setFctList []libnetwork.SandboxOption
|
|
|
|
if sc.HostName != "" {
|
|
|
|
setFctList = append(setFctList, libnetwork.OptionHostname(sc.HostName))
|
2015-04-22 19:17:03 -04:00
|
|
|
}
|
2015-07-02 01:00:48 -04:00
|
|
|
if sc.DomainName != "" {
|
|
|
|
setFctList = append(setFctList, libnetwork.OptionDomainname(sc.DomainName))
|
2015-04-22 19:17:03 -04:00
|
|
|
}
|
2015-07-02 01:00:48 -04:00
|
|
|
if sc.HostsPath != "" {
|
|
|
|
setFctList = append(setFctList, libnetwork.OptionHostsPath(sc.HostsPath))
|
2015-04-22 19:17:03 -04:00
|
|
|
}
|
2015-07-02 01:00:48 -04:00
|
|
|
if sc.ResolvConfPath != "" {
|
|
|
|
setFctList = append(setFctList, libnetwork.OptionResolvConfPath(sc.ResolvConfPath))
|
2015-04-22 19:17:03 -04:00
|
|
|
}
|
2015-07-02 01:00:48 -04:00
|
|
|
if sc.UseDefaultSandbox {
|
|
|
|
setFctList = append(setFctList, libnetwork.OptionUseDefaultSandbox())
|
2015-04-22 19:17:03 -04:00
|
|
|
}
|
2015-09-09 19:20:54 -04:00
|
|
|
if sc.UseExternalKey {
|
|
|
|
setFctList = append(setFctList, libnetwork.OptionUseExternalKey())
|
|
|
|
}
|
2015-07-02 01:00:48 -04:00
|
|
|
if sc.DNS != nil {
|
|
|
|
for _, d := range sc.DNS {
|
|
|
|
setFctList = append(setFctList, libnetwork.OptionDNS(d))
|
2015-04-22 19:17:03 -04:00
|
|
|
}
|
|
|
|
}
|
2015-07-02 01:00:48 -04:00
|
|
|
if sc.ExtraHosts != nil {
|
|
|
|
for _, e := range sc.ExtraHosts {
|
|
|
|
setFctList = append(setFctList, libnetwork.OptionExtraHost(e.Name, e.Address))
|
2015-04-22 19:17:03 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return setFctList
|
|
|
|
}
|
|
|
|
|
2015-07-02 01:00:48 -04:00
|
|
|
func (ej *endpointJoin) parseOptions() []libnetwork.EndpointOption {
|
|
|
|
// priority will go here
|
|
|
|
return []libnetwork.EndpointOption{}
|
|
|
|
}
|
|
|
|
|
2015-04-22 19:17:03 -04:00
|
|
|
/******************
|
|
|
|
Process functions
|
|
|
|
*******************/
|
|
|
|
|
2015-06-14 12:00:27 -04:00
|
|
|
func processCreateDefaults(c libnetwork.NetworkController, nc *networkCreate) {
|
|
|
|
if nc.NetworkType == "" {
|
|
|
|
nc.NetworkType = c.Config().Daemon.DefaultDriver
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-22 19:17:03 -04:00
|
|
|
/***************************
|
|
|
|
NetworkController interface
|
|
|
|
****************************/
|
|
|
|
func procCreateNetwork(c libnetwork.NetworkController, vars map[string]string, body []byte) (interface{}, *responseStatus) {
|
|
|
|
var create networkCreate
|
|
|
|
|
|
|
|
err := json.Unmarshal(body, &create)
|
|
|
|
if err != nil {
|
|
|
|
return "", &responseStatus{Status: "Invalid body: " + err.Error(), StatusCode: http.StatusBadRequest}
|
|
|
|
}
|
2015-06-14 12:00:27 -04:00
|
|
|
processCreateDefaults(c, &create)
|
2015-04-22 19:17:03 -04:00
|
|
|
|
2015-10-09 11:59:40 -04:00
|
|
|
options := []libnetwork.NetworkOption{}
|
2015-10-14 19:38:46 -04:00
|
|
|
if len(create.DriverOpts) > 0 {
|
|
|
|
options = append(options, libnetwork.NetworkOptionDriverOpts(create.DriverOpts))
|
2015-10-09 11:59:40 -04:00
|
|
|
}
|
|
|
|
nw, err := c.NewNetwork(create.NetworkType, create.Name, options...)
|
2015-04-22 19:17:03 -04:00
|
|
|
if err != nil {
|
|
|
|
return "", convertNetworkError(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nw.ID(), &createdResponse
|
|
|
|
}
|
|
|
|
|
|
|
|
func procGetNetwork(c libnetwork.NetworkController, vars map[string]string, body []byte) (interface{}, *responseStatus) {
|
|
|
|
t, by := detectNetworkTarget(vars)
|
|
|
|
nw, errRsp := findNetwork(c, t, by)
|
|
|
|
if !errRsp.isOK() {
|
|
|
|
return nil, errRsp
|
|
|
|
}
|
|
|
|
return buildNetworkResource(nw), &successResponse
|
|
|
|
}
|
|
|
|
|
|
|
|
func procGetNetworks(c libnetwork.NetworkController, vars map[string]string, body []byte) (interface{}, *responseStatus) {
|
|
|
|
var list []*networkResource
|
2015-05-19 16:04:16 -04:00
|
|
|
|
2015-05-21 14:58:28 -04:00
|
|
|
// Look for query filters and validate
|
|
|
|
name, queryByName := vars[urlNwName]
|
|
|
|
shortID, queryByPid := vars[urlNwPID]
|
|
|
|
if queryByName && queryByPid {
|
2015-05-22 13:56:36 -04:00
|
|
|
return nil, &badQueryResponse
|
2015-05-21 14:58:28 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if queryByName {
|
|
|
|
if nw, errRsp := findNetwork(c, name, byName); errRsp.isOK() {
|
2015-05-19 16:04:16 -04:00
|
|
|
list = append(list, buildNetworkResource(nw))
|
|
|
|
}
|
2015-05-21 14:58:28 -04:00
|
|
|
} else if queryByPid {
|
|
|
|
// Return all the prefix-matching networks
|
|
|
|
l := func(nw libnetwork.Network) bool {
|
|
|
|
if strings.HasPrefix(nw.ID(), shortID) {
|
|
|
|
list = append(list, buildNetworkResource(nw))
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
c.WalkNetworks(l)
|
2015-05-19 16:04:16 -04:00
|
|
|
} else {
|
|
|
|
for _, nw := range c.Networks() {
|
|
|
|
list = append(list, buildNetworkResource(nw))
|
|
|
|
}
|
2015-04-22 19:17:03 -04:00
|
|
|
}
|
2015-05-19 16:04:16 -04:00
|
|
|
|
2015-04-22 19:17:03 -04:00
|
|
|
return list, &successResponse
|
|
|
|
}
|
|
|
|
|
2015-07-02 01:00:48 -04:00
|
|
|
func procCreateSandbox(c libnetwork.NetworkController, vars map[string]string, body []byte) (interface{}, *responseStatus) {
|
|
|
|
var create sandboxCreate
|
|
|
|
|
|
|
|
err := json.Unmarshal(body, &create)
|
|
|
|
if err != nil {
|
|
|
|
return "", &responseStatus{Status: "Invalid body: " + err.Error(), StatusCode: http.StatusBadRequest}
|
|
|
|
}
|
|
|
|
|
|
|
|
sb, err := c.NewSandbox(create.ContainerID, create.parseOptions()...)
|
|
|
|
if err != nil {
|
|
|
|
return "", convertNetworkError(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return sb.ID(), &createdResponse
|
|
|
|
}
|
|
|
|
|
2015-04-22 19:17:03 -04:00
|
|
|
/******************
|
|
|
|
Network interface
|
|
|
|
*******************/
|
|
|
|
func procCreateEndpoint(c libnetwork.NetworkController, vars map[string]string, body []byte) (interface{}, *responseStatus) {
|
|
|
|
var ec endpointCreate
|
|
|
|
|
|
|
|
err := json.Unmarshal(body, &ec)
|
|
|
|
if err != nil {
|
|
|
|
return "", &responseStatus{Status: "Invalid body: " + err.Error(), StatusCode: http.StatusBadRequest}
|
|
|
|
}
|
|
|
|
|
|
|
|
nwT, nwBy := detectNetworkTarget(vars)
|
|
|
|
n, errRsp := findNetwork(c, nwT, nwBy)
|
|
|
|
if !errRsp.isOK() {
|
|
|
|
return "", errRsp
|
|
|
|
}
|
|
|
|
|
|
|
|
var setFctList []libnetwork.EndpointOption
|
|
|
|
if ec.ExposedPorts != nil {
|
|
|
|
setFctList = append(setFctList, libnetwork.CreateOptionExposedPorts(ec.ExposedPorts))
|
|
|
|
}
|
|
|
|
if ec.PortMapping != nil {
|
|
|
|
setFctList = append(setFctList, libnetwork.CreateOptionPortMapping(ec.PortMapping))
|
|
|
|
}
|
|
|
|
|
2015-05-19 16:04:16 -04:00
|
|
|
ep, err := n.CreateEndpoint(ec.Name, setFctList...)
|
2015-04-22 19:17:03 -04:00
|
|
|
if err != nil {
|
|
|
|
return "", convertNetworkError(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return ep.ID(), &createdResponse
|
|
|
|
}
|
|
|
|
|
|
|
|
func procGetEndpoint(c libnetwork.NetworkController, vars map[string]string, body []byte) (interface{}, *responseStatus) {
|
|
|
|
nwT, nwBy := detectNetworkTarget(vars)
|
|
|
|
epT, epBy := detectEndpointTarget(vars)
|
|
|
|
|
|
|
|
ep, errRsp := findEndpoint(c, nwT, epT, nwBy, epBy)
|
|
|
|
if !errRsp.isOK() {
|
|
|
|
return nil, errRsp
|
|
|
|
}
|
|
|
|
|
|
|
|
return buildEndpointResource(ep), &successResponse
|
|
|
|
}
|
|
|
|
|
|
|
|
func procGetEndpoints(c libnetwork.NetworkController, vars map[string]string, body []byte) (interface{}, *responseStatus) {
|
2015-05-21 14:58:28 -04:00
|
|
|
// Look for query filters and validate
|
|
|
|
name, queryByName := vars[urlEpName]
|
|
|
|
shortID, queryByPid := vars[urlEpPID]
|
|
|
|
if queryByName && queryByPid {
|
2015-05-22 13:56:36 -04:00
|
|
|
return nil, &badQueryResponse
|
2015-05-21 14:58:28 -04:00
|
|
|
}
|
|
|
|
|
2015-05-19 16:04:16 -04:00
|
|
|
nwT, nwBy := detectNetworkTarget(vars)
|
|
|
|
nw, errRsp := findNetwork(c, nwT, nwBy)
|
2015-04-22 19:17:03 -04:00
|
|
|
if !errRsp.isOK() {
|
|
|
|
return nil, errRsp
|
|
|
|
}
|
|
|
|
|
|
|
|
var list []*endpointResource
|
2015-05-19 16:04:16 -04:00
|
|
|
|
|
|
|
// If query parameter is specified, return a filtered collection
|
2015-05-21 14:58:28 -04:00
|
|
|
if queryByName {
|
|
|
|
if ep, errRsp := findEndpoint(c, nwT, name, nwBy, byName); errRsp.isOK() {
|
2015-05-19 16:04:16 -04:00
|
|
|
list = append(list, buildEndpointResource(ep))
|
|
|
|
}
|
2015-05-21 14:58:28 -04:00
|
|
|
} else if queryByPid {
|
2015-06-09 19:38:11 -04:00
|
|
|
// Return all the prefix-matching endpoints
|
2015-05-21 14:58:28 -04:00
|
|
|
l := func(ep libnetwork.Endpoint) bool {
|
|
|
|
if strings.HasPrefix(ep.ID(), shortID) {
|
|
|
|
list = append(list, buildEndpointResource(ep))
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
nw.WalkEndpoints(l)
|
2015-05-19 16:04:16 -04:00
|
|
|
} else {
|
|
|
|
for _, ep := range nw.Endpoints() {
|
|
|
|
epr := buildEndpointResource(ep)
|
|
|
|
list = append(list, epr)
|
|
|
|
}
|
2015-04-22 19:17:03 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return list, &successResponse
|
|
|
|
}
|
|
|
|
|
|
|
|
func procDeleteNetwork(c libnetwork.NetworkController, vars map[string]string, body []byte) (interface{}, *responseStatus) {
|
|
|
|
target, by := detectNetworkTarget(vars)
|
|
|
|
|
|
|
|
nw, errRsp := findNetwork(c, target, by)
|
|
|
|
if !errRsp.isOK() {
|
|
|
|
return nil, errRsp
|
|
|
|
}
|
|
|
|
|
|
|
|
err := nw.Delete()
|
|
|
|
if err != nil {
|
|
|
|
return nil, convertNetworkError(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil, &successResponse
|
|
|
|
}
|
|
|
|
|
|
|
|
/******************
|
|
|
|
Endpoint interface
|
|
|
|
*******************/
|
|
|
|
func procJoinEndpoint(c libnetwork.NetworkController, vars map[string]string, body []byte) (interface{}, *responseStatus) {
|
|
|
|
var ej endpointJoin
|
|
|
|
err := json.Unmarshal(body, &ej)
|
|
|
|
if err != nil {
|
|
|
|
return nil, &responseStatus{Status: "Invalid body: " + err.Error(), StatusCode: http.StatusBadRequest}
|
|
|
|
}
|
|
|
|
|
|
|
|
nwT, nwBy := detectNetworkTarget(vars)
|
|
|
|
epT, epBy := detectEndpointTarget(vars)
|
|
|
|
|
|
|
|
ep, errRsp := findEndpoint(c, nwT, epT, nwBy, epBy)
|
|
|
|
if !errRsp.isOK() {
|
|
|
|
return nil, errRsp
|
|
|
|
}
|
|
|
|
|
2015-07-02 01:00:48 -04:00
|
|
|
sb, errRsp := findSandbox(c, ej.SandboxID, byID)
|
|
|
|
if !errRsp.isOK() {
|
|
|
|
return nil, errRsp
|
|
|
|
}
|
|
|
|
|
|
|
|
err = ep.Join(sb)
|
2015-04-22 19:17:03 -04:00
|
|
|
if err != nil {
|
|
|
|
return nil, convertNetworkError(err)
|
|
|
|
}
|
2015-07-02 01:00:48 -04:00
|
|
|
return sb.Key(), &successResponse
|
2015-04-22 19:17:03 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func procLeaveEndpoint(c libnetwork.NetworkController, vars map[string]string, body []byte) (interface{}, *responseStatus) {
|
|
|
|
nwT, nwBy := detectNetworkTarget(vars)
|
|
|
|
epT, epBy := detectEndpointTarget(vars)
|
|
|
|
|
|
|
|
ep, errRsp := findEndpoint(c, nwT, epT, nwBy, epBy)
|
|
|
|
if !errRsp.isOK() {
|
|
|
|
return nil, errRsp
|
|
|
|
}
|
|
|
|
|
2015-07-02 01:00:48 -04:00
|
|
|
sb, errRsp := findSandbox(c, vars[urlSbID], byID)
|
|
|
|
if !errRsp.isOK() {
|
|
|
|
return nil, errRsp
|
|
|
|
}
|
|
|
|
|
|
|
|
err := ep.Leave(sb)
|
2015-04-22 19:17:03 -04:00
|
|
|
if err != nil {
|
|
|
|
return nil, convertNetworkError(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil, &successResponse
|
|
|
|
}
|
|
|
|
|
|
|
|
func procDeleteEndpoint(c libnetwork.NetworkController, vars map[string]string, body []byte) (interface{}, *responseStatus) {
|
|
|
|
nwT, nwBy := detectNetworkTarget(vars)
|
|
|
|
epT, epBy := detectEndpointTarget(vars)
|
|
|
|
|
|
|
|
ep, errRsp := findEndpoint(c, nwT, epT, nwBy, epBy)
|
|
|
|
if !errRsp.isOK() {
|
|
|
|
return nil, errRsp
|
|
|
|
}
|
|
|
|
|
|
|
|
err := ep.Delete()
|
|
|
|
if err != nil {
|
|
|
|
return nil, convertNetworkError(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil, &successResponse
|
|
|
|
}
|
|
|
|
|
2015-06-09 19:38:11 -04:00
|
|
|
/******************
|
|
|
|
Service interface
|
|
|
|
*******************/
|
|
|
|
func procGetServices(c libnetwork.NetworkController, vars map[string]string, body []byte) (interface{}, *responseStatus) {
|
|
|
|
// Look for query filters and validate
|
|
|
|
nwName, filterByNwName := vars[urlNwName]
|
|
|
|
svName, queryBySvName := vars[urlEpName]
|
|
|
|
shortID, queryBySvPID := vars[urlEpPID]
|
|
|
|
|
|
|
|
if filterByNwName && queryBySvName || filterByNwName && queryBySvPID || queryBySvName && queryBySvPID {
|
|
|
|
return nil, &badQueryResponse
|
|
|
|
}
|
|
|
|
|
|
|
|
var list []*endpointResource
|
|
|
|
|
|
|
|
switch {
|
|
|
|
case filterByNwName:
|
|
|
|
// return all service present on the specified network
|
|
|
|
nw, errRsp := findNetwork(c, nwName, byName)
|
|
|
|
if !errRsp.isOK() {
|
|
|
|
return list, &successResponse
|
|
|
|
}
|
|
|
|
for _, ep := range nw.Endpoints() {
|
|
|
|
epr := buildEndpointResource(ep)
|
|
|
|
list = append(list, epr)
|
|
|
|
}
|
|
|
|
case queryBySvName:
|
|
|
|
// Look in each network for the service with the specified name
|
|
|
|
l := func(ep libnetwork.Endpoint) bool {
|
|
|
|
if ep.Name() == svName {
|
|
|
|
list = append(list, buildEndpointResource(ep))
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
for _, nw := range c.Networks() {
|
|
|
|
nw.WalkEndpoints(l)
|
|
|
|
}
|
|
|
|
case queryBySvPID:
|
|
|
|
// Return all the prefix-matching services
|
|
|
|
l := func(ep libnetwork.Endpoint) bool {
|
|
|
|
if strings.HasPrefix(ep.ID(), shortID) {
|
|
|
|
list = append(list, buildEndpointResource(ep))
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
for _, nw := range c.Networks() {
|
|
|
|
nw.WalkEndpoints(l)
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
for _, nw := range c.Networks() {
|
|
|
|
for _, ep := range nw.Endpoints() {
|
|
|
|
epr := buildEndpointResource(ep)
|
|
|
|
list = append(list, epr)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return list, &successResponse
|
|
|
|
}
|
|
|
|
|
|
|
|
func procGetService(c libnetwork.NetworkController, vars map[string]string, body []byte) (interface{}, *responseStatus) {
|
|
|
|
epT, epBy := detectEndpointTarget(vars)
|
|
|
|
sv, errRsp := findService(c, epT, epBy)
|
|
|
|
if !errRsp.isOK() {
|
|
|
|
return nil, endpointToService(errRsp)
|
|
|
|
}
|
|
|
|
return buildEndpointResource(sv), &successResponse
|
|
|
|
}
|
|
|
|
|
|
|
|
func procPublishService(c libnetwork.NetworkController, vars map[string]string, body []byte) (interface{}, *responseStatus) {
|
|
|
|
var sp servicePublish
|
|
|
|
|
|
|
|
err := json.Unmarshal(body, &sp)
|
|
|
|
if err != nil {
|
|
|
|
return "", &responseStatus{Status: "Invalid body: " + err.Error(), StatusCode: http.StatusBadRequest}
|
|
|
|
}
|
|
|
|
|
|
|
|
n, errRsp := findNetwork(c, sp.Network, byName)
|
|
|
|
if !errRsp.isOK() {
|
|
|
|
return "", errRsp
|
|
|
|
}
|
|
|
|
|
|
|
|
var setFctList []libnetwork.EndpointOption
|
|
|
|
if sp.ExposedPorts != nil {
|
|
|
|
setFctList = append(setFctList, libnetwork.CreateOptionExposedPorts(sp.ExposedPorts))
|
|
|
|
}
|
|
|
|
if sp.PortMapping != nil {
|
|
|
|
setFctList = append(setFctList, libnetwork.CreateOptionPortMapping(sp.PortMapping))
|
|
|
|
}
|
|
|
|
|
|
|
|
ep, err := n.CreateEndpoint(sp.Name, setFctList...)
|
|
|
|
if err != nil {
|
|
|
|
return "", endpointToService(convertNetworkError(err))
|
|
|
|
}
|
|
|
|
|
|
|
|
return ep.ID(), &createdResponse
|
|
|
|
}
|
|
|
|
|
|
|
|
func procUnpublishService(c libnetwork.NetworkController, vars map[string]string, body []byte) (interface{}, *responseStatus) {
|
|
|
|
epT, epBy := detectEndpointTarget(vars)
|
|
|
|
sv, errRsp := findService(c, epT, epBy)
|
|
|
|
if !errRsp.isOK() {
|
|
|
|
return nil, errRsp
|
|
|
|
}
|
|
|
|
err := sv.Delete()
|
|
|
|
if err != nil {
|
|
|
|
return nil, endpointToService(convertNetworkError(err))
|
|
|
|
}
|
|
|
|
return nil, &successResponse
|
|
|
|
}
|
|
|
|
|
|
|
|
func procAttachBackend(c libnetwork.NetworkController, vars map[string]string, body []byte) (interface{}, *responseStatus) {
|
|
|
|
var bk endpointJoin
|
|
|
|
err := json.Unmarshal(body, &bk)
|
|
|
|
if err != nil {
|
|
|
|
return nil, &responseStatus{Status: "Invalid body: " + err.Error(), StatusCode: http.StatusBadRequest}
|
|
|
|
}
|
|
|
|
|
|
|
|
epT, epBy := detectEndpointTarget(vars)
|
|
|
|
sv, errRsp := findService(c, epT, epBy)
|
|
|
|
if !errRsp.isOK() {
|
|
|
|
return nil, errRsp
|
|
|
|
}
|
|
|
|
|
2015-07-02 01:00:48 -04:00
|
|
|
sb, errRsp := findSandbox(c, bk.SandboxID, byID)
|
|
|
|
if !errRsp.isOK() {
|
|
|
|
return nil, errRsp
|
|
|
|
}
|
|
|
|
|
|
|
|
err = sv.Join(sb)
|
2015-06-09 19:38:11 -04:00
|
|
|
if err != nil {
|
|
|
|
return nil, convertNetworkError(err)
|
|
|
|
}
|
2015-07-02 01:00:48 -04:00
|
|
|
return sb.Key(), &successResponse
|
2015-06-09 19:38:11 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func procDetachBackend(c libnetwork.NetworkController, vars map[string]string, body []byte) (interface{}, *responseStatus) {
|
|
|
|
epT, epBy := detectEndpointTarget(vars)
|
|
|
|
sv, errRsp := findService(c, epT, epBy)
|
|
|
|
if !errRsp.isOK() {
|
|
|
|
return nil, errRsp
|
|
|
|
}
|
|
|
|
|
2015-07-02 01:00:48 -04:00
|
|
|
sb, errRsp := findSandbox(c, vars[urlSbID], byID)
|
|
|
|
if !errRsp.isOK() {
|
|
|
|
return nil, errRsp
|
|
|
|
}
|
|
|
|
|
|
|
|
err := sv.Leave(sb)
|
|
|
|
if err != nil {
|
|
|
|
return nil, convertNetworkError(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil, &successResponse
|
|
|
|
}
|
|
|
|
|
|
|
|
/******************
|
|
|
|
Sandbox interface
|
|
|
|
*******************/
|
|
|
|
func procGetSandbox(c libnetwork.NetworkController, vars map[string]string, body []byte) (interface{}, *responseStatus) {
|
|
|
|
if epT, ok := vars[urlEpID]; ok {
|
|
|
|
sv, errRsp := findService(c, epT, byID)
|
|
|
|
if !errRsp.isOK() {
|
|
|
|
return nil, endpointToService(errRsp)
|
|
|
|
}
|
|
|
|
return buildSandboxResource(sv.Info().Sandbox()), &successResponse
|
|
|
|
}
|
|
|
|
|
|
|
|
sbT, by := detectSandboxTarget(vars)
|
|
|
|
sb, errRsp := findSandbox(c, sbT, by)
|
|
|
|
if !errRsp.isOK() {
|
|
|
|
return nil, errRsp
|
|
|
|
}
|
|
|
|
return buildSandboxResource(sb), &successResponse
|
|
|
|
}
|
|
|
|
|
|
|
|
type cndFnMkr func(string) cndFn
|
|
|
|
type cndFn func(libnetwork.Sandbox) bool
|
|
|
|
|
|
|
|
// list of (query type, condition function makers) couples
|
|
|
|
var cndMkrList = []struct {
|
|
|
|
identifier string
|
|
|
|
maker cndFnMkr
|
|
|
|
}{
|
|
|
|
{urlSbPID, func(id string) cndFn {
|
|
|
|
return func(sb libnetwork.Sandbox) bool { return strings.HasPrefix(sb.ID(), id) }
|
|
|
|
}},
|
|
|
|
{urlCnID, func(id string) cndFn {
|
|
|
|
return func(sb libnetwork.Sandbox) bool { return sb.ContainerID() == id }
|
|
|
|
}},
|
|
|
|
{urlCnPID, func(id string) cndFn {
|
|
|
|
return func(sb libnetwork.Sandbox) bool { return strings.HasPrefix(sb.ContainerID(), id) }
|
|
|
|
}},
|
|
|
|
}
|
|
|
|
|
|
|
|
func getQueryCondition(vars map[string]string) func(libnetwork.Sandbox) bool {
|
|
|
|
for _, im := range cndMkrList {
|
|
|
|
if val, ok := vars[im.identifier]; ok {
|
|
|
|
return im.maker(val)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return func(sb libnetwork.Sandbox) bool { return true }
|
|
|
|
}
|
|
|
|
|
|
|
|
func sandboxWalker(condition cndFn, list *[]*sandboxResource) libnetwork.SandboxWalker {
|
|
|
|
return func(sb libnetwork.Sandbox) bool {
|
|
|
|
if condition(sb) {
|
|
|
|
*list = append(*list, buildSandboxResource(sb))
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func procGetSandboxes(c libnetwork.NetworkController, vars map[string]string, body []byte) (interface{}, *responseStatus) {
|
|
|
|
var list []*sandboxResource
|
|
|
|
|
|
|
|
cnd := getQueryCondition(vars)
|
|
|
|
c.WalkSandboxes(sandboxWalker(cnd, &list))
|
|
|
|
|
|
|
|
return list, &successResponse
|
|
|
|
}
|
|
|
|
|
|
|
|
func procDeleteSandbox(c libnetwork.NetworkController, vars map[string]string, body []byte) (interface{}, *responseStatus) {
|
|
|
|
sbT, by := detectSandboxTarget(vars)
|
|
|
|
|
|
|
|
sb, errRsp := findSandbox(c, sbT, by)
|
|
|
|
if !errRsp.isOK() {
|
|
|
|
return nil, errRsp
|
|
|
|
}
|
|
|
|
|
|
|
|
err := sb.Delete()
|
2015-06-09 19:38:11 -04:00
|
|
|
if err != nil {
|
|
|
|
return nil, convertNetworkError(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil, &successResponse
|
|
|
|
}
|
|
|
|
|
2015-04-22 19:17:03 -04:00
|
|
|
/***********
|
|
|
|
Utilities
|
|
|
|
************/
|
|
|
|
const (
|
|
|
|
byID = iota
|
|
|
|
byName
|
|
|
|
)
|
|
|
|
|
|
|
|
func detectNetworkTarget(vars map[string]string) (string, int) {
|
|
|
|
if target, ok := vars[urlNwName]; ok {
|
|
|
|
return target, byName
|
|
|
|
}
|
|
|
|
if target, ok := vars[urlNwID]; ok {
|
|
|
|
return target, byID
|
|
|
|
}
|
|
|
|
// vars are populated from the URL, following cannot happen
|
|
|
|
panic("Missing URL variable parameter for network")
|
|
|
|
}
|
|
|
|
|
2015-07-02 01:00:48 -04:00
|
|
|
func detectSandboxTarget(vars map[string]string) (string, int) {
|
|
|
|
if target, ok := vars[urlSbID]; ok {
|
|
|
|
return target, byID
|
|
|
|
}
|
|
|
|
// vars are populated from the URL, following cannot happen
|
|
|
|
panic("Missing URL variable parameter for sandbox")
|
|
|
|
}
|
|
|
|
|
2015-04-22 19:17:03 -04:00
|
|
|
func detectEndpointTarget(vars map[string]string) (string, int) {
|
|
|
|
if target, ok := vars[urlEpName]; ok {
|
|
|
|
return target, byName
|
|
|
|
}
|
|
|
|
if target, ok := vars[urlEpID]; ok {
|
|
|
|
return target, byID
|
|
|
|
}
|
|
|
|
// vars are populated from the URL, following cannot happen
|
|
|
|
panic("Missing URL variable parameter for endpoint")
|
|
|
|
}
|
|
|
|
|
|
|
|
func findNetwork(c libnetwork.NetworkController, s string, by int) (libnetwork.Network, *responseStatus) {
|
|
|
|
var (
|
|
|
|
nw libnetwork.Network
|
|
|
|
err error
|
|
|
|
)
|
|
|
|
switch by {
|
|
|
|
case byID:
|
|
|
|
nw, err = c.NetworkByID(s)
|
|
|
|
case byName:
|
2015-06-14 12:00:27 -04:00
|
|
|
if s == "" {
|
|
|
|
s = c.Config().Daemon.DefaultNetwork
|
|
|
|
}
|
2015-04-22 19:17:03 -04:00
|
|
|
nw, err = c.NetworkByName(s)
|
|
|
|
default:
|
|
|
|
panic(fmt.Sprintf("unexpected selector for network search: %d", by))
|
|
|
|
}
|
|
|
|
if err != nil {
|
2015-06-09 19:38:11 -04:00
|
|
|
if _, ok := err.(types.NotFoundError); ok {
|
2015-05-15 19:04:09 -04:00
|
|
|
return nil, &responseStatus{Status: "Resource not found: Network", StatusCode: http.StatusNotFound}
|
|
|
|
}
|
2015-04-22 19:17:03 -04:00
|
|
|
return nil, &responseStatus{Status: err.Error(), StatusCode: http.StatusBadRequest}
|
|
|
|
}
|
|
|
|
return nw, &successResponse
|
|
|
|
}
|
|
|
|
|
2015-07-02 01:00:48 -04:00
|
|
|
func findSandbox(c libnetwork.NetworkController, s string, by int) (libnetwork.Sandbox, *responseStatus) {
|
|
|
|
var (
|
|
|
|
sb libnetwork.Sandbox
|
|
|
|
err error
|
|
|
|
)
|
|
|
|
|
|
|
|
switch by {
|
|
|
|
case byID:
|
|
|
|
sb, err = c.SandboxByID(s)
|
|
|
|
default:
|
|
|
|
panic(fmt.Sprintf("unexpected selector for sandbox search: %d", by))
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
if _, ok := err.(types.NotFoundError); ok {
|
|
|
|
return nil, &responseStatus{Status: "Resource not found: Sandbox", StatusCode: http.StatusNotFound}
|
|
|
|
}
|
|
|
|
return nil, &responseStatus{Status: err.Error(), StatusCode: http.StatusBadRequest}
|
|
|
|
}
|
|
|
|
return sb, &successResponse
|
|
|
|
}
|
|
|
|
|
2015-04-22 19:17:03 -04:00
|
|
|
func findEndpoint(c libnetwork.NetworkController, ns, es string, nwBy, epBy int) (libnetwork.Endpoint, *responseStatus) {
|
|
|
|
nw, errRsp := findNetwork(c, ns, nwBy)
|
|
|
|
if !errRsp.isOK() {
|
|
|
|
return nil, errRsp
|
|
|
|
}
|
|
|
|
var (
|
|
|
|
err error
|
|
|
|
ep libnetwork.Endpoint
|
|
|
|
)
|
|
|
|
switch epBy {
|
|
|
|
case byID:
|
|
|
|
ep, err = nw.EndpointByID(es)
|
|
|
|
case byName:
|
|
|
|
ep, err = nw.EndpointByName(es)
|
|
|
|
default:
|
|
|
|
panic(fmt.Sprintf("unexpected selector for endpoint search: %d", epBy))
|
|
|
|
}
|
|
|
|
if err != nil {
|
2015-06-09 19:38:11 -04:00
|
|
|
if _, ok := err.(types.NotFoundError); ok {
|
2015-05-15 19:04:09 -04:00
|
|
|
return nil, &responseStatus{Status: "Resource not found: Endpoint", StatusCode: http.StatusNotFound}
|
|
|
|
}
|
2015-04-22 19:17:03 -04:00
|
|
|
return nil, &responseStatus{Status: err.Error(), StatusCode: http.StatusBadRequest}
|
|
|
|
}
|
|
|
|
return ep, &successResponse
|
|
|
|
}
|
|
|
|
|
2015-06-09 19:38:11 -04:00
|
|
|
func findService(c libnetwork.NetworkController, svs string, svBy int) (libnetwork.Endpoint, *responseStatus) {
|
|
|
|
for _, nw := range c.Networks() {
|
|
|
|
var (
|
|
|
|
ep libnetwork.Endpoint
|
|
|
|
err error
|
|
|
|
)
|
|
|
|
switch svBy {
|
|
|
|
case byID:
|
|
|
|
ep, err = nw.EndpointByID(svs)
|
|
|
|
case byName:
|
|
|
|
ep, err = nw.EndpointByName(svs)
|
|
|
|
default:
|
|
|
|
panic(fmt.Sprintf("unexpected selector for service search: %d", svBy))
|
|
|
|
}
|
|
|
|
if err == nil {
|
|
|
|
return ep, &successResponse
|
|
|
|
} else if _, ok := err.(types.NotFoundError); !ok {
|
|
|
|
return nil, convertNetworkError(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil, &responseStatus{Status: "Service not found", StatusCode: http.StatusNotFound}
|
|
|
|
}
|
|
|
|
|
|
|
|
func endpointToService(rsp *responseStatus) *responseStatus {
|
|
|
|
rsp.Status = strings.Replace(rsp.Status, "endpoint", "service", -1)
|
|
|
|
return rsp
|
|
|
|
}
|
|
|
|
|
2015-04-22 19:17:03 -04:00
|
|
|
func convertNetworkError(err error) *responseStatus {
|
2015-05-14 17:56:15 -04:00
|
|
|
var code int
|
|
|
|
switch err.(type) {
|
|
|
|
case types.BadRequestError:
|
|
|
|
code = http.StatusBadRequest
|
|
|
|
case types.ForbiddenError:
|
|
|
|
code = http.StatusForbidden
|
|
|
|
case types.NotFoundError:
|
|
|
|
code = http.StatusNotFound
|
|
|
|
case types.TimeoutError:
|
|
|
|
code = http.StatusRequestTimeout
|
|
|
|
case types.NotImplementedError:
|
|
|
|
code = http.StatusNotImplemented
|
|
|
|
case types.NoServiceError:
|
|
|
|
code = http.StatusServiceUnavailable
|
|
|
|
case types.InternalError:
|
|
|
|
code = http.StatusInternalServerError
|
|
|
|
default:
|
|
|
|
code = http.StatusInternalServerError
|
|
|
|
}
|
|
|
|
return &responseStatus{Status: err.Error(), StatusCode: code}
|
2015-04-22 19:17:03 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func writeJSON(w http.ResponseWriter, code int, v interface{}) error {
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
|
|
w.WriteHeader(code)
|
|
|
|
return json.NewEncoder(w).Encode(v)
|
|
|
|
}
|