591b7e
From ccae8d2ac218366c529aac03b29c46400843d4a0 Mon Sep 17 00:00:00 2001
591b7e
From: Eduardo Otubo <otubo@redhat.com>
591b7e
Date: Tue, 5 May 2020 08:08:09 +0200
591b7e
Subject: [PATCH 1/5] New data source for the Exoscale.com cloud platform
591b7e
591b7e
RH-Author: Eduardo Otubo <otubo@redhat.com>
591b7e
Message-id: <20200504085238.25884-2-otubo@redhat.com>
591b7e
Patchwork-id: 96244
591b7e
O-Subject: [RHEL-7.8.z cloud-init PATCH 1/5] New data source for the Exoscale.com cloud platform
591b7e
Bugzilla: 1827207
591b7e
RH-Acked-by: Cathy Avery <cavery@redhat.com>
591b7e
RH-Acked-by: Mohammed Gamal <mgamal@redhat.com>
591b7e
RH-Acked-by: Vitaly Kuznetsov <vkuznets@redhat.com>
591b7e
591b7e
commit 4dfed67d0e82970f8717d0b524c593962698ca4f
591b7e
Author: Chris Glass <christopher.glass@exoscale.ch>
591b7e
Date:   Thu Aug 8 17:09:57 2019 +0000
591b7e
591b7e
    New data source for the Exoscale.com cloud platform
591b7e
591b7e
    - dsidentify switches to the new Exoscale datasource on matching DMI name
591b7e
    - New Exoscale datasource added
591b7e
591b7e
    Signed-off-by: Mathieu Corbin <mathieu.corbin@exoscale.ch>
591b7e
591b7e
Signed-off-by: Eduardo Otubo <otubo@redhat.com>
591b7e
Signed-off-by: Miroslav Rezanina <mrezanin@redhat.com>
591b7e
---
591b7e
 cloudinit/apport.py                              |   1 +
591b7e
 cloudinit/settings.py                            |   1 +
591b7e
 cloudinit/sources/DataSourceExoscale.py          | 258 +++++++++++++++++++++++
591b7e
 doc/rtd/topics/datasources.rst                   |   1 +
591b7e
 doc/rtd/topics/datasources/exoscale.rst          |  68 ++++++
591b7e
 tests/unittests/test_datasource/test_common.py   |   2 +
591b7e
 tests/unittests/test_datasource/test_exoscale.py | 203 ++++++++++++++++++
591b7e
 tools/ds-identify                                |   7 +-
591b7e
 8 files changed, 540 insertions(+), 1 deletion(-)
591b7e
 create mode 100644 cloudinit/sources/DataSourceExoscale.py
591b7e
 create mode 100644 doc/rtd/topics/datasources/exoscale.rst
591b7e
 create mode 100644 tests/unittests/test_datasource/test_exoscale.py
591b7e
591b7e
diff --git a/cloudinit/apport.py b/cloudinit/apport.py
591b7e
index 22cb7fd..003ff1f 100644
591b7e
--- a/cloudinit/apport.py
591b7e
+++ b/cloudinit/apport.py
591b7e
@@ -23,6 +23,7 @@ KNOWN_CLOUD_NAMES = [
591b7e
     'CloudStack',
591b7e
     'DigitalOcean',
591b7e
     'GCE - Google Compute Engine',
591b7e
+    'Exoscale',
591b7e
     'Hetzner Cloud',
591b7e
     'IBM - (aka SoftLayer or BlueMix)',
591b7e
     'LXD',
591b7e
diff --git a/cloudinit/settings.py b/cloudinit/settings.py
591b7e
index d982a4d..229b420 100644
591b7e
--- a/cloudinit/settings.py
591b7e
+++ b/cloudinit/settings.py
591b7e
@@ -39,6 +39,7 @@ CFG_BUILTIN = {
591b7e
         'Hetzner',
591b7e
         'IBMCloud',
591b7e
         'Oracle',
591b7e
+        'Exoscale',
591b7e
         # At the end to act as a 'catch' when none of the above work...
591b7e
         'None',
591b7e
     ],
591b7e
diff --git a/cloudinit/sources/DataSourceExoscale.py b/cloudinit/sources/DataSourceExoscale.py
591b7e
new file mode 100644
591b7e
index 0000000..52e7f6f
591b7e
--- /dev/null
591b7e
+++ b/cloudinit/sources/DataSourceExoscale.py
591b7e
@@ -0,0 +1,258 @@
591b7e
+# Author: Mathieu Corbin <mathieu.corbin@exoscale.com>
591b7e
+# Author: Christopher Glass <christopher.glass@exoscale.com>
591b7e
+#
591b7e
+# This file is part of cloud-init. See LICENSE file for license information.
591b7e
+
591b7e
+from cloudinit import ec2_utils as ec2
591b7e
+from cloudinit import log as logging
591b7e
+from cloudinit import sources
591b7e
+from cloudinit import url_helper
591b7e
+from cloudinit import util
591b7e
+
591b7e
+LOG = logging.getLogger(__name__)
591b7e
+
591b7e
+METADATA_URL = "http://169.254.169.254"
591b7e
+API_VERSION = "1.0"
591b7e
+PASSWORD_SERVER_PORT = 8080
591b7e
+
591b7e
+URL_TIMEOUT = 10
591b7e
+URL_RETRIES = 6
591b7e
+
591b7e
+EXOSCALE_DMI_NAME = "Exoscale"
591b7e
+
591b7e
+BUILTIN_DS_CONFIG = {
591b7e
+    # We run the set password config module on every boot in order to enable
591b7e
+    # resetting the instance's password via the exoscale console (and a
591b7e
+    # subsequent instance reboot).
591b7e
+    'cloud_config_modules': [["set-passwords", "always"]]
591b7e
+}
591b7e
+
591b7e
+
591b7e
+class DataSourceExoscale(sources.DataSource):
591b7e
+
591b7e
+    dsname = 'Exoscale'
591b7e
+
591b7e
+    def __init__(self, sys_cfg, distro, paths):
591b7e
+        super(DataSourceExoscale, self).__init__(sys_cfg, distro, paths)
591b7e
+        LOG.debug("Initializing the Exoscale datasource")
591b7e
+
591b7e
+        self.metadata_url = self.ds_cfg.get('metadata_url', METADATA_URL)
591b7e
+        self.api_version = self.ds_cfg.get('api_version', API_VERSION)
591b7e
+        self.password_server_port = int(
591b7e
+            self.ds_cfg.get('password_server_port', PASSWORD_SERVER_PORT))
591b7e
+        self.url_timeout = self.ds_cfg.get('timeout', URL_TIMEOUT)
591b7e
+        self.url_retries = self.ds_cfg.get('retries', URL_RETRIES)
591b7e
+
591b7e
+        self.extra_config = BUILTIN_DS_CONFIG
591b7e
+
591b7e
+    def wait_for_metadata_service(self):
591b7e
+        """Wait for the metadata service to be reachable."""
591b7e
+
591b7e
+        metadata_url = "{}/{}/meta-data/instance-id".format(
591b7e
+            self.metadata_url, self.api_version)
591b7e
+
591b7e
+        url = url_helper.wait_for_url(
591b7e
+            urls=[metadata_url],
591b7e
+            max_wait=self.url_max_wait,
591b7e
+            timeout=self.url_timeout,
591b7e
+            status_cb=LOG.critical)
591b7e
+
591b7e
+        return bool(url)
591b7e
+
591b7e
+    def crawl_metadata(self):
591b7e
+        """
591b7e
+        Crawl the metadata service when available.
591b7e
+
591b7e
+        @returns: Dictionary of crawled metadata content.
591b7e
+        """
591b7e
+        metadata_ready = util.log_time(
591b7e
+            logfunc=LOG.info,
591b7e
+            msg='waiting for the metadata service',
591b7e
+            func=self.wait_for_metadata_service)
591b7e
+
591b7e
+        if not metadata_ready:
591b7e
+            return {}
591b7e
+
591b7e
+        return read_metadata(self.metadata_url, self.api_version,
591b7e
+                             self.password_server_port, self.url_timeout,
591b7e
+                             self.url_retries)
591b7e
+
591b7e
+    def _get_data(self):
591b7e
+        """Fetch the user data, the metadata and the VM password
591b7e
+        from the metadata service.
591b7e
+
591b7e
+        Please refer to the datasource documentation for details on how the
591b7e
+        metadata server and password server are crawled.
591b7e
+        """
591b7e
+        if not self._is_platform_viable():
591b7e
+            return False
591b7e
+
591b7e
+        data = util.log_time(
591b7e
+            logfunc=LOG.debug,
591b7e
+            msg='Crawl of metadata service',
591b7e
+            func=self.crawl_metadata)
591b7e
+
591b7e
+        if not data:
591b7e
+            return False
591b7e
+
591b7e
+        self.userdata_raw = data['user-data']
591b7e
+        self.metadata = data['meta-data']
591b7e
+        password = data.get('password')
591b7e
+
591b7e
+        password_config = {}
591b7e
+        if password:
591b7e
+            # Since we have a password, let's make sure we are allowed to use
591b7e
+            # it by allowing ssh_pwauth.
591b7e
+            # The password module's default behavior is to leave the
591b7e
+            # configuration as-is in this regard, so that means it will either
591b7e
+            # leave the password always disabled if no password is ever set, or
591b7e
+            # leave the password login enabled if we set it once.
591b7e
+            password_config = {
591b7e
+                'ssh_pwauth': True,
591b7e
+                'password': password,
591b7e
+                'chpasswd': {
591b7e
+                    'expire': False,
591b7e
+                },
591b7e
+            }
591b7e
+
591b7e
+        # builtin extra_config overrides password_config
591b7e
+        self.extra_config = util.mergemanydict(
591b7e
+            [self.extra_config, password_config])
591b7e
+
591b7e
+        return True
591b7e
+
591b7e
+    def get_config_obj(self):
591b7e
+        return self.extra_config
591b7e
+
591b7e
+    def _is_platform_viable(self):
591b7e
+        return util.read_dmi_data('system-product-name').startswith(
591b7e
+            EXOSCALE_DMI_NAME)
591b7e
+
591b7e
+
591b7e
+# Used to match classes to dependencies
591b7e
+datasources = [
591b7e
+    (DataSourceExoscale, (sources.DEP_FILESYSTEM, sources.DEP_NETWORK)),
591b7e
+]
591b7e
+
591b7e
+
591b7e
+# Return a list of data sources that match this set of dependencies
591b7e
+def get_datasource_list(depends):
591b7e
+    return sources.list_from_depends(depends, datasources)
591b7e
+
591b7e
+
591b7e
+def get_password(metadata_url=METADATA_URL,
591b7e
+                 api_version=API_VERSION,
591b7e
+                 password_server_port=PASSWORD_SERVER_PORT,
591b7e
+                 url_timeout=URL_TIMEOUT,
591b7e
+                 url_retries=URL_RETRIES):
591b7e
+    """Obtain the VM's password if set.
591b7e
+
591b7e
+    Once fetched the password is marked saved. Future calls to this method may
591b7e
+    return empty string or 'saved_password'."""
591b7e
+    password_url = "{}:{}/{}/".format(metadata_url, password_server_port,
591b7e
+                                      api_version)
591b7e
+    response = url_helper.read_file_or_url(
591b7e
+        password_url,
591b7e
+        ssl_details=None,
591b7e
+        headers={"DomU_Request": "send_my_password"},
591b7e
+        timeout=url_timeout,
591b7e
+        retries=url_retries)
591b7e
+    password = response.contents.decode('utf-8')
591b7e
+    # the password is empty or already saved
591b7e
+    # Note: the original metadata server would answer an additional
591b7e
+    # 'bad_request' status, but the Exoscale implementation does not.
591b7e
+    if password in ['', 'saved_password']:
591b7e
+        return None
591b7e
+    # save the password
591b7e
+    url_helper.read_file_or_url(
591b7e
+        password_url,
591b7e
+        ssl_details=None,
591b7e
+        headers={"DomU_Request": "saved_password"},
591b7e
+        timeout=url_timeout,
591b7e
+        retries=url_retries)
591b7e
+    return password
591b7e
+
591b7e
+
591b7e
+def read_metadata(metadata_url=METADATA_URL,
591b7e
+                  api_version=API_VERSION,
591b7e
+                  password_server_port=PASSWORD_SERVER_PORT,
591b7e
+                  url_timeout=URL_TIMEOUT,
591b7e
+                  url_retries=URL_RETRIES):
591b7e
+    """Query the metadata server and return the retrieved data."""
591b7e
+    crawled_metadata = {}
591b7e
+    crawled_metadata['_metadata_api_version'] = api_version
591b7e
+    try:
591b7e
+        crawled_metadata['user-data'] = ec2.get_instance_userdata(
591b7e
+            api_version,
591b7e
+            metadata_url,
591b7e
+            timeout=url_timeout,
591b7e
+            retries=url_retries)
591b7e
+        crawled_metadata['meta-data'] = ec2.get_instance_metadata(
591b7e
+            api_version,
591b7e
+            metadata_url,
591b7e
+            timeout=url_timeout,
591b7e
+            retries=url_retries)
591b7e
+    except Exception as e:
591b7e
+        util.logexc(LOG, "failed reading from metadata url %s (%s)",
591b7e
+                    metadata_url, e)
591b7e
+        return {}
591b7e
+
591b7e
+    try:
591b7e
+        crawled_metadata['password'] = get_password(
591b7e
+            api_version=api_version,
591b7e
+            metadata_url=metadata_url,
591b7e
+            password_server_port=password_server_port,
591b7e
+            url_retries=url_retries,
591b7e
+            url_timeout=url_timeout)
591b7e
+    except Exception as e:
591b7e
+        util.logexc(LOG, "failed to read from password server url %s:%s (%s)",
591b7e
+                    metadata_url, password_server_port, e)
591b7e
+
591b7e
+    return crawled_metadata
591b7e
+
591b7e
+
591b7e
+if __name__ == "__main__":
591b7e
+    import argparse
591b7e
+
591b7e
+    parser = argparse.ArgumentParser(description='Query Exoscale Metadata')
591b7e
+    parser.add_argument(
591b7e
+        "--endpoint",
591b7e
+        metavar="URL",
591b7e
+        help="The url of the metadata service.",
591b7e
+        default=METADATA_URL)
591b7e
+    parser.add_argument(
591b7e
+        "--version",
591b7e
+        metavar="VERSION",
591b7e
+        help="The version of the metadata endpoint to query.",
591b7e
+        default=API_VERSION)
591b7e
+    parser.add_argument(
591b7e
+        "--retries",
591b7e
+        metavar="NUM",
591b7e
+        type=int,
591b7e
+        help="The number of retries querying the endpoint.",
591b7e
+        default=URL_RETRIES)
591b7e
+    parser.add_argument(
591b7e
+        "--timeout",
591b7e
+        metavar="NUM",
591b7e
+        type=int,
591b7e
+        help="The time in seconds to wait before timing out.",
591b7e
+        default=URL_TIMEOUT)
591b7e
+    parser.add_argument(
591b7e
+        "--password-port",
591b7e
+        metavar="PORT",
591b7e
+        type=int,
591b7e
+        help="The port on which the password endpoint listens",
591b7e
+        default=PASSWORD_SERVER_PORT)
591b7e
+
591b7e
+    args = parser.parse_args()
591b7e
+
591b7e
+    data = read_metadata(
591b7e
+        metadata_url=args.endpoint,
591b7e
+        api_version=args.version,
591b7e
+        password_server_port=args.password_port,
591b7e
+        url_timeout=args.timeout,
591b7e
+        url_retries=args.retries)
591b7e
+
591b7e
+    print(util.json_dumps(data))
591b7e
+
591b7e
+# vi: ts=4 expandtab
591b7e
diff --git a/doc/rtd/topics/datasources.rst b/doc/rtd/topics/datasources.rst
591b7e
index e34f145..fcfd91a 100644
591b7e
--- a/doc/rtd/topics/datasources.rst
591b7e
+++ b/doc/rtd/topics/datasources.rst
591b7e
@@ -96,6 +96,7 @@ Follow for more information.
591b7e
    datasources/configdrive.rst
591b7e
    datasources/digitalocean.rst
591b7e
    datasources/ec2.rst
591b7e
+   datasources/exoscale.rst
591b7e
    datasources/maas.rst
591b7e
    datasources/nocloud.rst
591b7e
    datasources/opennebula.rst
591b7e
diff --git a/doc/rtd/topics/datasources/exoscale.rst b/doc/rtd/topics/datasources/exoscale.rst
591b7e
new file mode 100644
591b7e
index 0000000..27aec9c
591b7e
--- /dev/null
591b7e
+++ b/doc/rtd/topics/datasources/exoscale.rst
591b7e
@@ -0,0 +1,68 @@
591b7e
+.. _datasource_exoscale:
591b7e
+
591b7e
+Exoscale
591b7e
+========
591b7e
+
591b7e
+This datasource supports reading from the metadata server used on the
591b7e
+`Exoscale platform <https://exoscale.com>`_.
591b7e
+
591b7e
+Use of the Exoscale datasource is recommended to benefit from new features of
591b7e
+the Exoscale platform.
591b7e
+
591b7e
+The datasource relies on the availability of a compatible metadata server
591b7e
+(``http://169.254.169.254`` is used by default) and its companion password
591b7e
+server, reachable at the same address (by default on port 8080).
591b7e
+
591b7e
+Crawling of metadata
591b7e
+--------------------
591b7e
+
591b7e
+The metadata service and password server are crawled slightly differently:
591b7e
+
591b7e
+ * The "metadata service" is crawled every boot.
591b7e
+ * The password server is also crawled every boot (the Exoscale datasource
591b7e
+   forces the password module to run with "frequency always").
591b7e
+
591b7e
+In the password server case, the following rules apply in order to enable the
591b7e
+"restore instance password" functionality:
591b7e
+
591b7e
+ * If a password is returned by the password server, it is then marked "saved"
591b7e
+   by the cloud-init datasource. Subsequent boots will skip setting the password
591b7e
+   (the password server will return "saved_password").
591b7e
+ * When the instance password is reset (via the Exoscale UI), the password
591b7e
+   server will return the non-empty password at next boot, therefore causing
591b7e
+   cloud-init to reset the instance's password.
591b7e
+
591b7e
+Configuration
591b7e
+-------------
591b7e
+
591b7e
+Users of this datasource are discouraged from changing the default settings
591b7e
+unless instructed to by Exoscale support.
591b7e
+
591b7e
+The following settings are available and can be set for the datasource in system
591b7e
+configuration (in `/etc/cloud/cloud.cfg.d/`).
591b7e
+
591b7e
+The settings available are:
591b7e
+
591b7e
+ * **metadata_url**: The URL for the metadata service (defaults to
591b7e
+   ``http://169.254.169.254``)
591b7e
+ * **api_version**: The API version path on which to query the instance metadata
591b7e
+   (defaults to ``1.0``)
591b7e
+ * **password_server_port**: The port (on the metadata server) on which the
591b7e
+   password server listens (defaults to ``8080``).
591b7e
+ * **timeout**: the timeout value provided to urlopen for each individual http
591b7e
+   request. (defaults to ``10``)
591b7e
+ * **retries**: The number of retries that should be done for an http request
591b7e
+   (defaults to ``6``)
591b7e
+
591b7e
+
591b7e
+An example configuration with the default values is provided below:
591b7e
+
591b7e
+.. sourcecode:: yaml
591b7e
+
591b7e
+   datasource:
591b7e
+     Exoscale:
591b7e
+       metadata_url: "http://169.254.169.254"
591b7e
+       api_version: "1.0"
591b7e
+       password_server_port: 8080
591b7e
+       timeout: 10
591b7e
+       retries: 6
591b7e
diff --git a/tests/unittests/test_datasource/test_common.py b/tests/unittests/test_datasource/test_common.py
591b7e
index 6b01a4e..24b0fac 100644
591b7e
--- a/tests/unittests/test_datasource/test_common.py
591b7e
+++ b/tests/unittests/test_datasource/test_common.py
591b7e
@@ -13,6 +13,7 @@ from cloudinit.sources import (
591b7e
     DataSourceConfigDrive as ConfigDrive,
591b7e
     DataSourceDigitalOcean as DigitalOcean,
591b7e
     DataSourceEc2 as Ec2,
591b7e
+    DataSourceExoscale as Exoscale,
591b7e
     DataSourceGCE as GCE,
591b7e
     DataSourceHetzner as Hetzner,
591b7e
     DataSourceIBMCloud as IBMCloud,
591b7e
@@ -53,6 +54,7 @@ DEFAULT_NETWORK = [
591b7e
     CloudStack.DataSourceCloudStack,
591b7e
     DSNone.DataSourceNone,
591b7e
     Ec2.DataSourceEc2,
591b7e
+    Exoscale.DataSourceExoscale,
591b7e
     GCE.DataSourceGCE,
591b7e
     MAAS.DataSourceMAAS,
591b7e
     NoCloud.DataSourceNoCloudNet,
591b7e
diff --git a/tests/unittests/test_datasource/test_exoscale.py b/tests/unittests/test_datasource/test_exoscale.py
591b7e
new file mode 100644
591b7e
index 0000000..350c330
591b7e
--- /dev/null
591b7e
+++ b/tests/unittests/test_datasource/test_exoscale.py
591b7e
@@ -0,0 +1,203 @@
591b7e
+# Author: Mathieu Corbin <mathieu.corbin@exoscale.com>
591b7e
+# Author: Christopher Glass <christopher.glass@exoscale.com>
591b7e
+#
591b7e
+# This file is part of cloud-init. See LICENSE file for license information.
591b7e
+from cloudinit import helpers
591b7e
+from cloudinit.sources.DataSourceExoscale import (
591b7e
+    API_VERSION,
591b7e
+    DataSourceExoscale,
591b7e
+    METADATA_URL,
591b7e
+    get_password,
591b7e
+    PASSWORD_SERVER_PORT,
591b7e
+    read_metadata)
591b7e
+from cloudinit.tests.helpers import HttprettyTestCase, mock
591b7e
+
591b7e
+import httpretty
591b7e
+import requests
591b7e
+
591b7e
+
591b7e
+TEST_PASSWORD_URL = "{}:{}/{}/".format(METADATA_URL,
591b7e
+                                       PASSWORD_SERVER_PORT,
591b7e
+                                       API_VERSION)
591b7e
+
591b7e
+TEST_METADATA_URL = "{}/{}/meta-data/".format(METADATA_URL,
591b7e
+                                              API_VERSION)
591b7e
+
591b7e
+TEST_USERDATA_URL = "{}/{}/user-data".format(METADATA_URL,
591b7e
+                                             API_VERSION)
591b7e
+
591b7e
+
591b7e
+@httpretty.activate
591b7e
+class TestDatasourceExoscale(HttprettyTestCase):
591b7e
+
591b7e
+    def setUp(self):
591b7e
+        super(TestDatasourceExoscale, self).setUp()
591b7e
+        self.tmp = self.tmp_dir()
591b7e
+        self.password_url = TEST_PASSWORD_URL
591b7e
+        self.metadata_url = TEST_METADATA_URL
591b7e
+        self.userdata_url = TEST_USERDATA_URL
591b7e
+
591b7e
+    def test_password_saved(self):
591b7e
+        """The password is not set when it is not found
591b7e
+        in the metadata service."""
591b7e
+        httpretty.register_uri(httpretty.GET,
591b7e
+                               self.password_url,
591b7e
+                               body="saved_password")
591b7e
+        self.assertFalse(get_password())
591b7e
+
591b7e
+    def test_password_empty(self):
591b7e
+        """No password is set if the metadata service returns
591b7e
+        an empty string."""
591b7e
+        httpretty.register_uri(httpretty.GET,
591b7e
+                               self.password_url,
591b7e
+                               body="")
591b7e
+        self.assertFalse(get_password())
591b7e
+
591b7e
+    def test_password(self):
591b7e
+        """The password is set to what is found in the metadata
591b7e
+        service."""
591b7e
+        expected_password = "p@ssw0rd"
591b7e
+        httpretty.register_uri(httpretty.GET,
591b7e
+                               self.password_url,
591b7e
+                               body=expected_password)
591b7e
+        password = get_password()
591b7e
+        self.assertEqual(expected_password, password)
591b7e
+
591b7e
+    def test_get_data(self):
591b7e
+        """The datasource conforms to expected behavior when supplied
591b7e
+        full test data."""
591b7e
+        path = helpers.Paths({'run_dir': self.tmp})
591b7e
+        ds = DataSourceExoscale({}, None, path)
591b7e
+        ds._is_platform_viable = lambda: True
591b7e
+        expected_password = "p@ssw0rd"
591b7e
+        expected_id = "12345"
591b7e
+        expected_hostname = "myname"
591b7e
+        expected_userdata = "#cloud-config"
591b7e
+        httpretty.register_uri(httpretty.GET,
591b7e
+                               self.userdata_url,
591b7e
+                               body=expected_userdata)
591b7e
+        httpretty.register_uri(httpretty.GET,
591b7e
+                               self.password_url,
591b7e
+                               body=expected_password)
591b7e
+        httpretty.register_uri(httpretty.GET,
591b7e
+                               self.metadata_url,
591b7e
+                               body="instance-id\nlocal-hostname")
591b7e
+        httpretty.register_uri(httpretty.GET,
591b7e
+                               "{}local-hostname".format(self.metadata_url),
591b7e
+                               body=expected_hostname)
591b7e
+        httpretty.register_uri(httpretty.GET,
591b7e
+                               "{}instance-id".format(self.metadata_url),
591b7e
+                               body=expected_id)
591b7e
+        self.assertTrue(ds._get_data())
591b7e
+        self.assertEqual(ds.userdata_raw.decode("utf-8"), "#cloud-config")
591b7e
+        self.assertEqual(ds.metadata, {"instance-id": expected_id,
591b7e
+                                       "local-hostname": expected_hostname})
591b7e
+        self.assertEqual(ds.get_config_obj(),
591b7e
+                         {'ssh_pwauth': True,
591b7e
+                          'password': expected_password,
591b7e
+                          'cloud_config_modules': [
591b7e
+                              ["set-passwords", "always"]],
591b7e
+                          'chpasswd': {
591b7e
+                              'expire': False,
591b7e
+                          }})
591b7e
+
591b7e
+    def test_get_data_saved_password(self):
591b7e
+        """The datasource conforms to expected behavior when saved_password is
591b7e
+        returned by the password server."""
591b7e
+        path = helpers.Paths({'run_dir': self.tmp})
591b7e
+        ds = DataSourceExoscale({}, None, path)
591b7e
+        ds._is_platform_viable = lambda: True
591b7e
+        expected_answer = "saved_password"
591b7e
+        expected_id = "12345"
591b7e
+        expected_hostname = "myname"
591b7e
+        expected_userdata = "#cloud-config"
591b7e
+        httpretty.register_uri(httpretty.GET,
591b7e
+                               self.userdata_url,
591b7e
+                               body=expected_userdata)
591b7e
+        httpretty.register_uri(httpretty.GET,
591b7e
+                               self.password_url,
591b7e
+                               body=expected_answer)
591b7e
+        httpretty.register_uri(httpretty.GET,
591b7e
+                               self.metadata_url,
591b7e
+                               body="instance-id\nlocal-hostname")
591b7e
+        httpretty.register_uri(httpretty.GET,
591b7e
+                               "{}local-hostname".format(self.metadata_url),
591b7e
+                               body=expected_hostname)
591b7e
+        httpretty.register_uri(httpretty.GET,
591b7e
+                               "{}instance-id".format(self.metadata_url),
591b7e
+                               body=expected_id)
591b7e
+        self.assertTrue(ds._get_data())
591b7e
+        self.assertEqual(ds.userdata_raw.decode("utf-8"), "#cloud-config")
591b7e
+        self.assertEqual(ds.metadata, {"instance-id": expected_id,
591b7e
+                                       "local-hostname": expected_hostname})
591b7e
+        self.assertEqual(ds.get_config_obj(),
591b7e
+                         {'cloud_config_modules': [
591b7e
+                             ["set-passwords", "always"]]})
591b7e
+
591b7e
+    def test_get_data_no_password(self):
591b7e
+        """The datasource conforms to expected behavior when no password is
591b7e
+        returned by the password server."""
591b7e
+        path = helpers.Paths({'run_dir': self.tmp})
591b7e
+        ds = DataSourceExoscale({}, None, path)
591b7e
+        ds._is_platform_viable = lambda: True
591b7e
+        expected_answer = ""
591b7e
+        expected_id = "12345"
591b7e
+        expected_hostname = "myname"
591b7e
+        expected_userdata = "#cloud-config"
591b7e
+        httpretty.register_uri(httpretty.GET,
591b7e
+                               self.userdata_url,
591b7e
+                               body=expected_userdata)
591b7e
+        httpretty.register_uri(httpretty.GET,
591b7e
+                               self.password_url,
591b7e
+                               body=expected_answer)
591b7e
+        httpretty.register_uri(httpretty.GET,
591b7e
+                               self.metadata_url,
591b7e
+                               body="instance-id\nlocal-hostname")
591b7e
+        httpretty.register_uri(httpretty.GET,
591b7e
+                               "{}local-hostname".format(self.metadata_url),
591b7e
+                               body=expected_hostname)
591b7e
+        httpretty.register_uri(httpretty.GET,
591b7e
+                               "{}instance-id".format(self.metadata_url),
591b7e
+                               body=expected_id)
591b7e
+        self.assertTrue(ds._get_data())
591b7e
+        self.assertEqual(ds.userdata_raw.decode("utf-8"), "#cloud-config")
591b7e
+        self.assertEqual(ds.metadata, {"instance-id": expected_id,
591b7e
+                                       "local-hostname": expected_hostname})
591b7e
+        self.assertEqual(ds.get_config_obj(),
591b7e
+                         {'cloud_config_modules': [
591b7e
+                             ["set-passwords", "always"]]})
591b7e
+
591b7e
+    @mock.patch('cloudinit.sources.DataSourceExoscale.get_password')
591b7e
+    def test_read_metadata_when_password_server_unreachable(self, m_password):
591b7e
+        """The read_metadata function returns partial results in case the
591b7e
+        password server (only) is unreachable."""
591b7e
+        expected_id = "12345"
591b7e
+        expected_hostname = "myname"
591b7e
+        expected_userdata = "#cloud-config"
591b7e
+
591b7e
+        m_password.side_effect = requests.Timeout('Fake Connection Timeout')
591b7e
+        httpretty.register_uri(httpretty.GET,
591b7e
+                               self.userdata_url,
591b7e
+                               body=expected_userdata)
591b7e
+        httpretty.register_uri(httpretty.GET,
591b7e
+                               self.metadata_url,
591b7e
+                               body="instance-id\nlocal-hostname")
591b7e
+        httpretty.register_uri(httpretty.GET,
591b7e
+                               "{}local-hostname".format(self.metadata_url),
591b7e
+                               body=expected_hostname)
591b7e
+        httpretty.register_uri(httpretty.GET,
591b7e
+                               "{}instance-id".format(self.metadata_url),
591b7e
+                               body=expected_id)
591b7e
+
591b7e
+        result = read_metadata()
591b7e
+
591b7e
+        self.assertIsNone(result.get("password"))
591b7e
+        self.assertEqual(result.get("user-data").decode("utf-8"),
591b7e
+                         expected_userdata)
591b7e
+
591b7e
+    def test_non_viable_platform(self):
591b7e
+        """The datasource fails fast when the platform is not viable."""
591b7e
+        path = helpers.Paths({'run_dir': self.tmp})
591b7e
+        ds = DataSourceExoscale({}, None, path)
591b7e
+        ds._is_platform_viable = lambda: False
591b7e
+        self.assertFalse(ds._get_data())
591b7e
diff --git a/tools/ds-identify b/tools/ds-identify
591b7e
index 1acfeeb..6c89b06 100755
591b7e
--- a/tools/ds-identify
591b7e
+++ b/tools/ds-identify
591b7e
@@ -124,7 +124,7 @@ DI_DSNAME=""
591b7e
 # be searched if there is no setting found in config.
591b7e
 DI_DSLIST_DEFAULT="MAAS ConfigDrive NoCloud AltCloud Azure Bigstep \
591b7e
 CloudSigma CloudStack DigitalOcean AliYun Ec2 GCE OpenNebula OpenStack \
591b7e
-OVF SmartOS Scaleway Hetzner IBMCloud Oracle"
591b7e
+OVF SmartOS Scaleway Hetzner IBMCloud Oracle Exoscale"
591b7e
 DI_DSLIST=""
591b7e
 DI_MODE=""
591b7e
 DI_ON_FOUND=""
591b7e
@@ -553,6 +553,11 @@ dscheck_CloudStack() {
591b7e
     return $DS_NOT_FOUND
591b7e
 }
591b7e
 
591b7e
+dscheck_Exoscale() {
591b7e
+    dmi_product_name_matches "Exoscale*" && return $DS_FOUND
591b7e
+    return $DS_NOT_FOUND
591b7e
+}
591b7e
+
591b7e
 dscheck_CloudSigma() {
591b7e
     # http://paste.ubuntu.com/23624795/
591b7e
     dmi_product_name_matches "CloudSigma" && return $DS_FOUND
591b7e
-- 
591b7e
1.8.3.1
591b7e