Updated Branches:
  refs/heads/trunk 9db6f5706 -> 6c1b9de65

Issue LIBCLOUD-477: Add extra utility function into ec2 driver

Signed-off-by: Tomaz Muraus <[email protected]>


Project: http://git-wip-us.apache.org/repos/asf/libcloud/repo
Commit: http://git-wip-us.apache.org/repos/asf/libcloud/commit/ffab16d5
Tree: http://git-wip-us.apache.org/repos/asf/libcloud/tree/ffab16d5
Diff: http://git-wip-us.apache.org/repos/asf/libcloud/diff/ffab16d5

Branch: refs/heads/trunk
Commit: ffab16d53312bc9eb7eacbf713b49af6230e94e7
Parents: 9db6f57
Author: Chris DeRamus <[email protected]>
Authored: Fri Dec 27 13:15:14 2013 -0500
Committer: Tomaz Muraus <[email protected]>
Committed: Fri Dec 27 19:59:14 2013 +0100

----------------------------------------------------------------------
 libcloud/compute/drivers/ec2.py | 100 +++++++++++++++++------------------
 1 file changed, 50 insertions(+), 50 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/libcloud/blob/ffab16d5/libcloud/compute/drivers/ec2.py
----------------------------------------------------------------------
diff --git a/libcloud/compute/drivers/ec2.py b/libcloud/compute/drivers/ec2.py
index abeec15..c9e34cc 100644
--- a/libcloud/compute/drivers/ec2.py
+++ b/libcloud/compute/drivers/ec2.py
@@ -675,57 +675,52 @@ class BaseEC2NodeDriver(NodeDriver):
         extra_attributes_map = {
             'instance_type': {
                 'xpath': 'instanceType',
-                'type': str
+                'cast_func': str
             },
             'availability': {
                 'xpath': 'availabilityZone',
-                'type': str
+                'cast_func': str
             },
             'start': {
                 'xpath': 'start',
-                'type': str
+                'cast_func': str
             },
             'duration': {
                 'xpath': 'duration',
-                'type': int
+                'cast_func': int
             },
             'usage_price': {
                 'xpath': 'usagePrice',
-                'type': float
+                'cast_func': float
             },
             'fixed_price': {
                 'xpath': 'fixedPrice',
-                'type': float
+                'cast_func': float
             },
             'instance_count': {
                 'xpath': 'instanceCount',
-                'type': int
+                'cast_func': int
             },
             'description': {
                 'xpath': 'productDescription',
-                'type': str
+                'cast_func': str
             },
             'instance_tenancy': {
                 'xpath': 'instanceTenancy',
-                'type': str
+                'cast_func': str
             },
             'currency_code': {
                 'xpath': 'currencyCode',
-                'type': str
+                'cast_func': str
             },
             'offering_type': {
                 'xpath': 'offeringType',
-                'type': str
+                'cast_func': str
             }
         }
 
-        # Define and build our extra dictionary
-        extra = {}
-        for attribute, values in extra_attributes_map.items():
-            type_func = values['type']
-            value = findattr(element=element, xpath=values['xpath'],
-                             namespace=NAMESPACE)
-            extra[attribute] = type_func(value)
+        # Get our extra dictionary
+        extra = self._get_extra_dict(element, extra_attributes_map)
 
         try:
             size = [size for size in self.list_sizes() if
@@ -931,16 +926,8 @@ class BaseEC2NodeDriver(NodeDriver):
             }
         }
 
-        # Define and build our extra dictionary
-        extra = {}
-        for attribute, values in extra_attributes_map.items():
-            cast_func = values['cast_func']
-            value = findattr(element=element, xpath=values['xpath'],
-                             namespace=NAMESPACE)
-            if value is not None:
-                extra[attribute] = cast_func(value)
-            else:
-                extra[attribute] = None
+        # Get our extra dictionary
+        extra = self._get_extra_dict(element, extra_attributes_map)
 
         return StorageVolume(id=volId,
                              name=name,
@@ -995,29 +982,24 @@ class BaseEC2NodeDriver(NodeDriver):
         extra_attributes_map = {
             'state': {
                 'xpath': 'state',
-                'type': str
+                'cast_func': str
             },
             'dhcp_options_id': {
                 'xpath': 'dhcpOptionsId',
-                'type': str
+                'cast_func': str
             },
             'instance_tenancy': {
                 'xpath': 'instanceTenancy',
-                'type': str
+                'cast_func': str
             },
             'is_default': {
                 'xpath': 'isDefault',
-                'type': str
+                'cast_func': str
             }
         }
 
-        # Define and build our extra dictionary
-        extra = {}
-        for attribute, values in extra_attributes_map.items():
-            type_func = values['type']
-            value = findattr(element=element, xpath=values['xpath'],
-                             namespace=NAMESPACE)
-            extra[attribute] = type_func(value)
+        # Get our extra dictionary
+        extra = self._get_extra_dict(element, extra_attributes_map)
 
         # Add tags to the extra dict
         extra['tags'] = tags
@@ -1049,29 +1031,24 @@ class BaseEC2NodeDriver(NodeDriver):
         extra_attributes_map = {
             'cidr_block': {
                 'xpath': 'cidrBlock',
-                'type': str
+                'cast_func': str
             },
             'available_ips': {
                 'xpath': 'availableIpAddressCount',
-                'type': int
+                'cast_func': int
             },
             'zone': {
                 'xpath': 'availabilityZone',
-                'type': str
+                'cast_func': str
             },
             'vpc_id': {
                 'xpath': 'vpcId',
-                'type': str
+                'cast_func': str
             }
         }
 
-        # Define and build our extra dictionary
-        extra = {}
-        for attribute, values in extra_attributes_map.items():
-            type_func = values['type']
-            value = findattr(element=element, xpath=values['xpath'],
-                             namespace=NAMESPACE)
-            extra[attribute] = type_func(value)
+        # Get our extra dictionary
+        extra = self._get_extra_dict(element, extra_attributes_map)
 
         # Also include our tags
         extra['tags'] = tags
@@ -2747,6 +2724,29 @@ class BaseEC2NodeDriver(NodeDriver):
                 'timestamp': timestamp,
                 'output': output}
 
+    def _get_extra_dict(self, element, mapping):
+        """
+        Build the extra dictionary based on an attribute mapping
+
+        :param      mapping: Dictionary with the extra layout
+        :type       node: :class:`Node`
+
+        :rtype: ``dict``
+        """
+        extra = {}
+        for attribute, values in mapping.items():
+            cast_func = values['cast_func']
+            value = findattr(element=element,
+                             xpath=values['xpath'],
+                             namespace=NAMESPACE)
+
+            if value is not None:
+                extra[attribute] = cast_func(value)
+            else:
+                extra[attribute] = None
+
+        return extra
+
     def _get_resource_tags(self, element):
         """
         Parse tags from the provided element and return a dictionary with

Reply via email to