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
64a2767e
Commit
64a2767e
authored
6 years ago
by
Erik Reid
Browse files
Options
Downloads
Patches
Plain Diff
initial data route module & unit test
parent
0e5dcce4
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/__init__.py
+43
-0
43 additions, 0 deletions
inventory_provider/__init__.py
inventory_provider/data_routes.py
+40
-0
40 additions, 0 deletions
inventory_provider/data_routes.py
test/test_data_routes.py
+80
-0
80 additions, 0 deletions
test/test_data_routes.py
with
163 additions
and
0 deletions
inventory_provider/__init__.py
+
43
−
0
View file @
64a2767e
"""
automatically invoked app factory
"""
import
logging
import
os
from
flask
import
Flask
def
create_app
():
"""
overrides default settings with those found
in the file read from env var SETTINGS_FILENAME
:return: a new flask app instance
"""
app
=
Flask
(
__name__
)
app
.
secret_key
=
"
super secret session key
"
from
inventory_provider
import
data_routes
app
.
register_blueprint
(
data_routes
.
routes
,
url_prefix
=
'
/data
'
)
if
"
SETTINGS_FILENAME
"
not
in
os
.
environ
:
assert
False
,
\
"
environment variable SETTINGS_FILENAME
'
must be defined
"
app
.
config
.
from_envvar
(
"
SETTINGS_FILENAME
"
)
assert
"
INVENTORY_PROVIDER_CONFIG_FILENAME
"
in
app
.
config
,
(
"
INVENTORY_PROVIDER_CONFIG_FILENAME not defined in %s
"
%
os
.
environ
[
"
SETTINGS_FILENAME
"
])
assert
os
.
path
.
isfile
(
app
.
config
[
"
INVENTORY_PROVIDER_CONFIG_FILENAME
"
]),
(
"
config file
'
%s
'
not found
"
%
app
.
config
[
"
INVENTORY_PROVIDER_CONFIG_FILENAME
"
])
from
inventory_provider
import
config
with
open
(
app
.
config
[
"
INVENTORY_PROVIDER_CONFIG_FILENAME
"
])
as
f
:
# test the config file can be loaded
config
.
load
(
f
)
logging
.
debug
(
app
.
config
)
return
app
This diff is collapsed.
Click to expand it.
inventory_provider/data_routes.py
0 → 100644
+
40
−
0
View file @
64a2767e
import
functools
import
json
from
flask
import
Blueprint
,
request
,
Response
#render_template, url_for
routes
=
Blueprint
(
"
python-utils-ui-routes
"
,
__name__
)
VERSION
=
{
"
api
"
:
"
0.1
"
,
"
module
"
:
"
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
def
version
():
return
Response
(
json
.
dumps
(
VERSION
),
mimetype
=
"
application/json
"
)
This diff is collapsed.
Click to expand it.
test/test_data_routes.py
0 → 100644
+
80
−
0
View file @
64a2767e
import
json
import
logging
import
os
import
tempfile
import
pytest
import
inventory_provider
logging
.
basicConfig
(
level
=
logging
.
DEBUG
)
DEFAULT_REQUEST_HEADERS
=
{
"
Content-type
"
:
"
application/json
"
,
"
Accept
"
:
[
"
application/json
"
]
}
MODULE_DIR
=
os
.
path
.
realpath
(
os
.
path
.
join
(
os
.
path
.
dirname
(
__file__
),
"
..
"
,
"
inventory_provider
"
))
DATA_CONFIG
=
{
"
alarms-db
"
:
{
"
hostname
"
:
"
test-dashboard01.geant.org
"
,
"
dbname
"
:
"
alarms
"
,
"
username
"
:
"
invprov
"
,
"
password
"
:
"
invprov-password
"
},
"
oid_list.conf
"
:
os
.
path
.
join
(
MODULE_DIR
,
"
oid_list.conf
"
),
"
routers_community.conf
"
:
os
.
path
.
join
(
MODULE_DIR
,
"
routers_community.conf
"
),
"
ssh
"
:
{
"
private-key
"
:
os
.
path
.
join
(
MODULE_DIR
,
"
ssh
"
,
"
monitor_dsa
"
),
"
known-hosts
"
:
os
.
path
.
join
(
MODULE_DIR
,
"
ssh
"
,
"
monitor_known_hosts
"
)
}
}
@pytest.fixture
def
app_config
():
with
tempfile
.
TemporaryDirectory
()
as
tmpdir
:
data_config_filename
=
os
.
path
.
join
(
tmpdir
,
"
config.json
"
)
with
open
(
data_config_filename
,
"
w
"
)
as
f
:
f
.
write
(
json
.
dumps
(
DATA_CONFIG
))
app_config_filename
=
os
.
path
.
join
(
tmpdir
,
"
app.config
"
)
with
open
(
app_config_filename
,
"
w
"
)
as
f
:
f
.
write
(
"
%s =
'
%s
'
\n
"
%
(
"
INVENTORY_PROVIDER_CONFIG_FILENAME
"
,
data_config_filename
))
yield
app_config_filename
@pytest.fixture
def
client
(
app_config
):
os
.
environ
[
"
SETTINGS_FILENAME
"
]
=
app_config
# with release_webservice.create_app().test_client() as c:
# yield c
with
inventory_provider
.
create_app
().
test_client
()
as
c
:
yield
c
def
test_version_request
(
client
):
rv
=
client
.
post
(
"
data/version
"
,
headers
=
DEFAULT_REQUEST_HEADERS
)
assert
rv
.
status_code
==
200
logging
.
debug
(
"
rv.data:
'
%s
'"
%
rv
.
data
.
decode
(
"
utf-8
"
))
print
(
rv
.
data
.
decode
(
"
utf-8
"
))
# validate(json.loads(rv.data.decode("utf-8")), SERVICES_SCHEMA)
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