Skip to content

Commit

Permalink
[changes] Rendered ZeroTier configuration in JSON format
Browse files Browse the repository at this point in the history
  • Loading branch information
Aryamanz29 committed Jun 5, 2023
1 parent 6270334 commit c78e68b
Show file tree
Hide file tree
Showing 5 changed files with 246 additions and 103 deletions.
21 changes: 0 additions & 21 deletions netjsonconfig/backends/zerotier/converters.py
Expand Up @@ -14,25 +14,4 @@ def to_intermediate_loop(self, block, result, index=None):
return result

def __intermediate_vpn(self, config, remove=[False, 0, '']):
# The Zerotier network configuration keys taken from:
# https://github.com/zerotier/ZeroTierOne/blob/dev/node/NetworkConfig.hpp
config['nwid'] = config.pop('id')
config['t'] = config.pop('objtype')
config['r'] = config.pop('revision')
config['ts'] = config.pop('creationTime')
config['n'] = config.pop('name')
config['p'] = config.pop('private')
config['eb'] = config.pop('enableBroadcast')
config['v4s'] = config.pop('v4AssignMode')
config['v6s'] = config.pop('v6AssignMode')
config['mtu'] = config.pop('mtu')
config['ml'] = config.pop('multicastLimit')
config['I'] = config.pop('ipAssignmentPools')
config['RT'] = config.pop('routes')
config['DNS'] = config.pop('dns')
config['R'] = config.pop('rules')
config['CAP'] = config.pop('capabilities')
config['TAG'] = config.pop('tags')
config['tt'] = config.pop('remoteTraceTarget')
config['tl'] = config.pop('remoteTraceLevel')
return self.sorted_dict(config)
4 changes: 2 additions & 2 deletions netjsonconfig/backends/zerotier/parser.py
Expand Up @@ -2,9 +2,9 @@

from ..base.parser import BaseParser

vpn_pattern = re.compile('^# zerotier config:\s', flags=re.MULTILINE)
vpn_pattern = re.compile('^// zerotier config:\s', flags=re.MULTILINE)
config_pattern = re.compile('^([^\s]*) ?(.*)$')
config_suffix = '.conf'
config_suffix = '.json'


class ZeroTierParser(BaseParser):
Expand Down
32 changes: 20 additions & 12 deletions netjsonconfig/backends/zerotier/schema.py
Expand Up @@ -19,29 +19,39 @@
"title": "ZeroTier",
"uniqueItems": True,
"additionalItems": True,
"propertyOrder": 12,
"items": {
"type": "object",
"title": "ZeroTier",
"title": "ZeroTier Network",
"additionalProperties": True,
"required": ["name"],
"properties": {
# Read-only properties
"name": {
"type": "string",
# Since it is intended to be set by
# the VPN backend's name field, it is read-only
"readOnly": True,
"example": "openwisp-wifi-network",
"description": "Name of the network",
},
"id": {
"type": "string",
"maxLength": 16,
"readOnly": True,
"example": "3e245e31af000001",
"description": "Network ID",
},
"nwid": {
"type": "string",
"maxLength": 16,
"readOnly": True,
"example": "3e245e31af000001",
"description": "Network ID legacy field (same as 'id')",
},
"objtype": {
"type": "string",
"readOnly": True,
"default": "network",
"example": "network",
},
"revision": {
Expand All @@ -54,19 +64,15 @@
"type": "number",
"readOnly": True,
"example": 1623101592,
"description": "Time the network was created",
"description": "Time when the network was created",
},
# Configurable properties
"name": {
"type": "string",
"example": "openwisp-wifi-network",
"description": "Name of the network",
},
"private": {
"type": "boolean",
"default": True,
"description": (
"Whether or not the network is private."
"If false, members will *NOT* need to be authorized to join."
"Whether or not the network is private "
"If false, members will NOT need to be authorized to join"
),
},
"enableBroadcast": {
Expand All @@ -76,8 +82,10 @@
"v4AssignMode": {
"type": "object",
"properties": {
"zt": {"type": "boolean"},
"description": "Whether ZeroTier should assign IPv4 addresses to members",
"zt": {
"type": "boolean",
"description": "Whether ZeroTier should assign IPv4 addresses to members",
},
},
},
"v6AssignMode": {
Expand Down
15 changes: 2 additions & 13 deletions netjsonconfig/backends/zerotier/templates/zerotier.jinja2
@@ -1,17 +1,6 @@
{% for vpn in data.zerotier %}
# zerotier config: {{ vpn.nwid }}
// zerotier config: {{ vpn.nwid }}

{% for key, value in vpn.items() %}
{% set key = key.replace('_', '-') %}
{% if key != 'name' %}
{% if value is string or value is number %}
{{ key }}={{ value }}
{% elif value is iterable %}
{% for element in value %}
{{ key }}={{ element }}
{% endfor %}
{% endif %}
{% endif %}
{% endfor %}
{{ vpn | tojson(indent=2)}}

{% endfor %}

0 comments on commit c78e68b

Please sign in to comment.