github.com/Pankov404/juju@v0.0.0-20150703034450-be266991dceb/apiserver/params/network.go (about) 1 // Copyright 2015 Canonical Ltd. 2 // Licensed under the AGPLv3, see LICENCE file for details. 3 4 package params 5 6 import ( 7 "github.com/juju/juju/network" 8 ) 9 10 // ----- 11 // Parameters field types. 12 // ----- 13 14 // Network describes a single network available on an instance. 15 type Network struct { 16 // Tag is the network's tag. 17 Tag string `json:"Tag"` 18 19 // ProviderId is the provider-specific network id. 20 ProviderId string `json:"ProviderId"` 21 22 // CIDR of the network, in "123.45.67.89/12" format. 23 CIDR string `json:"CIDR"` 24 25 // VLANTag needs to be between 1 and 4094 for VLANs and 0 for 26 // normal networks. It's defined by IEEE 802.1Q standard. 27 VLANTag int `json:"VLANTag"` 28 } 29 30 // NetworkInterface describes a single network interface available on 31 // an instance. 32 type NetworkInterface struct { 33 // MACAddress is the network interface's hardware MAC address 34 // (e.g. "aa:bb:cc:dd:ee:ff"). 35 MACAddress string `json:"MACAddress"` 36 37 // InterfaceName is the OS-specific network device name (e.g. 38 // "eth1", even for for a VLAN eth1.42 virtual interface). 39 InterfaceName string `json:"InterfaceName"` 40 41 // NetworkTag is this interface's network tag. 42 NetworkTag string `json:"NetworkTag"` 43 44 // IsVirtual is true when the interface is a virtual device, as 45 // opposed to a physical device. 46 IsVirtual bool `json:"IsVirtual"` 47 48 // Disabled returns whether the interface is disabled. 49 Disabled bool `json:"Disabled"` 50 } 51 52 // NetworkConfig describes the necessary information to configure 53 // a single network interface on a machine. This mostly duplicates 54 // network.InterfaceInfo type and it's defined here so it can be kept 55 // separate and stable as definition to ensure proper wire-format for 56 // the API. 57 type NetworkConfig struct { 58 // DeviceIndex specifies the order in which the network interface 59 // appears on the host. The primary interface has an index of 0. 60 DeviceIndex int `json:"DeviceIndex"` 61 62 // MACAddress is the network interface's hardware MAC address 63 // (e.g. "aa:bb:cc:dd:ee:ff"). 64 MACAddress string `json:"MACAddress"` 65 66 // CIDR of the network, in 123.45.67.89/24 format. 67 CIDR string `json:"CIDR"` 68 69 // NetworkName is juju-internal name of the network. 70 // TODO(dimitern) This should be removed or adapted to the model 71 // once spaces are introduced. 72 NetworkName string `json:"NetworkName"` 73 74 // ProviderId is a provider-specific network interface id. 75 ProviderId string `json:"ProviderId"` 76 77 // ProviderSubnetId is a provider-specific subnet id, to which the 78 // interface is attached to. 79 ProviderSubnetId string `json:"ProviderSubnetId"` 80 81 // VLANTag needs to be between 1 and 4094 for VLANs and 0 for 82 // normal networks. It's defined by IEEE 802.1Q standard. 83 VLANTag int `json:"VLANTag"` 84 85 // InterfaceName is the raw OS-specific network device name (e.g. 86 // "eth1", even for a VLAN eth1.42 virtual interface). 87 InterfaceName string `json:"InterfaceName"` 88 89 // Disabled is true when the interface needs to be disabled on the 90 // machine, e.g. not to configure it at all or stop it if running. 91 Disabled bool `json:"Disabled"` 92 93 // NoAutoStart is true when the interface should not be configured 94 // to start automatically on boot. By default and for 95 // backwards-compatibility, interfaces are configured to 96 // auto-start. 97 NoAutoStart bool `json:"NoAutoStart,omitempty"` 98 99 // ConfigType, if set, defines what type of configuration to use. 100 // See network.InterfaceConfigType for more info. If not set, for 101 // backwards-compatibility, "dhcp" is assumed. 102 ConfigType string `json:"ConfigType,omitempty"` 103 104 // Address contains an optional static IP address to configure for 105 // this network interface. The subnet mask to set will be inferred 106 // from the CIDR value. 107 Address string `json:"Address,omitempty"` 108 109 // DNSServers contains an optional list of IP addresses and/or 110 // hostnames to configure as DNS servers for this network 111 // interface. 112 DNSServers []string `json:"DNSServers,omitempty"` 113 114 // Gateway address, if set, defines the default gateway to 115 // configure for this network interface. For containers this 116 // usually (one of) the host address(es). 117 GatewayAddress string `json:"GatewayAddress,omitempty"` 118 119 // ExtraConfig can contain any valid setting and its value allowed 120 // inside an "iface" section of a interfaces(5) config file, e.g. 121 // "up", "down", "mtu", etc. 122 ExtraConfig map[string]string `json:"ExtraConfig,omitempty"` 123 } 124 125 // Port encapsulates a protocol and port number. It is used in API 126 // requests/responses. See also network.Port, from/to which this is 127 // transformed. 128 type Port struct { 129 Protocol string `json:"Protocol"` 130 Number int `json:"Number"` 131 } 132 133 // FromNetworkPort is a convenience helper to create a parameter 134 // out of the network type, here for Port. 135 func FromNetworkPort(p network.Port) Port { 136 return Port{ 137 Protocol: p.Protocol, 138 Number: p.Number, 139 } 140 } 141 142 // NetworkPort is a convenience helper to return the parameter 143 // as network type, here for Port. 144 func (p Port) NetworkPort() network.Port { 145 return network.Port{ 146 Protocol: p.Protocol, 147 Number: p.Number, 148 } 149 } 150 151 // PortRange represents a single range of ports. It is used in API 152 // requests/responses. See also network.PortRange, from/to which this is 153 // transformed. 154 type PortRange struct { 155 FromPort int `json:"FromPort"` 156 ToPort int `json:"ToPort"` 157 Protocol string `json:"Protocol"` 158 } 159 160 // FromNetworkPortRange is a convenience helper to create a parameter 161 // out of the network type, here for PortRange. 162 func FromNetworkPortRange(pr network.PortRange) PortRange { 163 return PortRange{ 164 FromPort: pr.FromPort, 165 ToPort: pr.ToPort, 166 Protocol: pr.Protocol, 167 } 168 } 169 170 // NetworkPortRange is a convenience helper to return the parameter 171 // as network type, here for PortRange. 172 func (pr PortRange) NetworkPortRange() network.PortRange { 173 return network.PortRange{ 174 FromPort: pr.FromPort, 175 ToPort: pr.ToPort, 176 Protocol: pr.Protocol, 177 } 178 } 179 180 // EntityPort holds an entity's tag, a protocol and a port. 181 type EntityPort struct { 182 Tag string `json:"Tag"` 183 Protocol string `json:"Protocol"` 184 Port int `json:"Port"` 185 } 186 187 // EntitiesPorts holds the parameters for making an OpenPort or 188 // ClosePort on some entities. 189 type EntitiesPorts struct { 190 Entities []EntityPort `json:"Entities"` 191 } 192 193 // EntityPortRange holds an entity's tag, a protocol and a port range. 194 type EntityPortRange struct { 195 Tag string `json:"Tag"` 196 Protocol string `json:"Protocol"` 197 FromPort int `json:"FromPort"` 198 ToPort int `json:"ToPort"` 199 } 200 201 // EntitiesPortRanges holds the parameters for making an OpenPorts or 202 // ClosePorts on some entities. 203 type EntitiesPortRanges struct { 204 Entities []EntityPortRange `json:"Entities"` 205 } 206 207 // Address represents the location of a machine, including metadata 208 // about what kind of location the address describes. It's used in 209 // the API requests/responses. See also network.Address, from/to 210 // which this is transformed. 211 type Address struct { 212 Value string `json:"Value"` 213 Type string `json:"Type"` 214 NetworkName string `json:"NetworkName"` 215 Scope string `json:"Scope"` 216 } 217 218 // FromNetworkAddress is a convenience helper to create a parameter 219 // out of the network type, here for Address. 220 func FromNetworkAddress(naddr network.Address) Address { 221 return Address{ 222 Value: naddr.Value, 223 Type: string(naddr.Type), 224 NetworkName: naddr.NetworkName, 225 Scope: string(naddr.Scope), 226 } 227 } 228 229 // NetworkAddress is a convenience helper to return the parameter 230 // as network type, here for Address. 231 func (addr Address) NetworkAddress() network.Address { 232 return network.Address{ 233 Value: addr.Value, 234 Type: network.AddressType(addr.Type), 235 NetworkName: addr.NetworkName, 236 Scope: network.Scope(addr.Scope), 237 } 238 } 239 240 // FromNetworkAddresses is a convenience helper to create a parameter 241 // out of the network type, here for a slice of Address. 242 func FromNetworkAddresses(naddrs []network.Address) []Address { 243 addrs := make([]Address, len(naddrs)) 244 for i, naddr := range naddrs { 245 addrs[i] = FromNetworkAddress(naddr) 246 } 247 return addrs 248 } 249 250 // NetworkAddresses is a convenience helper to return the parameter 251 // as network type, here for a slice of Address. 252 func NetworkAddresses(addrs []Address) []network.Address { 253 naddrs := make([]network.Address, len(addrs)) 254 for i, addr := range addrs { 255 naddrs[i] = addr.NetworkAddress() 256 } 257 return naddrs 258 } 259 260 // HostPort associates an address with a port. It's used in 261 // the API requests/responses. See also network.HostPort, from/to 262 // which this is transformed. 263 type HostPort struct { 264 Address 265 Port int `json:"Port"` 266 } 267 268 // FromNetworkHostPort is a convenience helper to create a parameter 269 // out of the network type, here for HostPort. 270 func FromNetworkHostPort(nhp network.HostPort) HostPort { 271 return HostPort{FromNetworkAddress(nhp.Address), nhp.Port} 272 } 273 274 // NetworkHostPort is a convenience helper to return the parameter 275 // as network type, here for HostPort. 276 func (hp HostPort) NetworkHostPort() network.HostPort { 277 return network.HostPort{hp.Address.NetworkAddress(), hp.Port} 278 } 279 280 // FromNetworkHostPorts is a helper to create a parameter 281 // out of the network type, here for a slice of HostPort. 282 func FromNetworkHostPorts(nhps []network.HostPort) []HostPort { 283 hps := make([]HostPort, len(nhps)) 284 for i, nhp := range nhps { 285 hps[i] = FromNetworkHostPort(nhp) 286 } 287 return hps 288 } 289 290 // NetworkHostPorts is a convenience helper to return the parameter 291 // as network type, here for a slice of HostPort. 292 func NetworkHostPorts(hps []HostPort) []network.HostPort { 293 nhps := make([]network.HostPort, len(hps)) 294 for i, hp := range hps { 295 nhps[i] = hp.NetworkHostPort() 296 } 297 return nhps 298 } 299 300 // FromNetworkHostsPorts is a helper to create a parameter 301 // out of the network type, here for a nested slice of HostPort. 302 func FromNetworkHostsPorts(nhpm [][]network.HostPort) [][]HostPort { 303 hpm := make([][]HostPort, len(nhpm)) 304 for i, nhps := range nhpm { 305 hpm[i] = FromNetworkHostPorts(nhps) 306 } 307 return hpm 308 } 309 310 // NetworkHostsPorts is a convenience helper to return the parameter 311 // as network type, here for a nested slice of HostPort. 312 func NetworkHostsPorts(hpm [][]HostPort) [][]network.HostPort { 313 nhpm := make([][]network.HostPort, len(hpm)) 314 for i, hps := range hpm { 315 nhpm[i] = NetworkHostPorts(hps) 316 } 317 return nhpm 318 } 319 320 // MachineAddresses holds an machine tag and addresses. 321 type MachineAddresses struct { 322 Tag string `json:"Tag"` 323 Addresses []Address `json:"Addresses"` 324 } 325 326 // SetMachinesAddresses holds the parameters for making an 327 // API call to update machine addresses. 328 type SetMachinesAddresses struct { 329 MachineAddresses []MachineAddresses `json:"MachineAddresses"` 330 } 331 332 // MachineAddressesResult holds a list of machine addresses or an 333 // error. 334 type MachineAddressesResult struct { 335 Error *Error `json:"Error"` 336 Addresses []Address `json:"Addresses"` 337 } 338 339 // MachineAddressesResults holds the results of calling an API method 340 // returning a list of addresses per machine. 341 type MachineAddressesResults struct { 342 Results []MachineAddressesResult `json:"Results"` 343 } 344 345 // MachinePortRange holds a single port range open on a machine for 346 // the given unit and relation tags. 347 type MachinePortRange struct { 348 UnitTag string `json:"UnitTag"` 349 RelationTag string `json:"RelationTag"` 350 PortRange PortRange `json:"PortRange"` 351 } 352 353 // MachinePorts holds a machine and network tags. It's used when 354 // referring to opened ports on the machine for a network. 355 type MachinePorts struct { 356 MachineTag string `json:"MachineTag"` 357 NetworkTag string `json:"NetworkTag"` 358 } 359 360 // ----- 361 // API request / response types. 362 // ----- 363 364 // PortsResults holds the bulk operation result of an API call 365 // that returns a slice of Port. 366 type PortsResults struct { 367 Results []PortsResult `json:"Results"` 368 } 369 370 // PortsResult holds the result of an API call that returns a slice 371 // of Port or an error. 372 type PortsResult struct { 373 Error *Error `json:"Error"` 374 Ports []Port `json:"Ports"` 375 } 376 377 // RequestedNetworkResult holds requested networks or an error. 378 type RequestedNetworkResult struct { 379 Error *Error `json:"Error"` 380 Networks []string `json:"Networks"` 381 } 382 383 // RequestedNetworksResults holds multiple requested networks results. 384 type RequestedNetworksResults struct { 385 Results []RequestedNetworkResult `json:"Results"` 386 } 387 388 // MachineNetworkConfigResult holds network configuration for a single machine. 389 type MachineNetworkConfigResult struct { 390 Error *Error `json:"Error"` 391 392 // Tagged to Info due to compatibility reasons. 393 Config []NetworkConfig `json:"Info"` 394 } 395 396 // MachineNetworkConfigResults holds network configuration for multiple machines. 397 type MachineNetworkConfigResults struct { 398 Results []MachineNetworkConfigResult `json:"Results"` 399 } 400 401 // MachinePortsParams holds the arguments for making a 402 // FirewallerAPIV1.GetMachinePorts() API call. 403 type MachinePortsParams struct { 404 Params []MachinePorts `json:"Params"` 405 } 406 407 // MachinePortsResult holds a single result of the 408 // FirewallerAPIV1.GetMachinePorts() and UniterAPI.AllMachinePorts() 409 // API calls. 410 type MachinePortsResult struct { 411 Error *Error `json:"Error"` 412 Ports []MachinePortRange `json:"Ports"` 413 } 414 415 // MachinePortsResults holds all the results of the 416 // FirewallerAPIV1.GetMachinePorts() and UniterAPI.AllMachinePorts() 417 // API calls. 418 type MachinePortsResults struct { 419 Results []MachinePortsResult `json:"Results"` 420 } 421 422 // APIHostPortsResult holds the result of an APIHostPorts 423 // call. Each element in the top level slice holds 424 // the addresses for one API server. 425 type APIHostPortsResult struct { 426 Servers [][]HostPort `json:"Servers"` 427 } 428 429 // NetworkHostsPorts is a convenience helper to return the contained 430 // result servers as network type. 431 func (r APIHostPortsResult) NetworkHostsPorts() [][]network.HostPort { 432 return NetworkHostsPorts(r.Servers) 433 }