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
ceea06a5
Commit
ceea06a5
authored
6 years ago
by
Erik Reid
Browse files
Options
Downloads
Patches
Plain Diff
removed some duplicated code
use common.require_accepts_json instead of copying the method
parent
dd51118f
No related branches found
No related tags found
No related merge requests found
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
inventory_provider/routes/classifier.py
+6
-26
6 additions, 26 deletions
inventory_provider/routes/classifier.py
inventory_provider/routes/data.py
+9
-28
9 additions, 28 deletions
inventory_provider/routes/data.py
inventory_provider/routes/opsdb.py
+1
-21
1 addition, 21 deletions
inventory_provider/routes/opsdb.py
with
16 additions
and
75 deletions
inventory_provider/routes/classifier.py
+
6
−
26
View file @
ceea06a5
import
functools
from
flask
import
Blueprint
,
request
,
Response
,
current_app
,
jsonify
import
json
from
flask
import
Blueprint
,
Response
,
current_app
,
jsonify
import
jsonschema
from
inventory_provider.routes
import
common
...
...
@@ -9,27 +8,8 @@ from inventory_provider.routes import common
routes
=
Blueprint
(
"
inventory-data-classifier-support-routes
"
,
__name__
)
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
@routes.route
(
"
/infinera-dna-addresses
"
,
methods
=
[
'
GET
'
,
'
POST
'
])
@require_accepts_json
@
common.
require_accepts_json
def
infinera_addresses
():
infinera_config
=
current_app
.
config
[
"
INVENTORY_PROVIDER_CONFIG
"
][
"
infinera-dna
"
]
...
...
@@ -37,7 +17,7 @@ def infinera_addresses():
@routes.route
(
"
/coriant-tnms-addresses
"
,
methods
=
[
'
GET
'
,
'
POST
'
])
@require_accepts_json
@
common.
require_accepts_json
def
coriant_addresses
():
coriant_config
=
current_app
.
config
[
"
INVENTORY_PROVIDER_CONFIG
"
][
"
coriant-tnms
"
]
...
...
@@ -45,7 +25,7 @@ def coriant_addresses():
@routes.route
(
"
/juniper-server-addresses
"
,
methods
=
[
'
GET
'
,
'
POST
'
])
@require_accepts_json
@
common.
require_accepts_json
def
juniper_addresses
():
backend_data_schema
=
{
"
$schema
"
:
"
http://json-schema.org/draft-07/schema#
"
,
...
...
@@ -73,7 +53,7 @@ def juniper_addresses():
@routes.route
(
"
/trap-metadata/<trap_type>/<source_equipment>/<path:interface>
"
,
methods
=
[
'
GET
'
,
'
POST
'
])
@require_accepts_json
@
common.
require_accepts_json
def
get_trap_metadata
(
trap_type
,
source_equipment
,
interface
):
# todo - Move this to config
interface_info_key
=
"
interface_services
"
...
...
This diff is collapsed.
Click to expand it.
inventory_provider/routes/data.py
+
9
−
28
View file @
ceea06a5
import
functools
import
json
import
pkg_resources
from
flask
import
Blueprint
,
jsonify
,
request
,
Response
,
current_app
from
flask
import
Blueprint
,
jsonify
,
Response
,
current_app
from
lxml
import
etree
import
redis
...
...
@@ -14,27 +13,8 @@ routes = Blueprint("inventory-data-query-routes", __name__)
API_VERSION
=
'
0.1
'
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
@routes.route
(
"
/version
"
,
methods
=
[
'
GET
'
,
'
POST
'
])
@require_accepts_json
@
common.
require_accepts_json
def
version
():
return
Response
(
json
.
dumps
({
...
...
@@ -47,7 +27,7 @@ def version():
@routes.route
(
"
/routers
"
,
methods
=
[
'
GET
'
,
'
POST
'
])
@require_accepts_json
@
common.
require_accepts_json
def
routers
():
redis_config
=
current_app
.
config
[
"
INVENTORY_PROVIDER_CONFIG
"
][
"
redis
"
]
r
=
redis
.
StrictRedis
(
...
...
@@ -59,7 +39,7 @@ def routers():
@routes.route
(
"
/interfaces/<hostname>
"
,
methods
=
[
'
GET
'
,
'
POST
'
])
@require_accepts_json
@
common.
require_accepts_json
def
router_interfaces
(
hostname
):
redis_config
=
current_app
.
config
[
"
INVENTORY_PROVIDER_CONFIG
"
][
"
redis
"
]
r
=
redis
.
StrictRedis
(
...
...
@@ -87,7 +67,7 @@ def router_interfaces(hostname):
@routes.route
(
"
/snmp/<hostname>
"
,
methods
=
[
'
GET
'
,
'
POST
'
])
@require_accepts_json
@
common.
require_accepts_json
def
snmp_ids
(
hostname
):
redis_config
=
current_app
.
config
[
"
INVENTORY_PROVIDER_CONFIG
"
][
"
redis
"
]
r
=
redis
.
StrictRedis
(
...
...
@@ -117,7 +97,7 @@ def snmp_ids(hostname):
@routes.route
(
"
/debug-dump/<hostname>
"
,
methods
=
[
'
GET
'
,
'
POST
'
])
@require_accepts_json
@
common.
require_accepts_json
def
debug_dump_router_info
(
hostname
):
redis_config
=
current_app
.
config
[
"
INVENTORY_PROVIDER_CONFIG
"
][
"
redis
"
]
r
=
redis
.
StrictRedis
(
...
...
@@ -138,7 +118,7 @@ def debug_dump_router_info(hostname):
@routes.route
(
"
/bgp/<hostname>
"
,
methods
=
[
'
GET
'
,
'
POST
'
])
@require_accepts_json
@
common.
require_accepts_json
def
bgp_configs
(
hostname
):
redis_config
=
current_app
.
config
[
"
INVENTORY_PROVIDER_CONFIG
"
][
"
redis
"
]
r
=
redis
.
StrictRedis
(
...
...
@@ -167,7 +147,7 @@ def bgp_configs(hostname):
@routes.route
(
"
/interfaces/status/<hostname>/<path:interface>
"
,
methods
=
[
'
GET
'
,
'
POST
'
])
@require_accepts_json
@
common.
require_accepts_json
def
interface_statuses
(
hostname
,
interface
):
r
=
common
.
get_redis
()
result
=
r
.
hget
(
"
interface_statuses
"
,
...
...
@@ -182,6 +162,7 @@ def interface_statuses(hostname, interface):
@routes.route
(
"
/services/<hostname>/<path:interface>
"
,
methods
=
[
'
GET
'
,
'
POST
'
])
@common.require_accepts_json
def
services_for_interface
(
hostname
,
interface
):
r
=
common
.
get_redis
()
result
=
r
.
hget
(
"
interface_services
"
,
...
...
This diff is collapsed.
Click to expand it.
inventory_provider/routes/opsdb.py
+
1
−
21
View file @
ceea06a5
import
functools
import
json
from
flask
import
Blueprint
,
request
,
Response
from
flask
import
Blueprint
,
Response
from
inventory_provider.routes
import
common
routes
=
Blueprint
(
"
inventory-opsdb-query-routes
"
,
__name__
)
...
...
@@ -13,25 +12,6 @@ service_parent_to_children_key = "parent_to_children_circuit_relations"
interface_status_key
=
"
interface_statuses
"
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
_decode_utf8_dict
(
d
):
return
{
k
.
decode
(
'
utf8
'
):
json
.
loads
(
v
)
for
k
,
v
in
d
.
items
()}
...
...
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