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