diff options
Diffstat (limited to 'moonv4/moon_manager/moon_manager/api/models.py')
-rw-r--r-- | moonv4/moon_manager/moon_manager/api/models.py | 246 |
1 files changed, 86 insertions, 160 deletions
diff --git a/moonv4/moon_manager/moon_manager/api/models.py b/moonv4/moon_manager/moon_manager/api/models.py index 56695c17..cec899f5 100644 --- a/moonv4/moon_manager/moon_manager/api/models.py +++ b/moonv4/moon_manager/moon_manager/api/models.py @@ -2,198 +2,124 @@ # This software is distributed under the terms and conditions of the 'Apache-2.0' # license which can be found in the file 'LICENSE' in this package distribution # or at 'http://www.apache.org/licenses/LICENSE-2.0'. +""" +Models aggregate multiple meta rules +""" +from flask import request +from flask_restful import Resource from oslo_log import log as logging -from oslo_config import cfg +from moon_utilities.security_functions import check_auth from moon_db.core import ModelManager -LOG = logging.getLogger(__name__) -CONF = cfg.CONF +__version__ = "0.1.0" +LOG = logging.getLogger("moon.manager.api." + __name__) -class Models(object): - def __init__(self): - self.manager = ModelManager +class Models(Resource): + """ + Endpoint for model requests + """ - def get_models(self, ctx, args): - try: - data = self.manager.get_models(user_id=ctx["user_id"], model_id=ctx.get("id")) - except Exception as e: - LOG.error(e, exc_info=True) - return {"result": False, - "error": str(e), - "ctx": ctx, "args": args} - return {"models": data} + __urls__ = ( + "/models", + "/models/", + "/models/<string:uuid>", + "/models/<string:uuid>/", + ) - def add_model(self, ctx, args): - try: - data = self.manager.add_model(user_id=ctx["user_id"], model_id=ctx.get("id"), value=args) - except Exception as e: - LOG.error(e, exc_info=True) - return {"result": False, - "error": str(e), - "ctx": ctx, "args": args} - return {"models": data} + @check_auth + def get(self, uuid=None, user_id=None): + """Retrieve all models - def delete_model(self, ctx, args): + :param uuid: uuid of the model + :param user_id: user ID who do the request + :return: { + "model_id1": { + "name": "...", + "description": "...", + "meta_rules": ["meta_rule_id1", ] + } + } + :internal_api: get_models + """ try: - data = self.manager.delete_model(user_id=ctx["user_id"], model_id=ctx["id"]) + data = ModelManager.get_models(user_id=user_id, model_id=uuid) except Exception as e: LOG.error(e, exc_info=True) return {"result": False, - "error": str(e), - "ctx": ctx, "args": args} - return {"result": True} - - def update_model(self, ctx, args): - try: - data = self.manager.update_model(user_id=ctx["user_id"], model_id=ctx["id"], value=args) - except Exception as e: - LOG.error(e, exc_info=True) - return {"result": False, - "error": str(e), - "ctx": ctx, "args": args} + "error": str(e)} return {"models": data} + @check_auth + def post(self, uuid=None, user_id=None): + """Create model. -class MetaRules(object): - - def __init__(self): - self.manager = ModelManager - - def add_meta_rules(self, ctx, args): + :param uuid: uuid of the model (not used here) + :param user_id: user ID who do the request + :request body: { + "name": "...", + "description": "...", + "meta_rules": ["meta_rule_id1", ] + } + :return: { + "model_id1": { + "name": "...", + "description": "...", + "meta_rules": ["meta_rule_id1", ] + } + } + :internal_api: add_model + """ try: - data = self.manager.add_meta_rule(user_id=ctx["user_id"], meta_rule_id=None, value=args) + data = ModelManager.add_model(user_id=user_id, model_id=uuid, value=request.json) except Exception as e: LOG.error(e, exc_info=True) return {"result": False, - "error": str(e), - "ctx": ctx, "args": args} - return {"meta_rules": data} - - def delete_meta_rules(self, ctx, args): - try: - data = self.manager.delete_meta_rule(user_id=ctx["user_id"], meta_rule_id=ctx["meta_rule_id"]) - except Exception as e: - LOG.error(e, exc_info=True) - return {"result": False, - "error": str(e), - "ctx": ctx, "args": args} - return {"result": True} - - def get_meta_rules(self, ctx, args): - try: - data = self.manager.get_meta_rules(user_id=ctx["user_id"], meta_rule_id=ctx["meta_rule_id"]) - except Exception as e: - LOG.error(e, exc_info=True) - return {"result": False, - "error": str(e), - "ctx": ctx, "args": args} - return {"meta_rules": data} - - def set_meta_rules(self, ctx, args): - try: - data = self.manager.set_meta_rule(user_id=ctx["user_id"], meta_rule_id=ctx["meta_rule_id"], value=args) - except Exception as e: - LOG.error(e, exc_info=True) - return {"result": False, - "error": str(e), - "ctx": ctx, "args": args} - return {"meta_rules": data} - - -class MetaData(object): - - def __init__(self): - self.manager = ModelManager - - def get_subject_categories(self, ctx, args): - try: - data = self.manager.get_subject_categories(user_id=ctx["user_id"], category_id=args["category_id"]) - except Exception as e: - LOG.error(e, exc_info=True) - return {"result": False, - "error": str(e), - "ctx": ctx, "args": args} - return {"subject_categories": data} - - def set_subject_category(self, ctx, args): - try: - data = self.manager.add_subject_category(user_id=ctx["user_id"], value=args) - except Exception as e: - LOG.error(e, exc_info=True) - return {"result": False, - "error": str(e), - "ctx": ctx, "args": args} - return {"subject_categories": data} - - def delete_subject_category(self, ctx, args): - try: - data = self.manager.delete_subject_category(user_id=ctx["user_id"], category_id=args["category_id"]) - except Exception as e: - LOG.error(e, exc_info=True) - return {"result": False, - "error": str(e), - "ctx": ctx, "args": args} - return {"result": True} - - def get_object_categories(self, ctx, args): - try: - data = self.manager.get_object_categories(user_id=ctx["user_id"], category_id=args["category_id"]) - except Exception as e: - LOG.error(e, exc_info=True) - return {"result": False, - "error": str(e), - "ctx": ctx, "args": args} - return {"object_categories": data} + "error": str(e)} + return {"models": data} - def set_object_category(self, ctx, args): - try: - data = self.manager.add_object_category(user_id=ctx["user_id"], value=args) - except Exception as e: - LOG.error(e, exc_info=True) - return {"result": False, - "error": str(e), - "ctx": ctx, "args": args} - return {"object_categories": data} + @check_auth + def delete(self, uuid=None, user_id=None): + """Delete a model - def delete_object_category(self, ctx, args): + :param uuid: uuid of the model to delete + :param user_id: user ID who do the request + :return: { + "result": "True or False", + "message": "optional message" + } + :internal_api: delete_model + """ try: - data = self.manager.delete_object_category(user_id=ctx["user_id"], category_id=args["category_id"]) + data = ModelManager.delete_model(user_id=user_id, model_id=uuid) except Exception as e: LOG.error(e, exc_info=True) return {"result": False, - "error": str(e), - "ctx": ctx, "args": args} + "error": str(e)} return {"result": True} - def get_action_categories(self, ctx, args): - try: - data = self.manager.get_action_categories(user_id=ctx["user_id"], category_id=args["category_id"]) - except Exception as e: - LOG.error(e, exc_info=True) - return {"result": False, - "error": str(e), - "ctx": ctx, "args": args} - return {"action_categories": data} + @check_auth + def patch(self, uuid=None, user_id=None): + """Update a model - def set_action_category(self, ctx, args): + :param uuid: uuid of the model to update + :param user_id: user ID who do the request + :return: { + "model_id1": { + "name": "...", + "description": "...", + "meta_rules": ["meta_rule_id1", ] + } + } + :internal_api: update_model + """ try: - data = self.manager.add_action_category(user_id=ctx["user_id"], value=args) + data = ModelManager.update_model(user_id=user_id, model_id=uuid, value=request.json) except Exception as e: LOG.error(e, exc_info=True) return {"result": False, - "error": str(e), - "ctx": ctx, "args": args} - return {"action_categories": data} + "error": str(e)} + return {"models": data} - def delete_action_category(self, ctx, args): - try: - data = self.manager.delete_action_category(user_id=ctx["user_id"], category_id=args["category_id"]) - except Exception as e: - LOG.error(e, exc_info=True) - return {"result": False, - "error": str(e), - "ctx": ctx, "args": args} - return {"result": True} |