github.com/a4a881d4/docker@v1.9.0-rc2/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/Sirupsen/logrus"
    11  	"github.com/docker/docker/api/server/httputils"
    12  	"github.com/docker/docker/api/types"
    13  	"github.com/docker/docker/daemon"
    14  	"github.com/docker/docker/daemon/network"
    15  	"github.com/docker/docker/pkg/parsers/filters"
    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  	list := []*types.NetworkResource{}
    31  	var nameFilter, idFilter bool
    32  	var names, ids []string
    33  	if names, nameFilter = netFilters["name"]; nameFilter {
    34  		for _, name := range names {
    35  			if nw, err := n.daemon.GetNetwork(name, daemon.NetworkByName); err == nil {
    36  				list = append(list, buildNetworkResource(nw))
    37  			} else {
    38  				logrus.Errorf("failed to get network for filter=%s : %v", name, err)
    39  			}
    40  		}
    41  	}
    42  
    43  	if ids, idFilter = netFilters["id"]; idFilter {
    44  		for _, id := range ids {
    45  			for _, nw := range n.daemon.GetNetworksByID(id) {
    46  				list = append(list, buildNetworkResource(nw))
    47  			}
    48  		}
    49  	}
    50  
    51  	if !nameFilter && !idFilter {
    52  		nwList := n.daemon.GetNetworksByID("")
    53  		for _, nw := range nwList {
    54  			list = append(list, buildNetworkResource(nw))
    55  		}
    56  	}
    57  	return httputils.WriteJSON(w, http.StatusOK, list)
    58  }
    59  
    60  func (n *networkRouter) getNetwork(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
    61  	if err := httputils.ParseForm(r); err != nil {
    62  		return err
    63  	}
    64  
    65  	nw, err := n.daemon.FindNetwork(vars["id"])
    66  	if err != nil {
    67  		return err
    68  	}
    69  	return httputils.WriteJSON(w, http.StatusOK, buildNetworkResource(nw))
    70  }
    71  
    72  func (n *networkRouter) postNetworkCreate(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
    73  	var create types.NetworkCreate
    74  	var warning string
    75  
    76  	if err := httputils.ParseForm(r); err != nil {
    77  		return err
    78  	}
    79  
    80  	if err := httputils.CheckForJSON(r); err != nil {
    81  		return err
    82  	}
    83  
    84  	if err := json.NewDecoder(r.Body).Decode(&create); err != nil {
    85  		return err
    86  	}
    87  
    88  	nw, err := n.daemon.GetNetwork(create.Name, daemon.NetworkByName)
    89  	if _, ok := err.(libnetwork.ErrNoSuchNetwork); err != nil && !ok {
    90  		return err
    91  	}
    92  	if nw != nil {
    93  		if create.CheckDuplicate {
    94  			return libnetwork.NetworkNameError(create.Name)
    95  		}
    96  		warning = fmt.Sprintf("Network with name %s (id : %s) already exists", nw.Name(), nw.ID())
    97  	}
    98  
    99  	nw, err = n.daemon.CreateNetwork(create.Name, create.Driver, create.IPAM, create.Options)
   100  	if err != nil {
   101  		return err
   102  	}
   103  
   104  	return httputils.WriteJSON(w, http.StatusCreated, &types.NetworkCreateResponse{
   105  		ID:      nw.ID(),
   106  		Warning: warning,
   107  	})
   108  }
   109  
   110  func (n *networkRouter) postNetworkConnect(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
   111  	var connect types.NetworkConnect
   112  	if err := httputils.ParseForm(r); err != nil {
   113  		return err
   114  	}
   115  
   116  	if err := httputils.CheckForJSON(r); err != nil {
   117  		return err
   118  	}
   119  
   120  	if err := json.NewDecoder(r.Body).Decode(&connect); err != nil {
   121  		return err
   122  	}
   123  
   124  	nw, err := n.daemon.FindNetwork(vars["id"])
   125  	if err != nil {
   126  		return err
   127  	}
   128  
   129  	container, err := n.daemon.Get(connect.Container)
   130  	if err != nil {
   131  		return fmt.Errorf("invalid container %s : %v", container, err)
   132  	}
   133  	return container.ConnectToNetwork(nw.Name())
   134  }
   135  
   136  func (n *networkRouter) postNetworkDisconnect(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
   137  	var disconnect types.NetworkDisconnect
   138  	if err := httputils.ParseForm(r); err != nil {
   139  		return err
   140  	}
   141  
   142  	if err := httputils.CheckForJSON(r); err != nil {
   143  		return err
   144  	}
   145  
   146  	if err := json.NewDecoder(r.Body).Decode(&disconnect); err != nil {
   147  		return err
   148  	}
   149  
   150  	nw, err := n.daemon.FindNetwork(vars["id"])
   151  	if err != nil {
   152  		return err
   153  	}
   154  
   155  	container, err := n.daemon.Get(disconnect.Container)
   156  	if err != nil {
   157  		return fmt.Errorf("invalid container %s : %v", container, err)
   158  	}
   159  	return container.DisconnectFromNetwork(nw)
   160  }
   161  
   162  func (n *networkRouter) deleteNetwork(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
   163  	if err := httputils.ParseForm(r); err != nil {
   164  		return err
   165  	}
   166  
   167  	nw, err := n.daemon.FindNetwork(vars["id"])
   168  	if err != nil {
   169  		return err
   170  	}
   171  
   172  	return nw.Delete()
   173  }
   174  
   175  func buildNetworkResource(nw libnetwork.Network) *types.NetworkResource {
   176  	r := &types.NetworkResource{}
   177  	if nw == nil {
   178  		return r
   179  	}
   180  
   181  	r.Name = nw.Name()
   182  	r.ID = nw.ID()
   183  	r.Scope = nw.Info().Scope()
   184  	r.Driver = nw.Type()
   185  	r.Options = nw.Info().DriverOptions()
   186  	r.Containers = make(map[string]types.EndpointResource)
   187  	buildIpamResources(r, nw)
   188  
   189  	epl := nw.Endpoints()
   190  	for _, e := range epl {
   191  		sb := e.Info().Sandbox()
   192  		if sb == nil {
   193  			continue
   194  		}
   195  
   196  		r.Containers[sb.ContainerID()] = buildEndpointResource(e)
   197  	}
   198  	return r
   199  }
   200  
   201  func buildIpamResources(r *types.NetworkResource, nw libnetwork.Network) {
   202  	id, ipv4conf, ipv6conf := nw.Info().IpamConfig()
   203  
   204  	r.IPAM.Driver = id
   205  
   206  	r.IPAM.Config = []network.IPAMConfig{}
   207  	for _, ip4 := range ipv4conf {
   208  		iData := network.IPAMConfig{}
   209  		iData.Subnet = ip4.PreferredPool
   210  		iData.IPRange = ip4.SubPool
   211  		iData.Gateway = ip4.Gateway
   212  		iData.AuxAddress = ip4.AuxAddresses
   213  		r.IPAM.Config = append(r.IPAM.Config, iData)
   214  	}
   215  
   216  	for _, ip6 := range ipv6conf {
   217  		iData := network.IPAMConfig{}
   218  		iData.Subnet = ip6.PreferredPool
   219  		iData.IPRange = ip6.SubPool
   220  		iData.Gateway = ip6.Gateway
   221  		iData.AuxAddress = ip6.AuxAddresses
   222  		r.IPAM.Config = append(r.IPAM.Config, iData)
   223  	}
   224  }
   225  
   226  func buildEndpointResource(e libnetwork.Endpoint) types.EndpointResource {
   227  	er := types.EndpointResource{}
   228  	if e == nil {
   229  		return er
   230  	}
   231  
   232  	er.EndpointID = e.ID()
   233  	if iface := e.Info().Iface(); iface != nil {
   234  		if mac := iface.MacAddress(); mac != nil {
   235  			er.MacAddress = mac.String()
   236  		}
   237  		if ip := iface.Address(); ip != nil && len(ip.IP) > 0 {
   238  			er.IPv4Address = ip.String()
   239  		}
   240  
   241  		if ipv6 := iface.AddressIPv6(); ipv6 != nil && len(ipv6.IP) > 0 {
   242  			er.IPv6Address = ipv6.String()
   243  		}
   244  	}
   245  	return er
   246  }