Avoid failure if dhcp_end or dhcp_start isn't set
Since dhcp_start and dhcp_end aren't mandatory option, we have to ensure
the validation doesn't fail when they aren't present.
This change introduce a new "default" param to the "ini" module,
allowing to return a success (with the default value) even if the key
isn't found in the file.
Change-Id: Ibd1e54b2d2748b3bf8d2c1cabad3a9faf832e9b2
(cherry picked from commit f84aeec907
)
This commit is contained in:
parent
51c4f26181
commit
7595cf2126
|
@ -53,7 +53,7 @@ def check_file(path, ignore_missing):
|
|||
return ''
|
||||
|
||||
|
||||
def get_result(path, section, key):
|
||||
def get_result(path, section, key, default=None):
|
||||
'''Get value based on section and key'''
|
||||
|
||||
msg = ''
|
||||
|
@ -74,10 +74,16 @@ def get_result(path, section, key):
|
|||
ret = ReturnValue.OK
|
||||
return (ret, msg, value)
|
||||
except ConfigParser.Error:
|
||||
value = None
|
||||
msg = "There is no key '{}' under the section '{}' in file {}.".format(
|
||||
key, section, path)
|
||||
ret = ReturnValue.KEY_NOT_FOUND
|
||||
if default:
|
||||
msg = ("There is no key '{}' under section '{}' in file {}. Using"
|
||||
" default value '{}'".format(key, section, path, default))
|
||||
ret = ReturnValue.OK
|
||||
value = default
|
||||
else:
|
||||
value = None
|
||||
msg = "There is no key '{}' under the section '{}' in file {}.".format(
|
||||
key, section, path)
|
||||
ret = ReturnValue.KEY_NOT_FOUND
|
||||
return (ret, msg, value)
|
||||
|
||||
|
||||
|
@ -103,6 +109,10 @@ options:
|
|||
description:
|
||||
- Section key to look up
|
||||
type: str
|
||||
default:
|
||||
required: false
|
||||
description:
|
||||
- Default value if key isn't found
|
||||
ignore_missing_file:
|
||||
required: false
|
||||
description:
|
||||
|
@ -140,8 +150,9 @@ def main():
|
|||
# Try to parse the result from ini file
|
||||
section = module.params.get('section')
|
||||
key = module.params.get('key')
|
||||
default = module.params.get('default')
|
||||
|
||||
ret, msg, value = get_result(ini_file_path, section, key)
|
||||
ret, msg, value = get_result(ini_file_path, section, key, default)
|
||||
|
||||
if ret == ReturnValue.INVALID_FORMAT:
|
||||
module.fail_json(msg=msg)
|
||||
|
|
|
@ -11,6 +11,7 @@
|
|||
section: ctlplane-subnet
|
||||
key: dhcp_start
|
||||
ignore_missing_file: True
|
||||
default: "192.0.2.5"
|
||||
register: dhcp_start
|
||||
|
||||
- name: Get dhcp_end value from the undercloud.conf file
|
||||
|
@ -20,10 +21,11 @@
|
|||
section: ctlplane-subnet
|
||||
key: dhcp_end
|
||||
ignore_missing_file: True
|
||||
default: "192.0.2.24"
|
||||
register: dhcp_end
|
||||
|
||||
- name: Check the size of the DHCP range for overcloud nodes
|
||||
ip_range:
|
||||
start: "{{ dhcp_start.value|default('192.0.2.5', true) }}"
|
||||
end: "{{ dhcp_end.value|default('192.0.2.24', true) }}"
|
||||
start: "{{ dhcp_start.value }}"
|
||||
end: "{{ dhcp_end.value }}"
|
||||
min_size: "{{ ctlplane_iprange_min_size }}"
|
||||
|
|
|
@ -101,6 +101,23 @@ class TestIni(base.TestCase):
|
|||
"in file {}.").format(tmp_name), msg)
|
||||
self.assertIsNone(value)
|
||||
|
||||
def test_get_result_key_not_found_with_default(self):
|
||||
'''Test ini when key is not found but has a default'''
|
||||
|
||||
tmpfile = self.create_tmp_ini()
|
||||
tmp_name = os.path.relpath(tmpfile.name)
|
||||
tmpfile.write(valid_content.encode('utf-8'))
|
||||
tmpfile.seek(0)
|
||||
ret, msg, value = validation.get_result(tmp_name, 'section', 'key',
|
||||
'foo')
|
||||
tmpfile.close()
|
||||
|
||||
self.assertEqual(validation.ReturnValue.OK, ret)
|
||||
self.assertEqual(("There is no key 'key' under section 'section' "
|
||||
"in file {}. Using default value '{}'"
|
||||
).format(tmp_name, 'foo'), msg)
|
||||
self.assertEqual(value, 'foo')
|
||||
|
||||
def test_get_result_ok(self):
|
||||
'''Test ini when key is not found'''
|
||||
|
||||
|
|
Loading…
Reference in New Issue