Skip to content

Commit b74c7c6

Browse files
shinmogbtorresgil
authored andcommitted
Fixes #92 - fixes device.Vsys and reclassify network.VirtualWire as VsysOperations (#94)
1 parent 2574c48 commit b74c7c6

File tree

7 files changed

+127
-37
lines changed

7 files changed

+127
-37
lines changed

pandevice/base.py

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -314,13 +314,13 @@ def _parent_xpath(self):
314314
if self.parent is None:
315315
return ""
316316
else:
317-
return self.parent._build_xpath(self.ROOT)
317+
return self.parent._build_xpath(self.ROOT, None)
318318

319-
def _build_xpath(self, root):
319+
def _build_xpath(self, root, vsys):
320320
if self.parent is None:
321321
# self with no parent
322322
return ""
323-
parent_xpath = self.parent._build_xpath(root) + self.XPATH
323+
parent_xpath = self.parent._build_xpath(root, vsys) + self.XPATH
324324
if self.SUFFIX is not None:
325325
parent_xpath += self.SUFFIX % (self.uid, )
326326
return parent_xpath
@@ -334,9 +334,12 @@ def xpath_panorama(self):
334334
return self.parent.xpath_panorama()
335335

336336
def _root_xpath_vsys(self, vsys):
337-
root_xpath = "/config/devices/entry[@name='localhost.localdomain']/vsys"
338-
specific_vsys = "/entry[@name='{0}']".format(vsys) if vsys else ""
339-
return root_xpath + specific_vsys
337+
if vsys == 'shared':
338+
return '/config/shared'
339+
340+
xpath = "/config/devices/entry[@name='localhost.localdomain']"
341+
xpath += "/vsys/entry[@name='{0}']".format(vsys or 'vsys1')
342+
return xpath
340343

341344
def element(self, with_children=True, comparable=False):
342345
"""Construct an ElementTree for this PanObject and all its children
@@ -3273,14 +3276,14 @@ def set_config_changed(self, scope=None):
32733276
if scope not in self.config_changed:
32743277
self.config_changed.append(scope)
32753278

3276-
def _build_xpath(self, root):
3277-
return self.xpath_root(root)
3279+
def _build_xpath(self, root, vsys):
3280+
return self.xpath_root(root, vsys or self.vsys)
32783281

3279-
def xpath_root(self, root_type):
3282+
def xpath_root(self, root_type, vsys):
32803283
if root_type == Root.DEVICE:
32813284
xpath = self.xpath_device()
32823285
elif root_type == Root.VSYS:
3283-
xpath = self.xpath_vsys()
3286+
xpath = self._root_xpath_vsys(vsys)
32843287
elif root_type == Root.MGTCONFIG:
32853288
xpath = self.xpath_mgtconfig()
32863289
elif root_type == Root.PANORAMA:

pandevice/device.py

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -131,22 +131,12 @@ def _setup(self):
131131
self._params = tuple(params)
132132

133133
def xpath_vsys(self):
134-
if self.name == "shared":
135-
return "/config/shared"
136-
elif self.name is None:
137-
return self._root_xpath_vsys('vsys1')
138-
else:
139-
return self._root_xpath_vsys(self.name)
140-
141-
def _build_xpath(self, root):
142-
if root == Root.VSYS:
143-
return super(Vsys, self)._build_xpath(root)
144-
else:
145-
# If original object is not a VSYS Root, then bypass the vsys part of the xpath
146-
if self.parent is None:
147-
return ""
148-
else:
149-
return self.parent._build_xpath(root)
134+
return self._root_xpath_vsys(self.name)
135+
136+
def _build_xpath(self, root, vsys):
137+
if self.parent is None:
138+
return ''
139+
return self.parent._build_xpath(root, self.name)
150140

151141
@property
152142
def vsys(self):

pandevice/firewall.py

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -150,12 +150,7 @@ def vsys(self, value):
150150
self.ha_peer._vsys = value
151151

152152
def xpath_vsys(self):
153-
if self.vsys == "shared":
154-
return "/config/shared"
155-
elif self.vsys is None:
156-
return self._root_xpath_vsys('vsys1')
157-
else:
158-
return self._root_xpath_vsys(self.vsys)
153+
return self._root_xpath_vsys(self.vsys)
159154

160155
def xpath_panorama(self):
161156
raise err.PanDeviceError("Attempt to modify Panorama configuration on non-Panorama device")

pandevice/network.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -505,7 +505,7 @@ def _setup(self):
505505
self._params = tuple(params)
506506

507507

508-
class VirtualWire(VersionedPanObject):
508+
class VirtualWire(VsysOperations):
509509
"""Virtual wires (vwire)
510510
511511
Args:
@@ -524,6 +524,9 @@ def _setup(self):
524524
# xpaths
525525
self._xpaths.add_profile(value='/network/virtual-wire')
526526

527+
# xpath imports
528+
self._xpath_imports.add_profile(value='/network/virtual-wire')
529+
527530
# params
528531
params = []
529532

pandevice/panorama.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -87,13 +87,14 @@ def xpath_vsys(self):
8787
else:
8888
return "/config/devices/entry[@name='localhost.localdomain']/device-group/entry[@name='%s']" % self.name
8989

90-
def _build_xpath(self, root):
90+
def _build_xpath(self, root, vsys):
9191
if (self.name == "shared" or self.name is None) and root == Root.VSYS:
9292
# This is a Vsys object in shared Panorama scope, so proceed normally
93-
return super(DeviceGroup, self)._build_xpath(root)
93+
return super(DeviceGroup, self)._build_xpath(root,
94+
self.name or 'shared')
9495
else:
9596
# This is a member of the device-group, so override to use DeviceGroup root
96-
return super(DeviceGroup, self)._build_xpath(self.ROOT)
97+
return super(DeviceGroup, self)._build_xpath(self.ROOT, self.name)
9798

9899

99100
class Panorama(base.PanDevice):

tests/test_base.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -372,7 +372,7 @@ def test_parent_xpath_for_xpath_parent(self):
372372
ret_val = self.obj._parent_xpath()
373373

374374
self.assertEqual(Path, ret_val)
375-
self.obj.parent._build_xpath.assert_called_once_with(0)
375+
self.obj.parent._build_xpath.assert_called_once_with(0, None)
376376

377377
def test_xpath_vsys_without_parent(self):
378378
ret_val = self.obj.xpath_vsys()

tests/test_vsys_xpaths.py

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
import pytest
2+
3+
from pandevice import device
4+
from pandevice import firewall
5+
from pandevice import network
6+
from pandevice import objects
7+
from pandevice import panorama
8+
9+
10+
def _check(obj, vsys, with_pano, chk_import=False):
11+
if chk_import:
12+
func = 'xpath_import_base'
13+
else:
14+
func = 'xpath'
15+
fw = firewall.Firewall('127.0.0.1', 'admin', 'admin', serial='01234567890')
16+
fw.vsys = vsys
17+
fw.add(obj)
18+
19+
if with_pano:
20+
pano = panorama.Panorama('127.0.0.1', 'admin2', 'admin2')
21+
pano.add(fw)
22+
23+
expected = getattr(obj, func)()
24+
25+
fw.remove(obj)
26+
fw.vsys = None
27+
vsys = device.Vsys(vsys or 'vsys1')
28+
fw.add(vsys)
29+
vsys.add(obj)
30+
31+
result = getattr(obj, func)()
32+
33+
assert expected == result
34+
35+
36+
@pytest.mark.parametrize('vsys', [None, 'vsys1', 'vsys3'])
37+
@pytest.mark.parametrize('with_pano', [False, True])
38+
def test_xpath_for_vsys_root(vsys, with_pano):
39+
obj = network.Zone('myzone')
40+
_check(obj, vsys, with_pano)
41+
42+
43+
@pytest.mark.parametrize('vsys', [None, 'vsys1', 'vsys3'])
44+
@pytest.mark.parametrize('with_pano', [False, True])
45+
def test_xpath_for_device_root(vsys, with_pano):
46+
obj = device.SystemSettings(hostname='example')
47+
_check(obj, vsys, with_pano)
48+
49+
50+
@pytest.mark.parametrize('vsys', [None, 'vsys1', 'vsys3'])
51+
@pytest.mark.parametrize('with_pano', [False, True])
52+
def test_xpath_for_mgtconfig_root(vsys, with_pano):
53+
obj = device.Administrator('newadmin')
54+
_check(obj, vsys, with_pano)
55+
56+
57+
@pytest.mark.parametrize('vsys', [None, 'vsys1', 'vsys3'])
58+
@pytest.mark.parametrize('with_pano', [False, True])
59+
@pytest.mark.parametrize('obj', [
60+
network.EthernetInterface('ethernet1/3', 'layer3'),
61+
network.Layer3Subinterface('ethernet1/4.42', 42),
62+
network.Layer2Subinterface('ethernet1/4.420', 420),
63+
network.VirtualRouter('someroute'),
64+
network.VirtualWire('tripwire'),
65+
network.Vlan('myvlan'),
66+
])
67+
def test_xpath_import(vsys, with_pano, obj):
68+
_check(obj, vsys, with_pano, True)
69+
70+
71+
def test_vsys_xpath_unchanged():
72+
expected = "/config/devices/entry[@name='localhost.localdomain']/vsys/entry[@name='vsys3']"
73+
c = firewall.Firewall('127.0.0.1', 'admin', 'admin')
74+
c.vsys = 'vsys3'
75+
76+
assert expected == c.xpath_vsys()
77+
78+
c.vsys = None
79+
vsys = device.Vsys('vsys3')
80+
c.add(vsys)
81+
82+
assert expected == vsys.xpath_vsys()
83+
84+
zone = network.Zone('myzone')
85+
vsys.add(zone)
86+
87+
assert expected == zone.xpath_vsys()
88+
89+
90+
def test_device_group_xpath_unchanged():
91+
expected = "/config/devices/entry[@name='localhost.localdomain']/device-group/entry[@name='somegroup']/address/entry[@name='intnet']"
92+
pano = panorama.Panorama('127.0.0.1')
93+
dg = panorama.DeviceGroup('somegroup')
94+
ao = objects.AddressObject('intnet', '192.168.0.0/16')
95+
pano.add(dg)
96+
dg.add(ao)
97+
98+
assert expected == ao.xpath()

0 commit comments

Comments
 (0)