github.com/cloud-green/juju@v0.0.0-20151002100041-a00291338d3d/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  	"net"
     8  
     9  	"github.com/juju/juju/network"
    10  )
    11  
    12  // -----
    13  // Parameters field types.
    14  // -----
    15  
    16  // Subnet describes a single subnet within a network.
    17  type Subnet struct {
    18  	// CIDR of the subnet in IPv4 or IPv6 notation.
    19  	CIDR string `json:"CIDR"`
    20  
    21  	// ProviderId is the provider-specific subnet ID (if applicable).
    22  	ProviderId string `json:"ProviderId,omitempty`
    23  
    24  	// VLANTag needs to be between 1 and 4094 for VLANs and 0 for
    25  	// normal networks. It's defined by IEEE 802.1Q standard.
    26  	VLANTag int `json:"VLANTag"`
    27  
    28  	// Life is the subnet's life cycle value - Alive means the subnet
    29  	// is in use by one or more machines, Dying or Dead means the
    30  	// subnet is about to be removed.
    31  	Life Life `json:"Life"`
    32  
    33  	// SpaceTag is the Juju network space this subnet is associated
    34  	// with.
    35  	SpaceTag string `json:"SpaceTag"`
    36  
    37  	// Zones contain one or more availability zones this subnet is
    38  	// associated with.
    39  	Zones []string `json:"Zones"`
    40  
    41  	// StaticRangeLowIP (if available) is the lower bound of the
    42  	// subnet's static IP allocation range.
    43  	StaticRangeLowIP net.IP `json:"StaticRangeLowIP,omitempty"`
    44  
    45  	// StaticRangeHighIP (if available) is the higher bound of the
    46  	// subnet's static IP allocation range.
    47  	StaticRangeHighIP net.IP `json:"StaticRangeHighIP,omitempty"`
    48  
    49  	// Status returns the status of the subnet, whether it is in use, not
    50  	// in use or terminating.
    51  	Status string `json:"Status,omitempty"`
    52  }
    53  
    54  // Network describes a single network available on an instance.
    55  type Network struct {
    56  	// Tag is the network's tag.
    57  	Tag string `json:"Tag"`
    58  
    59  	// ProviderId is the provider-specific network id.
    60  	ProviderId string `json:"ProviderId"`
    61  
    62  	// CIDR of the network, in "123.45.67.89/12" format.
    63  	CIDR string `json:"CIDR"`
    64  
    65  	// VLANTag needs to be between 1 and 4094 for VLANs and 0 for
    66  	// normal networks. It's defined by IEEE 802.1Q standard.
    67  	VLANTag int `json:"VLANTag"`
    68  }
    69  
    70  // NetworkInterface describes a single network interface available on
    71  // an instance.
    72  type NetworkInterface struct {
    73  	// MACAddress is the network interface's hardware MAC address
    74  	// (e.g. "aa:bb:cc:dd:ee:ff").
    75  	MACAddress string `json:"MACAddress"`
    76  
    77  	// InterfaceName is the OS-specific network device name (e.g.
    78  	// "eth1", even for for a VLAN eth1.42 virtual interface).
    79  	InterfaceName string `json:"InterfaceName"`
    80  
    81  	// NetworkTag is this interface's network tag.
    82  	NetworkTag string `json:"NetworkTag"`
    83  
    84  	// IsVirtual is true when the interface is a virtual device, as
    85  	// opposed to a physical device.
    86  	IsVirtual bool `json:"IsVirtual"`
    87  
    88  	// Disabled returns whether the interface is disabled.
    89  	Disabled bool `json:"Disabled"`
    90  }
    91  
    92  // NetworkConfig describes the necessary information to configure
    93  // a single network interface on a machine. This mostly duplicates
    94  // network.InterfaceInfo type and it's defined here so it can be kept
    95  // separate and stable as definition to ensure proper wire-format for
    96  // the API.
    97  type NetworkConfig struct {
    98  	// DeviceIndex specifies the order in which the network interface
    99  	// appears on the host. The primary interface has an index of 0.
   100  	DeviceIndex int `json:"DeviceIndex"`
   101  
   102  	// MACAddress is the network interface's hardware MAC address
   103  	// (e.g. "aa:bb:cc:dd:ee:ff").
   104  	MACAddress string `json:"MACAddress"`
   105  
   106  	// CIDR of the network, in 123.45.67.89/24 format.
   107  	CIDR string `json:"CIDR"`
   108  
   109  	// NetworkName is juju-internal name of the network.
   110  	// TODO(dimitern) This should be removed or adapted to the model
   111  	// once spaces are introduced.
   112  	NetworkName string `json:"NetworkName"`
   113  
   114  	// ProviderId is a provider-specific network interface id.
   115  	ProviderId string `json:"ProviderId"`
   116  
   117  	// ProviderSubnetId is a provider-specific subnet id, to which the
   118  	// interface is attached to.
   119  	ProviderSubnetId string `json:"ProviderSubnetId"`
   120  
   121  	// VLANTag needs to be between 1 and 4094 for VLANs and 0 for
   122  	// normal networks. It's defined by IEEE 802.1Q standard.
   123  	VLANTag int `json:"VLANTag"`
   124  
   125  	// InterfaceName is the raw OS-specific network device name (e.g.
   126  	// "eth1", even for a VLAN eth1.42 virtual interface).
   127  	InterfaceName string `json:"InterfaceName"`
   128  
   129  	// Disabled is true when the interface needs to be disabled on the
   130  	// machine, e.g. not to configure it at all or stop it if running.
   131  	Disabled bool `json:"Disabled"`
   132  
   133  	// NoAutoStart is true when the interface should not be configured
   134  	// to start automatically on boot. By default and for
   135  	// backwards-compatibility, interfaces are configured to
   136  	// auto-start.
   137  	NoAutoStart bool `json:"NoAutoStart,omitempty"`
   138  
   139  	// ConfigType, if set, defines what type of configuration to use.
   140  	// See network.InterfaceConfigType for more info. If not set, for
   141  	// backwards-compatibility, "dhcp" is assumed.
   142  	ConfigType string `json:"ConfigType,omitempty"`
   143  
   144  	// Address contains an optional static IP address to configure for
   145  	// this network interface. The subnet mask to set will be inferred
   146  	// from the CIDR value.
   147  	Address string `json:"Address,omitempty"`
   148  
   149  	// DNSServers contains an optional list of IP addresses and/or
   150  	// hostnames to configure as DNS servers for this network
   151  	// interface.
   152  	DNSServers []string `json:"DNSServers,omitempty"`
   153  
   154  	// Gateway address, if set, defines the default gateway to
   155  	// configure for this network interface. For containers this
   156  	// usually (one of) the host address(es).
   157  	GatewayAddress string `json:"GatewayAddress,omitempty"`
   158  
   159  	// ExtraConfig can contain any valid setting and its value allowed
   160  	// inside an "iface" section of a interfaces(5) config file, e.g.
   161  	// "up", "down", "mtu", etc.
   162  	ExtraConfig map[string]string `json:"ExtraConfig,omitempty"`
   163  }
   164  
   165  // Port encapsulates a protocol and port number. It is used in API
   166  // requests/responses. See also network.Port, from/to which this is
   167  // transformed.
   168  type Port struct {
   169  	Protocol string `json:"Protocol"`
   170  	Number   int    `json:"Number"`
   171  }
   172  
   173  // FromNetworkPort is a convenience helper to create a parameter
   174  // out of the network type, here for Port.
   175  func FromNetworkPort(p network.Port) Port {
   176  	return Port{
   177  		Protocol: p.Protocol,
   178  		Number:   p.Number,
   179  	}
   180  }
   181  
   182  // NetworkPort is a convenience helper to return the parameter
   183  // as network type, here for Port.
   184  func (p Port) NetworkPort() network.Port {
   185  	return network.Port{
   186  		Protocol: p.Protocol,
   187  		Number:   p.Number,
   188  	}
   189  }
   190  
   191  // PortRange represents a single range of ports. It is used in API
   192  // requests/responses. See also network.PortRange, from/to which this is
   193  // transformed.
   194  type PortRange struct {
   195  	FromPort int    `json:"FromPort"`
   196  	ToPort   int    `json:"ToPort"`
   197  	Protocol string `json:"Protocol"`
   198  }
   199  
   200  // FromNetworkPortRange is a convenience helper to create a parameter
   201  // out of the network type, here for PortRange.
   202  func FromNetworkPortRange(pr network.PortRange) PortRange {
   203  	return PortRange{
   204  		FromPort: pr.FromPort,
   205  		ToPort:   pr.ToPort,
   206  		Protocol: pr.Protocol,
   207  	}
   208  }
   209  
   210  // NetworkPortRange is a convenience helper to return the parameter
   211  // as network type, here for PortRange.
   212  func (pr PortRange) NetworkPortRange() network.PortRange {
   213  	return network.PortRange{
   214  		FromPort: pr.FromPort,
   215  		ToPort:   pr.ToPort,
   216  		Protocol: pr.Protocol,
   217  	}
   218  }
   219  
   220  // EntityPort holds an entity's tag, a protocol and a port.
   221  type EntityPort struct {
   222  	Tag      string `json:"Tag"`
   223  	Protocol string `json:"Protocol"`
   224  	Port     int    `json:"Port"`
   225  }
   226  
   227  // EntitiesPorts holds the parameters for making an OpenPort or
   228  // ClosePort on some entities.
   229  type EntitiesPorts struct {
   230  	Entities []EntityPort `json:"Entities"`
   231  }
   232  
   233  // EntityPortRange holds an entity's tag, a protocol and a port range.
   234  type EntityPortRange struct {
   235  	Tag      string `json:"Tag"`
   236  	Protocol string `json:"Protocol"`
   237  	FromPort int    `json:"FromPort"`
   238  	ToPort   int    `json:"ToPort"`
   239  }
   240  
   241  // EntitiesPortRanges holds the parameters for making an OpenPorts or
   242  // ClosePorts on some entities.
   243  type EntitiesPortRanges struct {
   244  	Entities []EntityPortRange `json:"Entities"`
   245  }
   246  
   247  // Address represents the location of a machine, including metadata
   248  // about what kind of location the address describes. It's used in
   249  // the API requests/responses. See also network.Address, from/to
   250  // which this is transformed.
   251  type Address struct {
   252  	Value       string `json:"Value"`
   253  	Type        string `json:"Type"`
   254  	NetworkName string `json:"NetworkName"`
   255  	Scope       string `json:"Scope"`
   256  }
   257  
   258  // FromNetworkAddress is a convenience helper to create a parameter
   259  // out of the network type, here for Address.
   260  func FromNetworkAddress(naddr network.Address) Address {
   261  	return Address{
   262  		Value:       naddr.Value,
   263  		Type:        string(naddr.Type),
   264  		NetworkName: naddr.NetworkName,
   265  		Scope:       string(naddr.Scope),
   266  	}
   267  }
   268  
   269  // NetworkAddress is a convenience helper to return the parameter
   270  // as network type, here for Address.
   271  func (addr Address) NetworkAddress() network.Address {
   272  	return network.Address{
   273  		Value:       addr.Value,
   274  		Type:        network.AddressType(addr.Type),
   275  		NetworkName: addr.NetworkName,
   276  		Scope:       network.Scope(addr.Scope),
   277  	}
   278  }
   279  
   280  // FromNetworkAddresses is a convenience helper to create a parameter
   281  // out of the network type, here for a slice of Address.
   282  func FromNetworkAddresses(naddrs []network.Address) []Address {
   283  	addrs := make([]Address, len(naddrs))
   284  	for i, naddr := range naddrs {
   285  		addrs[i] = FromNetworkAddress(naddr)
   286  	}
   287  	return addrs
   288  }
   289  
   290  // NetworkAddresses is a convenience helper to return the parameter
   291  // as network type, here for a slice of Address.
   292  func NetworkAddresses(addrs []Address) []network.Address {
   293  	naddrs := make([]network.Address, len(addrs))
   294  	for i, addr := range addrs {
   295  		naddrs[i] = addr.NetworkAddress()
   296  	}
   297  	return naddrs
   298  }
   299  
   300  // HostPort associates an address with a port. It's used in
   301  // the API requests/responses. See also network.HostPort, from/to
   302  // which this is transformed.
   303  type HostPort struct {
   304  	Address
   305  	Port int `json:"Port"`
   306  }
   307  
   308  // FromNetworkHostPort is a convenience helper to create a parameter
   309  // out of the network type, here for HostPort.
   310  func FromNetworkHostPort(nhp network.HostPort) HostPort {
   311  	return HostPort{FromNetworkAddress(nhp.Address), nhp.Port}
   312  }
   313  
   314  // NetworkHostPort is a convenience helper to return the parameter
   315  // as network type, here for HostPort.
   316  func (hp HostPort) NetworkHostPort() network.HostPort {
   317  	return network.HostPort{hp.Address.NetworkAddress(), hp.Port}
   318  }
   319  
   320  // FromNetworkHostPorts is a helper to create a parameter
   321  // out of the network type, here for a slice of HostPort.
   322  func FromNetworkHostPorts(nhps []network.HostPort) []HostPort {
   323  	hps := make([]HostPort, len(nhps))
   324  	for i, nhp := range nhps {
   325  		hps[i] = FromNetworkHostPort(nhp)
   326  	}
   327  	return hps
   328  }
   329  
   330  // NetworkHostPorts is a convenience helper to return the parameter
   331  // as network type, here for a slice of HostPort.
   332  func NetworkHostPorts(hps []HostPort) []network.HostPort {
   333  	nhps := make([]network.HostPort, len(hps))
   334  	for i, hp := range hps {
   335  		nhps[i] = hp.NetworkHostPort()
   336  	}
   337  	return nhps
   338  }
   339  
   340  // FromNetworkHostsPorts is a helper to create a parameter
   341  // out of the network type, here for a nested slice of HostPort.
   342  func FromNetworkHostsPorts(nhpm [][]network.HostPort) [][]HostPort {
   343  	hpm := make([][]HostPort, len(nhpm))
   344  	for i, nhps := range nhpm {
   345  		hpm[i] = FromNetworkHostPorts(nhps)
   346  	}
   347  	return hpm
   348  }
   349  
   350  // NetworkHostsPorts is a convenience helper to return the parameter
   351  // as network type, here for a nested slice of HostPort.
   352  func NetworkHostsPorts(hpm [][]HostPort) [][]network.HostPort {
   353  	nhpm := make([][]network.HostPort, len(hpm))
   354  	for i, hps := range hpm {
   355  		nhpm[i] = NetworkHostPorts(hps)
   356  	}
   357  	return nhpm
   358  }
   359  
   360  // MachineAddresses holds an machine tag and addresses.
   361  type MachineAddresses struct {
   362  	Tag       string    `json:"Tag"`
   363  	Addresses []Address `json:"Addresses"`
   364  }
   365  
   366  // SetMachinesAddresses holds the parameters for making an
   367  // API call to update machine addresses.
   368  type SetMachinesAddresses struct {
   369  	MachineAddresses []MachineAddresses `json:"MachineAddresses"`
   370  }
   371  
   372  // MachineAddressesResult holds a list of machine addresses or an
   373  // error.
   374  type MachineAddressesResult struct {
   375  	Error     *Error    `json:"Error"`
   376  	Addresses []Address `json:"Addresses"`
   377  }
   378  
   379  // MachineAddressesResults holds the results of calling an API method
   380  // returning a list of addresses per machine.
   381  type MachineAddressesResults struct {
   382  	Results []MachineAddressesResult `json:"Results"`
   383  }
   384  
   385  // MachinePortRange holds a single port range open on a machine for
   386  // the given unit and relation tags.
   387  type MachinePortRange struct {
   388  	UnitTag     string    `json:"UnitTag"`
   389  	RelationTag string    `json:"RelationTag"`
   390  	PortRange   PortRange `json:"PortRange"`
   391  }
   392  
   393  // MachinePorts holds a machine and network tags. It's used when
   394  // referring to opened ports on the machine for a network.
   395  type MachinePorts struct {
   396  	MachineTag string `json:"MachineTag"`
   397  	NetworkTag string `json:"NetworkTag"`
   398  }
   399  
   400  // -----
   401  // API request / response types.
   402  // -----
   403  
   404  // PortsResults holds the bulk operation result of an API call
   405  // that returns a slice of Port.
   406  type PortsResults struct {
   407  	Results []PortsResult `json:"Results"`
   408  }
   409  
   410  // PortsResult holds the result of an API call that returns a slice
   411  // of Port or an error.
   412  type PortsResult struct {
   413  	Error *Error `json:"Error"`
   414  	Ports []Port `json:"Ports"`
   415  }
   416  
   417  // RequestedNetworkResult holds requested networks or an error.
   418  type RequestedNetworkResult struct {
   419  	Error    *Error   `json:"Error"`
   420  	Networks []string `json:"Networks"`
   421  }
   422  
   423  // RequestedNetworksResults holds multiple requested networks results.
   424  type RequestedNetworksResults struct {
   425  	Results []RequestedNetworkResult `json:"Results"`
   426  }
   427  
   428  // MachineNetworkConfigResult holds network configuration for a single machine.
   429  type MachineNetworkConfigResult struct {
   430  	Error *Error `json:"Error"`
   431  
   432  	// Tagged to Info due to compatibility reasons.
   433  	Config []NetworkConfig `json:"Info"`
   434  }
   435  
   436  // MachineNetworkConfigResults holds network configuration for multiple machines.
   437  type MachineNetworkConfigResults struct {
   438  	Results []MachineNetworkConfigResult `json:"Results"`
   439  }
   440  
   441  // MachinePortsParams holds the arguments for making a
   442  // FirewallerAPIV1.GetMachinePorts() API call.
   443  type MachinePortsParams struct {
   444  	Params []MachinePorts `json:"Params"`
   445  }
   446  
   447  // MachinePortsResult holds a single result of the
   448  // FirewallerAPIV1.GetMachinePorts() and UniterAPI.AllMachinePorts()
   449  // API calls.
   450  type MachinePortsResult struct {
   451  	Error *Error             `json:"Error"`
   452  	Ports []MachinePortRange `json:"Ports"`
   453  }
   454  
   455  // MachinePortsResults holds all the results of the
   456  // FirewallerAPIV1.GetMachinePorts() and UniterAPI.AllMachinePorts()
   457  // API calls.
   458  type MachinePortsResults struct {
   459  	Results []MachinePortsResult `json:"Results"`
   460  }
   461  
   462  // APIHostPortsResult holds the result of an APIHostPorts
   463  // call. Each element in the top level slice holds
   464  // the addresses for one API server.
   465  type APIHostPortsResult struct {
   466  	Servers [][]HostPort `json:"Servers"`
   467  }
   468  
   469  // NetworkHostsPorts is a convenience helper to return the contained
   470  // result servers as network type.
   471  func (r APIHostPortsResult) NetworkHostsPorts() [][]network.HostPort {
   472  	return NetworkHostsPorts(r.Servers)
   473  }
   474  
   475  // ZoneResult holds the result of an API call that returns an
   476  // availability zone name and whether it's available for use.
   477  type ZoneResult struct {
   478  	Error     *Error `json:"Error"`
   479  	Name      string `json:"Name"`
   480  	Available bool   `json:"Available"`
   481  }
   482  
   483  // ZoneResults holds multiple ZoneResult results
   484  type ZoneResults struct {
   485  	Results []ZoneResult `json:"Results"`
   486  }
   487  
   488  // SpaceResult holds a single space tag or an error.
   489  type SpaceResult struct {
   490  	Error *Error `json:"Error"`
   491  	Tag   string `json:"Tag"`
   492  }
   493  
   494  // SpaceResults holds the bulk operation result of an API call
   495  // that returns space tags or an errors.
   496  type SpaceResults struct {
   497  	Results []SpaceResult `json:"Results"`
   498  }
   499  
   500  // ListSubnetsResults holds the result of a ListSubnets API call.
   501  type ListSubnetsResults struct {
   502  	Results []Subnet `json:"Results"`
   503  }
   504  
   505  // SubnetsFilters holds an optional SpaceTag and Zone for filtering
   506  // the subnets returned by a ListSubnets call.
   507  type SubnetsFilters struct {
   508  	SpaceTag string `json:"SpaceTag,omitempty"`
   509  	Zone     string `json:"Zone,omitempty"`
   510  }
   511  
   512  // AddSubnetsParams holds the arguments of AddSubnets API call.
   513  type AddSubnetsParams struct {
   514  	Subnets []AddSubnetParams `json:"Subnets"`
   515  }
   516  
   517  // AddSubnetParams holds a subnet and space tags, subnet provider ID,
   518  // and a list of zones to associate the subnet to. Either SubnetTag or
   519  // SubnetProviderId must be set, but not both. Zones can be empty if
   520  // they can be discovered
   521  type AddSubnetParams struct {
   522  	SubnetTag        string   `json:"SubnetTag,omitempty"`
   523  	SubnetProviderId string   `json:"SubnetProviderId,omitempty"`
   524  	SpaceTag         string   `json:"SpaceTag"`
   525  	Zones            []string `json:"Zones,omitempty"`
   526  }
   527  
   528  // CreateSubnetsParams holds the arguments of CreateSubnets API call.
   529  type CreateSubnetsParams struct {
   530  	Subnets []CreateSubnetParams `json:"Subnets"`
   531  }
   532  
   533  // CreateSubnetParams holds a subnet and space tags, vlan tag,
   534  // and a list of zones to associate the subnet to.
   535  type CreateSubnetParams struct {
   536  	SubnetTag string   `json:"SubnetTag,omitempty"`
   537  	SpaceTag  string   `json:"SpaceTag"`
   538  	Zones     []string `json:"Zones,omitempty"`
   539  	VLANTag   int      `json:"VLANTag,omitempty"`
   540  	IsPublic  bool     `json:"IsPublic"`
   541  }
   542  
   543  // CreateSpacesParams olds the arguments of the AddSpaces API call.
   544  type CreateSpacesParams struct {
   545  	Spaces []CreateSpaceParams `json:"Spaces"`
   546  }
   547  
   548  // CreateSpaceParams holds the space tag and at least one subnet
   549  // tag required to create a new space.
   550  type CreateSpaceParams struct {
   551  	SubnetTags []string `json:"SubnetTags"`
   552  	SpaceTag   string   `json:"SpaceTag"`
   553  	Public     bool     `json:"Public"`
   554  }
   555  
   556  // ListSpacesResults holds the list of all available spaces.
   557  type ListSpacesResults struct {
   558  	Results []Space `json:"Results"`
   559  }
   560  
   561  // Space holds the information about a single space and its associated subnets.
   562  type Space struct {
   563  	Name    string   `json:"Name"`
   564  	Subnets []Subnet `json:"Subnets"`
   565  	Error   *Error   `json:"Error,omitempty"`
   566  }