Skip to content

Commit

Permalink
Allow setting client class test expression for option.
Browse files Browse the repository at this point in the history
  • Loading branch information
reitermarkus committed Apr 7, 2024
1 parent 4c56f7d commit 1b5ea2f
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 52 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,13 @@
<label>Name</label>
<type>text</type>
</field>
<field>
<id>option.test</id>
<label>Client Class Test</label>
<type>text</type>
<advanced>true</advanced>
<help><![CDATA[Only send this option if the client matches the given <a rel="help" href="https://kea.readthedocs.io/en/kea-2.2.0/arm/classify.html?highlight=test%20expressions#using-expressions-in-classification">test expression</a>.]]></help>
</field>
<field>
<id>option.code</id>
<label>Code</label>
Expand Down Expand Up @@ -31,6 +38,7 @@
<label>Record Types</label>
<type>text</type>
<help><![CDATA[Comma-separated list of <a rel="help" href="https://kea.readthedocs.io/en/latest/arm/dhcp4-srv.html#dhcp-types">DHCP option types</a>. Only applies if <b>Type</b> is <code>record</code>.]]></help>
<style>record_types</style>
</field>
<field>
<id>option.data</id>
Expand Down
34 changes: 6 additions & 28 deletions src/opnsense/mvc/app/models/OPNsense/Kea/KeaDhcpv4.xml
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@
</check001>
</Constraints>
</name>
<test type="TextField">
<Default></Default>
</test>
<code type="IntegerField">
<Required>Y</Required>
</code>
Expand Down Expand Up @@ -86,39 +89,14 @@
<check>record</check>
</check001>
</Constraints>
<AsList>Y</AsList>
<FieldSeparator>,</FieldSeparator>
</record_types>
<data type="TextField">
<data type="TextField">
<Required>Y</Required>
</data>
</option>
</options>
<client_classes>
<client_class type="ArrayField">
<name type="TextField">
<Required>Y</Required>
<Constraints>
<check001>
<ValidationMessage>Duplicate entry exists</ValidationMessage>
<type>UniqueConstraint</type>
</check001>
</Constraints>
</name>
<test type="TextField"></test>
<option type="ModelRelationField">
<Multiple>Y</Multiple>
<Model>
<subnets>
<source>OPNsense.Kea.KeaDhcpv4</source>
<items>options.option</items>
<display>name</display>
</subnets>
</Model>
<ValidationMessage>Related option definition not found</ValidationMessage>
<Required>Y</Required>
<AsList>1</AsList>
</option>
</client_class>
</client_classes>
<subnets>
<subnet4 type="ArrayField">
<subnet type="NetworkField">
Expand Down
62 changes: 38 additions & 24 deletions src/opnsense/service/templates/OPNsense/Kea/kea-dhcp4.conf
Original file line number Diff line number Diff line change
Expand Up @@ -49,41 +49,55 @@
}{% if not loop.last %},{% endif +%}
{% endfor %}
],
{% for option in options %}
{% if loop.first %}
"client-classes": [
{%- endif +%}
{
"name": {{option.name|tojson}},
{%- if option.test +%}
"test": {{option.test|tojson}},
{%- else +%}
"test": "0 == 0",
{%- endif +%}
"only-if-required": true,
"option-data": [
{%- if option.space == 'vendor_encapsulated_options_space' +%}
{
"name": "vendor-encapsulated-options",
"always-send": true
},
{%- endif +%}
{
"name": {{option.name|tojson}},
"space": {{option.space.replace('_','-')|tojson}},
"code": {{option.code|int|tojson}},
"data": {{option.data|tojson}}
}
]
}{% if not loop.last %},{% endif +%}
{% if loop.last %}
],
{% endif %}
{% endfor %}
"subnet4": [
{% for subnet in helpers.toList('OPNsense.Kea.dhcp4.subnets.subnet4') %}
{%- for subnet in helpers.toList('OPNsense.Kea.dhcp4.subnets.subnet4') %}
{% set additional_options = (subnet.additional_options|default('')).split(',') +%}
{
"id": {{loop.index}},
"subnet": "{{subnet.subnet}}",
"require-client-classes": [
{%- for option in options if option['@uuid'] in additional_options +%}
{{option.name|tojson}}{% if not loop.last %},{% endif %}
{%- endfor +%}
],
"option-data": [

{% set ns = namespace(options_printed=false) %}
{% for od_attr in (subnet.option_data|list + option_data_defaults|list)|unique if subnet.option_data[od_attr]|length > 1 or od_attr in option_data_defaults %}
{% set ns.options_printed = True %}
{
"name": {{od_attr.replace('_','-')|tojson}},
"data": {{subnet.option_data[od_attr]|default(option_data_defaults[od_attr])|tojson}}
}{% if not loop.last %},{% endif +%}
{%- endfor %}

{%- set additional_options = (subnet.additional_options|default('')).split(',') -%}

{%- for option in helpers.toList('OPNsense.Kea.dhcp4.options.option') if option['@uuid'] in additional_options %}
{%- set print_leading_comma = loop.first and ns.options_printed -%}
{%- if print_leading_comma %},{% endif %}

{% if option.space == 'vendor_encapsulated_options_space' %}
{
"name": "vendor-encapsulated-options",
"always-send": true
},
{% endif %}
{
"name": {{option.name|tojson}},
"space": {{option.space.replace('_','-')|tojson}},
"code": {{option.code|int|tojson}},
"data": {{option.data|tojson}}
}{%- if not loop.last %},{% endif +%}
{% endfor %}
],
"pools": [
{% for pool in (subnet.pools|default('')).split("\n") if pool|length > 1%}
Expand Down

0 comments on commit 1b5ea2f

Please sign in to comment.