Allow config overrides for TripleOHashInfo creation
Allows passing in a config dictionary into the TripleOHashInfo constructor. This allows to override the loaded config for example to pass in a dlrn_url. The passed config doesn't need to contain all the config keys and the rest of the values are taken from the loaded config file as before. This also adds a new parameter for the ansible module so we can pass dlrn_url via ansible invocations. Includes 2 new test cases for the new functionality. Used by [1]. [1] https://review.opendev.org/c/openstack/tripleo-quickstart/+/791486 Change-Id: I6e3e8663b7ca1ec2758843795142f91aa29fe74d
This commit is contained in:
parent
e86d75219c
commit
f584561253
@ -204,3 +204,29 @@ class TestGetHashInfo(unittest.TestCase):
|
||||
None,
|
||||
'tripleo-ci-testing',
|
||||
)
|
||||
|
||||
def test_override_config_dlrn_url(self, mock_config):
|
||||
expected_dlrn_url = 'https://foo.bar.baz/centos8-master/component/common/current-tripleo/commit.yaml' # noqa
|
||||
with requests_mock.Mocker() as req_mock:
|
||||
req_mock.get(
|
||||
expected_dlrn_url,
|
||||
text=test_fakes.TEST_COMMIT_YAML_COMPONENT,
|
||||
)
|
||||
mock_hash_info = thi.TripleOHashInfo(
|
||||
'centos8', 'master', 'common', 'current-tripleo',
|
||||
{'dlrn_url': 'https://foo.bar.baz'}
|
||||
)
|
||||
self.assertEqual(expected_dlrn_url, mock_hash_info.dlrn_url)
|
||||
|
||||
def test_override_config_dlrn_url_empty_ignored(self, mock_config):
|
||||
expected_dlrn_url = 'https://trunk.rdoproject.org/centos8-master/component/common/current-tripleo/commit.yaml' # noqa
|
||||
with requests_mock.Mocker() as req_mock:
|
||||
req_mock.get(
|
||||
expected_dlrn_url,
|
||||
text=test_fakes.TEST_COMMIT_YAML_COMPONENT,
|
||||
)
|
||||
mock_hash_info = thi.TripleOHashInfo(
|
||||
'centos8', 'master', 'common', 'current-tripleo',
|
||||
{'dlrn_url': ''}
|
||||
)
|
||||
self.assertEqual(expected_dlrn_url, mock_hash_info.dlrn_url)
|
||||
|
@ -48,6 +48,11 @@ options:
|
||||
required: false
|
||||
type: str
|
||||
default: current-tripleo
|
||||
dlrn_url:
|
||||
description: The url of the DLRN server to use for hash queries
|
||||
required: false
|
||||
type: str
|
||||
default: https://trunk.rdoproject.org
|
||||
|
||||
author:
|
||||
- Marios Andreou (@marios)
|
||||
@ -59,6 +64,7 @@ EXAMPLES = r'''
|
||||
os_version: centos8
|
||||
release: victoria
|
||||
component: tripleo
|
||||
dlrn_url: 'https://foo.bar.baz'
|
||||
'''
|
||||
|
||||
RETURN = r'''
|
||||
@ -102,6 +108,9 @@ def run_module():
|
||||
release=dict(type='str', required=False, default='master'),
|
||||
component=dict(type='str', required=False, default=None),
|
||||
tag=dict(type='str', required=False, default='current-tripleo'),
|
||||
dlrn_url=dict(type='str',
|
||||
required=False,
|
||||
default='https://trunk.rdoproject.org'),
|
||||
)
|
||||
|
||||
module = AnsibleModule(
|
||||
@ -115,8 +124,10 @@ def run_module():
|
||||
release = module.params.get('release')
|
||||
component = module.params.get('component')
|
||||
tag = module.params.get('tag')
|
||||
dlrn_url = module.params.get('dlrn_url')
|
||||
|
||||
hash_result = TripleOHashInfo(os_version, release, component, tag)
|
||||
hash_result = TripleOHashInfo(os_version, release, component, tag,
|
||||
config={'dlrn_url': dlrn_url})
|
||||
result['commit_hash'] = hash_result.commit_hash
|
||||
result['distro_hash'] = hash_result.distro_hash
|
||||
result['full_hash'] = hash_result.full_hash
|
||||
|
@ -66,7 +66,7 @@ class TripleOHashInfo:
|
||||
logger.setLevel(logging.INFO)
|
||||
|
||||
@classmethod
|
||||
def load_config(cls):
|
||||
def load_config(cls, passed_config=None):
|
||||
"""
|
||||
This is a class method since we call it from the CLI entrypoint
|
||||
before the TripleOHashInfo object is created. The method will first
|
||||
@ -74,12 +74,15 @@ class TripleOHashInfo:
|
||||
a local config.yaml for example for invocations from a source checkout
|
||||
directory. If the file is not found TripleOHashMissingConfig is raised.
|
||||
If any of the contants.CONFIG_KEYS is missing from config.yaml then
|
||||
TripleOHashInvalidConfig is raised. Returns a dictionary containing
|
||||
TripleOHashInvalidConfig is raised. If the passed_config dict contains
|
||||
a given config value then that is used instead of the value from the
|
||||
loaded config file. Returns a dictionary containing
|
||||
the key->value for all the keys in constants.CONFIG_KEYS.
|
||||
|
||||
:param passed_config: dict with configuration overrides
|
||||
:raises TripleOHashMissingConfig for missing config.yaml
|
||||
:raises TripleOHashInvalidConfig for missing keys in config.yaml
|
||||
:returns a config dictionary with the keys in constants.CONFIG_KEYS
|
||||
:return: a config dictionary with the keys in constants.CONFIG_KEYS
|
||||
"""
|
||||
|
||||
def _check_read_file(filepath):
|
||||
@ -101,6 +104,7 @@ class TripleOHashInfo:
|
||||
if _check_read_file(_local_config):
|
||||
return _local_config
|
||||
|
||||
passed_config = passed_config or {}
|
||||
result_config = {}
|
||||
config_path = ''
|
||||
local_config = _resolve_local_config_path()
|
||||
@ -117,9 +121,9 @@ class TripleOHashInfo:
|
||||
)
|
||||
logging.info("Using config file at {}".format(config_path))
|
||||
with open(config_path, 'r') as config_yaml:
|
||||
conf_yaml = yaml.safe_load(config_yaml)
|
||||
loaded_config = yaml.safe_load(config_yaml)
|
||||
for k in const.CONFIG_KEYS:
|
||||
if k not in conf_yaml:
|
||||
if k not in loaded_config:
|
||||
error_str = (
|
||||
"Malformed config file - missing {}. Expected all"
|
||||
"of these configuration items: {}"
|
||||
@ -128,8 +132,11 @@ class TripleOHashInfo:
|
||||
)
|
||||
logging.error(error_str)
|
||||
raise exc.TripleOHashInvalidConfig(error_str)
|
||||
loaded_value = conf_yaml[k]
|
||||
result_config[k] = loaded_value
|
||||
# if the passed config contains the key then use that value
|
||||
if passed_config.get(k):
|
||||
result_config[k] = passed_config[k]
|
||||
else:
|
||||
result_config[k] = loaded_config[k]
|
||||
return result_config
|
||||
|
||||
def __init__(self, os_version, release, component, tag, config=None):
|
||||
@ -142,8 +149,7 @@ class TripleOHashInfo:
|
||||
:param config: Use an existing config dictionary and don't load it
|
||||
"""
|
||||
|
||||
if config is None:
|
||||
config = TripleOHashInfo.load_config()
|
||||
config = TripleOHashInfo.load_config(config)
|
||||
|
||||
self.os_version = os_version
|
||||
self.release = release
|
||||
|
Loading…
Reference in New Issue
Block a user