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
57cffe85
Commit
57cffe85
authored
6 years ago
by
Erik Reid
Browse files
Options
Downloads
Patches
Plain Diff
skeleton for poller api
parent
2dd88475
No related branches found
Branches containing commit
No related tags found
Tags containing commit
No related merge requests found
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
inventory_provider/__init__.py
+3
-0
3 additions, 0 deletions
inventory_provider/__init__.py
inventory_provider/routes/common.py
+34
-0
34 additions, 0 deletions
inventory_provider/routes/common.py
inventory_provider/routes/poller.py
+74
-0
74 additions, 0 deletions
inventory_provider/routes/poller.py
with
111 additions
and
0 deletions
inventory_provider/__init__.py
+
3
−
0
View file @
57cffe85
...
...
@@ -29,6 +29,9 @@ def create_app():
from
inventory_provider.routes
import
classifier
app
.
register_blueprint
(
classifier
.
routes
,
url_prefix
=
'
/classifier
'
)
from
inventory_provider.routes
import
poller
app
.
register_blueprint
(
poller
.
routes
,
url_prefix
=
'
/poller
'
)
if
"
SETTINGS_FILENAME
"
not
in
os
.
environ
:
assert
False
,
\
"
environment variable SETTINGS_FILENAME
'
must be defined
"
...
...
This diff is collapsed.
Click to expand it.
inventory_provider/routes/common.py
0 → 100644
+
34
−
0
View file @
57cffe85
import
functools
from
flask
import
request
,
Response
,
current_app
import
redis
def
require_accepts_json
(
f
):
"""
used as a route handler decorator to return an error
unless the request allows responses with type
"
application/json
"
:param f: the function to be decorated
:return: the decorated function
"""
@functools.wraps
(
f
)
def
decorated_function
(
*
args
,
**
kwargs
):
# TODO: use best_match to disallow */* ...?
if
not
request
.
accept_mimetypes
.
accept_json
:
return
Response
(
response
=
"
response will be json
"
,
status
=
406
,
mimetype
=
"
text/html
"
)
return
f
(
*
args
,
**
kwargs
)
return
decorated_function
def
redis_connection
():
"""
just a common place for acquiring a redis connection
:return: a redis connection
"""
redis_config
=
current_app
.
config
[
"
INVENTORY_PROVIDER_CONFIG
"
][
"
redis
"
]
return
redis
.
StrictRedis
(
host
=
redis_config
[
"
hostname
"
],
port
=
redis_config
[
"
port
"
])
This diff is collapsed.
Click to expand it.
inventory_provider/routes/poller.py
0 → 100644
+
74
−
0
View file @
57cffe85
import
json
from
flask
import
Blueprint
,
Response
,
jsonify
from
lxml
import
etree
from
inventory_provider
import
juniper
from
inventory_provider.routes
import
common
routes
=
Blueprint
(
'
poller-support-routes
'
,
__name__
)
@routes.route
(
'
/interfaces/<hostname>
'
,
methods
=
[
'
GET
'
,
'
POST
'
])
@common.require_accepts_json
def
poller_interface_oids
(
hostname
):
r
=
common
.
redis_connection
()
netconf_string
=
r
.
hget
(
hostname
,
'
netconf
'
)
if
not
netconf_string
:
return
Response
(
response
=
'
no netconf available info for %r
'
%
hostname
,
status
=
404
,
mimetype
=
'
text/html
'
)
snmp_data_string
=
r
.
hget
(
hostname
,
'
snmp-interfaces
'
)
if
not
snmp_data_string
:
return
Response
(
response
=
'
no snmp available info for
'
%
r
%
hostname
,
status
=
404
,
mimetype
=
'
text/html
'
)
def
_ifc_name
(
ifc
):
if
'
v4InterfaceName
'
in
ifc
:
return
ifc
[
'
v4InterfaceName
'
]
if
'
v6InterfaceName
'
in
ifc
:
return
ifc
[
'
v6InterfaceName
'
]
assert
False
,
'
sanity failure: no interface name found
'
snmp_indexes
=
dict
([
(
_ifc_name
(
ifc
),
int
(
ifc
[
'
index
'
]))
for
ifc
in
json
.
loads
(
snmp_data_string
.
decode
(
'
utf-8
'
))
])
interfaces
=
list
(
juniper
.
list_interfaces
(
etree
.
XML
(
netconf_string
.
decode
(
'
utf-8
'
))))
if
not
interfaces
:
return
Response
(
response
=
'
no interfaces found for %r
'
%
hostname
,
status
=
404
,
mimetype
=
'
text/html
'
)
result
=
[]
for
ifc
in
interfaces
:
if
not
ifc
[
'
description
'
]:
continue
ifc_data
=
{
'
name
'
:
ifc
[
'
name
'
],
'
snmp-index
'
:
snmp_indexes
.
get
(
ifc
[
'
name
'
],
None
),
'
description
'
:
ifc
[
'
description
'
],
'
circuits
'
:
[]
}
circuits
=
r
.
hget
(
'
interface_services
'
,
'
%s::%s
'
%
(
hostname
,
ifc
[
'
name
'
]))
if
circuits
:
ifc_data
[
'
circuits
'
]
=
[
{
'
type
'
:
c
[
'
circuit_type
'
],
'
id
'
:
c
[
'
id
'
]}
for
c
in
json
.
loads
(
circuits
.
decode
(
'
utf-8
'
))
]
result
.
append
(
ifc_data
)
return
jsonify
(
result
)
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