Sync 'validations/library/ini.py' with the validations framework

Ibd1e54b2d2748b3bf8d2c1cabad3a9faf832e9b2 introduced a new "default"
parameter to the "ini" module allowing to return a success (with a
default value) even if the key isn't found in the file.

This patch also synchronizes the ctlplane-ip-range validation in the
validations directory.

Change-Id: I5e3fd6286c8f65586416b75472678bacfa5a4cd6
Signed-off-by: Gael Chamoulaud <gchamoul@redhat.com>
This commit is contained in:
Gael Chamoulaud 2019-11-04 11:03:39 +01:00 committed by Cédric Jeanneret
parent 730ef1ff02
commit 3a2305fcc2
2 changed files with 21 additions and 8 deletions

View File

@ -24,6 +24,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
@ -33,10 +34,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 }}"

View File

@ -52,7 +52,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 = ''
@ -73,10 +73,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)
@ -102,6 +108,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:
@ -142,8 +152,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)