github.com/wallyworld/juju@v0.0.0-20161013125918-6cf1bc9d917a/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 "gopkg.in/juju/names.v2" 8 9 "github.com/juju/errors" 10 "github.com/juju/juju/instance" 11 "github.com/juju/juju/network" 12 ) 13 14 // SupportsNetworking is a convenience helper to check if an environment 15 // supports networking. It returns an interface containing Environ and 16 // Networking in this case. 17 var SupportsNetworking = supportsNetworking 18 19 // Networking interface defines methods that environments 20 // with networking capabilities must implement. 21 type Networking interface { 22 // Subnets returns basic information about subnets known 23 // by the provider for the environment. 24 Subnets(inst instance.Id, subnetIds []network.Id) ([]network.SubnetInfo, error) 25 26 // NetworkInterfaces requests information about the network 27 // interfaces on the given instance. 28 NetworkInterfaces(instId instance.Id) ([]network.InterfaceInfo, error) 29 30 // SupportsSpaces returns whether the current environment supports 31 // spaces. The returned error satisfies errors.IsNotSupported(), 32 // unless a general API failure occurs. 33 SupportsSpaces() (bool, error) 34 35 // SupportsSpaceDiscovery returns whether the current environment 36 // supports discovering spaces from the provider. The returned error 37 // satisfies errors.IsNotSupported(), unless a general API failure occurs. 38 SupportsSpaceDiscovery() (bool, error) 39 40 // Spaces returns a slice of network.SpaceInfo with info, including 41 // details of all associated subnets, about all spaces known to the 42 // provider that have subnets available. 43 Spaces() ([]network.SpaceInfo, error) 44 45 // AllocateContainerAddresses allocates a static address for each of the 46 // container NICs in preparedInfo, hosted by the hostInstanceID. Returns the 47 // network config including all allocated addresses on success. 48 AllocateContainerAddresses(hostInstanceID instance.Id, containerTag names.MachineTag, preparedInfo []network.InterfaceInfo) ([]network.InterfaceInfo, error) 49 50 // ReleaseContainerAddresses releases the previously allocated 51 // addresses matching the interface details passed in. 52 ReleaseContainerAddresses(interfaces []network.ProviderInterfaceInfo) error 53 } 54 55 // NetworkingEnviron combines the standard Environ interface with the 56 // functionality for networking. 57 type NetworkingEnviron interface { 58 // Environ represents a juju environment. 59 Environ 60 61 // Networking defines the methods of networking capable environments. 62 Networking 63 } 64 65 func supportsNetworking(environ Environ) (NetworkingEnviron, bool) { 66 ne, ok := environ.(NetworkingEnviron) 67 return ne, ok 68 } 69 70 // SupportsSpaces checks if the environment implements NetworkingEnviron 71 // and also if it supports spaces. 72 func SupportsSpaces(env Environ) bool { 73 netEnv, ok := supportsNetworking(env) 74 if !ok { 75 return false 76 } 77 ok, err := netEnv.SupportsSpaces() 78 if err != nil { 79 if !errors.IsNotSupported(err) { 80 logger.Errorf("checking model spaces support failed with: %v", err) 81 } 82 return false 83 } 84 return ok 85 }