Skip to content

Commit 8b1d978

Browse files
committed
[chores] Moved shared VPN backend logic to BaseVpnBackend
1 parent c4fa97c commit 8b1d978

3 files changed

Lines changed: 46 additions & 56 deletions

File tree

netjsonconfig/backends/base/backend.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -333,3 +333,39 @@ def __restore_intermediate_data(self):
333333
del self.intermediate_data
334334
self.intermediate_data = self._intermediate_copy
335335
del self._intermediate_copy
336+
337+
338+
class BaseVpnBackend(BaseBackend):
339+
"""
340+
Shared logic between VPN backends
341+
Requires setting the following attributes:
342+
343+
- vpn_pattern
344+
- config_suffix
345+
"""
346+
347+
def _generate_contents(self, tar):
348+
"""
349+
Adds configuration files to tarfile instance.
350+
351+
:param tar: tarfile instance
352+
:returns: None
353+
"""
354+
text = self.render(files=False)
355+
# create a list with all the packages (and remove empty entries)
356+
vpn_instances = self.vpn_pattern.split(text)
357+
if '' in vpn_instances:
358+
vpn_instances.remove('')
359+
# create a file for each VPN
360+
for vpn in vpn_instances:
361+
lines = vpn.split('\n')
362+
vpn_name = lines[0]
363+
text_contents = '\n'.join(lines[2:])
364+
# do not end with double new line
365+
if text_contents.endswith('\n\n'):
366+
text_contents = text_contents[0:-1]
367+
self._add_file(
368+
tar=tar,
369+
name='{0}{1}'.format(vpn_name, self.config_suffix),
370+
contents=text_contents,
371+
)

netjsonconfig/backends/openvpn/openvpn.py

Lines changed: 5 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
from ...schema import X509_FILE_MODE
2-
from ..base.backend import BaseBackend
2+
from ..base.backend import BaseVpnBackend
33
from . import converters
44
from .parser import OpenVpnParser, config_suffix, vpn_pattern
55
from .renderer import OpenVpnRenderer
66
from .schema import schema
77

88

9-
class OpenVpn(BaseBackend):
9+
class OpenVpn(BaseVpnBackend):
1010
"""
1111
OpenVPN 2.x Configuration Backend
1212
"""
@@ -16,32 +16,9 @@ class OpenVpn(BaseBackend):
1616
parser = OpenVpnParser
1717
renderer = OpenVpnRenderer
1818
list_identifiers = ['name']
19-
20-
def _generate_contents(self, tar):
21-
"""
22-
Adds configuration files to tarfile instance.
23-
24-
:param tar: tarfile instance
25-
:returns: None
26-
"""
27-
text = self.render(files=False)
28-
# create a list with all the packages (and remove empty entries)
29-
vpn_instances = vpn_pattern.split(text)
30-
if '' in vpn_instances:
31-
vpn_instances.remove('')
32-
# create a file for each VPN
33-
for vpn in vpn_instances:
34-
lines = vpn.split('\n')
35-
vpn_name = lines[0]
36-
text_contents = '\n'.join(lines[2:])
37-
# do not end with double new line
38-
if text_contents.endswith('\n\n'):
39-
text_contents = text_contents[0:-1]
40-
self._add_file(
41-
tar=tar,
42-
name='{0}{1}'.format(vpn_name, config_suffix),
43-
contents=text_contents,
44-
)
19+
# BaseVpnBackend attributes
20+
vpn_pattern = vpn_pattern
21+
config_suffix = config_suffix
4522

4623
@classmethod
4724
def auto_client(
Lines changed: 5 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,14 @@
1-
from ..base.backend import BaseBackend
1+
from ..base.backend import BaseVpnBackend
22
from . import converters
33
from .parser import config_suffix, vpn_pattern
44
from .renderer import WireguardRenderer
55
from .schema import schema
66

77

8-
class Wireguard(BaseBackend):
8+
class Wireguard(BaseVpnBackend):
99
schema = schema
1010
converters = [converters.Wireguard]
1111
renderer = WireguardRenderer
12-
13-
def _generate_contents(self, tar):
14-
"""
15-
Adds configuration files to tarfile instance.
16-
17-
:param tar: tarfile instance
18-
:returns: None
19-
"""
20-
text = self.render(files=False)
21-
# create a list with all the packages (and remove empty entries)
22-
vpn_instances = vpn_pattern.split(text)
23-
if '' in vpn_instances:
24-
vpn_instances.remove('')
25-
# create a file for each VPN
26-
for vpn in vpn_instances:
27-
lines = vpn.split('\n')
28-
vpn_name = lines[0]
29-
text_contents = '\n'.join(lines[2:])
30-
# do not end with double new line
31-
if text_contents.endswith('\n\n'):
32-
text_contents = text_contents[0:-1]
33-
self._add_file(
34-
tar=tar,
35-
name='{0}{1}'.format(vpn_name, config_suffix),
36-
contents=text_contents,
37-
)
12+
# BaseVpnBackend attributes
13+
vpn_pattern = vpn_pattern
14+
config_suffix = config_suffix

0 commit comments

Comments
 (0)