Skip to content

Commit

Permalink
[pfsensible-generate-module] Support for default value, better list h…
Browse files Browse the repository at this point in the history
…andling
  • Loading branch information
opoplawski committed Jan 31, 2025
1 parent d1a5784 commit 44651a8
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 7 deletions.
3 changes: 2 additions & 1 deletion GENERATING_MODULES.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
5 changes: 4 additions & 1 deletion misc/pfsense_module.py.j2
Original file line number Diff line number Diff line change
Expand Up @@ -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 %}
Expand Down
20 changes: 15 additions & 5 deletions misc/pfsensible-generate-module
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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'
Expand All @@ -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'

Expand Down

0 comments on commit 44651a8

Please sign in to comment.