github.com/ncdc/docker@v0.10.1-0.20160129113957-6c6729ef5b74/api/server/router/network/network_routes.go (about)

     1  package network
     2  
     3  import (
     4  	"encoding/json"
     5  	"fmt"
     6  	"net/http"
     7  
     8  	"golang.org/x/net/context"
     9  
    10  	"github.com/docker/docker/api/server/httputils"
    11  	"github.com/docker/docker/daemon"
    12  	"github.com/docker/docker/runconfig"
    13  	"github.com/docker/engine-api/types"
    14  	"github.com/docker/engine-api/types/filters"
    15  	"github.com/docker/engine-api/types/network"
    16  	"github.com/docker/libnetwork"
    17  )
    18  
    19  func (n *networkRouter) getNetworksList(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
    20  	if err := httputils.ParseForm(r); err != nil {
    21  		return err
    22  	}
    23  
    24  	filter := r.Form.Get("filters")
    25  	netFilters, err := filters.FromParam(filter)
    26  	if err != nil {
    27  		return err
    28  	}
    29  
    30  	if netFilters.Len() != 0 {
    31  		if err := netFilters.Validate(acceptedFilters); err != nil {
    32  			return err
    33  		}
    34  	}
    35  
    36  	list := []*types.NetworkResource{}
    37  
    38  	nwList := n.backend.GetAllNetworks()
    39  	displayable, err := filterNetworks(nwList, netFilters)
    40  	if err != nil {
    41  		return err
    42  	}
    43  
    44  	for _, nw := range displayable {
    45  		list = append(list, buildNetworkResource(nw))
    46  	}
    47  
    48  	return httputils.WriteJSON(w, http.StatusOK, list)
    49  }
    50  
    51  func (n *networkRouter) getNetwork(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
    52  	if err := httputils.ParseForm(r); err != nil {
    53  		return err
    54  	}
    55  
    56  	nw, err := n.backend.FindNetwork(vars["id"])
    57  	if err != nil {
    58  		return err
    59  	}
    60  	return httputils.WriteJSON(w, http.StatusOK, buildNetworkResource(nw))
    61  }
    62  
    63  func (n *networkRouter) postNetworkCreate(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
    64  	var create types.NetworkCreate
    65  	var warning string
    66  
    67  	if err := httputils.ParseForm(r); err != nil {
    68  		return err
    69  	}
    70  
    71  	if err := httputils.CheckForJSON(r); err != nil {
    72  		return err
    73  	}
    74  
    75  	if err := json.NewDecoder(r.Body).Decode(&create); err != nil {
    76  		return err
    77  	}
    78  
    79  	if runconfig.IsPreDefinedNetwork(create.Name) {
    80  		return httputils.WriteJSON(w, http.StatusForbidden,
    81  			fmt.Sprintf("%s is a pre-defined network and cannot be created", create.Name))
    82  	}
    83  
    84  	nw, err := n.backend.GetNetwork(create.Name, daemon.NetworkByName)
    85  	if _, ok := err.(libnetwork.ErrNoSuchNetwork); err != nil && !ok {
    86  		return err
    87  	}
    88  	if nw != nil {
    89  		if create.CheckDuplicate {
    90  			return libnetwork.NetworkNameError(create.Name)
    91  		}
    92  		warning = fmt.Sprintf("Network with name %s (id : %s) already exists", nw.Name(), nw.ID())
    93  	}
    94  
    95  	nw, err = n.backend.CreateNetwork(create.Name, create.Driver, create.IPAM, create.Options, create.Internal)
    96  	if err != nil {
    97  		return err
    98  	}
    99  
   100  	return httputils.WriteJSON(w, http.StatusCreated, &types.NetworkCreateResponse{
   101  		ID:      nw.ID(),
   102  		Warning: warning,
   103  	})
   104  }
   105  
   106  func (n *networkRouter) postNetworkConnect(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
   107  	var connect types.NetworkConnect
   108  	if err := httputils.ParseForm(r); err != nil {
   109  		return err
   110  	}
   111  
   112  	if err := httputils.CheckForJSON(r); err != nil {
   113  		return err
   114  	}
   115  
   116  	if err := json.NewDecoder(r.Body).Decode(&connect); err != nil {
   117  		return err
   118  	}
   119  
   120  	nw, err := n.backend.FindNetwork(vars["id"])
   121  	if err != nil {
   122  		return err
   123  	}
   124  
   125  	return n.backend.ConnectContainerToNetwork(connect.Container, nw.Name(), connect.EndpointConfig)
   126  }
   127  
   128  func (n *networkRouter) postNetworkDisconnect(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
   129  	var disconnect types.NetworkDisconnect
   130  	if err := httputils.ParseForm(r); err != nil {
   131  		return err
   132  	}
   133  
   134  	if err := httputils.CheckForJSON(r); err != nil {
   135  		return err
   136  	}
   137  
   138  	if err := json.NewDecoder(r.Body).Decode(&disconnect); err != nil {
   139  		return err
   140  	}
   141  
   142  	nw, err := n.backend.FindNetwork(vars["id"])
   143  	if err != nil {
   144  		return err
   145  	}
   146  
   147  	return n.backend.DisconnectContainerFromNetwork(disconnect.Container, nw, disconnect.Force)
   148  }
   149  
   150  func (n *networkRouter) deleteNetwork(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
   151  	return n.backend.DeleteNetwork(vars["id"])
   152  }
   153  
   154  func buildNetworkResource(nw libnetwork.Network) *types.NetworkResource {
   155  	r := &types.NetworkResource{}
   156  	if nw == nil {
   157  		return r
   158  	}
   159  
   160  	r.Name = nw.Name()
   161  	r.ID = nw.ID()
   162  	r.Scope = nw.Info().Scope()
   163  	r.Driver = nw.Type()
   164  	r.Options = nw.Info().DriverOptions()
   165  	r.Containers = make(map[string]types.EndpointResource)
   166  	buildIpamResources(r, nw)
   167  
   168  	epl := nw.Endpoints()
   169  	for _, e := range epl {
   170  		ei := e.Info()
   171  		if ei == nil {
   172  			continue
   173  		}
   174  		sb := ei.Sandbox()
   175  		if sb == nil {
   176  			continue
   177  		}
   178  
   179  		r.Containers[sb.ContainerID()] = buildEndpointResource(e)
   180  	}
   181  	return r
   182  }
   183  
   184  func buildIpamResources(r *types.NetworkResource, nw libnetwork.Network) {
   185  	id, opts, ipv4conf, ipv6conf := nw.Info().IpamConfig()
   186  
   187  	ipv4Info, ipv6Info := nw.Info().IpamInfo()
   188  
   189  	r.IPAM.Driver = id
   190  
   191  	r.IPAM.Options = opts
   192  
   193  	r.IPAM.Config = []network.IPAMConfig{}
   194  	for _, ip4 := range ipv4conf {
   195  		if ip4.PreferredPool == "" {
   196  			continue
   197  		}
   198  		iData := network.IPAMConfig{}
   199  		iData.Subnet = ip4.PreferredPool
   200  		iData.IPRange = ip4.SubPool
   201  		iData.Gateway = ip4.Gateway
   202  		iData.AuxAddress = ip4.AuxAddresses
   203  		r.IPAM.Config = append(r.IPAM.Config, iData)
   204  	}
   205  
   206  	if len(r.IPAM.Config) == 0 {
   207  		for _, ip4Info := range ipv4Info {
   208  			iData := network.IPAMConfig{}
   209  			iData.Subnet = ip4Info.IPAMData.Pool.String()
   210  			iData.Gateway = ip4Info.IPAMData.Gateway.String()
   211  			r.IPAM.Config = append(r.IPAM.Config, iData)
   212  		}
   213  	}
   214  
   215  	hasIpv6Conf := false
   216  	for _, ip6 := range ipv6conf {
   217  		if ip6.PreferredPool == "" {
   218  			continue
   219  		}
   220  		hasIpv6Conf = true
   221  		iData := network.IPAMConfig{}
   222  		iData.Subnet = ip6.PreferredPool
   223  		iData.IPRange = ip6.SubPool
   224  		iData.Gateway = ip6.Gateway
   225  		iData.AuxAddress = ip6.AuxAddresses
   226  		r.IPAM.Config = append(r.IPAM.Config, iData)
   227  	}
   228  
   229  	if !hasIpv6Conf {
   230  		for _, ip6Info := range ipv6Info {
   231  			iData := network.IPAMConfig{}
   232  			iData.Subnet = ip6Info.IPAMData.Pool.String()
   233  			iData.Gateway = ip6Info.IPAMData.Gateway.String()
   234  			r.IPAM.Config = append(r.IPAM.Config, iData)
   235  		}
   236  	}
   237  }
   238  
   239  func buildEndpointResource(e libnetwork.Endpoint) types.EndpointResource {
   240  	er := types.EndpointResource{}
   241  	if e == nil {
   242  		return er
   243  	}
   244  
   245  	er.EndpointID = e.ID()
   246  	er.Name = e.Name()
   247  	ei := e.Info()
   248  	if ei == nil {
   249  		return er
   250  	}
   251  
   252  	if iface := ei.Iface(); iface != nil {
   253  		if mac := iface.MacAddress(); mac != nil {
   254  			er.MacAddress = mac.String()
   255  		}
   256  		if ip := iface.Address(); ip != nil && len(ip.IP) > 0 {
   257  			er.IPv4Address = ip.String()
   258  		}
   259  
   260  		if ipv6 := iface.AddressIPv6(); ipv6 != nil && len(ipv6.IP) > 0 {
   261  			er.IPv6Address = ipv6.String()
   262  		}
   263  	}
   264  	return er
   265  }