github.com/goern/docker@v1.9.0-rc1/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) 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.Containers = make(map[string]types.EndpointResource) 186 buildIpamResources(r, nw) 187 188 epl := nw.Endpoints() 189 for _, e := range epl { 190 sb := e.Info().Sandbox() 191 if sb == nil { 192 continue 193 } 194 195 r.Containers[sb.ContainerID()] = buildEndpointResource(e) 196 } 197 return r 198 } 199 200 func buildIpamResources(r *types.NetworkResource, nw libnetwork.Network) { 201 id, ipv4conf, ipv6conf := nw.Info().IpamConfig() 202 203 r.IPAM.Driver = id 204 205 r.IPAM.Config = []network.IPAMConfig{} 206 for _, ip4 := range ipv4conf { 207 iData := network.IPAMConfig{} 208 iData.Subnet = ip4.PreferredPool 209 iData.IPRange = ip4.SubPool 210 iData.Gateway = ip4.Gateway 211 iData.AuxAddress = ip4.AuxAddresses 212 r.IPAM.Config = append(r.IPAM.Config, iData) 213 } 214 215 for _, ip6 := range ipv6conf { 216 iData := network.IPAMConfig{} 217 iData.Subnet = ip6.PreferredPool 218 iData.IPRange = ip6.SubPool 219 iData.Gateway = ip6.Gateway 220 iData.AuxAddress = ip6.AuxAddresses 221 r.IPAM.Config = append(r.IPAM.Config, iData) 222 } 223 } 224 225 func buildEndpointResource(e libnetwork.Endpoint) types.EndpointResource { 226 er := types.EndpointResource{} 227 if e == nil { 228 return er 229 } 230 231 er.EndpointID = e.ID() 232 if iface := e.Info().Iface(); iface != nil { 233 if mac := iface.MacAddress(); mac != nil { 234 er.MacAddress = mac.String() 235 } 236 if ip := iface.Address(); ip != nil && len(ip.IP) > 0 { 237 er.IPv4Address = ip.String() 238 } 239 240 if ipv6 := iface.AddressIPv6(); ipv6 != nil && len(ipv6.IP) > 0 { 241 er.IPv6Address = ipv6.String() 242 } 243 } 244 return er 245 }