-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refs #1. Added database model and started work on user API.
- Loading branch information
Showing
6 changed files
with
237 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
from flask import request | ||
from flask_restx import Resource, inputs | ||
from sqlalchemy import exc, inspect | ||
|
||
from FlaskModule import user_api_ns as api | ||
from libDashboards.db.models.DashDashboards import DashDashboards | ||
from opentera.services.ServiceAccessManager import ServiceAccessManager, current_login_type, LoginType, \ | ||
current_user_client | ||
from flask_babel import gettext | ||
|
||
|
||
# Parser definition(s) | ||
# GET | ||
get_parser = api.parser() | ||
get_parser.add_argument('uuid', type=str, help='Specific dashboard uuid to query information for.') | ||
get_parser.add_argument('id_site', type=int, help='ID of the site to query all dashboards for') | ||
get_parser.add_argument('id_project', type=int, help='ID of the project to query all dashboards for') | ||
get_parser.add_argument('globals', type=inputs.boolean, help='Query globals dashboards') | ||
|
||
get_parser.add_argument('all_versions', type=inputs.boolean, help='Return all versions of the dashboard(s)') | ||
get_parser.add_argument('list', type=inputs.boolean, help='Return minimal information (to display in a list, for ' | ||
'example)') | ||
|
||
# POST | ||
post_schema = api.schema_model('criteria', {'properties': DashDashboards.get_json_schema(), 'type': 'object', | ||
'location': 'json'}) | ||
|
||
# DELETE | ||
delete_parser = api.parser() | ||
delete_parser.add_argument('id', type=int, help='ID to delete') | ||
|
||
|
||
class QueryDashboard(Resource): | ||
|
||
def __init__(self, _api, *args, **kwargs): | ||
Resource.__init__(self, _api, *args, **kwargs) | ||
self.module = kwargs.get('flaskModule', None) | ||
self.test = kwargs.get('test', False) | ||
|
||
@api.doc(description='Get dashboard information. Should specify only one id or the "globals" parameter', | ||
responses={200: 'Success - returns list of dashboards', | ||
400: 'Required parameter is missing', | ||
403: 'Logged user doesn\'t have permission to access the requested data'}, | ||
params={'token': 'Secret token'}) | ||
@api.expect(get_parser) | ||
@ServiceAccessManager.token_required(allow_static_tokens=False, allow_dynamic_tokens=True) | ||
def get(self): | ||
if current_login_type != LoginType.USER_LOGIN: | ||
return gettext('Only users can use this API.'), 403 | ||
|
||
# Parse arguments | ||
request_args = get_parser.parse_args(strict=False) | ||
|
||
dashboards = [] | ||
if 'uuid' in request_args: | ||
dashboard = DashDashboards.get_dashboard_by_uuid(request_args['uuid']) | ||
if not dashboard: | ||
return gettext('Forbidden'), 403 # Explicitely vague for security purpose | ||
|
||
if dashboard.id_site: | ||
site_role = current_user_client.get_role_for_site(dashboard.id_site) | ||
if site_role == 'Undefined': | ||
return gettext('Forbidden'), 403 | ||
if dashboard.id_project: | ||
project_role = current_user_client.get_role_for_project(dashboard.id_project) | ||
if project_role == 'Undefined': | ||
return gettext('Forbidden'), 403 | ||
|
||
if not dashboard.id_project and not dashboard.id_site: | ||
# Global dashboard - only for super admins | ||
if not current_user_client.user_superadmin: | ||
return gettext('Forbidden'), 403 | ||
dashboards = [dashboard] | ||
|
||
elif 'id_site' in request_args: | ||
site_role = current_user_client.get_role_for_site(request_args['id_site']) | ||
if site_role == 'Undefined': | ||
return gettext('Forbidden'), 403 | ||
dashboards = DashDashboards.get_dashboards_for_site(request_args['id_site']) | ||
|
||
elif 'id_project' in request_args: | ||
project_role = current_user_client.get_role_for_project(request_args['id_project']) | ||
if project_role == 'Undefined': | ||
return gettext('Forbidden'), 403 | ||
dashboards = DashDashboards.get_dashboards_for_project(request_args['id_project']) | ||
|
||
elif request_args['globals']: | ||
if not current_user_client.user_superadmin: | ||
return gettext('Forbidden'), 403 | ||
dashboards = DashDashboards.get_dashboards_globals() | ||
else: | ||
return gettext('Must specify at least one id parameter or "globals"') | ||
|
||
# Convert to json and return | ||
dashboards_json = [dash.to_json(request_args['list']) for dash in dashboards] | ||
return dashboards_json | ||
|
||
@api.expect(post_schema) | ||
@api.doc(description='Create or update a dashboard', | ||
responses={200: 'Success', | ||
403: 'No access to this API', | ||
400: 'Missing parameter in request' | ||
}, | ||
params={'token': 'Secret token'}) | ||
@ServiceAccessManager.token_required(allow_static_tokens=False, allow_dynamic_tokens=True) | ||
def post(self): | ||
if current_login_type != LoginType.USER_LOGIN: | ||
return gettext('Only users can use this API.'), 403 | ||
|
||
@api.expect(delete_parser, validate=True) | ||
@api.doc(description='Delete a dashboard', | ||
responses={200: 'Success - deleted', | ||
400: 'Bad request', | ||
403: 'Access denied'}, | ||
params={'token': 'Secret token'}) | ||
@ServiceAccessManager.token_required(allow_static_tokens=False, allow_dynamic_tokens=True) | ||
def delete(self): | ||
if current_login_type != LoginType.USER_LOGIN: | ||
return gettext('Only users can use this API.'), 403 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
from libDashboards.db.models.BaseModel import BaseModel | ||
from sqlalchemy import Column, Integer, Sequence, String, Boolean | ||
import uuid | ||
|
||
|
||
class DashDashboards(BaseModel): | ||
__tablename__ = 't_dashboards' | ||
id_dashboard = Column(Integer, Sequence('id_dashboard_sequence'), primary_key=True, autoincrement=True) | ||
id_site = Column(Integer, nullable=True) | ||
id_project = Column(Integer, nullable=True) | ||
|
||
dashboard_uuid = Column(String(36), nullable=False) | ||
dashboard_name = Column(String, nullable=False) | ||
dashboard_enabled = Column(Boolean, nullable=False, default=True) | ||
dashboard_description = Column(String, nullable=True) # Dashboard user-visible description | ||
dashboard_definition = Column(String, nullable=False) # Dashboard definition string | ||
dashboard_version = Column(Integer, nullable=False, default=1) | ||
|
||
def to_json(self, ignore_fields=None, minimal=False): | ||
if ignore_fields is None: | ||
ignore_fields = [] | ||
|
||
if minimal: | ||
ignore_fields.extend(['asset_definition']) | ||
|
||
asset_json = super().to_json(ignore_fields=ignore_fields) | ||
|
||
return asset_json | ||
|
||
@staticmethod | ||
def get_dashboard_by_uuid(dashboard_uuid: str, latest=True): | ||
query = DashDashboards.query.filter_by(dashboard_uuid=dashboard_uuid) | ||
if latest: | ||
query = query.order_by(DashDashboards.dashboard_version).desc() | ||
|
||
return query.first() | ||
|
||
@staticmethod | ||
def get_dashboards_for_site(site_id: int, latest=True) -> []: | ||
return DashDashboards.query.filter_by(id_site=site_id).all() | ||
|
||
@staticmethod | ||
def get_dashboards_for_project(project_id: int, latest=True) -> []: | ||
query = DashDashboards.query.filter_by(id_project=project_id) | ||
# if latest: | ||
# query = query.group_by(DashDashboards.dashboard_uuid) | ||
return query.all() | ||
|
||
@staticmethod | ||
def get_dashboards_globals(latest=True) -> []: | ||
return DashDashboards.query.filter_by(id_project=None, id_site=None).all() | ||
|
||
@classmethod | ||
def insert(cls, dashboard): | ||
# Generate UUID | ||
if not dashboard.dashboard_uuid: | ||
dashboard.dashboard_uuid = str(uuid.uuid4()) | ||
|
||
super().insert(dashboard) | ||
|
||
@staticmethod | ||
def create_defaults(test=False): | ||
if test: | ||
# Create dashboard for site... | ||
dashboard = DashDashboards() | ||
dashboard.id_site = 1 | ||
dashboard.dashboard_name = 'Site 1 - Global' | ||
dashboard.dashboard_description = 'Test dashboard for global site overview' | ||
dashboard.dashboard_definition = '{}' | ||
DashDashboards.insert(dashboard) | ||
|
||
# ... for project... | ||
dashboard = DashDashboards() | ||
dashboard.id_project = 1 | ||
dashboard.dashboard_name = 'Project 1 - Global' | ||
dashboard.dashboard_description = 'Test dashboard for global project overview' | ||
dashboard.dashboard_definition = '{}' | ||
DashDashboards.insert(dashboard) | ||
|
||
dashboard = DashDashboards() | ||
dashboard.id_project = 1 | ||
dashboard.dashboard_name = 'Project 1 - Alerts' | ||
dashboard.dashboard_description = 'Test dashboard for project alerts' | ||
dashboard.dashboard_definition = '{}' | ||
DashDashboards.insert(dashboard) | ||
uuid = dashboard.dashboard_uuid | ||
|
||
dashboard = DashDashboards() | ||
dashboard.id_project = 1 | ||
dashboard.dashboard_name = 'Project 1 - Alerts v2' | ||
dashboard.dashboard_description = 'Test dashboard for project alerts' | ||
dashboard.dashboard_definition = '{}' | ||
dashboard.dashboard_version = 2 | ||
dashboard.dashboard_uuid = uuid | ||
DashDashboards.insert(dashboard) | ||
|
||
# ... and globals | ||
dashboard = DashDashboards() | ||
dashboard.dashboard_name = 'System Dashboard' | ||
dashboard.dashboard_description = 'Global system dashboard' | ||
dashboard.dashboard_definition = '{}' | ||
DashDashboards.insert(dashboard) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
from .DashDashboards import DashDashboards | ||
|
||
# All exported symbols | ||
__all__ = [ | ||
'DashDashboards' | ||
] |