Explorar o código

Add a to_json method to (Base)CloudResource

Enis Afgan %!s(int64=10) %!d(string=hai) anos
pai
achega
54897d5162

+ 8 - 0
cloudbridge/cloud/base/resources.py

@@ -1,7 +1,9 @@
 """
 """
 Base implementation for data objects exposed through a provider or service
 Base implementation for data objects exposed through a provider or service
 """
 """
+import inspect
 import itertools
 import itertools
+import json
 import logging
 import logging
 import time
 import time
 import six
 import six
@@ -47,6 +49,12 @@ class BaseCloudResource(CloudResource):
     def _provider(self):
     def _provider(self):
         return self.__provider
         return self.__provider
 
 
+    def to_json(self):
+        # Get all attributes but filter methods and private/magic ones
+        attr = inspect.getmembers(self, lambda a: not(inspect.isroutine(a)))
+        js = {k: v for(k, v) in attr if not k.startswith('_')}
+        return json.dumps(js)
+
 
 
 class BaseObjectLifeCycleMixin(ObjectLifeCycleMixin):
 class BaseObjectLifeCycleMixin(ObjectLifeCycleMixin):
     """
     """

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

@@ -44,6 +44,13 @@ class CloudResource(object):
         """
         """
         pass
         pass
 
 
+    @abstractmethod
+    def to_json(self):
+        """
+        Returns a JSON representation of the CloudResource object.
+        """
+        pass
+
 
 
 class CloudBridgeBaseException(Exception):
 class CloudBridgeBaseException(Exception):