libcloud-notifications mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From anthonys...@apache.org
Subject [2/3] libcloud git commit: Adding helper function to get a node location/adding string handling for all locations
Date Thu, 11 Feb 2016 02:29:24 GMT
Adding helper function to get a node location/adding string handling for all locations


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

Branch: refs/heads/trunk
Commit: 8301b2ca44d3ba762875b033b727ce59b858057a
Parents: 4d926bd
Author: Jeffrey Dunham <jeffrey.a.dunham@gmail.com>
Authored: Wed Feb 10 18:15:15 2016 -0500
Committer: anthony-shaw <anthony.p.shaw@gmail.com>
Committed: Thu Feb 11 13:27:59 2016 +1100

----------------------------------------------------------------------
 libcloud/common/dimensiondata.py          | 12 +++++--
 libcloud/compute/drivers/dimensiondata.py | 50 +++++++++++++++-----------
 2 files changed, 40 insertions(+), 22 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/libcloud/blob/8301b2ca/libcloud/common/dimensiondata.py
----------------------------------------------------------------------
diff --git a/libcloud/common/dimensiondata.py b/libcloud/common/dimensiondata.py
index fb0607b..37d4572 100644
--- a/libcloud/common/dimensiondata.py
+++ b/libcloud/common/dimensiondata.py
@@ -18,8 +18,8 @@ Dimension Data Common Components
 from base64 import b64encode
 from time import sleep
 from libcloud.utils.py3 import httplib
-from libcloud.utils.py3 import b
-
+from libcloud.utils.py3 import b, basestring
+from libcloud.compute.base import NodeLocation
 from libcloud.common.base import ConnectionUserAndKey, XmlResponse
 from libcloud.common.types import LibcloudError, InvalidCredsError
 from libcloud.utils.xml import findtext
@@ -94,6 +94,14 @@ BAD_MESSAGE_XML_ELEMENTS = (
     ('resultDetail', GENERAL_NS)
 )
 
+def location_to_location_id(location):
+    if location is not None:
+        if isinstance(location, NodeLocation):
+            return location.id
+        elif isinstance(location, basestring):
+            return location
+        else:
+            raise TypeError("Invalid location type for location_to_location_id()")
 
 class NetworkDomainServicePlan(object):
     ESSENTIALS = "ESSENTIALS"

http://git-wip-us.apache.org/repos/asf/libcloud/blob/8301b2ca/libcloud/compute/drivers/dimensiondata.py
----------------------------------------------------------------------
diff --git a/libcloud/compute/drivers/dimensiondata.py b/libcloud/compute/drivers/dimensiondata.py
index 11bad58..6226af5 100644
--- a/libcloud/compute/drivers/dimensiondata.py
+++ b/libcloud/compute/drivers/dimensiondata.py
@@ -37,6 +37,7 @@ from libcloud.common.dimensiondata import NetworkDomainServicePlan
 from libcloud.common.dimensiondata import API_ENDPOINTS, DEFAULT_REGION
 from libcloud.common.dimensiondata import TYPES_URN
 from libcloud.common.dimensiondata import SERVER_NS, NETWORK_NS, GENERAL_NS
+from libcloud.common.dimensiondata import location_to_location_id
 from libcloud.utils.py3 import urlencode
 from libcloud.utils.xml import fixxpath, findtext, findall
 from libcloud.utils.py3 import basestring
@@ -323,7 +324,7 @@ class DimensionDataNodeDriver(NodeDriver):
         """
         params = {}
         if location is not None:
-            params['datacenterId'] = location.id
+            params['datacenterId'] = location_to_location_id(location)
 
         return self._to_base_images(
             self.connection.request_with_orgId_api_2(
@@ -349,16 +350,25 @@ class DimensionDataNodeDriver(NodeDriver):
                      driver=self.connection.driver),
         ]
 
-    def list_locations(self):
+    def list_locations(self, id=None):
         """
         list locations (datacenters) available for instantiating servers and
         networks.
 
         @inherits: :class:`NodeDriver.list_locations`
         """
+
+        params = {}
+        if id is not None:
+            params['id'] = id
+
         return self._to_locations(
             self.connection
-            .request_with_orgId_api_2('infrastructure/datacenter').object)
+            .request_with_orgId_api_2(
+                'infrastructure/datacenter',
+                params=params
+            ).object
+        )
 
     def list_networks(self, location=None):
         """
@@ -367,14 +377,14 @@ class DimensionDataNodeDriver(NodeDriver):
 
 
         :keyword location: The location
-        :type    location: :class:`NodeLocation`
+        :type    location: :class:`NodeLocation` or ``str``
 
         :return: a list of DimensionDataNetwork objects
         :rtype: ``list`` of :class:`DimensionDataNetwork`
         """
         url_ext = ''
         if location is not None:
-            url_ext = '/' + location.id
+            url_ext = '/' + location_to_location_id(location)
 
         return self._to_networks(
             self.connection
@@ -435,10 +445,7 @@ class DimensionDataNodeDriver(NodeDriver):
 
         params = {}
         if location is not None:
-            if isinstance(location, NodeLocation):
-                params['datacenterId'] = location.id
-            else:
-                params['datacenterId'] = location
+            params['datacenterId'] = location_to_location_id(location)
 
         if ipv6 is not None:
             params['ipv6'] = ipv6
@@ -676,11 +683,11 @@ class DimensionDataNodeDriver(NodeDriver):
         organization.  The response includes the location of each network.
 
         :return: a list of DimensionDataNetwork objects
-        :rtype: ``list`` of :class:`DimensionDataNetwork`
+        :rtype: ``list`` of :class:`DimensionDataNetwork` or ``str``
         """
         params = {}
         if location is not None:
-            params['location'] = location.id
+            params['location'] = location_to_location_id(location)
 
         response = self.connection \
             .request_with_orgId_api_1('networkWithLocation',
@@ -692,7 +699,7 @@ class DimensionDataNodeDriver(NodeDriver):
         Create a new network in an MCP 1.0 location
 
         :param   location: The target location (MCP1)
-        :type    location: :class:`NodeLocation`
+        :type    location: :class:`NodeLocation` or ``str``
 
         :param   name: The name of the network
         :type    name: ``str``
@@ -703,12 +710,15 @@ class DimensionDataNodeDriver(NodeDriver):
         :return: A new instance of `DimensionDataNetwork`
         :rtype:  Instance of :class:`DimensionDataNetwork`
         """
+        if location is not None:
+            network_location = location_to_location_id(location)
+
         create_node = ET.Element('NewNetworkWithLocation',
                                  {'xmlns': NETWORK_NS})
         ET.SubElement(create_node, "name").text = name
         if description is not None:
             ET.SubElement(create_node, "description").text = description
-        ET.SubElement(create_node, "location").text = location.id
+        ET.SubElement(create_node, "location").text = network_location
 
         self.connection.request_with_orgId_api_1(
             'networkWithLocation',
@@ -777,14 +787,14 @@ class DimensionDataNodeDriver(NodeDriver):
         The response includes the location of each network domain.
 
         :param      location: The data center to list (optional)
-        :type       location: :class:`NodeLocation`
+        :type       location: :class:`NodeLocation` or ``str``
 
         :return: a list of `DimensionDataNetwork` objects
         :rtype: ``list`` of :class:`DimensionDataNetwork`
         """
         params = {}
         if location is not None:
-            params['datacenterId'] = location.id
+            params['datacenterId'] = location_to_location_id(location)
 
         response = self.connection \
             .request_with_orgId_api_2('network/networkDomain',
@@ -814,7 +824,8 @@ class DimensionDataNodeDriver(NodeDriver):
         :rtype: :class:`DimensionDataNetworkDomain`
         """
         create_node = ET.Element('deployNetworkDomain', {'xmlns': TYPES_URN})
-        ET.SubElement(create_node, "datacenterId").text = location.id
+        ET.SubElement(create_node,
+                      "datacenterId").text = location_to_location_id(location)
         ET.SubElement(create_node, "name").text = name
         if description is not None:
             ET.SubElement(create_node, "description").text = description
@@ -1035,7 +1046,7 @@ class DimensionDataNodeDriver(NodeDriver):
         """
         params = {}
         if location is not None:
-            params['datacenterId'] = location.id
+            params['datacenterId'] = location_to_location_id(location)
         if network_domain is not None:
             params['networkDomainId'] = network_domain.id
         response = self.connection.request_with_orgId_api_2('network/vlan',
@@ -1302,8 +1313,7 @@ class DimensionDataNodeDriver(NodeDriver):
         """
         location = None
         if id is not None:
-            location = list(
-                filter(lambda x: x.id == id, self.list_locations()))[0]
+            location = self.list_locations(id=id)[0]
         return location
 
     def ex_wait_for_state(self, state, func, poll_interval=2,
@@ -1568,7 +1578,7 @@ class DimensionDataNodeDriver(NodeDriver):
         """
         params = {}
         if location is not None:
-            params['datacenterId'] = location.id
+            params['datacenterId'] = location_to_location_id(location)
 
         return self._to_base_images(
             self.connection.request_with_orgId_api_2(


Mime
View raw message