github.com/makyo/juju@v0.0.0-20160425123129-2608902037e9/environs/networking.go (about)

     1  // Copyright 2015 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package environs
     5  
     6  import (
     7  	"github.com/juju/utils/featureflag"
     8  
     9  	"github.com/juju/juju/feature"
    10  	"github.com/juju/juju/instance"
    11  	"github.com/juju/juju/network"
    12  	"github.com/juju/juju/provider"
    13  )
    14  
    15  // SupportsNetworking is a convenience helper to check if an environment
    16  // supports networking. It returns an interface containing Environ and
    17  // Networking in this case.
    18  var SupportsNetworking = supportsNetworking
    19  
    20  // Networking interface defines methods that environments
    21  // with networking capabilities must implement.
    22  type Networking interface {
    23  	// AllocateAddress requests a specific address to be allocated for the given
    24  	// instance on the given subnet, using the specified macAddress and
    25  	// hostnameSuffix. If addr is empty, this is interpreted as an output
    26  	// argument, which will contain the allocated address. Otherwise, addr must
    27  	// be non-empty and will be allocated as specified, if possible.
    28  	AllocateAddress(instId instance.Id, subnetId network.Id, addr *network.Address, macAddress, hostnameSuffix string) error
    29  
    30  	// ReleaseAddress releases a specific address previously allocated with
    31  	// AllocateAddress.
    32  	ReleaseAddress(instId instance.Id, subnetId network.Id, addr network.Address, macAddress, hostname string) error
    33  
    34  	// Subnets returns basic information about subnets known
    35  	// by the provider for the environment.
    36  	Subnets(inst instance.Id, subnetIds []network.Id) ([]network.SubnetInfo, error)
    37  
    38  	// NetworkInterfaces requests information about the network
    39  	// interfaces on the given instance.
    40  	NetworkInterfaces(instId instance.Id) ([]network.InterfaceInfo, error)
    41  
    42  	// SupportsAddressAllocation returns whether the given subnetId
    43  	// supports static IP address allocation using AllocateAddress and
    44  	// ReleaseAddress. If subnetId is network.AnySubnet, the provider
    45  	// can decide whether it can return true or a false and an error
    46  	// (e.g. "subnetId must be set").
    47  	SupportsAddressAllocation(subnetId network.Id) (bool, error)
    48  
    49  	// SupportsSpaces returns whether the current environment supports
    50  	// spaces. The returned error satisfies errors.IsNotSupported(),
    51  	// unless a general API failure occurs.
    52  	SupportsSpaces() (bool, error)
    53  
    54  	// SupportsSpaceDiscovery returns whether the current environment
    55  	// supports discovering spaces from the provider. The returned error
    56  	// satisfies errors.IsNotSupported(), unless a general API failure occurs.
    57  	SupportsSpaceDiscovery() (bool, error)
    58  
    59  	// Spaces returns a slice of network.SpaceInfo with info, including
    60  	// details of all associated subnets, about all spaces known to the
    61  	// provider that have subnets available.
    62  	Spaces() ([]network.SpaceInfo, error)
    63  
    64  	// AllocateContainerAddresses allocates a static address for each of the
    65  	// container NICs in preparedInfo, hosted by the hostInstanceID. Returns the
    66  	// network config including all allocated addresses on success.
    67  	AllocateContainerAddresses(hostInstanceID instance.Id, preparedInfo []network.InterfaceInfo) ([]network.InterfaceInfo, error)
    68  }
    69  
    70  // NetworkingEnviron combines the standard Environ interface with the
    71  // functionality for networking.
    72  type NetworkingEnviron interface {
    73  	// Environ represents a juju environment.
    74  	Environ
    75  
    76  	// Networking defines the methods of networking capable environments.
    77  	Networking
    78  }
    79  
    80  func supportsNetworking(environ Environ) (NetworkingEnviron, bool) {
    81  	ne, ok := environ.(NetworkingEnviron)
    82  	return ne, ok
    83  }
    84  
    85  // AddressAllocationEnabled is a shortcut for checking if the AddressAllocation
    86  // feature flag is enabled. The providerType is used to distinguish between MAAS
    87  // and other providers that still support the legacy address allocation (EC2 and
    88  // Dummy) until the support can be removed across the board.
    89  func AddressAllocationEnabled(providerType string) bool {
    90  	if providerType == provider.MAAS {
    91  		return false
    92  	}
    93  	return featureflag.Enabled(feature.AddressAllocation)
    94  }