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
2b1da82a
Commit
2b1da82a
authored
4 years ago
by
Erik Reid
Browse files
Options
Downloads
Patches
Plain Diff
added basic response format & validation/unit test
parent
f1a80578
No related branches found
Branches containing commit
No related tags found
Tags containing commit
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
inventory_provider/routes/poller.py
+76
-0
76 additions, 0 deletions
inventory_provider/routes/poller.py
test/per_router/test_poller_routes.py
+15
-1
15 additions, 1 deletion
test/per_router/test_poller_routes.py
with
91 additions
and
1 deletion
inventory_provider/routes/poller.py
+
76
−
0
View file @
2b1da82a
...
...
@@ -59,6 +59,26 @@ INTERFACE_LIST_SCHEMA = {
'
items
'
:
{
'
$ref
'
:
'
#/definitions/interface
'
}
}
INTERFACE_SPEED_LIST_SCHEMA
=
{
'
$schema
'
:
'
http://json-schema.org/draft-07/schema#
'
,
'
definitions
'
:
{
'
interface
'
:
{
'
type
'
:
'
object
'
,
'
properties
'
:
{
'
router
'
:
{
'
type
'
:
'
string
'
},
'
name
'
:
{
'
type
'
:
'
string
'
},
'
speed
'
:
{
'
type
'
:
'
integer
'
}
},
'
required
'
:
[
'
router
'
,
'
name
'
,
'
speed
'
],
'
additionalProperties
'
:
False
},
},
'
type
'
:
'
array
'
,
'
items
'
:
{
'
$ref
'
:
'
#/definitions/interface
'
}
}
@routes.after_request
def
after_request
(
resp
):
...
...
@@ -228,3 +248,59 @@ def interfaces(hostname=None):
r
.
set
(
cache_key
,
result
.
encode
(
'
utf-8
'
))
return
Response
(
result
,
mimetype
=
"
application/json
"
)
@routes.route
(
"
/speeds
"
,
methods
=
[
'
GET
'
,
'
POST
'
])
@routes.route
(
'
/speeds/<hostname>
'
,
methods
=
[
'
GET
'
,
'
POST
'
])
@common.require_accepts_json
def
interface_speeds
(
hostname
=
None
):
"""
Handler for `/poller/speeds` and
`/poller/speeds/<hostname>`
which returns information for either all interfaces
or those on the requested hostname.
The response is a list of speed information for all
known interfaces.
.. asjson::
inventory_provider.routes.poller.INTERFACE_SPEED_LIST_SCHEMA
:param hostname: optional, if present should be a router hostname
:return:
"""
cache_key
=
f
'
classifier-cache:poller-interface-speeds:
{
hostname
}
'
\
if
hostname
else
'
classifier-cache:poller-interface-speeds:all
'
r
=
common
.
get_current_redis
()
result
=
r
.
get
(
cache_key
)
if
result
:
result
=
result
.
decode
(
'
utf-8
'
)
else
:
def
_speed
(
ifc
):
# TODO
return
-
1
def
_result_ifc
(
ifc
):
return
{
'
router
'
:
ifc
[
'
router
'
],
'
name
'
:
ifc
[
'
name
'
],
'
speed
'
:
_speed
(
ifc
)
}
result
=
list
(
map
(
_result_ifc
,
_load_interfaces
(
hostname
)))
if
not
result
:
return
Response
(
response
=
'
no interfaces found
'
,
status
=
404
,
mimetype
=
'
text/html
'
)
result
=
json
.
dumps
(
result
)
# cache this data for the next call
r
.
set
(
cache_key
,
result
.
encode
(
'
utf-8
'
))
return
Response
(
result
,
mimetype
=
"
application/json
"
)
This diff is collapsed.
Click to expand it.
test/per_router/test_poller_routes.py
+
15
−
1
View file @
2b1da82a
import
json
import
jsonschema
from
inventory_provider.routes.poller
import
INTERFACE_LIST_SCHEMA
from
inventory_provider.routes.poller
\
import
INTERFACE_LIST_SCHEMA
,
INTERFACE_SPEED_LIST_SCHEMA
DEFAULT_REQUEST_HEADERS
=
{
"
Content-type
"
:
"
application/json
"
,
...
...
@@ -19,3 +20,16 @@ def test_router_interfaces(router, client):
assert
response
# at least shouldn't be empty
response_routers
=
{
ifc
[
'
router
'
]
for
ifc
in
response
}
assert
response_routers
==
{
router
}
def
test_router_interface_speeds
(
router
,
client
):
rv
=
client
.
post
(
f
'
/poller/speeds/
{
router
}
'
,
headers
=
DEFAULT_REQUEST_HEADERS
)
assert
rv
.
status_code
==
200
response
=
json
.
loads
(
rv
.
data
.
decode
(
"
utf-8
"
))
jsonschema
.
validate
(
response
,
INTERFACE_SPEED_LIST_SCHEMA
)
assert
response
# at least shouldn't be empty
response_routers
=
{
ifc
[
'
router
'
]
for
ifc
in
response
}
assert
response_routers
==
{
router
}
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