Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
I
inventory-provider
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Container Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
geant-swd
dashboardv3
inventory-provider
Commits
c03e7f0b
Commit
c03e7f0b
authored
6 years ago
by
Erik Reid
Browse files
Options
Downloads
Patches
Plain Diff
emulate old interfaces/<hostname> route
parent
603e3b6f
No related branches found
No related tags found
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
inventory_provider/pyez_test.py
+57
-0
57 additions, 0 deletions
inventory_provider/pyez_test.py
test/test_pyez_parsing.py
+36
-0
36 additions, 0 deletions
test/test_pyez_parsing.py
with
93 additions
and
0 deletions
inventory_provider/pyez_test.py
+
57
−
0
View file @
c03e7f0b
# import os
from
jnpr.junos
import
Device
# from inventory_provider import config
from
lxml
import
etree
def
load_config
(
hostname
,
ssh_params
):
dev
=
Device
(
host
=
hostname
,
user
=
"
Monit0r
"
,
ssh_private_key_file
=
ssh_params
[
'
private-key
'
])
dev
.
open
()
# data = dev.rpc.get_config(options={'format': 'json'})
return
dev
.
rpc
.
get_config
()
def
load_interfaces
(
hostname
,
ssh_params
):
data
=
load_config
(
hostname
,
ssh_params
)
# print(etree.tostring(data, encoding='unicode'))
def
_ifc_info
(
e
):
name
=
e
.
find
(
'
name
'
)
assert
name
is
not
None
,
"
expected interface
'
name
'
child element
"
description
=
e
.
find
(
'
description
'
)
return
{
'
name
'
:
name
.
text
,
'
description
'
:
description
.
text
if
description
is
not
None
else
None
}
# for i in data.xpath('//configuration/groups/interfaces/interface'):
for
i
in
data
.
xpath
(
'
//configuration/interfaces/interface
'
):
info
=
_ifc_info
(
i
)
yield
info
# ifc_name = i.xpath('./name/text()')
# assert ifc_name
# print(ifc_name[0])
for
u
in
i
.
xpath
(
'
./unit
'
):
unit_info
=
_ifc_info
(
u
)
unit_info
[
'
name
'
]
=
"
%s.%s
"
%
(
info
[
'
name
'
],
unit_info
[
'
name
'
])
yield
unit_info
# print("%s.%s" % (ifc_name[0], u))
#
# print("-----routing-instances-----")
# for i in data.xpath("//configuration/routing-instances/instance/interface"):
# print(i.xpath('./name/text()'))
# if __name__ == "__main__":
# with open("config.json") as f:
# params = config.load(f)
# for r in params['routers']:
# netconf = _load_netconf(r['hostname'], params['ssh'])
# with open(os.path.join(TEST_DATA_DIR, "%s-netconf.xml" % r['hostname']), 'w') as f:
# f.write(etree.tostring(netconf, encoding='unicode'))
This diff is collapsed.
Click to expand it.
test/test_pyez_parsing.py
0 → 100644
+
36
−
0
View file @
c03e7f0b
import
os
from
inventory_provider
import
pyez_test
from
lxml
import
etree
TEST_DATA_DIR
=
os
.
path
.
join
(
os
.
path
.
dirname
(
__file__
),
'
data
'
)
class
MockedJunosRpc
(
object
):
def
__init__
(
self
,
hostname
):
filename
=
os
.
path
.
join
(
TEST_DATA_DIR
,
"
%s-netconf.xml
"
%
hostname
)
self
.
config
=
etree
.
parse
(
filename
)
def
get_config
(
self
):
return
self
.
config
class
MockedJunosDevice
(
object
):
def
__init__
(
self
,
**
kwargs
):
self
.
rpc
=
MockedJunosRpc
(
kwargs
[
'
host
'
])
def
open
(
self
):
pass
def
test_nr1
(
mocker
,
data_config
):
mocker
.
patch
(
'
inventory_provider.pyez_test.Device
'
,
# 'inventory_provider.pyez_test.jnpr.junos.Device',
MockedJunosDevice
)
import
json
for
r
in
data_config
[
'
routers
'
]:
print
(
r
[
'
hostname
'
])
x
=
pyez_test
.
load_interfaces
(
r
[
'
hostname
'
],
data_config
[
'
ssh
'
])
print
(
json
.
dumps
(
list
(
x
),
indent
=
4
))
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment