Просмотр исходного кода

Add methods for removing a route from a router.

Enis Afgan 9 лет назад
Родитель
Сommit
d0c9e52d44

+ 17 - 1
cloudbridge/cloud/interfaces/resources.py

@@ -1141,8 +1141,24 @@ class Router(CloudResource):
         """
         Add a route to this router.
 
+        Note that a router must be attached to a network (to which the supplied
+        subnet belongs to) before a route can be added.
+
+        :type subnet_id: ``str``
+        :param subnet_id: The ID of a subnet to add to this router.
+
+        :rtype: ``bool``
+        :return: ``True`` if successful.
+        """
+        pass
+
+    @abstractmethod
+    def remove_route(self, subnet_id):
+        """
+        Remove a route from this router.
+
         :type subnet_id: ``str``
-        :param subnet_id: The ID of a subnet to attach to this router.
+        :param subnet_id: The ID of a subnet to remove to this router.
 
         :rtype: ``bool``
         :return: ``True`` if successful.

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

@@ -1026,6 +1026,8 @@ class AWSRouter(BaseRouter):
     def __init__(self, provider, router):
         super(AWSRouter, self).__init__(provider)
         self._router = router
+        self._route_table = None
+        self._ROUTE_CIDR = '0.0.0.0/0'
 
     @property
     def id(self):
@@ -1078,12 +1080,35 @@ class AWSRouter(BaseRouter):
             self.id, self.network_id)
 
     def add_route(self, subnet_id):
-        # For AWS, routes are added to a route table. A route table is assoc.
-        # with a network vs. a subnet so we don't use the supplied subnet.
-        rt = self._provider.vpc_conn.get_all_route_tables(
-            filters={'vpc-id': self.network_id})[0]
+        """
+        Add a default route to this router.
+
+        For AWS, routes are added to a route table. A route table is assoc.
+        with a network vs. a subnet so we retrieve the network via the subnet.
+        Note that the subnet must belong to the same network as the router
+        is attached to.
+
+        Further, only a single route can be added, targeting the Internet
+        (i.e., destination CIDR block ``0.0.0.0/0``).
+        """
+        sn = self._provider.vpc_conn.get_all_subnets([subnet_id])[0]
+        self._route_table = self._provider.vpc_conn.get_all_route_tables(
+            filters={'vpc-id': sn.vpc_id})[0]
         return self._provider.vpc_conn.create_route(
-            rt.id, '0.0.0.0/0', self.id)
+            self._route_table.id, self._ROUTE_CIDR, self.id)
+
+    def remove_route(self, subnet_id):
+        """
+        Remove the default Internet route from this router.
+
+        .. seealso:: ``add_route`` method
+        """
+        if not self._route_table:
+            sn = self._provider.vpc_conn.get_all_subnets([subnet_id])[0]
+            self._route_table = self._provider.vpc_conn.get_all_route_tables(
+                filters={'vpc-id': sn.vpc_id})[0]
+        return self._provider.vpc_conn.remove_route(
+            self._route_table.id, self._ROUTE_CIDR)
 
 
 class AWSLaunchConfig(BaseLaunchConfig):

+ 8 - 0
cloudbridge/cloud/providers/openstack/resources.py

@@ -818,6 +818,14 @@ class OpenStackRouter(BaseRouter):
             return True
         return False
 
+    def remove_route(self, subnet_id):
+        router_interface = {'subnet_id': subnet_id}
+        ret = self._provider.neutron.remove_interface_router(
+            self.id, router_interface)
+        if subnet_id in ret.get('subnet_ids', ""):
+            return True
+        return False
+
 
 class OpenStackKeyPair(BaseKeyPair):