Kaynağa Gözat

Add a method to determine a default subnet; remove one for the network

Enis Afgan 9 yıl önce
ebeveyn
işleme
5478065e3d

+ 3 - 0
cloudbridge/cloud/base/resources.py

@@ -663,6 +663,9 @@ class BaseNetwork(BaseCloudResource, Network, BaseObjectLifeCycleMixin):
 
 class BaseSubnet(Subnet, BaseCloudResource):
 
+    CB_DEFAULT_SUBNET_NAME = os.environ.get('CB_DEFAULT_SUBNET_NAME',
+                                            'CloudBridgeSubnet')
+
     def __init__(self, provider):
         super(BaseSubnet, self).__init__(provider)
 

+ 20 - 15
cloudbridge/cloud/interfaces/services.py

@@ -520,21 +520,6 @@ class NetworkService(PageableObjectMixin, CloudService):
         """
         pass
 
-    @abstractmethod
-    def get_or_create_default(self):
-        """
-        Return a default network for the account or create one if not found.
-
-        A default network is one marked as such by the provider or matches the
-        default network name used by this library (e.g., CloudBridgeNet).
-        In addition to creating a subnet, this will create subnet(s) within
-        the network.
-
-        :rtype: ``object`` of :class:`.Network`
-        :return: a Network object
-        """
-        pass
-
     @abstractmethod
     def list(self, limit=None, marker=None):
         """
@@ -706,6 +691,26 @@ class SubnetService(PageableObjectMixin, CloudService):
         """
         pass
 
+    @abstractmethod
+    def get_or_create_default(self, zone=None):
+        """
+        Return a default subnet for the account or create one if not found.
+
+        A default network is one marked as such by the provider or matches the
+        default name used by this library (e.g., CloudBridgeNet).
+
+        If this method creates a new subnet, it will create one in each zone
+        available from the provider.
+
+        :type zone: ``str``
+        :param zone: Placement zone where to look for the subnet. If not
+                     supplied, a subnet from random zone will be selected.
+
+        :rtype: ``object`` of :class:`.Subnet`
+        :return: A Subnet object
+        """
+        pass
+
     @abstractmethod
     def delete(self, subnet):
         """

+ 27 - 26
cloudbridge/cloud/providers/aws/services.py

@@ -855,32 +855,6 @@ class AWSNetworkService(BaseNetworkService):
             return AWSNetwork(self.provider, network[0])
         return None
 
-    def get_or_create_default(self):
-        default_vpc = None
-        vpcs = self.provider.vpc_conn.get_all_vpcs()
-        for vpc in vpcs:
-            if vpc.is_default:
-                return AWSNetwork(self.provider, vpc)
-        if not default_vpc:
-            for vpc in vpcs:
-                if vpc.tags.get('Name', '') == \
-                   AWSNetwork.CB_DEFAULT_NETWORK_NAME:
-                    return AWSNetwork(self.provider, vpc)
-        # No default network exists; create one.
-        default_vpc = self.provider.network.create(
-            name=AWSNetwork.CB_DEFAULT_NETWORK_NAME)
-        # Create a subnet in each of the region's zones
-        # Otherwise, a CloudBridge-default network will exist but
-        # possibly not work in future requests when the zone for the
-        # only existing subnet and a target launch zone don't match.
-        region = self.provider.compute.regions.get(
-            self.provider.vpc_conn.region.name)
-        for i, zone in enumerate(region.zones):
-            self.provider.vpc_conn.create_subnet(
-                default_vpc.id, '10.0.{0}.0/24'.format(i),
-                availability_zone=zone.name)
-        return default_vpc
-
     def list(self, limit=None, marker=None):
         networks = [AWSNetwork(self.provider, network)
                     for network in self.provider.vpc_conn.get_all_vpcs()]
@@ -956,6 +930,33 @@ class AWSSubnetService(BaseSubnetService):
             cb_subnet.name = name
         return cb_subnet
 
+    def get_or_create_default(self, zone=None):
+        filtr = {'availabilityZone': zone} if zone else None
+        sns = self.provider.vpc_conn.get_all_subnets(filters=filtr)
+        for sn in sns:
+            if sn.defaultForAz:
+                return AWSSubnet(self.provider, sn)
+        # No provider-default Subnet exists, look for a library-default one
+        for sn in sns:
+            if sn.tags.get('Name') == AWSSubnet.CB_DEFAULT_SUBNET_NAME:
+                return AWSSubnet(self.provider, sn)
+        # No provider-default Subnet exists, try to create it (net + subnets)
+        default_net = self.provider.network.create(
+            name=AWSNetwork.CB_DEFAULT_NETWORK_NAME)
+        # Create a subnet in each of the region's zones
+        region = self.provider.compute.regions.get(
+            self.provider.vpc_conn.region.name)
+        default_sn = None
+        for i, z in enumerate(region.zones):
+            sn = self.create(default_net, '10.0.{0}.0/24'.format(i),
+                             AWSSubnet.CB_DEFAULT_SUBNET_NAME, z.name)
+            if zone and zone == z.name:
+                default_sn = sn
+        # No specific zone was supplied; return the last created subnet
+        if not default_sn:
+            default_sn = sn
+        return default_sn
+
     def delete(self, subnet):
         subnet_id = subnet.id if isinstance(subnet, AWSSubnet) else subnet
         return self.provider.vpc_conn.delete_subnet(subnet_id=subnet_id)

+ 12 - 10
cloudbridge/cloud/providers/openstack/services.py

@@ -743,16 +743,6 @@ class OpenStackNetworkService(BaseNetworkService):
         network = (n for n in self.list() if n.id == network_id)
         return next(network, None)
 
-    def get_or_create_default(self):
-        for net in self.list():
-            if net.name == OpenStackNetwork.CB_DEFAULT_NETWORK_NAME:
-                return net
-        net = self.create(OpenStackNetwork.CB_DEFAULT_NETWORK_NAME)
-        net.create_subnet(
-            cidr_block='10.0.0.0/24',
-            name="{0}Subnet".format(OpenStackNetwork.CB_DEFAULT_NETWORK_NAME))
-        return net
-
     def list(self, limit=None, marker=None):
         networks = [OpenStackNetwork(self.provider, network)
                     for network in self.provider.neutron.list_networks()
@@ -825,6 +815,18 @@ class OpenStackSubnetService(BaseSubnetService):
                   .get('subnet'))
         return OpenStackSubnet(self.provider, subnet)
 
+    def get_or_create_default(self, zone=None):
+        """
+        Subnet zone is not supported by OpenStack and is thus ignored.
+        """
+        for sn in self.list():
+            if sn.name == OpenStackSubnet.CB_DEFAULT_SUBNET_NAME:
+                return sn
+        net = self.create(OpenStackNetwork.CB_DEFAULT_NETWORK_NAME)
+        sn = net.create_subnet(cidr_block='10.0.0.0/24',
+                               name=OpenStackSubnet.CB_DEFAULT_SUBNET_NAME)
+        return sn
+
     def delete(self, subnet):
         subnet_id = (subnet.id if isinstance(subnet, OpenStackSubnet)
                      else subnet)

+ 17 - 11
docs/topics/setup.rst

@@ -124,14 +124,20 @@ In addition to the provider specific configuration variables above, there are
 some general configuration environment variables that apply to CloudBridge as
 a whole
 
-=====================  ==================
-Variable		       Description
-=====================  ==================
-CB_DEBUG               Setting ``CB_DEBUG=True`` will cause detailed debug
-                       output to be printed for each provider (including HTTP
-                       traces).
-CB_USE_MOCK_PROVIDERS  Setting this to ``True`` will cause the CloudBridge test
-                       suite to use mock drivers when available.
-CB_TEST_PROVIDER       Set this value to a valid :class:`.ProviderList` value
-                       such as ``aws``, to limit tests to that provider only.
-=====================  ==================
+======================  ==================
+Variable		            Description
+======================  ==================
+CB_DEBUG                Setting ``CB_DEBUG=True`` will cause detailed debug
+                        output to be printed for each provider (including HTTP
+                        traces).
+CB_USE_MOCK_PROVIDERS   Setting this to ``True`` will cause the CloudBridge test
+                        suite to use mock drivers when available.
+CB_TEST_PROVIDER        Set this value to a valid :class:`.ProviderList` value
+                        such as ``aws``, to limit tests to that provider only.
+CB_DEFAULT_SUBNET_NAME  Name to be used for a subnet that will be considered
+                        the 'default' by the library. This default will be used
+                        only in cases there is no subnet marked as the default by the provider.
+CB_DEFAULT_NETWORK_NAME Name to be used for a network that will be considered
+                        the 'default' by the library. This default will be used
+                        only in cases there is no network marked as the default by the provider.
+======================= ==================