Jelajahi Sumber

Finalize steps to require zone param when creating a subnet

Enis Afgan 7 tahun lalu
induk
melakukan
15e6a27578

+ 4 - 8
cloudbridge/cloud/interfaces/services.py

@@ -752,21 +752,17 @@ class SubnetService(PageableObjectMixin, CloudService):
         pass
 
     @abstractmethod
-    def get_or_create_default(self, zone=None):
+    def get_or_create_default(self, zone):
         """
         Return a default subnet for the account or create one if not found.
         This provides a convenience method for obtaining a network if you
         are not particularly concerned with how the network is structured.
 
         A default network is one marked as such by the provider or matches the
-        default name used by this library (e.g., CloudBridgeNet).
+        default name used by this library (e.g., cloudbridge-net).
 
-        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.
+        :type zone: :class:`.PlacementZone` object ``str``
+        :param zone: Placement zone where to look for the subnet.
 
         :rtype: ``object`` of :class:`.Subnet`
         :return: A Subnet object

+ 4 - 5
cloudbridge/cloud/providers/aws/services.py

@@ -42,6 +42,7 @@ from .resources import AWSKeyPair
 from .resources import AWSLaunchConfig
 from .resources import AWSMachineImage
 from .resources import AWSNetwork
+from .resources import AWSPlacementZone
 from .resources import AWSRegion
 from .resources import AWSRouter
 from .resources import AWSSnapshot
@@ -724,11 +725,9 @@ class AWSSubnetService(BaseSubnetService):
         return subnet
 
     def get_or_create_default(self, zone):
-        if zone:
-            snl = self.svc.find('availabilityZone', zone)
-        else:
-            snl = self.svc.list()
-
+        zone_name = zone.name if isinstance(
+            zone, AWSPlacementZone) else zone
+        snl = self.svc.find('availabilityZone', zone_name)
         # Find first available default subnet by sorted order
         # of availability zone. (e.g. prefer us-east-1a over 1e,
         # This is because newer zones tend to have less compatibility

+ 6 - 2
cloudbridge/cloud/providers/azure/services.py

@@ -480,7 +480,11 @@ class AzureInstanceService(BaseInstanceService):
             isinstance(vm_type, VMType) else vm_type
 
         if not subnet:
-            subnet = self.provider.networking.subnets.get_or_create_default()
+            # Azure has only a single zone per region; use the current one
+            zone = self.provider.compute.regions.get(
+                self.provider.region_name).zones[0]
+            subnet = self.provider.networking.subnets.get_or_create_default(
+                zone)
         else:
             subnet = (self.provider.networking.subnets.get(subnet)
                       if isinstance(subnet, str) else subnet)
@@ -1008,7 +1012,7 @@ class AzureSubnetService(BaseSubnetService):
 
         return AzureSubnet(self.provider, subnet_info)
 
-    def get_or_create_default(self, zone=None):
+    def get_or_create_default(self, zone):
         default_cidr = '10.0.1.0/24'
 
         # No provider-default Subnet exists, look for a library-default one

+ 7 - 6
docs/topics/launch.rst

@@ -14,23 +14,24 @@ and 4 GB RAM.
 
 .. code-block:: python
 
-    img = provider.compute.images.get('ami-f4cc1de2')  # Ubuntu 16.04 on AWS
+    img = provider.compute.images.get('ami-759bc50a')  # Ubuntu 16.04 on AWS
     vm_type = sorted([t for t in provider.compute.vm_types
                       if t.vcpus >= 2 and t.ram >= 4],
                       key=lambda x: x.vcpus*x.ram)[0]
 
 In addition, CloudBridge instances must be launched into a private subnet.
 While it is possible to create complex network configurations as shown in the
-`Private networking`_ section, if you don't particularly care where the
-instance is launched, CloudBridge provides a convenience function to quickly
-obtain a default subnet for use.
+`Private networking`_ section, if you don't particularly care in which subnet
+the instance is launched, CloudBridge provides a convenience function to
+quickly obtain a default subnet for use. We just need to supply a zone to use.
 
 .. code-block:: python
 
-    subnet = provider.networking.subnets.get_or_create_default()
+    zone = provider.compute.regions.get(provider.region_name).zones[0]
+    subnet = provider.networking.subnets.get_or_create_default(zone)
 
 When launching an instance, you can also specify several optional arguments
-such as the firewall (a.k.a security group), a key pair, or instance user data.
+such as the firewall (aka security group), a key pair, or instance user data.
 To allow you to connect to the launched instances, we will also supply those
 parameters (note that we're making an assumption here these resources exist;
 if you don't have those resources under your account, take a look at the