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
585842da
Commit
585842da
authored
5 years ago
by
Erik Reid
Browse files
Options
Downloads
Patches
Plain Diff
moved some parsing into coriant classifier handler
parent
c6c54f42
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/classifier.py
+27
-15
27 additions, 15 deletions
inventory_provider/routes/classifier.py
test/test_classifier_routes.py
+22
-10
22 additions, 10 deletions
test/test_classifier_routes.py
with
49 additions
and
25 deletions
inventory_provider/routes/classifier.py
+
27
−
15
View file @
585842da
import
ipaddress
import
ipaddress
import
json
import
json
import
logging
import
re
import
re
from
flask
import
Blueprint
,
Response
,
current_app
from
flask
import
Blueprint
,
Response
,
current_app
...
@@ -9,6 +10,8 @@ from inventory_provider.db import opsdb, db
...
@@ -9,6 +10,8 @@ from inventory_provider.db import opsdb, db
routes
=
Blueprint
(
"
inventory-data-classifier-support-routes
"
,
__name__
)
routes
=
Blueprint
(
"
inventory-data-classifier-support-routes
"
,
__name__
)
logger
=
logging
.
getLogger
(
__name__
)
class
ClassifierRequestError
(
Exception
):
class
ClassifierRequestError
(
Exception
):
status_code
=
500
status_code
=
500
...
@@ -280,37 +283,46 @@ def get_trap_metadata(source_equipment, interface, circuit_id):
...
@@ -280,37 +283,46 @@ def get_trap_metadata(source_equipment, interface, circuit_id):
return
Response
(
result
,
mimetype
=
"
application/json
"
)
return
Response
(
result
,
mimetype
=
"
application/json
"
)
@routes.route
(
"
/coriant-info/
"
@routes.route
(
'
/coriant-info/<equipment_name>/<path:entity_string>
'
,
"
<equipment_name>/<card_id>/<port_number>
"
,
methods
=
[
'
GET
'
,
'
POST
'
])
methods
=
[
'
GET
'
,
'
POST
'
])
@common.require_accepts_json
@common.require_accepts_json
def
get_coriant_info
(
equipment_name
,
card_id
,
port_number
):
def
get_coriant_info
(
equipment_name
,
entity_string
):
r
=
common
.
get_redis
()
r
=
common
.
get_redis
()
cache_key
=
'
classifier-cache:coriant:%s:%s
:%s
'
%
(
cache_key
=
'
classifier-cache:coriant:%s:%s
'
%
(
equipment_name
,
card_id
,
port_number
)
equipment_name
,
entity_string
)
result
=
r
.
get
(
cache_key
)
result
=
r
.
get
(
cache_key
)
if
result
:
if
result
:
result
=
result
.
decode
(
'
utf-8
'
)
result
=
result
.
decode
(
'
utf-8
'
)
else
:
else
:
m
=
re
.
match
(
r
'
^(\d+\-\d+)\.(\d+)
'
,
entity_string
)
if
not
m
:
logger
.
warning
(
'
invalid coriant entity string format: %r
'
%
entity_string
)
return
Response
(
response
=
"
no available info for
'
{}
'
'
{}
'"
.
format
(
equipment_name
,
entity_string
),
status
=
404
,
mimetype
=
"
text/html
"
)
result
=
{
'
equipment name
'
:
equipment_name
,
'
card id
'
:
m
.
group
(
1
),
'
port number
'
:
m
.
group
(
2
)
}
config
=
current_app
.
config
[
'
INVENTORY_PROVIDER_CONFIG
'
]
config
=
current_app
.
config
[
'
INVENTORY_PROVIDER_CONFIG
'
]
with
db
.
connection
(
config
[
'
ops-db
'
])
as
cx
:
with
db
.
connection
(
config
[
'
ops-db
'
])
as
cx
:
path
=
opsdb
.
lookup_coriant_path
(
path
=
opsdb
.
lookup_coriant_path
(
cx
,
equipment_name
,
card
_
id
,
port
_
number
)
cx
,
equipment_name
,
result
[
'
card
id
'
],
result
[
'
port
number
'
]
)
if
not
path
:
if
path
:
return
Response
(
result
[
'
path
'
]
=
path
response
=
"
no available info for {} {} {}
"
.
format
(
equipment_name
,
card_id
,
port_number
),
status
=
404
,
mimetype
=
"
text/html
"
)
result
=
json
.
dumps
({
'
path
'
:
path
})
# cache this data for the next call
# cache this data for the next call
r
.
set
(
cache_key
,
result
.
encode
(
'
utf-8
'
))
result
=
json
.
dumps
(
result
).
encode
(
'
utf-8
'
)
r
.
set
(
cache_key
,
result
)
return
Response
(
result
,
mimetype
=
"
application/json
"
)
return
Response
(
result
,
mimetype
=
"
application/json
"
)
This diff is collapsed.
Click to expand it.
test/test_classifier_routes.py
+
22
−
10
View file @
585842da
...
@@ -278,16 +278,14 @@ def test_coriant_info(client, mocker):
...
@@ -278,16 +278,14 @@ def test_coriant_info(client, mocker):
"""
"""
just check the correct method is called, but mock out all sql access
just check the correct method is called, but mock out all sql access
"""
"""
expected_path
=
{
CONNECTION
=
'
bogus connection
'
'
C
'
:
'
bogus connection
'
,
EQUIPMENT
=
'
bogus equipment name
'
'
E
'
:
'
bogus equipment name
'
,
CARD_ID
=
'
123-456
'
'
CID
'
:
'
bogus card id
'
,
PORT_NUMBER
=
'
789
'
'
P
'
:
'
bogus card number
'
}
@contextlib.contextmanager
@contextlib.contextmanager
def
mocked_connection
(
ignored
):
def
mocked_connection
(
ignored
):
yield
expected_path
[
'
C
'
]
yield
CONNECTION
mocker
.
patch
(
mocker
.
patch
(
'
inventory_provider.db.db.connection
'
,
mocked_connection
)
'
inventory_provider.db.db.connection
'
,
mocked_connection
)
...
@@ -296,14 +294,28 @@ def test_coriant_info(client, mocker):
...
@@ -296,14 +294,28 @@ def test_coriant_info(client, mocker):
lambda
a
,
b
,
c
,
d
:
{
'
C
'
:
a
,
'
E
'
:
b
,
'
CID
'
:
c
,
'
P
'
:
d
})
lambda
a
,
b
,
c
,
d
:
{
'
C
'
:
a
,
'
E
'
:
b
,
'
CID
'
:
c
,
'
P
'
:
d
})
rv
=
client
.
get
(
rv
=
client
.
get
(
'
/classifier/coriant-info/{E}/{CID}/{P}
'
.
format
(
**
expected_path
),
'
/classifier/coriant-info/{eq}/{cid}.{pn}abc.234 af/23
'
.
format
(
eq
=
EQUIPMENT
,
cid
=
CARD_ID
,
pn
=
PORT_NUMBER
),
headers
=
DEFAULT_REQUEST_HEADERS
)
headers
=
DEFAULT_REQUEST_HEADERS
)
assert
rv
.
status_code
==
200
assert
rv
.
status_code
==
200
assert
rv
.
is_json
assert
rv
.
is_json
response_data
=
json
.
loads
(
rv
.
data
.
decode
(
'
utf-8
'
))
response_data
=
json
.
loads
(
rv
.
data
.
decode
(
'
utf-8
'
))
expected_response
=
{
'
path
'
:
expected_path
}
expected_response
=
{
'
equipment name
'
:
EQUIPMENT
,
'
card id
'
:
CARD_ID
,
'
port number
'
:
PORT_NUMBER
,
'
path
'
:
{
'
C
'
:
CONNECTION
,
'
E
'
:
EQUIPMENT
,
'
CID
'
:
CARD_ID
,
'
P
'
:
PORT_NUMBER
}
}
assert
response_data
==
expected_response
assert
response_data
==
expected_response
...
@@ -323,7 +335,7 @@ def test_coriant_info_not_found(client, mocker):
...
@@ -323,7 +335,7 @@ def test_coriant_info_not_found(client, mocker):
lambda
a
,
b
,
c
,
d
:
None
)
lambda
a
,
b
,
c
,
d
:
None
)
rv
=
client
.
get
(
rv
=
client
.
get
(
'
/classifier/coriant-info/aaa/
bbb/ccc
'
,
'
/classifier/coriant-info/aaa/
unparseableentitystring
'
,
headers
=
DEFAULT_REQUEST_HEADERS
)
headers
=
DEFAULT_REQUEST_HEADERS
)
assert
rv
.
status_code
==
404
assert
rv
.
status_code
==
404
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