You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@libcloud.apache.org by to...@apache.org on 2020/11/05 21:52:49 UTC

[libcloud] 04/08: resolve NodeImage, NodeSize in list_nodes (#3)

This is an automated email from the ASF dual-hosted git repository.

tomaz pushed a commit to branch trunk
in repository https://gitbox.apache.org/repos/asf/libcloud.git

commit 64f4f823d8384c09dd255cb9e60fc830e586f464
Author: sergerdn <64...@users.noreply.github.com>
AuthorDate: Thu Oct 15 15:25:00 2020 +0300

    resolve NodeImage, NodeSize in list_nodes (#3)
---
 libcloud/compute/drivers/vultr.py   | 595 +++++++++++++++++++++++++++++++++++-
 libcloud/test/compute/test_vultr.py |  13 +-
 2 files changed, 596 insertions(+), 12 deletions(-)

diff --git a/libcloud/compute/drivers/vultr.py b/libcloud/compute/drivers/vultr.py
index 4628d86..44d65ca 100644
--- a/libcloud/compute/drivers/vultr.py
+++ b/libcloud/compute/drivers/vultr.py
@@ -15,7 +15,6 @@
 """
 Vultr Driver
 """
-
 import time
 from functools import update_wrapper
 
@@ -30,6 +29,561 @@ from libcloud.utils.iso8601 import parse_date
 from libcloud.utils.py3 import httplib
 from libcloud.utils.py3 import urlencode
 
+# For matching region by id
+VULTR_COMPUTE_INSTANCE_LOCATIONS = {
+    "1": {
+        "DCID": "1",
+        "name": "New Jersey",
+        "country": "US",
+        "continent": "North America",
+        "state": "NJ",
+        "regioncode": "EWR"
+    },
+    "2": {
+        "DCID": "2",
+        "name": "Chicago",
+        "country": "US",
+        "continent": "North America",
+        "state": "IL",
+        "regioncode": "ORD"
+    },
+    "3": {
+        "DCID": "3",
+        "name": "Dallas",
+        "country": "US",
+        "continent": "North America",
+        "state": "TX",
+        "regioncode": "DFW"
+    },
+    "4": {
+        "DCID": "4",
+        "name": "Seattle",
+        "country": "US",
+        "continent": "North America",
+        "state": "WA",
+        "regioncode": "SEA"
+    },
+    "5": {
+        "DCID": "5",
+        "name": "Los Angeles",
+        "country": "US",
+        "continent": "North America",
+        "state": "CA",
+        "regioncode": "LAX"
+    },
+    "6": {
+        "DCID": "6",
+        "name": "Atlanta",
+        "country": "US",
+        "continent": "North America",
+        "state": "GA",
+        "regioncode": "ATL"
+    },
+    "7": {
+        "DCID": "7",
+        "name": "Amsterdam",
+        "country": "NL",
+        "continent": "Europe",
+        "state": "",
+        "regioncode": "AMS"
+    },
+    "8": {
+        "DCID": "8",
+        "name": "London",
+        "country": "GB",
+        "continent": "Europe",
+        "state": "",
+        "regioncode": "LHR"
+    },
+    "9": {
+        "DCID": "9",
+        "name": "Frankfurt",
+        "country": "DE",
+        "continent": "Europe",
+        "state": "",
+        "regioncode": "FRA"
+    },
+    "12": {
+        "DCID": "12",
+        "name": "Silicon Valley",
+        "country": "US",
+        "continent": "North America",
+        "state": "CA",
+        "regioncode": "SJC"
+    },
+    "19": {
+        "DCID": "19",
+        "name": "Sydney",
+        "country": "AU",
+        "continent": "Australia",
+        "state": "",
+        "regioncode": "SYD"
+    },
+    "22": {
+        "DCID": "22",
+        "name": "Toronto",
+        "country": "CA",
+        "continent": "North America",
+        "state": "",
+        "regioncode": "YTO"
+    },
+    "24": {
+        "DCID": "24",
+        "name": "Paris",
+        "country": "FR",
+        "continent": "Europe",
+        "state": "",
+        "regioncode": "CDG"
+    },
+    "25": {
+        "DCID": "25",
+        "name": "Tokyo",
+        "country": "JP",
+        "continent": "Asia",
+        "state": "",
+        "regioncode": "NRT"
+    },
+    "34": {
+        "DCID": "34",
+        "name": "Seoul",
+        "country": "KR",
+        "continent": "Asia",
+        "state": "",
+        "regioncode": "ICN"
+    },
+    "39": {
+        "DCID": "39",
+        "name": "Miami",
+        "country": "US",
+        "continent": "North America",
+        "state": "FL",
+        "regioncode": "MIA"
+    },
+    "40": {
+        "DCID": "40",
+        "name": "Singapore",
+        "country": "SG",
+        "continent": "Asia",
+        "state": "",
+        "regioncode": "SGP"
+    }
+}
+# For matching image by id
+VULTR_COMPUTE_INSTANCE_IMAGES = {
+    "127": {
+        "OSID": 127,
+        "name": "CentOS 6 x64",
+        "arch": "x64",
+        "family": "centos",
+        "windows": False
+    },
+    "147": {
+        "OSID": 147,
+        "name": "CentOS 6 i386",
+        "arch": "i386",
+        "family": "centos",
+        "windows": False
+    },
+    "167": {
+        "OSID": 167,
+        "name": "CentOS 7 x64",
+        "arch": "x64",
+        "family": "centos",
+        "windows": False
+    },
+    "381": {
+        "OSID": 381,
+        "name": "CentOS 7 SELinux x64",
+        "arch": "x64",
+        "family": "centos",
+        "windows": False
+    },
+    "362": {
+        "OSID": 362,
+        "name": "CentOS 8 x64",
+        "arch": "x64",
+        "family": "centos",
+        "windows": False
+    },
+    "401": {
+        "OSID": 401,
+        "name": "CentOS 8 Stream x64",
+        "arch": "x64",
+        "family": "centos",
+        "windows": False
+    },
+    "215": {
+        "OSID": 215,
+        "name": "Ubuntu 16.04 x64",
+        "arch": "x64",
+        "family": "ubuntu",
+        "windows": False
+    },
+    "216": {
+        "OSID": 216,
+        "name": "Ubuntu 16.04 i386",
+        "arch": "i386",
+        "family": "ubuntu",
+        "windows": False
+    },
+    "270": {
+        "OSID": 270,
+        "name": "Ubuntu 18.04 x64",
+        "arch": "x64",
+        "family": "ubuntu",
+        "windows": False
+    },
+    "387": {
+        "OSID": 387,
+        "name": "Ubuntu 20.04 x64",
+        "arch": "x64",
+        "family": "ubuntu",
+        "windows": False
+    },
+    "194": {
+        "OSID": 194,
+        "name": "Debian 8 i386 (jessie)",
+        "arch": "i386",
+        "family": "debian",
+        "windows": False
+    },
+    "244": {
+        "OSID": 244,
+        "name": "Debian 9 x64 (stretch)",
+        "arch": "x64",
+        "family": "debian",
+        "windows": False
+    },
+    "352": {
+        "OSID": 352,
+        "name": "Debian 10 x64 (buster)",
+        "arch": "x64",
+        "family": "debian",
+        "windows": False
+    },
+    "230": {
+        "OSID": 230,
+        "name": "FreeBSD 11 x64",
+        "arch": "x64",
+        "family": "freebsd",
+        "windows": False
+    },
+    "327": {
+        "OSID": 327,
+        "name": "FreeBSD 12 x64",
+        "arch": "x64",
+        "family": "freebsd",
+        "windows": False
+    },
+    "366": {
+        "OSID": 366,
+        "name": "OpenBSD 6.6 x64",
+        "arch": "x64",
+        "family": "openbsd",
+        "windows": False
+    },
+    "394": {
+        "OSID": 394,
+        "name": "OpenBSD 6.7 x64",
+        "arch": "x64",
+        "family": "openbsd",
+        "windows": False
+    },
+    "391": {
+        "OSID": 391,
+        "name": "Fedora CoreOS",
+        "arch": "x64",
+        "family": "fedora-coreos",
+        "windows": False
+    },
+    "367": {
+        "OSID": 367,
+        "name": "Fedora 31 x64",
+        "arch": "x64",
+        "family": "fedora",
+        "windows": False
+    },
+    "389": {
+        "OSID": 389,
+        "name": "Fedora 32 x64",
+        "arch": "x64",
+        "family": "fedora",
+        "windows": False
+    },
+    "124": {
+        "OSID": 124,
+        "name": "Windows 2012 R2 x64",
+        "arch": "x64",
+        "family": "windows",
+        "windows": False
+    },
+    "240": {
+        "OSID": 240,
+        "name": "Windows 2016 x64",
+        "arch": "x64",
+        "family": "windows",
+        "windows": False
+    },
+    "159": {
+        "OSID": 159,
+        "name": "Custom",
+        "arch": "x64",
+        "family": "iso",
+        "windows": False
+    },
+    "164": {
+        "OSID": 164,
+        "name": "Snapshot",
+        "arch": "x64",
+        "family": "snapshot",
+        "windows": False
+    },
+    "180": {
+        "OSID": 180,
+        "name": "Backup",
+        "arch": "x64",
+        "family": "backup",
+        "windows": False
+    },
+    "186": {
+        "OSID": 186,
+        "name": "Application",
+        "arch": "x64",
+        "family": "application",
+        "windows": False
+    }
+}
+VULTR_COMPUTE_INSTANCE_SIZES = {
+    "201": {
+        "VPSPLANID": "201",
+        "name": "1024 MB RAM,25 GB SSD,1.00 TB BW",
+        "vcpu_count": "1",
+        "ram": "1024",
+        "disk": "25",
+        "bandwidth": "1.00",
+        "bandwidth_gb": "1024",
+        "price_per_month": "5.00",
+        "plan_type": "SSD",
+        "windows": False,
+    },
+    "202": {
+        "VPSPLANID": "202",
+        "name": "2048 MB RAM,55 GB SSD,2.00 TB BW",
+        "vcpu_count": "1",
+        "ram": "2048",
+        "disk": "55",
+        "bandwidth": "2.00",
+        "bandwidth_gb": "2048",
+        "price_per_month": "10.00",
+        "plan_type": "SSD",
+        "windows": False,
+    },
+    "203": {
+        "VPSPLANID": "203",
+        "name": "4096 MB RAM,80 GB SSD,3.00 TB BW",
+        "vcpu_count": "2",
+        "ram": "4096",
+        "disk": "80",
+        "bandwidth": "3.00",
+        "bandwidth_gb": "3072",
+        "price_per_month": "20.00",
+        "plan_type": "SSD",
+        "windows": False,
+    },
+    "204": {
+        "VPSPLANID": "204",
+        "name": "8192 MB RAM,160 GB SSD,4.00 TB BW",
+        "vcpu_count": "4",
+        "ram": "8192",
+        "disk": "160",
+        "bandwidth": "4.00",
+        "bandwidth_gb": "4096",
+        "price_per_month": "40.00",
+        "plan_type": "SSD",
+        "windows": False,
+    },
+    "205": {
+        "VPSPLANID": "205",
+        "name": "16384 MB RAM,320 GB SSD,5.00 TB BW",
+        "vcpu_count": "6",
+        "ram": "16384",
+        "disk": "320",
+        "bandwidth": "5.00",
+        "bandwidth_gb": "5120",
+        "price_per_month": "80.00",
+        "plan_type": "SSD",
+        "windows": False,
+    },
+    "206": {
+        "VPSPLANID": "206",
+        "name": "32768 MB RAM,640 GB SSD,6.00 TB BW",
+        "vcpu_count": "8",
+        "ram": "32768",
+        "disk": "640",
+        "bandwidth": "6.00",
+        "bandwidth_gb": "6144",
+        "price_per_month": "160.00",
+        "plan_type": "SSD",
+        "windows": False,
+    },
+    "207": {
+        "VPSPLANID": "207",
+        "name": "65536 MB RAM,1280 GB SSD,10.00 TB BW",
+        "vcpu_count": "16",
+        "ram": "65536",
+        "disk": "1280",
+        "bandwidth": "10.00",
+        "bandwidth_gb": "10240",
+        "price_per_month": "320.00",
+        "plan_type": "SSD",
+        "windows": False,
+    },
+    "208": {
+        "VPSPLANID": "208",
+        "name": "98304 MB RAM,1600 GB SSD,15.00 TB BW",
+        "vcpu_count": "24",
+        "ram": "98304",
+        "disk": "1600",
+        "bandwidth": "15.00",
+        "bandwidth_gb": "15360",
+        "price_per_month": "640.00",
+        "plan_type": "SSD",
+        "windows": False,
+    },
+    "115": {
+        "VPSPLANID": "115",
+        "name": "8192 MB RAM,110 GB SSD,10.00 TB BW",
+        "vcpu_count": "2",
+        "ram": "8192",
+        "disk": "110",
+        "bandwidth": "10.00",
+        "bandwidth_gb": "10240",
+        "price_per_month": "60.00",
+        "plan_type": "DEDICATED",
+        "windows": False,
+    },
+    "116": {
+        "VPSPLANID": "116",
+        "name": "16384 MB RAM,2x110 GB SSD,20.00 TB BW",
+        "vcpu_count": "4",
+        "ram": "16384",
+        "disk": "110",
+        "bandwidth": "20.00",
+        "bandwidth_gb": "20480",
+        "price_per_month": "120.00",
+        "plan_type": "DEDICATED",
+        "windows": False,
+    },
+    "117": {
+        "VPSPLANID": "117",
+        "name": "24576 MB RAM,3x110 GB SSD,30.00 TB BW",
+        "vcpu_count": "6",
+        "ram": "24576",
+        "disk": "110",
+        "bandwidth": "30.00",
+        "bandwidth_gb": "30720",
+        "price_per_month": "180.00",
+        "plan_type": "DEDICATED",
+        "windows": False,
+    },
+    "118": {
+        "VPSPLANID": "118",
+        "name": "32768 MB RAM,4x110 GB SSD,40.00 TB BW",
+        "vcpu_count": "8",
+        "ram": "32768",
+        "disk": "110",
+        "bandwidth": "40.00",
+        "bandwidth_gb": "40960",
+        "price_per_month": "240.00",
+        "plan_type": "DEDICATED",
+        "windows": False,
+    },
+    "400": {
+        "VPSPLANID": "400",
+        "name": "1024 MB RAM,32 GB SSD,1.00 TB BW",
+        "vcpu_count": "1",
+        "ram": "1024",
+        "disk": "32",
+        "bandwidth": "1.00",
+        "bandwidth_gb": "1024",
+        "price_per_month": "6.00",
+        "plan_type": "HIGHFREQUENCY",
+        "windows": False,
+    },
+    "401": {
+        "VPSPLANID": "401",
+        "name": "2048 MB RAM,64 GB SSD,2.00 TB BW",
+        "vcpu_count": "1",
+        "ram": "2048",
+        "disk": "64",
+        "bandwidth": "2.00",
+        "bandwidth_gb": "2048",
+        "price_per_month": "12.00",
+        "plan_type": "HIGHFREQUENCY",
+        "windows": False,
+    },
+    "402": {
+        "VPSPLANID": "402",
+        "name": "4096 MB RAM,128 GB SSD,3.00 TB BW",
+        "vcpu_count": "2",
+        "ram": "4096",
+        "disk": "128",
+        "bandwidth": "3.00",
+        "bandwidth_gb": "3072",
+        "price_per_month": "24.00",
+        "plan_type": "HIGHFREQUENCY",
+        "windows": False,
+    },
+    "403": {
+        "VPSPLANID": "403",
+        "name": "8192 MB RAM,256 GB SSD,4.00 TB BW",
+        "vcpu_count": "3",
+        "ram": "8192",
+        "disk": "256",
+        "bandwidth": "4.00",
+        "bandwidth_gb": "4096",
+        "price_per_month": "48.00",
+        "plan_type": "HIGHFREQUENCY",
+        "windows": False,
+    },
+    "404": {
+        "VPSPLANID": "404",
+        "name": "16384 MB RAM,384 GB SSD,5.00 TB BW",
+        "vcpu_count": "4",
+        "ram": "16384",
+        "disk": "384",
+        "bandwidth": "5.00",
+        "bandwidth_gb": "5120",
+        "price_per_month": "96.00",
+        "plan_type": "HIGHFREQUENCY",
+        "windows": False,
+    },
+    "405": {
+        "VPSPLANID": "405",
+        "name": "32768 MB RAM,512 GB SSD,6.00 TB BW",
+        "vcpu_count": "8",
+        "ram": "32768",
+        "disk": "512",
+        "bandwidth": "6.00",
+        "bandwidth_gb": "6144",
+        "price_per_month": "192.00",
+        "plan_type": "HIGHFREQUENCY",
+        "windows": False,
+    },
+    "406": {
+        "VPSPLANID": "406",
+        "name": "49152 MB RAM,768 GB SSD,8.00 TB BW",
+        "vcpu_count": "12",
+        "ram": "49152",
+        "disk": "768",
+        "bandwidth": "8.00",
+        "bandwidth_gb": "8192",
+        "price_per_month": "256.00",
+        "plan_type": "HIGHFREQUENCY",
+        "windows": False,
+    }
+}
+
 
 class rate_limited:
     """
@@ -377,30 +931,49 @@ class VultrNodeDriver(NodeDriver):
             private_ips = []
         created_at = parse_date(data['date_created'])
 
-        # TODO: remove extra keys and return Node with full api:
-        # TODO: size = None,  # type: NodeSize
-        # TODO: image = None,  # type: NodeImage
         # response ordering
         extra_keys = [
-            "ram", "disk", "vcpu_count",
             "location",  # Location name
-            "DCID",  # Location id. See v1/regions/list
             "default_password", "pending_charges", "cost_per_month",
             "current_bandwidth_gb", "allowed_bandwidth_gb", "netmask_v4",
             "gateway_v4", "power_status", "server_state",
-            "VPSPLANID",  # Plan id, see /v1/plans/list
             "v6_networks",
             # TODO: Does we really need kvm_url?
             "kvm_url",
             "auto_backups", "tag",
-            "OSID",  # Operating system to use. See v1/os/list.
+            # "OSID",  # Operating system to use. See v1/os/list.
             "APPID", "FIREWALLGROUPID"
         ]
         extra = self._helper.handle_extra(extra_keys, data)
 
-        node = Node(id=data['SUBID'], name=data['label'], state=state,
-                    public_ips=public_ips, private_ips=private_ips,
-                    extra=extra, created_at=created_at, driver=self)
+        resolve_data = VULTR_COMPUTE_INSTANCE_IMAGES.get(data["OSID"])
+        if resolve_data:
+            image = self._to_image(resolve_data)
+        else:
+            image = None
+
+        resolve_data = VULTR_COMPUTE_INSTANCE_SIZES.get(data["VPSPLANID"])
+        if resolve_data:
+            size = self._to_size(resolve_data)
+        else:
+            size = None
+
+        # resolve_data = VULTR_COMPUTE_INSTANCE_LOCATIONS.get(data['DCID'])
+        # if resolve_data:
+        #     location = self._to_location(resolve_data)
+        # extra['location'] = location
+
+        node = Node(
+            id=data['SUBID'],
+            name=data['label'],
+            state=state,
+            public_ips=public_ips,
+            private_ips=private_ips,
+            image=image,
+            size=size,
+            extra=extra,
+            created_at=created_at,
+            driver=self)
 
         return node
 
diff --git a/libcloud/test/compute/test_vultr.py b/libcloud/test/compute/test_vultr.py
index 8bb07ec..7caff46 100644
--- a/libcloud/test/compute/test_vultr.py
+++ b/libcloud/test/compute/test_vultr.py
@@ -26,6 +26,7 @@ from libcloud.utils.py3 import httplib
 from libcloud.common.types import ServiceUnavailableError
 
 from libcloud.compute.drivers.vultr import VultrNodeDriver
+from libcloud.compute.base import NodeImage, NodeSize
 
 from libcloud.test import LibcloudTestCase, MockHttp
 from libcloud.test.file_fixtures import ComputeFileFixtures
@@ -111,9 +112,19 @@ class VultrTests(LibcloudTestCase):
         self.assertEqual(nodes[0].private_ips, ['10.7.96.85'])
         self.assertEqual(nodes[2].private_ips, [])
 
+    def test_list_nodes_image_success(self):
+        nodes = self.driver.list_nodes()
+        node = nodes[0]
+        self.assertTrue(isinstance(node.image, NodeImage))
+
+    def test_list_nodes_size_success(self):
+        nodes = self.driver.list_nodes()
+        node = nodes[0]
+        self.assertTrue(isinstance(node.size, NodeSize))
+
     def test_list_nodes_success_extra(self):
         extra_keys = [
-            "ram", "disk", "vcpu_count", "default_password", "power_status",
+            "default_password", "pending_charges", "cost_per_month",
         ]
         nodes = self.driver.list_nodes()
         for node in nodes: