def insert_category(self, data: (CmdbCategory, dict)):
        new_category = data
        if isinstance(data, dict):
            new_category = CmdbCategory(**data)

        return self._insert(collection=CmdbCategory.COLLECTION,
                            data=new_category.to_database())
Beispiel #2
0
 def generate_categories() -> list:
     category_list = [
         CmdbCategory(
             **{
                 "public_id": 1,
                 "name": "product_categories",
                 "label": "Product Categories",
                 "root": True,
                 "parent_id": 0,
             }),
         CmdbCategory(
             **{
                 "public_id": 2,
                 "name": "infrastructure",
                 "label": "Infrastructure",
                 "root": False,
                 "parent_id": 1,
             }),
         CmdbCategory(
             **{
                 "public_id": 3,
                 "name": "devices",
                 "label": "Devices",
                 "root": False,
                 "parent_id": 1,
             }),
     ]
     return category_list
Beispiel #3
0
 def update_category(self, category: CmdbCategory):
     """Update a existing category into the database"""
     try:
         return self._update(collection=CmdbCategory.COLLECTION,
                             public_id=category.get_public_id(),
                             data=CmdbCategory.to_json(category))
     except Exception as err:
         raise ObjectManagerUpdateError(err=err)
 def update_category(self, data: dict):
     update_category = data
     if isinstance(data, dict):
         update_category = CmdbCategory(**data)
     ack = self._update(collection=CmdbCategory.COLLECTION,
                        public_id=update_category.get_public_id()
                        or update_category.public_id,
                        data=update_category.to_database())
     return ack
Beispiel #5
0
def find_categories_by_name(regex: str, regex_options: str,
                            request_user: User):
    """HTTP GET call for a list of categories by a given regex name
    Examples:
        Input `ex` matches for example, Example, test_example
    Notes:
        The regex will match for name parameter and label
    """
    query_builder = QueryBuilder()

    if not regex or (regex == '') or regex is None or len(regex) == 0:
        return abort(400, 'No valid selection parameter was passed!')

    if any(ro not in 'imsx' for ro in regex_options):
        return abort(400, 'No valid regex options!')

    query_name = query_builder.regex_('name', f'{regex}', regex_options)
    query_label = query_builder.regex_('label', f'{regex}', regex_options)
    query = query_builder.or_([query_name, query_label])

    try:
        categories: List[CmdbCategory] = object_manager.get_categories_by(
            **query)
    except ObjectManagerInitError as err:
        return abort(500, err.message)
    except ObjectManagerGetError as err:
        return abort(400, err.message)

    if len(categories) == 0:
        return make_response([], 204)

    categories_in_json = [
        CmdbCategory.to_json(category) for category in categories
    ]
    return make_response(categories_in_json)
Beispiel #6
0
 def insert_category(self, category: CmdbCategory):
     """Add a new category into the database or add the children list an existing category"""
     try:
         return self._insert(collection=CmdbCategory.COLLECTION,
                             data=CmdbCategory.to_json(category))
     except Exception as err:
         raise ObjectManagerInsertError(err=err)
Beispiel #7
0
def get_category_by_name(name: str, request_user: User):
    """HTTP GET call - get a specific category by its name"""
    try:
        category_instance = object_manager.get_category_by(name=name)
    except ObjectManagerGetError as err:
        return abort(404, err.message)
    except ObjectManagerInitError as err:
        return abort(501, err.message)
    return make_response(CmdbCategory.to_json(category_instance))
Beispiel #8
0
def get_category(public_id: int, request_user: User):
    """HTTP GET call for a single category by the public id"""
    try:
        category_instance = object_manager.get_category(public_id)
    except ObjectManagerInitError as err:
        return abort(500, err.message)
    except ObjectManagerGetError as err:
        return abort(404, err.message)
    return make_response(CmdbCategory.to_json(category_instance))
 def get_all_categories(self):
     ack = []
     cats = self._get_many(collection=CmdbCategory.COLLECTION,
                           sort='public_id')
     for cat_obj in cats:
         try:
             ack.append(CmdbCategory(**cat_obj))
         except CMDBError as e:
             LOGGER.debug("Error while parsing Category")
             LOGGER.debug(e.message)
     return ack
Beispiel #10
0
 def get_all_categories(self) -> List[CmdbCategory]:
     """Get all categories as nested list"""
     try:
         raw_categories = self._get_many(collection=CmdbCategory.COLLECTION,
                                         sort='public_id')
     except Exception as err:
         raise ObjectManagerGetError(err)
     try:
         return [
             CmdbCategory.from_data(category) for category in raw_categories
         ]
     except Exception as err:
         raise ObjectManagerInitError(err)
 def get_categories_by(self,
                       sort='public_id',
                       **requirements: dict) -> List[CmdbCategory]:
     ack = []
     categories = self._get_many(collection=CmdbCategory.COLLECTION,
                                 sort=sort,
                                 **requirements)
     for cat_obj in categories:
         try:
             ack.append(CmdbCategory(**cat_obj))
         except CMDBError as e:
             continue
     return ack
Beispiel #12
0
 def get_category(self, public_id: int) -> CmdbCategory:
     """Get a category from the database"""
     try:
         raw_category: dict = self._get(collection=CmdbCategory.COLLECTION,
                                        public_id=public_id)
     except Exception as err:
         raise ObjectManagerGetError(err=err)
     if not raw_category:
         raise ObjectManagerGetError(
             err=f'No category with the id: {public_id} was found')
     try:
         return CmdbCategory.from_data(raw_category)
     except Exception as err:
         raise ObjectManagerInitError(err=err)
Beispiel #13
0
    def get_category_by(self, **requirements) -> CmdbCategory:
        """Get a single category by requirements
        Notes:
            Even if multiple categories match the requirements only the first matched will be returned
        """
        try:
            raw_category = self._get_by(collection=CmdbCategory.COLLECTION,
                                        **requirements)
        except Exception as err:
            raise ObjectManagerGetError(err)

        try:
            return CmdbCategory.from_data(raw_category)
        except Exception as err:
            raise ObjectManagerInitError(err)
Beispiel #14
0
def get_categories(request_user: User):
    """HTTP GET call for all categories without any kind of selection"""
    request_arguments = request.args
    try:
        if request_arguments.get('order') == 'tree':
            flatted_tree: List[
                CmdbCategory] = object_manager.get_category_tree().flat()
            categories_list: List[dict] = [
                CmdbCategory.to_json(node) for node in flatted_tree
            ]
        else:
            categories_list: List[dict] = [
                CmdbCategory.to_json(category)
                for category in object_manager.get_all_categories()
            ]
    except ObjectManagerInitError as err:
        return abort(500, err.message)
    except ObjectManagerGetError as err:
        return abort(400, err.message)

    if len(categories_list) == 0:
        return make_response([], 204)

    return make_response(categories_list)
Beispiel #15
0
 def get_categories_by(self,
                       sort='public_id',
                       **requirements: dict) -> List[CmdbCategory]:
     """Get a list of categories by special requirements"""
     try:
         raw_categories = self._get_many(collection=CmdbCategory.COLLECTION,
                                         sort=sort,
                                         **requirements)
     except Exception as err:
         raise ObjectManagerGetError(err)
     try:
         return [
             CmdbCategory.from_data(category) for category in raw_categories
         ]
     except Exception as err:
         raise ObjectManagerInitError(err)
Beispiel #16
0
def update_category(request_user: User):
    try:
        request_data = request.get_data().decode('UTF-8')
        update_data = json.loads(request_data)
    except TypeError as te:
        return abort(400, str(te))
    try:
        updated_category = CmdbCategory.from_data(update_data)
    except Exception as err:
        return abort(400, str(err))

    try:
        ack = object_manager.update_category(updated_category)
    except ObjectManagerUpdateError as err:
        return abort(500, err.message)

    resp = make_response(ack.modified_count)
    return resp
Beispiel #17
0
def add_category(request_user: User):
    """HTTP POST call - add a new category from json post data"""
    try:
        request_data = request.get_data().decode('UTF-8')
        insert_data = json.loads(request_data)
    except TypeError as te:
        return abort(400, str(te))

    insert_data['public_id'] = int(
        object_manager.get_new_id(CmdbCategory.COLLECTION))
    try:
        new_category = CmdbCategory.from_data(insert_data)
    except Exception as err:
        return abort(400, str(err))

    try:
        insert_acknowledge = object_manager.insert_category(new_category)
    except ObjectManagerInsertError as err:
        return abort(400, err.message)
    return make_response(insert_acknowledge)
 def get_category(self, public_id: int):
     try:
         return CmdbCategory(**self._get(collection=CmdbCategory.COLLECTION,
                                         public_id=public_id))
     except (CMDBError, Exception) as e:
         raise ObjectManagerGetError(err=e)