From d6cb008d9bb5636af16439cbbe1d45a5924d82ba Mon Sep 17 00:00:00 2001 From: Ernst Oudhof <17832702+ernst-s@users.noreply.github.com> Date: Mon, 18 Nov 2024 12:44:24 +0100 Subject: [PATCH] Fix use_options in nios_network and nios_range --- plugins/modules/nios_network.py | 29 +++++++++++++---------------- plugins/modules/nios_range.py | 29 +++++++++++++---------------- 2 files changed, 26 insertions(+), 32 deletions(-) diff --git a/plugins/modules/nios_network.py b/plugins/modules/nios_network.py index 4ec26a85..a4b1114a 100644 --- a/plugins/modules/nios_network.py +++ b/plugins/modules/nios_network.py @@ -294,15 +294,27 @@ def options(module): vendor_class: } It will remove any options that are set to None since WAPI will error on - that condition. It will also verify that either `name` or `num` is + that condition. The use_option field only applies + to special options that are displayed separately from other options and + have a use flag. This function removes the use_option flag from all + other options. It will also verify that either `name` or `num` is set in the structure but does not validate the values are equal. The remainder of the value validation is performed by WAPI ''' + special_options = ['routers', 'router-templates', 'domain-name-servers', + 'domain-name', 'broadcast-address', 'broadcast-address-offset', + 'dhcp-lease-time', 'dhcp6.name-servers'] + # options-router-templates, broadcast-address-offset, dhcp6.name-servers don't have any associated number + special_num = [3, 6, 15, 28, 51] options = list() for item in module.params['options']: opt = dict([(k, v) for k, v in iteritems(item) if v is not None]) if 'name' not in opt and 'num' not in opt: module.fail_json(msg='one of `name` or `num` is required for option value') + if 'name' in opt and opt['name'] not in special_options: + del opt['use_option'] + if 'num' in opt and opt['num'] not in special_num: + del opt['use_option'] options.append(opt) return options @@ -329,19 +341,6 @@ def check_ip_addr_type(obj_filter, ib_spec): return NIOS_IPV6_NETWORK, ib_spec -def check_vendor_specific_dhcp_option(module, ib_spec): - '''This function will check if the argument dhcp option belongs to vendor-specific and if yes then will remove - use_options flag which is not supported with vendor-specific dhcp options. - ''' - for key, value in iteritems(ib_spec): - if isinstance(module.params[key], list): - for temp_dict in module.params[key]: - if 'num' in temp_dict: - if temp_dict['num'] in (43, 124, 125, 67, 60): - del temp_dict['use_option'] - return ib_spec - - def main(): ''' Main entry point for module execution ''' @@ -387,8 +386,6 @@ def main(): network_type, ib_spec = check_ip_addr_type(obj_filter, ib_spec) wapi = WapiModule(module) - # to check for vendor specific dhcp option - ib_spec = check_vendor_specific_dhcp_option(module, ib_spec) result = wapi.run(network_type, ib_spec) diff --git a/plugins/modules/nios_range.py b/plugins/modules/nios_range.py index 47cd49a9..605835f5 100644 --- a/plugins/modules/nios_range.py +++ b/plugins/modules/nios_range.py @@ -300,32 +300,31 @@ def options(module): vendor_class: } It will remove any options that are set to None since WAPI will error on - that condition. It will also verify that either `name` or `num` is + that condition. The use_option field only applies + to special options that are displayed separately from other options and + have a use flag. This function removes the use_option flag from all + other options. It will also verify that either `name` or `num` is set in the structure but does not validate the values are equal. The remainder of the value validation is performed by WAPI ''' + special_options = ['routers', 'router-templates', 'domain-name-servers', + 'domain-name', 'broadcast-address', 'broadcast-address-offset', + 'dhcp-lease-time', 'dhcp6.name-servers'] + # options-router-templates, broadcast-address-offset, dhcp6.name-servers don't have any associated number + special_num = [3, 6, 15, 28, 51] options = list() for item in module.params['options']: opt = dict([(k, v) for k, v in iteritems(item) if v is not None]) if 'name' not in opt and 'num' not in opt: module.fail_json(msg='one of `name` or `num` is required for option value') + if 'name' in opt and opt['name'] not in special_options: + del opt['use_option'] + if 'num' in opt and opt['num'] not in special_num: + del opt['use_option'] options.append(opt) return options -def check_vendor_specific_dhcp_option(module, ib_spec): - '''This function will check if the argument dhcp option belongs to vendor-specific and if yes then will remove - use_options flag which is not supported with vendor-specific dhcp options. - ''' - for key, value in iteritems(ib_spec): - if isinstance(module.params[key], list): - for temp_dict in module.params[key]: - if 'num' in temp_dict: - if temp_dict['num'] in (43, 124, 125, 67, 60): - del temp_dict['use_option'] - return ib_spec - - def convert_range_member_to_struct(module): '''This function will check the module input to ensure that only one member assignment type is specified at once. Member passed in is converted to the correct struct for the API to understand bassed on the member type. @@ -396,8 +395,6 @@ def main(): module = AnsibleModule(argument_spec=argument_spec, supports_check_mode=True) module = convert_range_member_to_struct(module) wapi = WapiModule(module) - # to check for vendor specific dhcp option - ib_spec = check_vendor_specific_dhcp_option(module, ib_spec) result = wapi.run(NIOS_RANGE, ib_spec)