diff --git a/src/lifted/providers.py b/src/lifted/providers.py index 19fadf83..e4688da2 100644 --- a/src/lifted/providers.py +++ b/src/lifted/providers.py @@ -170,3 +170,38 @@ def save_settings(ucfg, provider_name, profile, settings): with open(path, "w") as settings_file: toml.dump(settings, settings_file) + +def load_settings(ucfg, provider_name, profile): + """Load settings for a provider's profile + + :param ucfg: upload config + :type ucfg: object + :param provider_name: the name of the cloud provider, e.g. "azure" + :type provider_name: str + :param profile: the name of the profile to save + :type profile: str != "" + :returns: The profile settings for the selected provider + :rtype: dict + :raises: ValueError when passed invalid settings or an invalid profile name + :raises: RuntimeError when the provider or profile couldn't be found + :raises: ValueError when the passed settings are invalid + + This also calls validate_settings on the loaded settings, potentially + raising an error if the saved settings are invalid. + """ + if not profile: + raise ValueError("Profile name cannot be empty!") + if not provider_name: + raise ValueError("Provider name cannot be empty!") + directory = os.path.join(ucfg["settings_dir"], provider_name) + if not os.path.isdir(directory): + raise RuntimeError(f'Couldn\'t find provider "{provider_name}"!') + + path = os.path.join(directory, f"{profile}.toml") + if not os.path.isfile(path): + raise RuntimeError(f'Couldn\'t find provider "{provider_name}"!') + + with open(path) as file: + settings = toml.load(file) + validate_settings(ucfg, provider_name, settings) + return settings diff --git a/tests/lifted/test_providers.py b/tests/lifted/test_providers.py index 0c73261f..0e79b61e 100644 --- a/tests/lifted/test_providers.py +++ b/tests/lifted/test_providers.py @@ -21,7 +21,7 @@ import unittest import lifted.config from lifted.providers import list_providers, resolve_provider, resolve_playbook_path, save_settings -from lifted.providers import load_profiles, validate_settings +from lifted.providers import load_profiles, validate_settings, load_settings import pylorax.api.config from pylorax.sysutils import joinpaths @@ -93,3 +93,28 @@ class ProvidersTestCase(unittest.TestCase): print(p) profile = load_profiles(self.config["upload"], p) self.assertTrue(test_profiles[p][0] in profile) + + # This *must* run after test_save_settings, _zz_ ensures that happens + def test_zz_load_settings_errors(self): + """Test returning the correct errors for missing profiles and providers""" + with self.assertRaises(ValueError): + load_settings(self.config["upload"], "", "") + + with self.assertRaises(ValueError): + load_settings(self.config["upload"], "", "default") + + with self.assertRaises(ValueError): + load_settings(self.config["upload"], "azure", "") + + with self.assertRaises(RuntimeError): + load_settings(self.config["upload"], "foo", "default") + + with self.assertRaises(RuntimeError): + load_settings(self.config["upload"], "azure", "missing-test") + + # This *must* run after test_save_settings, _zz_ ensures that happens + def test_zz_load_settings(self): + """Test loading settings""" + for p in list_providers(self.config["upload"]): + settings = load_settings(self.config["upload"], p, test_profiles[p][0]) + self.assertEqual(settings, test_profiles[p][1])