Răsfoiți Sursa

Add support for adding/removing security groups for a running instance

Closes: https://github.com/gvlproject/cloudbridge/issues/6
Nuwan Goonasekera 9 ani în urmă
părinte
comite
e637722760

+ 20 - 0
cloudbridge/cloud/interfaces/resources.py

@@ -580,6 +580,26 @@ class Instance(ObjectLifeCycleMixin, CloudResource):
         """
         pass
 
+    @abstractmethod
+    def add_security_group(self, sg):
+        """
+        Add a security group to this instance
+
+        :type sg: ``SecurityGroup``
+        :param sg: The SecurityGroup to associate with the instance.
+        """
+        pass
+
+    @abstractmethod
+    def remove_security_group(self, sg):
+        """
+        Remove a security group from this instance
+
+        :type sg: ``SecurityGroup``
+        :param sg: The SecurityGroup to associate with the instance.
+        """
+        pass
+
 
 class MachineImageState(object):
 

+ 15 - 0
cloudbridge/cloud/providers/aws/resources.py

@@ -368,6 +368,21 @@ class AWSInstance(BaseInstance):
         raise NotImplementedError(
             'remove_floating_ip not implemented by this provider.')
 
+    def add_security_group(self, sg):
+        """
+        Add a security group to this instance
+        """
+        self._ec2_instance.modify_attribute(
+            'groupSet', [g.id for g in self._ec2_instance.groups] + [sg.id])
+
+    def remove_security_group(self, sg):
+        """
+        Remove a security group from this instance
+        """
+        self._ec2_instance.modify_attribute(
+            'groupSet', [g.id for g in self._ec2_instance.groups
+                         if g.id != sg.id])
+
     @property
     def state(self):
         return AWSInstance.INSTANCE_STATE_MAP.get(

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

@@ -374,6 +374,18 @@ class OpenStackInstance(BaseInstance):
         """
         self._os_instance.remove_floating_ip(ip_address)
 
+    def add_security_group(self, sg):
+        """
+        Add a security group to this instance
+        """
+        self._os_instance.add_security_group(sg.id)
+
+    def remove_security_group(self, sg):
+        """
+        Remove a security group from this instance
+        """
+        self._os_instance.remove_security_group(sg.id)
+
     @property
     def state(self):
         return OpenStackInstance.INSTANCE_STATE_MAP.get(