Browse Source

Add methods for adding routes to a router.

Enis Afgan 9 năm trước cách đây
mục cha
commit
1ea5b80e36

+ 16 - 3
cloudbridge/cloud/interfaces/resources.py

@@ -1114,12 +1114,12 @@ class Router(CloudResource):
         pass
         pass
 
 
     @abstractmethod
     @abstractmethod
-    def attach(self, network_id):
+    def attach_network(self, network_id):
         """
         """
         Attach this router to a network.
         Attach this router to a network.
 
 
         :type network_id: ``str``
         :type network_id: ``str``
-        :param instance: The ID of a network to which to attach this router.
+        :param network_id: The ID of a network to which to attach this router.
 
 
         :rtype: ``bool``
         :rtype: ``bool``
         :return: ``True`` if successful.
         :return: ``True`` if successful.
@@ -1127,7 +1127,7 @@ class Router(CloudResource):
         pass
         pass
 
 
     @abstractmethod
     @abstractmethod
-    def detach(self):
+    def detach_network(self):
         """
         """
         Detach this router from a network.
         Detach this router from a network.
 
 
@@ -1136,6 +1136,19 @@ class Router(CloudResource):
         """
         """
         pass
         pass
 
 
+    @abstractmethod
+    def add_route(self, subnet_id):
+        """
+        Add a route to this router.
+
+        :type subnet_id: ``str``
+        :param subnet_id: The ID of a subnet to attach to this router.
+
+        :rtype: ``bool``
+        :return: ``True`` if successful.
+        """
+        pass
+
 
 
 class AttachmentInfo(object):
 class AttachmentInfo(object):
     """
     """

+ 11 - 2
cloudbridge/cloud/providers/aws/resources.py

@@ -1054,6 +1054,7 @@ class AWSRouter(BaseRouter):
 
 
     @property
     @property
     def state(self):
     def state(self):
+        self.refresh()  # Explicitly refresh the local object
         if self._router.attachments and \
         if self._router.attachments and \
            self._router.attachments[0].state == 'available':
            self._router.attachments[0].state == 'available':
             return RouterState.ATTACHED
             return RouterState.ATTACHED
@@ -1068,14 +1069,22 @@ class AWSRouter(BaseRouter):
     def delete(self):
     def delete(self):
         return self._provider._vpc_conn.delete_internet_gateway(self.id)
         return self._provider._vpc_conn.delete_internet_gateway(self.id)
 
 
-    def attach(self, network_id):
+    def attach_network(self, network_id):
         return self._provider.vpc_conn.attach_internet_gateway(
         return self._provider.vpc_conn.attach_internet_gateway(
             self.id, network_id)
             self.id, network_id)
 
 
-    def detach(self):
+    def detach_network(self):
         return self._provider.vpc_conn.detach_internet_gateway(
         return self._provider.vpc_conn.detach_internet_gateway(
             self.id, self.network_id)
             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]
+        return self._provider.vpc_conn.create_route(
+            rt.id, '0.0.0.0/0', self.id)
+
 
 
 class AWSLaunchConfig(BaseLaunchConfig):
 class AWSLaunchConfig(BaseLaunchConfig):
 
 

+ 10 - 2
cloudbridge/cloud/providers/openstack/resources.py

@@ -796,20 +796,28 @@ class OpenStackRouter(BaseRouter):
         if self.id not in str(self._provider.neutron.list_routers()):
         if self.id not in str(self._provider.neutron.list_routers()):
             return True
             return True
 
 
-    def attach(self, network_id):
+    def attach_network(self, network_id):
         self._router = self._provider.neutron.add_gateway_router(
         self._router = self._provider.neutron.add_gateway_router(
             self.id, {'network_id': network_id}).get('router', self._router)
             self.id, {'network_id': network_id}).get('router', self._router)
         if self.network_id and self.network_id == network_id:
         if self.network_id and self.network_id == network_id:
             return True
             return True
         return False
         return False
 
 
-    def detach(self):
+    def detach_network(self):
         self._router = self._provider.neutron.remove_gateway_router(
         self._router = self._provider.neutron.remove_gateway_router(
             self.id).get('router', self._router)
             self.id).get('router', self._router)
         if not self.network_id:
         if not self.network_id:
             return True
             return True
         return False
         return False
 
 
+    def add_route(self, subnet_id):
+        router_interface = {'subnet_id': subnet_id}
+        ret = self._provider.neutron.add_interface_router(
+            self.id, router_interface)
+        if subnet_id in ret.get('subnet_ids', ""):
+            return True
+        return False
+
 
 
 class OpenStackKeyPair(BaseKeyPair):
 class OpenStackKeyPair(BaseKeyPair):