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
a0d581cc
Commit
a0d581cc
authored
4 years ago
by
Erik Reid
Browse files
Options
Downloads
Patches
Plain Diff
added bgp group name list apis
parent
a48b7cd5
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/routes/msr.py
+84
-2
84 additions, 2 deletions
inventory_provider/routes/msr.py
test/test_msr_routes.py
+12
-2
12 additions, 2 deletions
test/test_msr_routes.py
with
96 additions
and
4 deletions
inventory_provider/routes/msr.py
+
84
−
2
View file @
a0d581cc
...
@@ -38,6 +38,12 @@ ACCESS_SERVICES_LIST_SCHEMA = {
...
@@ -38,6 +38,12 @@ ACCESS_SERVICES_LIST_SCHEMA = {
"
items
"
:
{
"
$ref
"
:
"
#/definitions/service
"
}
"
items
"
:
{
"
$ref
"
:
"
#/definitions/service
"
}
}
}
PEERING_GROUP_LIST_SCHEMA
=
{
"
$schema
"
:
"
http://json-schema.org/draft-07/schema#
"
,
"
type
"
:
"
array
"
,
"
items
"
:
{
"
type
"
:
"
string
"
}
}
PEERING_LIST_SCHEMA
=
{
PEERING_LIST_SCHEMA
=
{
"
$schema
"
:
"
http://json-schema.org/draft-07/schema#
"
,
"
$schema
"
:
"
http://json-schema.org/draft-07/schema#
"
,
...
@@ -140,7 +146,7 @@ def _handle_peering_group_request(name, cache_key, group_key_base):
...
@@ -140,7 +146,7 @@ def _handle_peering_group_request(name, cache_key, group_key_base):
r
=
common
.
get_current_redis
()
r
=
common
.
get_current_redis
()
def
_get_all_
ls_
keys
():
def
_get_all_
sub
keys
():
keys
=
[]
keys
=
[]
for
k
in
r
.
scan_iter
(
f
'
{
group_key_base
}
:*
'
,
count
=
1000
):
for
k
in
r
.
scan_iter
(
f
'
{
group_key_base
}
:*
'
,
count
=
1000
):
keys
.
append
(
k
.
decode
(
'
utf-8
'
))
keys
.
append
(
k
.
decode
(
'
utf-8
'
))
...
@@ -163,7 +169,7 @@ def _handle_peering_group_request(name, cache_key, group_key_base):
...
@@ -163,7 +169,7 @@ def _handle_peering_group_request(name, cache_key, group_key_base):
if
name
:
if
name
:
items
=
_load_list_items
(
f
'
{
group_key_base
}
:
{
name
}
'
)
items
=
_load_list_items
(
f
'
{
group_key_base
}
:
{
name
}
'
)
else
:
else
:
gen_list
=
list
(
map
(
_load_list_items
,
_get_all_
ls_
keys
()))
gen_list
=
list
(
map
(
_load_list_items
,
_get_all_
sub
keys
()))
items
=
itertools
.
chain
(
*
gen_list
)
items
=
itertools
.
chain
(
*
gen_list
)
items
=
list
(
items
)
items
=
list
(
items
)
...
@@ -214,3 +220,79 @@ def bgp_group_peerings(name=None):
...
@@ -214,3 +220,79 @@ def bgp_group_peerings(name=None):
name
=
name
,
name
=
name
,
cache_key
=
'
classifier-cache:msr:group-peerings
'
,
cache_key
=
'
classifier-cache:msr:group-peerings
'
,
group_key_base
=
'
juniper-peerings:group
'
)
group_key_base
=
'
juniper-peerings:group
'
)
def
_handle_peering_group_list_request
(
cache_key
,
group_key_base
):
"""
Common method for used by
:meth:`inventory_provider.routes.msr.get_logical_systems` and
:meth:`inventory_provider.routes.msr.get_peering_groups`.
This method will return a list of all immediate subkeys of
`group_key_base`.
The response will be formatted according to the following schema:
.. asjson::
inventory_provider.routes.msr.PEERING_GROUP_LIST_SCHEMA
:param cache_key: base cache key for this type of request
:param group_key_base: key above which the peerings are grouped
:return: a json list, formatted as above
"""
r
=
common
.
get_current_redis
()
def
_get_all_subkeys
():
for
k
in
r
.
scan_iter
(
f
'
{
group_key_base
}
:*
'
,
count
=
1000
):
k
=
k
.
decode
(
'
utf-8
'
)
yield
k
[
len
(
group_key_base
)
+
1
:]
names
=
r
.
get
(
cache_key
)
if
names
:
names
=
json
.
loads
(
names
.
decode
(
'
utf-8
'
))
else
:
names
=
list
(
_get_all_subkeys
())
if
not
names
:
return
Response
(
response
=
'
no groups found
'
,
status
=
404
,
mimetype
=
"
text/html
"
)
names
=
sorted
(
names
)
r
.
set
(
cache_key
,
json
.
dumps
(
names
).
encode
(
'
utf-8
'
))
return
jsonify
(
names
)
@routes.route
(
"
/bgp/logical-systems
"
,
methods
=
[
'
GET
'
,
'
POST
'
])
@common.require_accepts_json
def
get_logical_systems
():
"""
Handler for `/msr/bgp/logical-systems`
Returns a list of logical system names for which peering
information is available.
:return: see :meth:`inventory_provider.routes.msr._handle_peering_group_list_request`
"""
return
_handle_peering_group_list_request
(
cache_key
=
'
classifier-cache:msr:logical-systems
'
,
group_key_base
=
'
juniper-peerings:logical-system
'
)
@routes.route
(
"
/bgp/groups
"
,
methods
=
[
'
GET
'
,
'
POST
'
])
@common.require_accepts_json
def
get_peering_groups
():
"""
Handler for `/msr/bgp/groups`
Returns a list of group names for which peering
information is available.
:return: see :meth:`inventory_provider.routes.msr._handle_peering_group_list_request`
"""
return
_handle_peering_group_list_request
(
cache_key
=
'
classifier-cache:msr:peering-groups
'
,
group_key_base
=
'
juniper-peerings:group
'
)
This diff is collapsed.
Click to expand it.
test/test_msr_routes.py
+
12
−
2
View file @
a0d581cc
...
@@ -3,8 +3,8 @@ import jsonschema
...
@@ -3,8 +3,8 @@ import jsonschema
import
pytest
import
pytest
from
inventory_provider.routes.msr
\
from
inventory_provider.routes.msr
import
ACCESS_SERVICES_LIST_SCHEMA
,
\
import
ACCESS_SERVICES
_LIST_SCHEMA
,
PEERING_LIST_SCHEMA
PEERING
_LIST_SCHEMA
,
PEERING_
GROUP_
LIST_SCHEMA
DEFAULT_REQUEST_HEADERS
=
{
DEFAULT_REQUEST_HEADERS
=
{
"
Content-type
"
:
"
application/json
"
,
"
Content-type
"
:
"
application/json
"
,
...
@@ -99,4 +99,14 @@ def test_logical_system_peerings_404(client, name):
...
@@ -99,4 +99,14 @@ def test_logical_system_peerings_404(client, name):
assert
rv
.
status_code
==
404
assert
rv
.
status_code
==
404
@pytest.mark.parametrize
(
'
uri
'
,
[
'
/msr/bgp/logical-systems
'
,
'
/msr/bgp/groups
'
])
def
test_peerings_group_list
(
client
,
uri
):
rv
=
client
.
get
(
uri
,
headers
=
DEFAULT_REQUEST_HEADERS
)
assert
rv
.
status_code
==
200
assert
rv
.
is_json
response_data
=
json
.
loads
(
rv
.
data
.
decode
(
'
utf-8
'
))
jsonschema
.
validate
(
response_data
,
PEERING_GROUP_LIST_SCHEMA
)
assert
response_data
# test data is non-empty
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