lifted: Add a function to load the settings for a provider's profile
This returns the settings after loading them and running verify_settings on them.
This commit is contained in:
parent
1030dd083c
commit
bc06abceab
@ -170,3 +170,38 @@ def save_settings(ucfg, provider_name, profile, settings):
|
|||||||
|
|
||||||
with open(path, "w") as settings_file:
|
with open(path, "w") as settings_file:
|
||||||
toml.dump(settings, 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
|
||||||
|
@ -21,7 +21,7 @@ import unittest
|
|||||||
|
|
||||||
import lifted.config
|
import lifted.config
|
||||||
from lifted.providers import list_providers, resolve_provider, resolve_playbook_path, save_settings
|
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
|
import pylorax.api.config
|
||||||
from pylorax.sysutils import joinpaths
|
from pylorax.sysutils import joinpaths
|
||||||
|
|
||||||
@ -93,3 +93,28 @@ class ProvidersTestCase(unittest.TestCase):
|
|||||||
print(p)
|
print(p)
|
||||||
profile = load_profiles(self.config["upload"], p)
|
profile = load_profiles(self.config["upload"], p)
|
||||||
self.assertTrue(test_profiles[p][0] in profile)
|
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])
|
||||||
|
Loading…
Reference in New Issue
Block a user