2015-09-25 06:19:17 -04:00
|
|
|
package network
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
|
|
|
|
"golang.org/x/net/context"
|
|
|
|
|
|
|
|
"github.com/docker/docker/api/server/httputils"
|
|
|
|
"github.com/docker/docker/api/types"
|
2015-12-15 16:40:11 -05:00
|
|
|
"github.com/docker/docker/api/types/filters"
|
2015-12-09 16:38:39 -05:00
|
|
|
"github.com/docker/docker/api/types/network"
|
2015-09-25 06:19:17 -04:00
|
|
|
"github.com/docker/docker/daemon"
|
2015-10-25 19:09:54 -04:00
|
|
|
"github.com/docker/docker/runconfig"
|
2015-09-25 06:19:17 -04:00
|
|
|
"github.com/docker/libnetwork"
|
|
|
|
)
|
|
|
|
|
|
|
|
func (n *networkRouter) getNetworksList(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
|
|
|
|
if err := httputils.ParseForm(r); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
filter := r.Form.Get("filters")
|
|
|
|
netFilters, err := filters.FromParam(filter)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-11-10 03:57:06 -05:00
|
|
|
if netFilters.Len() != 0 {
|
|
|
|
if err := netFilters.Validate(acceptedFilters); err != nil {
|
|
|
|
return err
|
2015-09-25 06:19:17 -04:00
|
|
|
}
|
2015-11-10 03:57:06 -05:00
|
|
|
}
|
2015-09-25 06:19:17 -04:00
|
|
|
|
2015-11-10 03:57:06 -05:00
|
|
|
list := []*types.NetworkResource{}
|
2015-09-25 06:19:17 -04:00
|
|
|
|
2015-11-10 03:57:06 -05:00
|
|
|
nwList := n.backend.GetAllNetworks()
|
|
|
|
displayable, err := filterNetworks(nwList, netFilters)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2015-09-25 06:19:17 -04:00
|
|
|
}
|
2015-11-10 03:57:06 -05:00
|
|
|
|
|
|
|
for _, nw := range displayable {
|
|
|
|
list = append(list, buildNetworkResource(nw))
|
|
|
|
}
|
|
|
|
|
2015-09-25 06:19:17 -04:00
|
|
|
return httputils.WriteJSON(w, http.StatusOK, list)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n *networkRouter) getNetwork(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
|
|
|
|
if err := httputils.ParseForm(r); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-10-26 17:47:48 -04:00
|
|
|
nw, err := n.backend.FindNetwork(vars["id"])
|
2015-09-25 06:19:17 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return httputils.WriteJSON(w, http.StatusOK, buildNetworkResource(nw))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n *networkRouter) postNetworkCreate(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
|
|
|
|
var create types.NetworkCreate
|
|
|
|
var warning string
|
|
|
|
|
|
|
|
if err := httputils.ParseForm(r); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := httputils.CheckForJSON(r); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := json.NewDecoder(r.Body).Decode(&create); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-10-25 19:09:54 -04:00
|
|
|
if runconfig.IsPreDefinedNetwork(create.Name) {
|
|
|
|
return httputils.WriteJSON(w, http.StatusForbidden,
|
|
|
|
fmt.Sprintf("%s is a pre-defined network and cannot be created", create.Name))
|
|
|
|
}
|
|
|
|
|
2015-10-26 17:47:48 -04:00
|
|
|
nw, err := n.backend.GetNetwork(create.Name, daemon.NetworkByName)
|
2015-09-25 06:19:17 -04:00
|
|
|
if _, ok := err.(libnetwork.ErrNoSuchNetwork); err != nil && !ok {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if nw != nil {
|
|
|
|
if create.CheckDuplicate {
|
|
|
|
return libnetwork.NetworkNameError(create.Name)
|
|
|
|
}
|
|
|
|
warning = fmt.Sprintf("Network with name %s (id : %s) already exists", nw.Name(), nw.ID())
|
|
|
|
}
|
|
|
|
|
2015-10-26 17:47:48 -04:00
|
|
|
nw, err = n.backend.CreateNetwork(create.Name, create.Driver, create.IPAM, create.Options)
|
2015-09-25 06:19:17 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return httputils.WriteJSON(w, http.StatusCreated, &types.NetworkCreateResponse{
|
|
|
|
ID: nw.ID(),
|
|
|
|
Warning: warning,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n *networkRouter) postNetworkConnect(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
|
|
|
|
var connect types.NetworkConnect
|
|
|
|
if err := httputils.ParseForm(r); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := httputils.CheckForJSON(r); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := json.NewDecoder(r.Body).Decode(&connect); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-10-26 17:47:48 -04:00
|
|
|
nw, err := n.backend.FindNetwork(vars["id"])
|
2015-09-25 06:19:17 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-10-26 17:47:48 -04:00
|
|
|
return n.backend.ConnectContainerToNetwork(connect.Container, nw.Name())
|
2015-09-25 06:19:17 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (n *networkRouter) postNetworkDisconnect(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
|
|
|
|
var disconnect types.NetworkDisconnect
|
|
|
|
if err := httputils.ParseForm(r); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := httputils.CheckForJSON(r); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := json.NewDecoder(r.Body).Decode(&disconnect); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-10-26 17:47:48 -04:00
|
|
|
nw, err := n.backend.FindNetwork(vars["id"])
|
2015-09-25 06:19:17 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-10-26 17:47:48 -04:00
|
|
|
return n.backend.DisconnectContainerFromNetwork(disconnect.Container, nw)
|
2015-09-25 06:19:17 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (n *networkRouter) deleteNetwork(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
|
2015-12-21 23:35:30 -05:00
|
|
|
return n.backend.DeleteNetwork(vars["id"])
|
2015-09-25 06:19:17 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func buildNetworkResource(nw libnetwork.Network) *types.NetworkResource {
|
|
|
|
r := &types.NetworkResource{}
|
|
|
|
if nw == nil {
|
|
|
|
return r
|
|
|
|
}
|
|
|
|
|
|
|
|
r.Name = nw.Name()
|
|
|
|
r.ID = nw.ID()
|
2015-10-09 14:21:48 -04:00
|
|
|
r.Scope = nw.Info().Scope()
|
2015-09-25 06:19:17 -04:00
|
|
|
r.Driver = nw.Type()
|
2015-10-14 21:49:27 -04:00
|
|
|
r.Options = nw.Info().DriverOptions()
|
2015-09-25 06:19:17 -04:00
|
|
|
r.Containers = make(map[string]types.EndpointResource)
|
2015-10-09 14:21:48 -04:00
|
|
|
buildIpamResources(r, nw)
|
|
|
|
|
2015-09-25 06:19:17 -04:00
|
|
|
epl := nw.Endpoints()
|
|
|
|
for _, e := range epl {
|
2015-11-03 17:15:34 -05:00
|
|
|
ei := e.Info()
|
|
|
|
if ei == nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
sb := ei.Sandbox()
|
2015-09-25 06:19:17 -04:00
|
|
|
if sb == nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
r.Containers[sb.ContainerID()] = buildEndpointResource(e)
|
|
|
|
}
|
|
|
|
return r
|
|
|
|
}
|
|
|
|
|
2015-10-09 14:21:48 -04:00
|
|
|
func buildIpamResources(r *types.NetworkResource, nw libnetwork.Network) {
|
|
|
|
id, ipv4conf, ipv6conf := nw.Info().IpamConfig()
|
|
|
|
|
|
|
|
r.IPAM.Driver = id
|
|
|
|
|
|
|
|
r.IPAM.Config = []network.IPAMConfig{}
|
|
|
|
for _, ip4 := range ipv4conf {
|
|
|
|
iData := network.IPAMConfig{}
|
|
|
|
iData.Subnet = ip4.PreferredPool
|
|
|
|
iData.IPRange = ip4.SubPool
|
|
|
|
iData.Gateway = ip4.Gateway
|
|
|
|
iData.AuxAddress = ip4.AuxAddresses
|
|
|
|
r.IPAM.Config = append(r.IPAM.Config, iData)
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, ip6 := range ipv6conf {
|
|
|
|
iData := network.IPAMConfig{}
|
|
|
|
iData.Subnet = ip6.PreferredPool
|
|
|
|
iData.IPRange = ip6.SubPool
|
|
|
|
iData.Gateway = ip6.Gateway
|
|
|
|
iData.AuxAddress = ip6.AuxAddresses
|
|
|
|
r.IPAM.Config = append(r.IPAM.Config, iData)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-25 06:19:17 -04:00
|
|
|
func buildEndpointResource(e libnetwork.Endpoint) types.EndpointResource {
|
|
|
|
er := types.EndpointResource{}
|
|
|
|
if e == nil {
|
|
|
|
return er
|
|
|
|
}
|
|
|
|
|
|
|
|
er.EndpointID = e.ID()
|
2015-11-02 11:29:58 -05:00
|
|
|
er.Name = e.Name()
|
2015-11-03 17:15:34 -05:00
|
|
|
ei := e.Info()
|
|
|
|
if ei == nil {
|
|
|
|
return er
|
|
|
|
}
|
|
|
|
|
|
|
|
if iface := ei.Iface(); iface != nil {
|
2015-09-25 06:19:17 -04:00
|
|
|
if mac := iface.MacAddress(); mac != nil {
|
|
|
|
er.MacAddress = mac.String()
|
|
|
|
}
|
2015-10-10 12:43:03 -04:00
|
|
|
if ip := iface.Address(); ip != nil && len(ip.IP) > 0 {
|
|
|
|
er.IPv4Address = ip.String()
|
2015-09-25 06:19:17 -04:00
|
|
|
}
|
|
|
|
|
2015-10-10 12:43:03 -04:00
|
|
|
if ipv6 := iface.AddressIPv6(); ipv6 != nil && len(ipv6.IP) > 0 {
|
|
|
|
er.IPv6Address = ipv6.String()
|
2015-09-25 06:19:17 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return er
|
|
|
|
}
|