mirror of
https://github.com/moby/moby.git
synced 2026-08-05 15:40:54 +00:00
@@ -91,16 +91,26 @@ func (h *httpHandler) initRouter() {
|
||||
{"/networks/" + nwID + "/endpoints", []string{"partial-id", epPID}, procGetEndpoints},
|
||||
{"/networks/" + nwID + "/endpoints", nil, procGetEndpoints},
|
||||
{"/networks/" + nwID + "/endpoints/" + epID, nil, procGetEndpoint},
|
||||
{"/services", []string{"network", nwName}, procGetServices},
|
||||
{"/services", []string{"name", epName}, procGetServices},
|
||||
{"/services", []string{"partial-id", epPID}, procGetServices},
|
||||
{"/services", nil, procGetServices},
|
||||
{"/services/" + epID, nil, procGetService},
|
||||
{"/services/" + epID + "/backend", nil, procGetContainers},
|
||||
},
|
||||
"POST": {
|
||||
{"/networks", nil, procCreateNetwork},
|
||||
{"/networks/" + nwID + "/endpoints", nil, procCreateEndpoint},
|
||||
{"/networks/" + nwID + "/endpoints/" + epID + "/containers", nil, procJoinEndpoint},
|
||||
{"/services", nil, procPublishService},
|
||||
{"/services/" + epID + "/backend", nil, procAttachBackend},
|
||||
},
|
||||
"DELETE": {
|
||||
{"/networks/" + nwID, nil, procDeleteNetwork},
|
||||
{"/networks/" + nwID + "/endpoints/" + epID, nil, procDeleteEndpoint},
|
||||
{"/networks/" + nwID + "/endpoints/" + epID + "/containers/" + cnID, nil, procLeaveEndpoint},
|
||||
{"/services/" + epID, nil, procUnpublishService},
|
||||
{"/services/" + epID + "/backend/" + cnID, nil, procDetachBackend},
|
||||
},
|
||||
}
|
||||
|
||||
@@ -175,6 +185,14 @@ func buildEndpointResource(ep libnetwork.Endpoint) *endpointResource {
|
||||
return r
|
||||
}
|
||||
|
||||
func buildContainerResource(ci libnetwork.ContainerInfo) *containerResource {
|
||||
r := &containerResource{}
|
||||
if ci != nil {
|
||||
r.ID = ci.ID()
|
||||
}
|
||||
return r
|
||||
}
|
||||
|
||||
/****************
|
||||
Options Parsers
|
||||
*****************/
|
||||
@@ -355,7 +373,7 @@ func procGetEndpoints(c libnetwork.NetworkController, vars map[string]string, bo
|
||||
list = append(list, buildEndpointResource(ep))
|
||||
}
|
||||
} else if queryByPid {
|
||||
// Return all the prefix-matching networks
|
||||
// Return all the prefix-matching endpoints
|
||||
l := func(ep libnetwork.Endpoint) bool {
|
||||
if strings.HasPrefix(ep.ID(), shortID) {
|
||||
list = append(list, buildEndpointResource(ep))
|
||||
@@ -448,6 +466,166 @@ func procDeleteEndpoint(c libnetwork.NetworkController, vars map[string]string,
|
||||
return nil, &successResponse
|
||||
}
|
||||
|
||||
/******************
|
||||
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 procGetContainers(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)
|
||||
}
|
||||
var list []*containerResource
|
||||
if sv.ContainerInfo() != nil {
|
||||
list = append(list, buildContainerResource(sv.ContainerInfo()))
|
||||
}
|
||||
return list, &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
|
||||
}
|
||||
|
||||
err = sv.Join(bk.ContainerID, bk.parseOptions()...)
|
||||
if err != nil {
|
||||
return nil, convertNetworkError(err)
|
||||
}
|
||||
return sv.Info().SandboxKey(), &successResponse
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
err := sv.Leave(vars[urlCnID])
|
||||
if err != nil {
|
||||
return nil, convertNetworkError(err)
|
||||
}
|
||||
|
||||
return nil, &successResponse
|
||||
}
|
||||
|
||||
/***********
|
||||
Utilities
|
||||
************/
|
||||
@@ -492,7 +670,7 @@ func findNetwork(c libnetwork.NetworkController, s string, by int) (libnetwork.N
|
||||
panic(fmt.Sprintf("unexpected selector for network search: %d", by))
|
||||
}
|
||||
if err != nil {
|
||||
if _, ok := err.(libnetwork.ErrNoSuchNetwork); ok {
|
||||
if _, ok := err.(types.NotFoundError); ok {
|
||||
return nil, &responseStatus{Status: "Resource not found: Network", StatusCode: http.StatusNotFound}
|
||||
}
|
||||
return nil, &responseStatus{Status: err.Error(), StatusCode: http.StatusBadRequest}
|
||||
@@ -518,7 +696,7 @@ func findEndpoint(c libnetwork.NetworkController, ns, es string, nwBy, epBy int)
|
||||
panic(fmt.Sprintf("unexpected selector for endpoint search: %d", epBy))
|
||||
}
|
||||
if err != nil {
|
||||
if _, ok := err.(libnetwork.ErrNoSuchEndpoint); ok {
|
||||
if _, ok := err.(types.NotFoundError); ok {
|
||||
return nil, &responseStatus{Status: "Resource not found: Endpoint", StatusCode: http.StatusNotFound}
|
||||
}
|
||||
return nil, &responseStatus{Status: err.Error(), StatusCode: http.StatusBadRequest}
|
||||
@@ -526,6 +704,34 @@ func findEndpoint(c libnetwork.NetworkController, ns, es string, nwBy, epBy int)
|
||||
return ep, &successResponse
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
func convertNetworkError(err error) *responseStatus {
|
||||
var code int
|
||||
switch err.(type) {
|
||||
|
||||
@@ -70,6 +70,14 @@ func i2nL(i interface{}) []*networkResource {
|
||||
return s
|
||||
}
|
||||
|
||||
func i2cL(i interface{}) []*containerResource {
|
||||
s, ok := i.([]*containerResource)
|
||||
if !ok {
|
||||
panic(fmt.Sprintf("Failed i2cL for %v", i))
|
||||
}
|
||||
return s
|
||||
}
|
||||
|
||||
func createTestNetwork(t *testing.T, network string) (libnetwork.NetworkController, libnetwork.Network) {
|
||||
c, err := libnetwork.New("")
|
||||
if err != nil {
|
||||
@@ -81,7 +89,14 @@ func createTestNetwork(t *testing.T, network string) (libnetwork.NetworkControll
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
nw, err := c.NewNetwork(bridgeNetType, network, nil)
|
||||
netOption := options.Generic{
|
||||
netlabel.GenericData: options.Generic{
|
||||
"BridgeName": network,
|
||||
"AllowNonDefaultBridge": true,
|
||||
},
|
||||
}
|
||||
netGeneric := libnetwork.NetworkOptionGeneric(netOption)
|
||||
nw, err := c.NewNetwork(bridgeNetType, network, netGeneric)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
@@ -507,6 +522,484 @@ func TestGetNetworksAndEndpoints(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestProcGetServices(t *testing.T) {
|
||||
defer netutils.SetupTestNetNS(t)()
|
||||
|
||||
c, err := libnetwork.New("")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
err = c.ConfigureNetworkDriver(bridgeNetType, nil)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
// Create 2 networks
|
||||
netName1 := "production"
|
||||
netOption := options.Generic{
|
||||
netlabel.GenericData: options.Generic{
|
||||
"BridgeName": netName1,
|
||||
"AllowNonDefaultBridge": true,
|
||||
},
|
||||
}
|
||||
nw1, err := c.NewNetwork(bridgeNetType, netName1, libnetwork.NetworkOptionGeneric(netOption))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
netName2 := "work-dev"
|
||||
netOption = options.Generic{
|
||||
netlabel.GenericData: options.Generic{
|
||||
"BridgeName": netName2,
|
||||
"AllowNonDefaultBridge": true,
|
||||
},
|
||||
}
|
||||
nw2, err := c.NewNetwork(bridgeNetType, netName2, libnetwork.NetworkOptionGeneric(netOption))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
vars := make(map[string]string)
|
||||
li, errRsp := procGetServices(c, vars, nil)
|
||||
if !errRsp.isOK() {
|
||||
t.Fatalf("Unexpected failure: %v", errRsp)
|
||||
}
|
||||
list := i2eL(li)
|
||||
if len(list) != 0 {
|
||||
t.Fatalf("Unexpected services in response: %v", list)
|
||||
}
|
||||
|
||||
// Add a couple of services on one network and one on the other network
|
||||
ep11, err := nw1.CreateEndpoint("db-prod")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
ep12, err := nw1.CreateEndpoint("web-prod")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
ep21, err := nw2.CreateEndpoint("db-dev")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
li, errRsp = procGetServices(c, vars, nil)
|
||||
if !errRsp.isOK() {
|
||||
t.Fatalf("Unexpected failure: %v", errRsp)
|
||||
}
|
||||
list = i2eL(li)
|
||||
if len(list) != 3 {
|
||||
t.Fatalf("Unexpected services in response: %v", list)
|
||||
}
|
||||
|
||||
// Filter by network
|
||||
vars[urlNwName] = netName1
|
||||
li, errRsp = procGetServices(c, vars, nil)
|
||||
if !errRsp.isOK() {
|
||||
t.Fatalf("Unexpected failure: %v", errRsp)
|
||||
}
|
||||
list = i2eL(li)
|
||||
if len(list) != 2 {
|
||||
t.Fatalf("Unexpected services in response: %v", list)
|
||||
}
|
||||
|
||||
vars[urlNwName] = netName2
|
||||
li, errRsp = procGetServices(c, vars, nil)
|
||||
if !errRsp.isOK() {
|
||||
t.Fatalf("Unexpected failure: %v", errRsp)
|
||||
}
|
||||
list = i2eL(li)
|
||||
if len(list) != 1 {
|
||||
t.Fatalf("Unexpected services in response: %v", list)
|
||||
}
|
||||
|
||||
vars[urlNwName] = "unknown-network"
|
||||
li, errRsp = procGetServices(c, vars, nil)
|
||||
if !errRsp.isOK() {
|
||||
t.Fatalf("Unexpected failure: %v", errRsp)
|
||||
}
|
||||
list = i2eL(li)
|
||||
if len(list) != 0 {
|
||||
t.Fatalf("Unexpected services in response: %v", list)
|
||||
}
|
||||
|
||||
// Query by name
|
||||
delete(vars, urlNwName)
|
||||
vars[urlEpName] = "db-prod"
|
||||
li, errRsp = procGetServices(c, vars, nil)
|
||||
if !errRsp.isOK() {
|
||||
t.Fatalf("Unexpected failure: %v", errRsp)
|
||||
}
|
||||
list = i2eL(li)
|
||||
if len(list) != 1 {
|
||||
t.Fatalf("Unexpected services in response: %v", list)
|
||||
}
|
||||
|
||||
vars[urlEpName] = "no-service"
|
||||
li, errRsp = procGetServices(c, vars, nil)
|
||||
if !errRsp.isOK() {
|
||||
t.Fatalf("Unexpected failure: %v", errRsp)
|
||||
}
|
||||
list = i2eL(li)
|
||||
if len(list) != 0 {
|
||||
t.Fatalf("Unexpected services in response: %v", list)
|
||||
}
|
||||
|
||||
// Query by id or partial id
|
||||
delete(vars, urlEpName)
|
||||
vars[urlEpPID] = ep12.ID()
|
||||
li, errRsp = procGetServices(c, vars, nil)
|
||||
if !errRsp.isOK() {
|
||||
t.Fatalf("Unexpected failure: %v", errRsp)
|
||||
}
|
||||
list = i2eL(li)
|
||||
if len(list) != 1 {
|
||||
t.Fatalf("Unexpected services in response: %v", list)
|
||||
}
|
||||
if list[0].ID != ep12.ID() {
|
||||
t.Fatalf("Unexpected element in response: %v", list)
|
||||
}
|
||||
|
||||
vars[urlEpPID] = "non-id"
|
||||
li, errRsp = procGetServices(c, vars, nil)
|
||||
if !errRsp.isOK() {
|
||||
t.Fatalf("Unexpected failure: %v", errRsp)
|
||||
}
|
||||
list = i2eL(li)
|
||||
if len(list) != 0 {
|
||||
t.Fatalf("Unexpected services in response: %v", list)
|
||||
}
|
||||
|
||||
delete(vars, urlEpPID)
|
||||
err = ep11.Delete()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
err = ep12.Delete()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
err = ep21.Delete()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
li, errRsp = procGetServices(c, vars, nil)
|
||||
if !errRsp.isOK() {
|
||||
t.Fatalf("Unexpected failure: %v", errRsp)
|
||||
}
|
||||
list = i2eL(li)
|
||||
if len(list) != 0 {
|
||||
t.Fatalf("Unexpected services in response: %v", list)
|
||||
}
|
||||
}
|
||||
|
||||
func TestProcGetService(t *testing.T) {
|
||||
defer netutils.SetupTestNetNS(t)()
|
||||
|
||||
c, nw := createTestNetwork(t, "network")
|
||||
ep1, err := nw.CreateEndpoint("db")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
ep2, err := nw.CreateEndpoint("web")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
vars := map[string]string{urlEpID: ""}
|
||||
_, errRsp := procGetService(c, vars, nil)
|
||||
if errRsp.isOK() {
|
||||
t.Fatalf("Expected failure, but suceeded")
|
||||
}
|
||||
if errRsp.StatusCode != http.StatusBadRequest {
|
||||
t.Fatalf("Expected %d, but got: %d", http.StatusBadRequest, errRsp.StatusCode)
|
||||
}
|
||||
|
||||
vars[urlEpID] = "unknown-service-id"
|
||||
_, errRsp = procGetService(c, vars, nil)
|
||||
if errRsp.isOK() {
|
||||
t.Fatalf("Expected failure, but suceeded")
|
||||
}
|
||||
if errRsp.StatusCode != http.StatusNotFound {
|
||||
t.Fatalf("Expected %d, but got: %d. (%v)", http.StatusNotFound, errRsp.StatusCode, errRsp)
|
||||
}
|
||||
|
||||
vars[urlEpID] = ep1.ID()
|
||||
si, errRsp := procGetService(c, vars, nil)
|
||||
if !errRsp.isOK() {
|
||||
t.Fatalf("Unexpected failure: %v", errRsp)
|
||||
}
|
||||
sv := i2e(si)
|
||||
if sv.ID != ep1.ID() {
|
||||
t.Fatalf("Unexpected service resource returned: %v", sv)
|
||||
}
|
||||
|
||||
vars[urlEpID] = ep2.ID()
|
||||
si, errRsp = procGetService(c, vars, nil)
|
||||
if !errRsp.isOK() {
|
||||
t.Fatalf("Unexpected failure: %v", errRsp)
|
||||
}
|
||||
sv = i2e(si)
|
||||
if sv.ID != ep2.ID() {
|
||||
t.Fatalf("Unexpected service resource returned: %v", sv)
|
||||
}
|
||||
}
|
||||
|
||||
func TestProcPublishUnpublishService(t *testing.T) {
|
||||
defer netutils.SetupTestNetNS(t)()
|
||||
|
||||
c, _ := createTestNetwork(t, "network")
|
||||
vars := make(map[string]string)
|
||||
|
||||
vbad, err := json.Marshal("bad service create data")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
_, errRsp := procPublishService(c, vars, vbad)
|
||||
if errRsp == &createdResponse {
|
||||
t.Fatalf("Expected to fail but succeeded")
|
||||
}
|
||||
if errRsp.StatusCode != http.StatusBadRequest {
|
||||
t.Fatalf("Expected %d. Got: %v", http.StatusBadRequest, errRsp)
|
||||
}
|
||||
|
||||
b, err := json.Marshal(servicePublish{Name: ""})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
_, errRsp = procPublishService(c, vars, b)
|
||||
if errRsp == &createdResponse {
|
||||
t.Fatalf("Expected to fail but succeeded")
|
||||
}
|
||||
if errRsp.StatusCode != http.StatusBadRequest {
|
||||
t.Fatalf("Expected %d. Got: %v", http.StatusBadRequest, errRsp)
|
||||
}
|
||||
|
||||
b, err = json.Marshal(servicePublish{Name: "db"})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
_, errRsp = procPublishService(c, vars, b)
|
||||
if errRsp == &createdResponse {
|
||||
t.Fatalf("Expected to fail but succeeded")
|
||||
}
|
||||
if errRsp.StatusCode != http.StatusBadRequest {
|
||||
t.Fatalf("Expected %d. Got: %v", http.StatusBadRequest, errRsp)
|
||||
}
|
||||
|
||||
b, err = json.Marshal(servicePublish{Name: "db", Network: "unknown-network"})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
_, errRsp = procPublishService(c, vars, b)
|
||||
if errRsp == &createdResponse {
|
||||
t.Fatalf("Expected to fail but succeeded")
|
||||
}
|
||||
if errRsp.StatusCode != http.StatusNotFound {
|
||||
t.Fatalf("Expected %d. Got: %v", http.StatusNotFound, errRsp)
|
||||
}
|
||||
|
||||
b, err = json.Marshal(servicePublish{Name: "", Network: "network"})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
_, errRsp = procPublishService(c, vars, b)
|
||||
if errRsp == &createdResponse {
|
||||
t.Fatalf("Expected to fail but succeeded")
|
||||
}
|
||||
if errRsp.StatusCode != http.StatusBadRequest {
|
||||
t.Fatalf("Expected %d. Got: %v", http.StatusBadRequest, errRsp)
|
||||
}
|
||||
|
||||
b, err = json.Marshal(servicePublish{Name: "db", Network: "network"})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
_, errRsp = procPublishService(c, vars, b)
|
||||
if errRsp != &createdResponse {
|
||||
t.Fatalf("Unexpected failure: %v", errRsp)
|
||||
}
|
||||
|
||||
sp := servicePublish{
|
||||
Name: "web",
|
||||
Network: "network",
|
||||
ExposedPorts: []types.TransportPort{
|
||||
types.TransportPort{Proto: types.TCP, Port: uint16(6000)},
|
||||
types.TransportPort{Proto: types.UDP, Port: uint16(500)},
|
||||
types.TransportPort{Proto: types.TCP, Port: uint16(700)},
|
||||
},
|
||||
PortMapping: []types.PortBinding{
|
||||
types.PortBinding{Proto: types.TCP, Port: uint16(1230), HostPort: uint16(37000)},
|
||||
types.PortBinding{Proto: types.UDP, Port: uint16(1200), HostPort: uint16(36000)},
|
||||
types.PortBinding{Proto: types.TCP, Port: uint16(1120), HostPort: uint16(35000)},
|
||||
},
|
||||
}
|
||||
b, err = json.Marshal(sp)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
si, errRsp := procPublishService(c, vars, b)
|
||||
if errRsp != &createdResponse {
|
||||
t.Fatalf("Unexpected failure: %v", errRsp)
|
||||
}
|
||||
sid := i2s(si)
|
||||
|
||||
vars[urlEpID] = ""
|
||||
_, errRsp = procUnpublishService(c, vars, nil)
|
||||
if errRsp.isOK() {
|
||||
t.Fatalf("Expected failure but succeeded")
|
||||
}
|
||||
if errRsp.StatusCode != http.StatusBadRequest {
|
||||
t.Fatalf("Expected %d. Got: %v", http.StatusBadRequest, errRsp)
|
||||
}
|
||||
|
||||
vars[urlEpID] = "unknown-service-id"
|
||||
_, errRsp = procUnpublishService(c, vars, nil)
|
||||
if errRsp.isOK() {
|
||||
t.Fatalf("Expected failure but succeeded")
|
||||
}
|
||||
if errRsp.StatusCode != http.StatusNotFound {
|
||||
t.Fatalf("Expected %d. Got: %v", http.StatusNotFound, errRsp)
|
||||
}
|
||||
|
||||
vars[urlEpID] = sid
|
||||
_, errRsp = procUnpublishService(c, vars, nil)
|
||||
if !errRsp.isOK() {
|
||||
t.Fatalf("Unexpected failure: %v", errRsp)
|
||||
}
|
||||
|
||||
_, errRsp = procGetService(c, vars, nil)
|
||||
if errRsp.isOK() {
|
||||
t.Fatalf("Expected failure, but suceeded")
|
||||
}
|
||||
if errRsp.StatusCode != http.StatusNotFound {
|
||||
t.Fatalf("Expected %d, but got: %d. (%v)", http.StatusNotFound, errRsp.StatusCode, errRsp)
|
||||
}
|
||||
}
|
||||
|
||||
func TestAttachDetachBackend(t *testing.T) {
|
||||
defer netutils.SetupTestNetNS(t)()
|
||||
|
||||
c, nw := createTestNetwork(t, "network")
|
||||
ep1, err := nw.CreateEndpoint("db")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
vars := make(map[string]string)
|
||||
|
||||
vbad, err := json.Marshal("bad data")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
_, errRsp := procAttachBackend(c, vars, vbad)
|
||||
if errRsp == &successResponse {
|
||||
t.Fatalf("Expected failure, got: %v", errRsp)
|
||||
}
|
||||
|
||||
vars[urlEpName] = "endpoint"
|
||||
bad, err := json.Marshal(endpointJoin{})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
_, errRsp = procAttachBackend(c, vars, bad)
|
||||
if errRsp == &successResponse {
|
||||
t.Fatalf("Expected failure, got: %v", errRsp)
|
||||
}
|
||||
if errRsp.StatusCode != http.StatusNotFound {
|
||||
t.Fatalf("Expected %d. Got: %v", http.StatusNotFound, errRsp)
|
||||
}
|
||||
|
||||
_, errRsp = procGetContainers(c, vars, nil)
|
||||
if errRsp.isOK() {
|
||||
t.Fatalf("Expected failure. Got %v", errRsp)
|
||||
}
|
||||
if errRsp.StatusCode != http.StatusNotFound {
|
||||
t.Fatalf("Expected %d. Got: %v", http.StatusNotFound, errRsp)
|
||||
}
|
||||
|
||||
vars[urlEpName] = "db"
|
||||
_, errRsp = procAttachBackend(c, vars, bad)
|
||||
if errRsp == &successResponse {
|
||||
t.Fatalf("Expected failure, got: %v", errRsp)
|
||||
}
|
||||
if errRsp.StatusCode != http.StatusBadRequest {
|
||||
t.Fatalf("Expected %d. Got: %v", http.StatusBadRequest, errRsp)
|
||||
}
|
||||
|
||||
cid := "abcdefghi"
|
||||
jl := endpointJoin{ContainerID: cid}
|
||||
jlb, err := json.Marshal(jl)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
_, errRsp = procAttachBackend(c, vars, jlb)
|
||||
if errRsp != &successResponse {
|
||||
t.Fatalf("Unexpected failure, got: %v", errRsp)
|
||||
}
|
||||
|
||||
cli, errRsp := procGetContainers(c, vars, nil)
|
||||
if errRsp != &successResponse {
|
||||
t.Fatalf("Unexpected failure, got: %v", errRsp)
|
||||
}
|
||||
cl := i2cL(cli)
|
||||
if len(cl) != 1 {
|
||||
t.Fatalf("Did not find expected number of containers attached to the service: %d", len(cl))
|
||||
}
|
||||
if cl[0].ID != cid {
|
||||
t.Fatalf("Did not find expected container attached to the service: %v", cl[0])
|
||||
}
|
||||
|
||||
_, errRsp = procUnpublishService(c, vars, nil)
|
||||
if errRsp.isOK() {
|
||||
t.Fatalf("Expected failure but succeeded")
|
||||
}
|
||||
if errRsp.StatusCode != http.StatusForbidden {
|
||||
t.Fatalf("Expected %d. Got: %v", http.StatusForbidden, errRsp)
|
||||
}
|
||||
|
||||
vars[urlEpName] = "endpoint"
|
||||
_, errRsp = procDetachBackend(c, vars, nil)
|
||||
if errRsp == &successResponse {
|
||||
t.Fatalf("Expected failure, got: %v", errRsp)
|
||||
}
|
||||
if errRsp.StatusCode != http.StatusNotFound {
|
||||
t.Fatalf("Expected %d. Got: %v", http.StatusNotFound, errRsp)
|
||||
}
|
||||
|
||||
vars[urlEpName] = "db"
|
||||
_, errRsp = procDetachBackend(c, vars, nil)
|
||||
if errRsp == &successResponse {
|
||||
t.Fatalf("Expected failure, got: %v", errRsp)
|
||||
}
|
||||
if errRsp.StatusCode != http.StatusBadRequest {
|
||||
t.Fatalf("Expected %d. Got: %v", http.StatusBadRequest, errRsp)
|
||||
}
|
||||
|
||||
vars[urlCnID] = cid
|
||||
_, errRsp = procDetachBackend(c, vars, nil)
|
||||
if errRsp != &successResponse {
|
||||
t.Fatalf("Unexpected failure, got: %v", errRsp)
|
||||
}
|
||||
|
||||
cli, errRsp = procGetContainers(c, vars, nil)
|
||||
if errRsp != &successResponse {
|
||||
t.Fatalf("Unexpected failure, got: %v", errRsp)
|
||||
}
|
||||
cl = i2cL(cli)
|
||||
if len(cl) != 0 {
|
||||
t.Fatalf("Did not find expected number of containers attached to the service: %d", len(cl))
|
||||
}
|
||||
|
||||
err = ep1.Delete()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestDetectGetNetworksInvalidQueryComposition(t *testing.T) {
|
||||
c, err := libnetwork.New("")
|
||||
if err != nil {
|
||||
@@ -532,15 +1025,29 @@ func TestDetectGetEndpointsInvalidQueryComposition(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestDetectGetServicesInvalidQueryComposition(t *testing.T) {
|
||||
defer netutils.SetupTestNetNS(t)()
|
||||
|
||||
c, _ := createTestNetwork(t, "network")
|
||||
|
||||
vars := map[string]string{urlNwName: "network", urlEpName: "x", urlEpPID: "y"}
|
||||
_, errRsp := procGetServices(c, vars, nil)
|
||||
if errRsp.StatusCode != http.StatusBadRequest {
|
||||
t.Fatalf("Expected %d. Got: %v", http.StatusBadRequest, errRsp)
|
||||
}
|
||||
}
|
||||
|
||||
func TestFindNetworkUtilPanic(t *testing.T) {
|
||||
defer checkPanic(t)
|
||||
findNetwork(nil, "", -1)
|
||||
}
|
||||
|
||||
func TestFindNetworkUtil(t *testing.T) {
|
||||
defer netutils.SetupTestNetNS(t)()
|
||||
|
||||
c, nw := createTestNetwork(t, "network")
|
||||
nid := nw.ID()
|
||||
|
||||
defer checkPanic(t)
|
||||
findNetwork(c, "", -1)
|
||||
|
||||
_, errRsp := findNetwork(c, "", byName)
|
||||
if errRsp == &successResponse {
|
||||
t.Fatalf("Expected to fail but succeeded")
|
||||
@@ -577,7 +1084,9 @@ func TestFindNetworkUtil(t *testing.T) {
|
||||
t.Fatalf("Incorrect libnetwork.Network resource. It has different name: %v", n)
|
||||
}
|
||||
|
||||
n.Delete()
|
||||
if err := n.Delete(); err != nil {
|
||||
t.Fatalf("Failed to delete the network: %s", err.Error())
|
||||
}
|
||||
|
||||
_, errRsp = findNetwork(c, nid, byID)
|
||||
if errRsp == &successResponse {
|
||||
@@ -878,6 +1387,21 @@ func TestJoinLeave(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestFindEndpointUtilPanic(t *testing.T) {
|
||||
defer netutils.SetupTestNetNS(t)()
|
||||
defer checkPanic(t)
|
||||
c, nw := createTestNetwork(t, "network")
|
||||
nid := nw.ID()
|
||||
findEndpoint(c, nid, "", byID, -1)
|
||||
}
|
||||
|
||||
func TestFindServiceUtilPanic(t *testing.T) {
|
||||
defer netutils.SetupTestNetNS(t)()
|
||||
defer checkPanic(t)
|
||||
c, _ := createTestNetwork(t, "network")
|
||||
findService(c, "random_service", -1)
|
||||
}
|
||||
|
||||
func TestFindEndpointUtil(t *testing.T) {
|
||||
defer netutils.SetupTestNetNS(t)()
|
||||
|
||||
@@ -890,9 +1414,6 @@ func TestFindEndpointUtil(t *testing.T) {
|
||||
}
|
||||
eid := ep.ID()
|
||||
|
||||
defer checkPanic(t)
|
||||
findEndpoint(c, nid, "", byID, -1)
|
||||
|
||||
_, errRsp := findEndpoint(c, nid, "", byID, byName)
|
||||
if errRsp == &successResponse {
|
||||
t.Fatalf("Expected failure, but got: %v", errRsp)
|
||||
@@ -906,7 +1427,7 @@ func TestFindEndpointUtil(t *testing.T) {
|
||||
t.Fatalf("Unexepected failure: %v", errRsp)
|
||||
}
|
||||
|
||||
ep1, errRsp := findEndpoint(c, "second", "secondEp", byName, byName)
|
||||
ep1, errRsp := findEndpoint(c, "network", "secondEp", byName, byName)
|
||||
if errRsp != &successResponse {
|
||||
t.Fatalf("Unexepected failure: %v", errRsp)
|
||||
}
|
||||
@@ -916,12 +1437,22 @@ func TestFindEndpointUtil(t *testing.T) {
|
||||
t.Fatalf("Unexepected failure: %v", errRsp)
|
||||
}
|
||||
|
||||
ep3, errRsp := findEndpoint(c, "second", eid, byName, byID)
|
||||
ep3, errRsp := findEndpoint(c, "network", eid, byName, byID)
|
||||
if errRsp != &successResponse {
|
||||
t.Fatalf("Unexepected failure: %v", errRsp)
|
||||
}
|
||||
|
||||
if ep0 != ep1 || ep0 != ep2 || ep0 != ep3 {
|
||||
ep4, errRsp := findService(c, "secondEp", byName)
|
||||
if errRsp != &successResponse {
|
||||
t.Fatalf("Unexepected failure: %v", errRsp)
|
||||
}
|
||||
|
||||
ep5, errRsp := findService(c, eid, byID)
|
||||
if errRsp != &successResponse {
|
||||
t.Fatalf("Unexepected failure: %v", errRsp)
|
||||
}
|
||||
|
||||
if ep0 != ep1 || ep0 != ep2 || ep0 != ep3 || ep0 != ep4 || ep0 != ep5 {
|
||||
t.Fatalf("Diffenrent queries returned different endpoints")
|
||||
}
|
||||
|
||||
@@ -935,7 +1466,7 @@ func TestFindEndpointUtil(t *testing.T) {
|
||||
t.Fatalf("Expected %d, but got: %d", http.StatusNotFound, errRsp.StatusCode)
|
||||
}
|
||||
|
||||
_, errRsp = findEndpoint(c, "second", "secondEp", byName, byName)
|
||||
_, errRsp = findEndpoint(c, "network", "secondEp", byName, byName)
|
||||
if errRsp == &successResponse {
|
||||
t.Fatalf("Expected failure, but got: %v", errRsp)
|
||||
}
|
||||
@@ -951,13 +1482,43 @@ func TestFindEndpointUtil(t *testing.T) {
|
||||
t.Fatalf("Expected %d, but got: %d", http.StatusNotFound, errRsp.StatusCode)
|
||||
}
|
||||
|
||||
_, errRsp = findEndpoint(c, "second", eid, byName, byID)
|
||||
_, errRsp = findEndpoint(c, "network", eid, byName, byID)
|
||||
if errRsp == &successResponse {
|
||||
t.Fatalf("Expected failure, but got: %v", errRsp)
|
||||
}
|
||||
if errRsp.StatusCode != http.StatusNotFound {
|
||||
t.Fatalf("Expected %d, but got: %d", http.StatusNotFound, errRsp.StatusCode)
|
||||
}
|
||||
|
||||
_, errRsp = findService(c, "secondEp", byName)
|
||||
if errRsp == &successResponse {
|
||||
t.Fatalf("Expected failure, but got: %v", errRsp)
|
||||
}
|
||||
if errRsp.StatusCode != http.StatusNotFound {
|
||||
t.Fatalf("Expected %d, but got: %d", http.StatusNotFound, errRsp.StatusCode)
|
||||
}
|
||||
|
||||
_, errRsp = findService(c, eid, byID)
|
||||
if errRsp == &successResponse {
|
||||
t.Fatalf("Expected failure, but got: %v", errRsp)
|
||||
}
|
||||
if errRsp.StatusCode != http.StatusNotFound {
|
||||
t.Fatalf("Expected %d, but got: %d", http.StatusNotFound, errRsp.StatusCode)
|
||||
}
|
||||
}
|
||||
|
||||
func TestEndpointToService(t *testing.T) {
|
||||
r := &responseStatus{Status: "this is one endpoint", StatusCode: http.StatusOK}
|
||||
r = endpointToService(r)
|
||||
if r.Status != "this is one service" {
|
||||
t.Fatalf("endpointToService returned unexpected status string: %s", r.Status)
|
||||
}
|
||||
|
||||
r = &responseStatus{Status: "this is one network", StatusCode: http.StatusOK}
|
||||
r = endpointToService(r)
|
||||
if r.Status != "this is one network" {
|
||||
t.Fatalf("endpointToService returned unexpected status string: %s", r.Status)
|
||||
}
|
||||
}
|
||||
|
||||
func checkPanic(t *testing.T) {
|
||||
|
||||
@@ -21,6 +21,12 @@ type endpointResource struct {
|
||||
Network string `json:"network"`
|
||||
}
|
||||
|
||||
// containerResource is the body of "get service backend" response message
|
||||
type containerResource struct {
|
||||
ID string `json:"id"`
|
||||
// will add more fields once labels change is in
|
||||
}
|
||||
|
||||
/***********
|
||||
Body types
|
||||
************/
|
||||
@@ -52,6 +58,14 @@ type endpointJoin struct {
|
||||
UseDefaultSandbox bool `json:"use_default_sandbox"`
|
||||
}
|
||||
|
||||
// servicePublish represents the body of the "publish service" http request message
|
||||
type servicePublish struct {
|
||||
Name string `json:"name"`
|
||||
Network string `json:"network_name"`
|
||||
ExposedPorts []types.TransportPort `json:"exposed_ports"`
|
||||
PortMapping []types.PortBinding `json:"port_mapping"`
|
||||
}
|
||||
|
||||
// EndpointExtraHost represents the extra host object
|
||||
type endpointExtraHost struct {
|
||||
Name string `json:"name"`
|
||||
|
||||
@@ -7,7 +7,7 @@ import (
|
||||
_ "github.com/docker/libnetwork/netutils"
|
||||
)
|
||||
|
||||
func TestClientNetworkServiceInvalidCommand(t *testing.T) {
|
||||
func TestClientServiceInvalidCommand(t *testing.T) {
|
||||
var out, errOut bytes.Buffer
|
||||
cli := NewNetworkCli(&out, &errOut, callbackFunc)
|
||||
|
||||
@@ -17,73 +17,73 @@ func TestClientNetworkServiceInvalidCommand(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestClientNetworkServiceCreate(t *testing.T) {
|
||||
func TestClientServiceCreate(t *testing.T) {
|
||||
var out, errOut bytes.Buffer
|
||||
cli := NewNetworkCli(&out, &errOut, callbackFunc)
|
||||
|
||||
err := cli.Cmd("docker", "service", "create", mockServiceName, mockNwName)
|
||||
err := cli.Cmd("docker", "service", "publish", "-net="+mockNwName, mockServiceName)
|
||||
if err != nil {
|
||||
t.Fatal(err.Error())
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestClientNetworkServiceRm(t *testing.T) {
|
||||
func TestClientServiceRm(t *testing.T) {
|
||||
var out, errOut bytes.Buffer
|
||||
cli := NewNetworkCli(&out, &errOut, callbackFunc)
|
||||
|
||||
err := cli.Cmd("docker", "service", "rm", mockServiceName, mockNwName)
|
||||
err := cli.Cmd("docker", "service", "unpublish", "-net="+mockNwName, mockServiceName)
|
||||
if err != nil {
|
||||
t.Fatal(err.Error())
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestClientNetworkServiceLs(t *testing.T) {
|
||||
func TestClientServiceLs(t *testing.T) {
|
||||
var out, errOut bytes.Buffer
|
||||
cli := NewNetworkCli(&out, &errOut, callbackFunc)
|
||||
|
||||
err := cli.Cmd("docker", "service", "ls", mockNwName)
|
||||
err := cli.Cmd("docker", "service", "ls")
|
||||
if err != nil {
|
||||
t.Fatal(err.Error())
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestClientNetworkServiceInfo(t *testing.T) {
|
||||
func TestClientServiceInfo(t *testing.T) {
|
||||
var out, errOut bytes.Buffer
|
||||
cli := NewNetworkCli(&out, &errOut, callbackFunc)
|
||||
|
||||
err := cli.Cmd("docker", "service", "info", mockServiceName, mockNwName)
|
||||
err := cli.Cmd("docker", "service", "info", "-net="+mockNwName, mockServiceName)
|
||||
if err != nil {
|
||||
t.Fatal(err.Error())
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestClientNetworkServiceInfoById(t *testing.T) {
|
||||
func TestClientServiceInfoById(t *testing.T) {
|
||||
var out, errOut bytes.Buffer
|
||||
cli := NewNetworkCli(&out, &errOut, callbackFunc)
|
||||
|
||||
err := cli.Cmd("docker", "service", "info", mockServiceID, mockNwID)
|
||||
err := cli.Cmd("docker", "service", "info", "-net="+mockNwName, mockServiceID)
|
||||
if err != nil {
|
||||
t.Fatal(err.Error())
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestClientNetworkServiceJoin(t *testing.T) {
|
||||
func TestClientServiceJoin(t *testing.T) {
|
||||
var out, errOut bytes.Buffer
|
||||
cli := NewNetworkCli(&out, &errOut, callbackFunc)
|
||||
|
||||
err := cli.Cmd("docker", "service", "join", mockContainerID, mockServiceName, mockNwName)
|
||||
err := cli.Cmd("docker", "service", "attach", "-net="+mockNwName, mockContainerID, mockServiceName)
|
||||
if err != nil {
|
||||
t.Fatal(err.Error())
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestClientNetworkServiceLeave(t *testing.T) {
|
||||
func TestClientServiceLeave(t *testing.T) {
|
||||
var out, errOut bytes.Buffer
|
||||
cli := NewNetworkCli(&out, &errOut, callbackFunc)
|
||||
|
||||
err := cli.Cmd("docker", "service", "leave", mockContainerID, mockServiceName, mockNwName)
|
||||
err := cli.Cmd("docker", "service", "detach", "-net="+mockNwName, mockContainerID, mockServiceName)
|
||||
if err != nil {
|
||||
t.Fatal(err.Error())
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -39,8 +39,8 @@ func setupMockHTTPCallback() {
|
||||
list = append(list, nw)
|
||||
mockNwListJSON, _ = json.Marshal(list)
|
||||
|
||||
var srvList []endpointResource
|
||||
ep := endpointResource{Name: mockServiceName, ID: mockServiceID, Network: mockNwName}
|
||||
var srvList []serviceResource
|
||||
ep := serviceResource{Name: mockServiceName, ID: mockServiceID, Network: mockNwName}
|
||||
mockServiceJSON, _ = json.Marshal(ep)
|
||||
srvList = append(srvList, ep)
|
||||
mockServiceListJSON, _ = json.Marshal(srvList)
|
||||
@@ -61,26 +61,26 @@ func setupMockHTTPCallback() {
|
||||
rsp = string(mockNwListJSON)
|
||||
} else if strings.HasSuffix(path, "networks/"+mockNwID) {
|
||||
rsp = string(mockNwJSON)
|
||||
} else if strings.Contains(path, fmt.Sprintf("endpoints?name=%s", mockServiceName)) {
|
||||
} else if strings.Contains(path, fmt.Sprintf("services?name=%s", mockServiceName)) {
|
||||
rsp = string(mockServiceListJSON)
|
||||
} else if strings.Contains(path, "endpoints?name=") {
|
||||
} else if strings.Contains(path, "services?name=") {
|
||||
rsp = "[]"
|
||||
} else if strings.Contains(path, fmt.Sprintf("endpoints?partial-id=%s", mockServiceID)) {
|
||||
} else if strings.Contains(path, fmt.Sprintf("services?partial-id=%s", mockServiceID)) {
|
||||
rsp = string(mockServiceListJSON)
|
||||
} else if strings.Contains(path, "endpoints?partial-id=") {
|
||||
} else if strings.Contains(path, "services?partial-id=") {
|
||||
rsp = "[]"
|
||||
} else if strings.HasSuffix(path, "endpoints") {
|
||||
} else if strings.HasSuffix(path, "services") {
|
||||
rsp = string(mockServiceListJSON)
|
||||
} else if strings.HasSuffix(path, "endpoints/"+mockServiceID) {
|
||||
} else if strings.HasSuffix(path, "services/"+mockServiceID) {
|
||||
rsp = string(mockServiceJSON)
|
||||
}
|
||||
case "POST":
|
||||
var data []byte
|
||||
if strings.HasSuffix(path, "networks") {
|
||||
data, _ = json.Marshal(mockNwID)
|
||||
} else if strings.HasSuffix(path, "endpoints") {
|
||||
} else if strings.HasSuffix(path, "services") {
|
||||
data, _ = json.Marshal(mockServiceID)
|
||||
} else if strings.HasSuffix(path, "containers") {
|
||||
} else if strings.HasSuffix(path, "backend") {
|
||||
data, _ = json.Marshal(mockContainerID)
|
||||
}
|
||||
rsp = string(data)
|
||||
|
||||
@@ -177,10 +177,10 @@ func (cli *NetworkCli) CmdNetworkInfo(chain string, args ...string) error {
|
||||
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)
|
||||
if networkResource.Endpoints != nil {
|
||||
for _, endpointResource := range networkResource.Endpoints {
|
||||
fmt.Fprintf(cli.out, " Service Id: %s\n", endpointResource.ID)
|
||||
fmt.Fprintf(cli.out, "\tName: %s\n", endpointResource.Name)
|
||||
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)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -13,64 +13,77 @@ import (
|
||||
|
||||
var (
|
||||
serviceCommands = []command{
|
||||
{"create", "Create a service endpoint"},
|
||||
{"rm", "Remove a service endpoint"},
|
||||
{"join", "Join a container to a service endpoint"},
|
||||
{"leave", "Leave a container from a service endpoint"},
|
||||
{"ls", "Lists all service endpoints on a network"},
|
||||
{"info", "Display information of a service endpoint"},
|
||||
{"publish", "Publish a service"},
|
||||
{"unpublish", "Remove a service"},
|
||||
{"attach", "Attach a provider (container) to the service"},
|
||||
{"detach", "Detach the provider from the service"},
|
||||
{"ls", "Lists all services"},
|
||||
{"info", "Display information about a service"},
|
||||
}
|
||||
)
|
||||
|
||||
func lookupServiceID(cli *NetworkCli, networkID string, nameID string) (string, error) {
|
||||
obj, statusCode, err := readBody(cli.call("GET", fmt.Sprintf("/networks/%s/endpoints?name=%s", networkID, nameID), nil, nil))
|
||||
func lookupServiceID(cli *NetworkCli, nwName, svNameID string) (string, error) {
|
||||
// Sanity Check
|
||||
obj, _, err := readBody(cli.call("GET", fmt.Sprintf("/networks?name=%s", nwName), nil, nil))
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
var nwList []networkResource
|
||||
if err = json.Unmarshal(obj, &nwList); err != nil {
|
||||
return "", err
|
||||
}
|
||||
if len(nwList) == 0 {
|
||||
return "", fmt.Errorf("Network %s does not exist", nwName)
|
||||
}
|
||||
|
||||
// Query service by name
|
||||
obj, statusCode, err := readBody(cli.call("GET", fmt.Sprintf("/services?name=%s", svNameID), 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))
|
||||
return "", fmt.Errorf("name query failed for %s due to: (%d) %s", svNameID, statusCode, string(obj))
|
||||
}
|
||||
|
||||
var list []*networkResource
|
||||
err = json.Unmarshal(obj, &list)
|
||||
if err != nil {
|
||||
var list []*serviceResource
|
||||
if err = json.Unmarshal(obj, &list); err != nil {
|
||||
return "", err
|
||||
}
|
||||
if len(list) > 0 {
|
||||
// name query filter will always return a single-element collection
|
||||
return list[0].ID, nil
|
||||
for _, sr := range list {
|
||||
if sr.Network == nwName {
|
||||
return sr.ID, nil
|
||||
}
|
||||
}
|
||||
|
||||
// Check for Partial-id
|
||||
obj, statusCode, err = readBody(cli.call("GET", fmt.Sprintf("/networks/%s/endpoints?partial-id=%s", networkID, nameID), nil, nil))
|
||||
// Query service by Partial-id (this covers full id as well)
|
||||
obj, statusCode, err = readBody(cli.call("GET", fmt.Sprintf("/services?partial-id=%s", svNameID), 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))
|
||||
return "", fmt.Errorf("partial-id match query failed for %s due to: (%d) %s", svNameID, statusCode, string(obj))
|
||||
}
|
||||
|
||||
err = json.Unmarshal(obj, &list)
|
||||
if err != nil {
|
||||
if err = json.Unmarshal(obj, &list); err != nil {
|
||||
return "", err
|
||||
}
|
||||
if len(list) == 0 {
|
||||
return "", fmt.Errorf("resource not found %s", nameID)
|
||||
for _, sr := range list {
|
||||
if sr.Network == nwName {
|
||||
return sr.ID, nil
|
||||
}
|
||||
}
|
||||
if len(list) > 1 {
|
||||
return "", fmt.Errorf("multiple services matching the partial identifier (%s). Please use full identifier", nameID)
|
||||
}
|
||||
return list[0].ID, nil
|
||||
|
||||
return "", fmt.Errorf("Service %s not found on network %s", svNameID, nwName)
|
||||
}
|
||||
|
||||
func lookupContainerID(cli *NetworkCli, nameID string) (string, error) {
|
||||
func lookupContainerID(cli *NetworkCli, cnNameID string) (string, error) {
|
||||
// TODO : containerID to sandbox-key ?
|
||||
return nameID, nil
|
||||
return cnNameID, nil
|
||||
}
|
||||
|
||||
// CmdService handles the network service UI
|
||||
// CmdService handles the service UI
|
||||
func (cli *NetworkCli) CmdService(chain string, args ...string) error {
|
||||
cmd := cli.Subcmd(chain, "service", "COMMAND [OPTIONS] [arg...]", serviceUsage(chain), false)
|
||||
cmd.Require(flag.Min, 1)
|
||||
@@ -82,23 +95,25 @@ func (cli *NetworkCli) CmdService(chain string, args ...string) error {
|
||||
return err
|
||||
}
|
||||
|
||||
// CmdServiceCreate handles service create UI
|
||||
func (cli *NetworkCli) CmdServiceCreate(chain string, args ...string) error {
|
||||
cmd := cli.Subcmd(chain, "create", "SERVICE NETWORK", "Creates a new service on a network", false)
|
||||
cmd.Require(flag.Min, 2)
|
||||
// CmdServicePublish handles service create UI
|
||||
func (cli *NetworkCli) CmdServicePublish(chain string, args ...string) error {
|
||||
cmd := cli.Subcmd(chain, "publish", "SERVICE", "Publish a new service on a network", false)
|
||||
flNetwork := cmd.String([]string{"net", "-network"}, "", "Network where to publish the service")
|
||||
cmd.Require(flag.Min, 1)
|
||||
|
||||
err := cmd.ParseFlags(args, true)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
networkID, err := lookupNetworkID(cli, cmd.Arg(1))
|
||||
if err != nil {
|
||||
return err
|
||||
// Default network changes will come later
|
||||
nw := "docker0"
|
||||
if *flNetwork != "" {
|
||||
nw = *flNetwork
|
||||
}
|
||||
|
||||
ec := endpointCreate{Name: cmd.Arg(0), NetworkID: networkID}
|
||||
|
||||
obj, _, err := readBody(cli.call("POST", "/networks/"+networkID+"/endpoints", ec, nil))
|
||||
sc := serviceCreate{Name: cmd.Arg(0), Network: nw}
|
||||
obj, _, err := readBody(cli.call("POST", "/services", sc, nil))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -113,39 +128,40 @@ func (cli *NetworkCli) CmdServiceCreate(chain string, args ...string) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// CmdServiceRm handles service delete UI
|
||||
func (cli *NetworkCli) CmdServiceRm(chain string, args ...string) error {
|
||||
cmd := cli.Subcmd(chain, "rm", "SERVICE NETWORK", "Deletes a service", false)
|
||||
cmd.Require(flag.Min, 2)
|
||||
// CmdServiceUnpublish handles service delete UI
|
||||
func (cli *NetworkCli) CmdServiceUnpublish(chain string, args ...string) error {
|
||||
cmd := cli.Subcmd(chain, "unpublish", "SERVICE", "Removes a service", false)
|
||||
flNetwork := cmd.String([]string{"net", "-network"}, "", "Network where to publish the service")
|
||||
cmd.Require(flag.Min, 1)
|
||||
|
||||
err := cmd.ParseFlags(args, true)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
networkID, err := lookupNetworkID(cli, cmd.Arg(1))
|
||||
// Default network changes will come later
|
||||
nw := "docker0"
|
||||
if *flNetwork != "" {
|
||||
nw = *flNetwork
|
||||
}
|
||||
|
||||
serviceID, err := lookupServiceID(cli, nw, cmd.Arg(0))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
serviceID, err := lookupServiceID(cli, networkID, cmd.Arg(0))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
_, _, err = readBody(cli.call("DELETE", "/services/"+serviceID, nil, nil))
|
||||
|
||||
_, _, err = readBody(cli.call("DELETE", "/networks/"+networkID+"/endpoints/"+serviceID, nil, nil))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
return err
|
||||
}
|
||||
|
||||
// CmdServiceLs handles service list UI
|
||||
func (cli *NetworkCli) CmdServiceLs(chain string, args ...string) error {
|
||||
cmd := cli.Subcmd(chain, "ls", "NETWORK", "Lists all the services on a network", false)
|
||||
cmd := cli.Subcmd(chain, "ls", "SERVICE", "Lists all the services on a network", false)
|
||||
flNetwork := cmd.String([]string{"net", "-network"}, "", "Only show the services that are published on the specified network")
|
||||
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")
|
||||
|
||||
err := cmd.ParseFlags(args, true)
|
||||
if err != nil {
|
||||
return err
|
||||
@@ -153,97 +169,126 @@ func (cli *NetworkCli) CmdServiceLs(chain string, args ...string) error {
|
||||
|
||||
cmd.Require(flag.Min, 1)
|
||||
|
||||
networkID, err := lookupNetworkID(cli, cmd.Arg(0))
|
||||
var obj []byte
|
||||
if *flNetwork == "" {
|
||||
obj, _, err = readBody(cli.call("GET", "/services", nil, nil))
|
||||
} else {
|
||||
obj, _, err = readBody(cli.call("GET", "/services?network="+*flNetwork, nil, nil))
|
||||
}
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
obj, _, err := readBody(cli.call("GET", "/networks/"+networkID+"/endpoints", nil, nil))
|
||||
if err != nil {
|
||||
fmt.Fprintf(cli.err, "%s", err.Error())
|
||||
return err
|
||||
}
|
||||
if *last == -1 && *nLatest {
|
||||
*last = 1
|
||||
}
|
||||
|
||||
var endpointResources []endpointResource
|
||||
err = json.Unmarshal(obj, &endpointResources)
|
||||
var serviceResources []serviceResource
|
||||
err = json.Unmarshal(obj, &serviceResources)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
return err
|
||||
}
|
||||
|
||||
wr := tabwriter.NewWriter(cli.out, 20, 1, 3, ' ', 0)
|
||||
// unless quiet (-q) is specified, print field titles
|
||||
if !*quiet {
|
||||
fmt.Fprintln(wr, "NETWORK SERVICE ID\tNAME\tNETWORK")
|
||||
fmt.Fprintln(wr, "SERVICE ID\tNAME\tNETWORK\tPROVIDER")
|
||||
}
|
||||
|
||||
for _, networkResource := range endpointResources {
|
||||
ID := networkResource.ID
|
||||
netName := networkResource.Name
|
||||
for _, sr := range serviceResources {
|
||||
ID := sr.ID
|
||||
bkID, err := getBackendID(cli, ID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if !*noTrunc {
|
||||
ID = stringid.TruncateID(ID)
|
||||
bkID = stringid.TruncateID(bkID)
|
||||
}
|
||||
if *quiet {
|
||||
if !*quiet {
|
||||
fmt.Fprintf(wr, "%s\t%s\t%s\t%s\n", ID, sr.Name, sr.Network, bkID)
|
||||
} else {
|
||||
fmt.Fprintln(wr, ID)
|
||||
continue
|
||||
}
|
||||
network := networkResource.Network
|
||||
fmt.Fprintf(wr, "%s\t%s\t%s",
|
||||
ID,
|
||||
netName,
|
||||
network)
|
||||
fmt.Fprint(wr, "\n")
|
||||
}
|
||||
wr.Flush()
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func getBackendID(cli *NetworkCli, servID string) (string, error) {
|
||||
var (
|
||||
obj []byte
|
||||
err error
|
||||
bk string
|
||||
)
|
||||
|
||||
if obj, _, err = readBody(cli.call("GET", "/services/"+servID+"/backend", nil, nil)); err == nil {
|
||||
var bkl []backendResource
|
||||
if err := json.NewDecoder(bytes.NewReader(obj)).Decode(&bkl); err == nil {
|
||||
if len(bkl) > 0 {
|
||||
bk = bkl[0].ID
|
||||
}
|
||||
} else {
|
||||
// Only print a message, don't make the caller cli fail for this
|
||||
fmt.Fprintf(cli.out, "Failed to retrieve provider list for service %s (%v)", servID, err)
|
||||
}
|
||||
}
|
||||
|
||||
return bk, err
|
||||
}
|
||||
|
||||
// CmdServiceInfo handles service info UI
|
||||
func (cli *NetworkCli) CmdServiceInfo(chain string, args ...string) error {
|
||||
cmd := cli.Subcmd(chain, "info", "SERVICE NETWORK", "Displays detailed information on a service", false)
|
||||
cmd := cli.Subcmd(chain, "info", "SERVICE", "Displays detailed information about a service", false)
|
||||
flNetwork := cmd.String([]string{"net", "-network"}, "", "Network where to publish the service")
|
||||
cmd.Require(flag.Min, 1)
|
||||
|
||||
err := cmd.ParseFlags(args, true)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// Default network changes will come later
|
||||
nw := "docker0"
|
||||
if *flNetwork != "" {
|
||||
nw = *flNetwork
|
||||
}
|
||||
|
||||
serviceID, err := lookupServiceID(cli, nw, cmd.Arg(0))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
obj, _, err := readBody(cli.call("GET", "/services/"+serviceID, nil, nil))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
sr := &serviceResource{}
|
||||
if err := json.NewDecoder(bytes.NewReader(obj)).Decode(sr); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
fmt.Fprintf(cli.out, "Service Id: %s\n", sr.ID)
|
||||
fmt.Fprintf(cli.out, "\tName: %s\n", sr.Name)
|
||||
fmt.Fprintf(cli.out, "\tNetwork: %s\n", sr.Network)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// CmdServiceAttach handles service attach UI
|
||||
func (cli *NetworkCli) CmdServiceAttach(chain string, args ...string) error {
|
||||
cmd := cli.Subcmd(chain, "attach", "CONTAINER SERVICE", "Sets a container as a service backend", false)
|
||||
flNetwork := cmd.String([]string{"net", "-network"}, "", "Network where to publish the service")
|
||||
cmd.Require(flag.Min, 2)
|
||||
|
||||
err := cmd.ParseFlags(args, true)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
networkID, err := lookupNetworkID(cli, cmd.Arg(1))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
serviceID, err := lookupServiceID(cli, networkID, cmd.Arg(0))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
obj, _, err := readBody(cli.call("GET", "/networks/"+networkID+"/endpoints/"+serviceID, nil, nil))
|
||||
if err != nil {
|
||||
fmt.Fprintf(cli.err, "%s", err.Error())
|
||||
return err
|
||||
}
|
||||
|
||||
endpointResource := &endpointResource{}
|
||||
if err := json.NewDecoder(bytes.NewReader(obj)).Decode(endpointResource); err != nil {
|
||||
return err
|
||||
}
|
||||
fmt.Fprintf(cli.out, "Service Id: %s\n", endpointResource.ID)
|
||||
fmt.Fprintf(cli.out, "\tName: %s\n", endpointResource.Name)
|
||||
fmt.Fprintf(cli.out, "\tNetwork: %s\n", endpointResource.Network)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// CmdServiceJoin handles service join UI
|
||||
func (cli *NetworkCli) CmdServiceJoin(chain string, args ...string) error {
|
||||
cmd := cli.Subcmd(chain, "join", "CONTAINER SERVICE NETWORK", "Sets a container as a service backend", false)
|
||||
cmd.Require(flag.Min, 3)
|
||||
err := cmd.ParseFlags(args, true)
|
||||
if err != nil {
|
||||
return err
|
||||
// Default network changes will come later
|
||||
nw := "docker0"
|
||||
if *flNetwork != "" {
|
||||
nw = *flNetwork
|
||||
}
|
||||
|
||||
containerID, err := lookupContainerID(cli, cmd.Arg(0))
|
||||
@@ -251,55 +296,49 @@ func (cli *NetworkCli) CmdServiceJoin(chain string, args ...string) error {
|
||||
return err
|
||||
}
|
||||
|
||||
networkID, err := lookupNetworkID(cli, cmd.Arg(2))
|
||||
serviceID, err := lookupServiceID(cli, nw, cmd.Arg(1))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
serviceID, err := lookupServiceID(cli, networkID, cmd.Arg(1))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
nc := serviceAttach{ContainerID: containerID}
|
||||
|
||||
nc := endpointJoin{ContainerID: containerID}
|
||||
_, _, err = readBody(cli.call("POST", "/services/"+serviceID+"/backend", nc, nil))
|
||||
|
||||
_, _, err = readBody(cli.call("POST", "/networks/"+networkID+"/endpoints/"+serviceID+"/containers", nc, nil))
|
||||
if err != nil {
|
||||
fmt.Fprintf(cli.err, "%s", err.Error())
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
return err
|
||||
}
|
||||
|
||||
// CmdServiceLeave handles service leave UI
|
||||
func (cli *NetworkCli) CmdServiceLeave(chain string, args ...string) error {
|
||||
cmd := cli.Subcmd(chain, "leave", "CONTAINER SERVICE NETWORK", "Removes a container from service backend", false)
|
||||
cmd.Require(flag.Min, 3)
|
||||
// CmdServiceDetach handles service detach UI
|
||||
func (cli *NetworkCli) CmdServiceDetach(chain string, args ...string) error {
|
||||
cmd := cli.Subcmd(chain, "detach", "CONTAINER SERVICE", "Removes a container from service backend", false)
|
||||
flNetwork := cmd.String([]string{"net", "-network"}, "", "Network where to publish the service")
|
||||
cmd.Require(flag.Min, 2)
|
||||
|
||||
err := cmd.ParseFlags(args, true)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// Default network changes will come later
|
||||
nw := "docker0"
|
||||
if *flNetwork != "" {
|
||||
nw = *flNetwork
|
||||
}
|
||||
|
||||
containerID, err := lookupContainerID(cli, cmd.Arg(0))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
networkID, err := lookupNetworkID(cli, cmd.Arg(2))
|
||||
serviceID, err := lookupServiceID(cli, nw, cmd.Arg(1))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
serviceID, err := lookupServiceID(cli, networkID, cmd.Arg(1))
|
||||
_, _, err = readBody(cli.call("DELETE", "/services/"+serviceID+"/backend/"+containerID, nil, nil))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
_, _, err = readBody(cli.call("DELETE", "/networks/"+networkID+"/endpoints/"+serviceID+"/containers/"+containerID, nil, nil))
|
||||
if err != nil {
|
||||
fmt.Fprintf(cli.err, "%s", err.Error())
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
|
||||
@@ -8,19 +8,24 @@ import "github.com/docker/libnetwork/types"
|
||||
|
||||
// networkResource is the body of the "get network" http response message
|
||||
type networkResource struct {
|
||||
Name string `json:"name"`
|
||||
ID string `json:"id"`
|
||||
Type string `json:"type"`
|
||||
Endpoints []*endpointResource `json:"endpoints"`
|
||||
Name string `json:"name"`
|
||||
ID string `json:"id"`
|
||||
Type string `json:"type"`
|
||||
Services []*serviceResource `json:"services"`
|
||||
}
|
||||
|
||||
// endpointResource is the body of the "get endpoint" http response message
|
||||
type endpointResource struct {
|
||||
// serviceResource is the body of the "get service" http response message
|
||||
type serviceResource struct {
|
||||
Name string `json:"name"`
|
||||
ID string `json:"id"`
|
||||
Network string `json:"network"`
|
||||
}
|
||||
|
||||
// backendResource is the body of "get service backend" response message
|
||||
type backendResource struct {
|
||||
ID string `json:"id"`
|
||||
}
|
||||
|
||||
/***********
|
||||
Body types
|
||||
************/
|
||||
@@ -32,37 +37,37 @@ type networkCreate struct {
|
||||
Options map[string]interface{} `json:"options"`
|
||||
}
|
||||
|
||||
// endpointCreate represents the body of the "create endpoint" http request message
|
||||
type endpointCreate struct {
|
||||
// serviceCreate represents the body of the "publish service" http request message
|
||||
type serviceCreate struct {
|
||||
Name string `json:"name"`
|
||||
NetworkID string `json:"network_id"`
|
||||
Network string `json:"network_name"`
|
||||
ExposedPorts []types.TransportPort `json:"exposed_ports"`
|
||||
PortMapping []types.PortBinding `json:"port_mapping"`
|
||||
}
|
||||
|
||||
// endpointJoin represents the expected body of the "join endpoint" or "leave endpoint" http request messages
|
||||
type endpointJoin struct {
|
||||
ContainerID string `json:"container_id"`
|
||||
HostName string `json:"host_name"`
|
||||
DomainName string `json:"domain_name"`
|
||||
HostsPath string `json:"hosts_path"`
|
||||
ResolvConfPath string `json:"resolv_conf_path"`
|
||||
DNS []string `json:"dns"`
|
||||
ExtraHosts []endpointExtraHost `json:"extra_hosts"`
|
||||
ParentUpdates []endpointParentUpdate `json:"parent_updates"`
|
||||
UseDefaultSandbox bool `json:"use_default_sandbox"`
|
||||
// serviceAttach represents the expected body of the "attach/detach backend to/from service" http request messages
|
||||
type serviceAttach struct {
|
||||
ContainerID string `json:"container_id"`
|
||||
HostName string `json:"host_name"`
|
||||
DomainName string `json:"domain_name"`
|
||||
HostsPath string `json:"hosts_path"`
|
||||
ResolvConfPath string `json:"resolv_conf_path"`
|
||||
DNS []string `json:"dns"`
|
||||
ExtraHosts []serviceExtraHost `json:"extra_hosts"`
|
||||
ParentUpdates []serviceParentUpdate `json:"parent_updates"`
|
||||
UseDefaultSandbox bool `json:"use_default_sandbox"`
|
||||
}
|
||||
|
||||
// EndpointExtraHost represents the extra host object
|
||||
type endpointExtraHost struct {
|
||||
// serviceExtraHost represents the extra host object
|
||||
type serviceExtraHost struct {
|
||||
Name string `json:"name"`
|
||||
Address string `json:"address"`
|
||||
}
|
||||
|
||||
// EndpointParentUpdate is the object carrying the information about the
|
||||
// endpoint parent that needs to be updated
|
||||
type endpointParentUpdate struct {
|
||||
EndpointID string `json:"endpoint_id"`
|
||||
type serviceParentUpdate struct {
|
||||
EndpointID string `json:"service_id"`
|
||||
Name string `json:"name"`
|
||||
Address string `json:"address"`
|
||||
}
|
||||
|
||||
BIN
libnetwork/cmd/dnet/dnet
Executable file
BIN
libnetwork/cmd/dnet/dnet
Executable file
Binary file not shown.
@@ -122,6 +122,10 @@ func (d *dnetConnection) dnetDaemon() error {
|
||||
post.Methods("GET", "PUT", "POST", "DELETE").HandlerFunc(httpHandler)
|
||||
post = r.PathPrefix("/networks").Subrouter()
|
||||
post.Methods("GET", "PUT", "POST", "DELETE").HandlerFunc(httpHandler)
|
||||
post = r.PathPrefix("/{.*}/services").Subrouter()
|
||||
post.Methods("GET", "PUT", "POST", "DELETE").HandlerFunc(httpHandler)
|
||||
post = r.PathPrefix("/services").Subrouter()
|
||||
post.Methods("GET", "PUT", "POST", "DELETE").HandlerFunc(httpHandler)
|
||||
return http.ListenAndServe(d.addr, r)
|
||||
}
|
||||
|
||||
|
||||
@@ -46,6 +46,9 @@ type Endpoint interface {
|
||||
// DriverInfo returns a collection of driver operational data related to this endpoint retrieved from the driver
|
||||
DriverInfo() (map[string]interface{}, error)
|
||||
|
||||
// ContainerInfo returns the info available at the endpoint about the attached container
|
||||
ContainerInfo() ContainerInfo
|
||||
|
||||
// Delete and detaches this endpoint from the network.
|
||||
Delete() error
|
||||
}
|
||||
@@ -102,6 +105,14 @@ type containerInfo struct {
|
||||
sync.Mutex
|
||||
}
|
||||
|
||||
func (ci *containerInfo) ID() string {
|
||||
return ci.id
|
||||
}
|
||||
|
||||
func (ci *containerInfo) Labels() map[string]interface{} {
|
||||
return ci.config.generic
|
||||
}
|
||||
|
||||
type endpoint struct {
|
||||
name string
|
||||
id types.UUID
|
||||
|
||||
@@ -40,6 +40,14 @@ type InterfaceInfo interface {
|
||||
AddressIPv6() net.IPNet
|
||||
}
|
||||
|
||||
// ContainerInfo provides an interface to retrieve the info about the container attached to the endpoint
|
||||
type ContainerInfo interface {
|
||||
// ID returns the ID of the container
|
||||
ID() string
|
||||
// Labels returns the container's labels
|
||||
Labels() map[string]interface{}
|
||||
}
|
||||
|
||||
type endpointInterface struct {
|
||||
id int
|
||||
mac net.HardwareAddr
|
||||
@@ -111,6 +119,18 @@ type endpointJoinInfo struct {
|
||||
StaticRoutes []*types.StaticRoute
|
||||
}
|
||||
|
||||
func (ep *endpoint) ContainerInfo() ContainerInfo {
|
||||
ep.Lock()
|
||||
ci := ep.container
|
||||
defer ep.Unlock()
|
||||
|
||||
// Need this since we return the interface
|
||||
if ci == nil {
|
||||
return nil
|
||||
}
|
||||
return ci
|
||||
}
|
||||
|
||||
func (ep *endpoint) Info() EndpointInfo {
|
||||
return ep
|
||||
}
|
||||
|
||||
@@ -11,8 +11,8 @@ func (nsn ErrNoSuchNetwork) Error() string {
|
||||
return fmt.Sprintf("network %s not found", string(nsn))
|
||||
}
|
||||
|
||||
// BadRequest denotes the type of this error
|
||||
func (nsn ErrNoSuchNetwork) BadRequest() {}
|
||||
// NotFound denotes the type of this error
|
||||
func (nsn ErrNoSuchNetwork) NotFound() {}
|
||||
|
||||
// ErrNoSuchEndpoint is returned when a endpoint query finds no result
|
||||
type ErrNoSuchEndpoint string
|
||||
@@ -21,8 +21,8 @@ func (nse ErrNoSuchEndpoint) Error() string {
|
||||
return fmt.Sprintf("endpoint %s not found", string(nse))
|
||||
}
|
||||
|
||||
// BadRequest denotes the type of this error
|
||||
func (nse ErrNoSuchEndpoint) BadRequest() {}
|
||||
// NotFound denotes the type of this error
|
||||
func (nse ErrNoSuchEndpoint) NotFound() {}
|
||||
|
||||
// ErrInvalidNetworkDriver is returned if an invalid driver
|
||||
// name is passed.
|
||||
|
||||
@@ -1045,6 +1045,11 @@ func TestEndpointJoin(t *testing.T) {
|
||||
t.Fatalf("Expected an non-empty sandbox key for a joined endpoint. Instead found a empty sandbox key")
|
||||
}
|
||||
|
||||
// Check endpoint provided container information
|
||||
if ep1.ContainerInfo().ID() != containerID {
|
||||
t.Fatalf("Endpoint ContainerInfo returned unexpected id: %s", ep1.ContainerInfo().ID())
|
||||
}
|
||||
|
||||
// Now test the container joining another network
|
||||
n2, err := createTestNetwork(bridgeNetType, "testnetwork2",
|
||||
options.Generic{
|
||||
@@ -1077,6 +1082,10 @@ func TestEndpointJoin(t *testing.T) {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
if ep1.ContainerInfo().ID() != ep2.ContainerInfo().ID() {
|
||||
t.Fatalf("ep1 and ep2 returned different container info")
|
||||
}
|
||||
|
||||
defer func() {
|
||||
err = ep2.Leave(containerID)
|
||||
if err != nil {
|
||||
|
||||
Reference in New Issue
Block a user