Parcourir la source

Add network param when fetching inet gateway

The AWS implementation will now automatically attach the gateway to the supplied network as part of creating the gateway. This seems appropriate because a gateway will only be functional if attached to a network and since we not know which network to work with, we can attach it.
Enis Afgan il y a 8 ans
Parent
commit
bd83106a33

+ 10 - 11
cloudbridge/cloud/interfaces/services.py

@@ -934,20 +934,19 @@ class GatewayService(CloudService):
     __metaclass__ = ABCMeta
 
     @abstractmethod
-    def get_or_create_inet_gateway(self, name):
+    def get_or_create_inet_gateway(self, network, name=None):
         """
-        Creates and returns a new internet gateway or returns an existing
-        singleton gateway, depending on the cloud provider. The returned
-        gateway object can subsequently be attached to a router to provide
-        internet routing to a network. If the gateway is no longer required,
-        clients should call gateway.delete() to delete the gateway. On some
-        cloud providers this will result in the gateway being deleted. On
-        others, it will result in a no-op if the cloud has only a single/public
-        gateway.
+        Creates new or returns an existing internet gateway for a network.
+
+        The returned gateway object can subsequently be attached to a router to
+        provide internet routing to a network.
+
+        :type network: :class:`.Network` object or ``str``
+        :param network: Network object or ID to which the gateway is attached.
 
         :type  name: ``str``
-        :param name: The gateway name. The name will be set if the provider
-                     supports it.
+        :param name: The gateway name. This applies only if creating a gateway
+                     and if the provider supports it.
 
         :rtype: ``object``  of :class:`.InternetGateway` or ``None``
         :return: an InternetGateway object of ``None`` if not found.

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

@@ -1083,11 +1083,10 @@ class AWSRouter(BaseRouter):
     def attach_gateway(self, gateway):
         gw_id = (gateway.id if isinstance(gateway, AWSInternetGateway)
                  else gateway)
-        result = self._provider.ec2_conn.meta.client.attach_internet_gateway(
-            InternetGatewayId=gw_id, VpcId=self._route_table.vpc_id)
-        self._route_table.create_route(
-            DestinationCidrBlock='0.0.0.0/0', GatewayId=gw_id)
-        return result
+        if self._route_table.create_route(
+                DestinationCidrBlock='0.0.0.0/0', GatewayId=gw_id):
+            return True
+        return False
 
     def detach_gateway(self, gateway):
         gw_id = (gateway.id if isinstance(gateway, AWSInternetGateway)

+ 16 - 3
cloudbridge/cloud/providers/aws/services.py

@@ -790,15 +790,28 @@ class AWSGatewayService(BaseGatewayService):
                                   cb_resource=AWSInternetGateway,
                                   boto_collection_name='internet_gateways')
 
-    def get_or_create_inet_gateway(self, name):
+    def get_or_create_inet_gateway(self, network, name=None):
+        log.debug("Get or create inet gateway %s on net %s", name, network)
         AWSInternetGateway.assert_valid_resource_name(name)
 
+        network_id = network.id if isinstance(network, AWSNetwork) else network
+        # Don't filter by name because it may conflict with at least the
+        # default VPC that most accounts have but that network is typically
+        # without a name.
+        gtw = self.svc.find(filter_name='attachment.vpc-id',
+                            filter_value=network_id)
+        if gtw:
+            return gtw[0]  # There can be only one gtw attached to a VPC
+        # Gateway does not exist so create one and attach to the supplied net
         cb_gateway = self.svc.create('create_internet_gateway')
         cb_gateway.name = name
+        cb_gateway._gateway.attach_to_vpc(VpcId=network_id)
         return cb_gateway
 
-    def delete(self, gateway_id):
-        log.debug("Deleting AWS Gateway Service with the id %s", gateway_id)
+    def delete(self, gateway):
+        log.debug("Service deleting AWS Gateway %s", gateway)
+        gateway_id = gateway.id if isinstance(
+            gateway, AWSInternetGateway) else gateway
         gateway = self.svc.get(gateway_id)
         if gateway:
             gateway.delete()

+ 1 - 1
cloudbridge/cloud/providers/openstack/services.py

@@ -960,7 +960,7 @@ class OpenStackGatewayService(BaseGatewayService):
     def __init__(self, provider):
         super(OpenStackGatewayService, self).__init__(provider)
 
-    def get_or_create_inet_gateway(self, name):
+    def get_or_create_inet_gateway(self, network, name=None):
         OpenStackInternetGateway.assert_valid_resource_name(name)
 
         for n in self.provider.networking.networks: