From 44651a8503d595c47d817f1f35a2d2c1d7902e04 Mon Sep 17 00:00:00 2001 From: Orion Poplawski Date: Fri, 31 Jan 2025 06:53:07 -0700 Subject: [PATCH] [pfsensible-generate-module] Support for default value, better list handling --- GENERATING_MODULES.md | 3 ++- misc/pfsense_module.py.j2 | 5 ++++- misc/pfsensible-generate-module | 20 +++++++++++++++----- 3 files changed, 21 insertions(+), 7 deletions(-) diff --git a/GENERATING_MODULES.md b/GENERATING_MODULES.md index 80cfc1f..a76230a 100644 --- a/GENERATING_MODULES.md +++ b/GENERATING_MODULES.md @@ -24,7 +24,8 @@ if you needed to use different names for the items than `item_min` and `item_ful ## Modules that configure something -If this is a module that will just configure something, add the `--is-config` option: +If this is a module that will just configure something, it is best to start with the default configuration. Then add the +--is-config` option: misc/pfsensible-generate-module --url URL --is-config diff --git a/misc/pfsense_module.py.j2 b/misc/pfsense_module.py.j2 index 4efab73..f2894f8 100644 --- a/misc/pfsense_module.py.j2 +++ b/misc/pfsense_module.py.j2 @@ -33,12 +33,15 @@ options: {% for name, param in params.items() %} {{ name }}: description: {{ "'" if ':' in param['description'] else '' }}{{ param['description'] | default('') }}{{ "'" if ':' in param['description'] else '' }} +{% if 'default' in param %} + default: {{ param['default'] }} +{% endif %} {% if 'choices' in param %} choices: {{ param['choices'] }} {% endif %} type: {{ param['type'] | default('') }} {% if param['type'] == 'list' %} - elements: 'str' + elements: {{ param['elements'] | default('str') }} {% endif %} {% endfor %} diff --git a/misc/pfsensible-generate-module b/misc/pfsensible-generate-module index 0f83ea6..580ee87 100755 --- a/misc/pfsensible-generate-module +++ b/misc/pfsensible-generate-module @@ -266,11 +266,13 @@ for input in html.forms[0].inputs: param['example'] = 'true' elif input.type == 'number': param['type'] = 'int' + param['default'] = input.value elif input.type == 'password': param['type'] = 'str' param['password'] = True # TODO - set nolog elif input.type == 'text': param['type'] = 'str' + param['default'] = input.value # TOOD - handle placeholder as 'default' value - description? create_default? example? for attr in ['min', 'placeholder', 'step']: if attr in input.attrib: @@ -280,10 +282,17 @@ for input in html.forms[0].inputs: param['description'] = enforce_period(input.tail.strip()) elif isinstance(input, lxml.html.SelectElement): if args.verbose >= 2: - print(f'select name={input.name} value={input.value} value_options={input.value_options} multiple={input.multiple}') + print(f'Found select element: name={input.name} value={input.value} value_options={input.value_options} multiple={input.multiple} attrib={input.attrib}') + + # Strip any trailing [] + input.name = re.sub(r'\[]$', '', input.name) + if input.attrib.get('class') == 'form-control' and input.attrib.get('data-toggle') == 'collapse': args.type_param = input.name + if input.value is not None: + param['default'] = input.value + if input.value_options is not None: if input.name == 'interface': param['type'] = 'str' @@ -293,16 +302,17 @@ for input in html.forms[0].inputs: if 'p2o_interface_without_virtual' not in args_imports: args_imports.append('p2o_interface_without_virtual') else: - param['choices'] = input.value_options if input.multiple: param['type'] = 'list' + param['default'] = [] + for selected in input.value: + print(f'selected = {selected}') + param['default'].append(selected) else: param['type'] = 'str' + param['choices'] = input.value_options param['multiple'] = input.multiple - if input.value is not None: - param['default'] = input.value - elif input.tag == 'textarea': param['type'] = 'str'