async def get_unsupported_features(self,
                                       component_name="",
                                       feature_name=""):
        """
        Get Unsupported Features in Following Formats:
            1) No Component Name : Return All Features List.
            2) No Feature Name : Return All the Features Related to Components Provided.
        :param component_name: Name of Component :type: String
        :param feature_name: Name of Feature :type: String
        :return: List of Features Found.
        """
        query = Query()
        # Generate Key
        if component_name:
            query.filter_by(
                Compare(UnsupportedFeaturesModel.component_name, "=",
                        component_name))
            if feature_name:
                query.filter_by(
                    And(
                        Compare(UnsupportedFeaturesModel.feature_name, "=",
                                feature_name)))

        feature_details = await self.storage(UnsupportedFeaturesModel).get(
            query)
        if not feature_details:
            return []
        return [
            each_feature.to_primitive() for each_feature in feature_details
        ]
Exemple #2
0
 async def delete(self, name: str) -> None:
     """Removes the data from the encrypted storage."""
     neb = await self._get_item(name)
     if neb is None:
         raise KeyError(f'Item "{name}" was not found in secure storage')
     await self._storage(NamedEncryptedBytes).delete(
         Compare(NamedEncryptedBytes.name, '=', name))
Exemple #3
0
    async def get_by_id(self, obj_id: Any) -> Union[BaseModel, None]:
        """
        Simple implementation of get function.

        Important note: in terms of this API 'id' means BaseModel.primary_key reference. If model
        contains 'id' field please use ordinary get call. For example,

            await db(YourBaseModel).get(Query().filter_by(Compare(YourBaseModel.id, "=", obj_id)))

        This API call is equivalent to

            await db(YourBaseModel).get(Query().filter_by(
                                                    Compare(YourBaseModel.primary_key, "=", obj_id)))

        :param Any obj_id:
        :return: BaseModel if object was found by its id and None otherwise
        """
        id_field = getattr(self._model, self._model.primary_key)
        try:
            converted = id_field.to_native(obj_id)
        except ConversionError as e:
            raise DataAccessInternalError(f"{e}")

        query = Query().filter_by(
            Compare(self._model.primary_key, "=", converted))

        result = await self.get(query)

        if result:
            return result.pop()

        return None
Exemple #4
0
 async def retrieve_all(self, bundle_id) -> [SupportBundleModel]:
     if bundle_id:
         query = Query().limit(SupportBundleModel.max_count).filter_by(\
             Compare(SupportBundleModel.bundle_id, \
             '=', bundle_id))
     else:
         query = Query().limit(SupportBundleModel.max_count)
     return await self.db(SupportBundleModel).get(query)
Exemple #5
0
    async def _get_item(self, name: str) -> NamedEncryptedBytes:
        """
        Gets NamedEncryptedBytes object with encrypted payload from the storage.

        Returns NamedEncryptedBytes object if the item with provided name exists or None
        """
        query = Query().filter_by(Compare(NamedEncryptedBytes.name, '=', name))
        neb = next(iter(await self._storage(NamedEncryptedBytes).get(query)),
                   None)
        return neb
 async def is_feature_supported(self, component_name, feature_name):
     """
     Check whether the feature supported or not.
     :param component_name: Name of Component :type: String
     :param feature_name: Name of Feature :type: String
     :return: Supported -> True/Not-Supported -> False
     """
     feature_id = UnsupportedFeaturesModel.create_feature_id(
         *(component_name, const.UNSUPPORTED_FEATURE, feature_name))
     # Create Query
     query = Query().filter_by(
         Compare(UnsupportedFeaturesModel.feature_id, '=', feature_id))
     feature = await self.storage(UnsupportedFeaturesModel).get(query)
     if feature:
         return False
     return True
 async def delete_event(self, entity, entity_id, component, component_id):
     """
     Delete all Component Related Events.
     :param entity: Entity Name :type: Str
     :param entity_id: Entity Id :type: Str
     :param component: Component Name :type: Str
     :param component_id: Component Id :type: Str
     :return:
     """
     # Generate Key
     decision_id = DecisionModel.create_decision_id(entity, entity_id,
                                                    component, component_id)
     Log.debug(f"Deleting event for {decision_id}")
     # Delete all the Decisions Related to The Event.
     await self.storage(DecisionModel).delete(
         Compare(DecisionModel.decision_id, 'like', decision_id))
Exemple #8
0
    async def delete_by_id(self, obj_id: Any) -> bool:
        """
        Delete base model by its id.

        :param Any obj_id: id of the object to be deleted
        :return: BaseModel if object was found by its id and None otherwise
        :return: `True` if object was deleted successfully and `False` otherwise
        """
        # Generic implementation of delete by id functionality
        id_field = getattr(self._model, self._model.primary_key)
        try:
            converted = id_field.to_native(obj_id)
        except ConversionError as e:
            raise DataAccessInternalError(f"{e}")

        filter_condition = Compare(self._model.primary_key, "=", converted)

        result = await self.delete(filter_condition)
        return result > 0
 async def get_event(self, entity, entity_id, component, component_id,
                     alert_time):
     """
     Get a event with specific time.
     :param entity: Entity Name :type: Str
     :param entity_id: Entity Id :type: Str
     :param component: Component Name :type: Str
     :param component_id: Component Id :type: Str
     :param alert_time: Alert Generated time :type: Str
     :return: action For Respective Alert :type: Str
     """
     # Generate Key
     decision_id = DecisionModel.create_decision_id(entity, entity_id,
                                                    component, component_id,
                                                    alert_time)
     Log.debug(f"Fetch event for {decision_id}")
     # Create Query
     query = Query().filter_by(
         Compare(DecisionModel.decision_id, '=', decision_id))
     return await self.storage(DecisionModel).get(query)
Exemple #10
0
    async def update_by_id(self, obj_id: Any, to_update: dict) -> bool:
        """
        Update base model in db by id (primary key).

        :param Any obj_id: id-value of the object which should be updated (primary key value)
        :param dict to_update: dictionary with fields and values which should be updated
        :return: `True` if object was updated and `False` otherwise
        """
        # Generic code for update_by_id method of particular method
        id_field = getattr(self._model, self._model.primary_key)
        try:
            converted = id_field.to_native(obj_id)
        except ConversionError as e:
            raise DataAccessInternalError(f"{e}")

        model_filter = Compare(self._model.primary_key, "=", converted)

        result = await self.update(model_filter, to_update)

        return result > 0
    async def get_event_time(self, entity, entity_id, component, component_id,
                             **kwargs):
        """
        Fetch All Event with All Components Name.
        :param entity: Entity Name :type: Str
        :param entity_id: Entity Id :type: Str
        :param component: Component Name :type: Str
        :param component_id: Component Id :type: Str
        :return:
        """
        # Generate Key
        decision_id = DecisionModel.create_decision_id(entity, entity_id,
                                                       component, component_id)
        Log.debug(f"Fetch event time for {decision_id}")
        # Create Query
        query = Query().filter_by(
            Compare(DecisionModel.decision_id, 'like', decision_id))

        if kwargs.get("sort_by"):
            query.order_by(kwargs["sort_by"].field, kwargs['sort_by'].order)

        return await self.storage(DecisionModel).get(query)
async def example():
    conf = GeneralConfig({
        "databases": {
            "es_db": {
                "import_path": "ElasticSearchDB",
                "config": {
                    "hosts": ["localhost"],
                    "port": 9200,
                    "login": "",
                    "password": ""
                }
            },
            "consul_db": {
                "import_path": "ConsulDB",
                "config": {
                    "hosts": ["127.0.0.1"],
                    "port": 8500,  # HTTP API Port
                    "login": "",
                    "password": ""
                }
            }
        },
        "models": [
            {
                "import_path":
                "cortx.utils.data.db.examples.consul_storage.AlertModel",
                "database": "consul_db",
                "config": {
                    "es_db": {
                        "collection": "alerts"
                    },
                    "consul_db": {
                        "collection": "alerts"
                    }
                }
            },
        ]
    })

    db = DataBaseProvider(conf)

    alert1 = AlertModel(ALERT1)
    alert2 = AlertModel(ALERT2)
    alert3 = AlertModel(ALERT3)
    alert4 = AlertModel(ALERT4)

    await db(AlertModel).store(alert1)
    await db(AlertModel).store(alert2)
    await db(AlertModel).store(alert3)
    await db(AlertModel).store(alert4)

    limit = 1
    offset = 0
    for _ in range(4):
        res = await db(AlertModel).get(Query().offset(offset).limit(limit))
        for model in res:
            print(
                f"Get by offset = {offset}, limit = {limit} : {model.to_primitive()}"
            )
            offset += 1

    res = await db(AlertModel)._get_all_raw()
    print([model for model in res])

    _id = 2
    res = await db(AlertModel).get_by_id(_id)
    if res is not None:
        print(f"Get by id = {_id}: {res.to_primitive()}")

    to_update = {'state': "Good", 'location': "USA"}

    await db(AlertModel).update_by_id(_id, to_update)

    res = await db(AlertModel).get_by_id(_id)
    if res is not None:
        print(f"Get by id after update = {_id}: {res.to_primitive()}")

    filter_obj = Or(
        Compare(AlertModel.status, "=", "Success"),
        And(Compare(AlertModel.alert_uuid, "<=", 3),
            Compare(AlertModel.status, "=", "Success")))

    query = Query().filter_by(filter_obj)
    res = await db(AlertModel).get(query)

    for model in res:
        print(f"Get by query ={model.to_primitive()}")

    await db(AlertModel).update(filter_obj, to_update)

    res = await db(AlertModel).get(query)

    for model in res:
        print(f"Get by query after update={model.to_primitive()}")

    query = Query().filter_by(filter_obj).order_by(AlertModel.location)
    res = await db(AlertModel).get(query)

    for model in res:
        print(f"Get by query with order_by ={model.to_primitive()}")

    count = await db(AlertModel).count(filter_obj)

    print(f"Count by filter = {count}")

    num = await db(AlertModel).delete(filter_obj)
    print(f"Deleted by filter={num}")

    _id = 2
    is_deleted = await db(AlertModel).delete_by_id(_id)
    print(f"Object by id = {_id} was deleted: {is_deleted}")

    count = await db(AlertModel).count()

    print(f"Remaining objects in consul = {count}")
async def example():
    conf = GeneralConfig({
        "databases": {
            "es_db": {
                "import_path": "ElasticSearchDB",
                "config": {
                    "hosts": ["localhost"],
                    "port": 9200,
                    "login": "",
                    "password": ""
                }
            }
        },
        "models": [
            {
                "import_path": "cortx.utils.data.db.examples.elasticsearch_storage.AlertModel",
                "database": "es_db",
                "config": {
                    "es_db":
                        {
                            "collection": "alert"
                        }
                }
            }]
    })

    db = DataBaseProvider(conf)

    alert1 = AlertModel(ALERT1)
    alert2 = AlertModel(ALERT2)
    alert3 = AlertModel(ALERT3)
    alert4 = AlertModel(ALERT4)

    await db(AlertModel).store(alert1)
    await db(AlertModel).store(alert2)
    await db(AlertModel).store(alert3)
    await db(AlertModel).store(alert4)

    res = await db(AlertModel).get(
        Query().filter_by(Compare(AlertModel.severity, "=", "Neutral")).order_by(
            AlertModel.severity,
            SortOrder.ASC))

    if res:
        for i, model in enumerate(res):
            print(f"Model {i}: {model.to_primitive()}")

    filter = And(Compare(AlertModel.primary_key, "=", 1),
                 And(Compare(AlertModel.status, "=", "Success"),
                     Compare(AlertModel.primary_key, ">", 1)))

    query = Query().filter_by(filter).order_by(AlertModel.primary_key, SortOrder.DESC)
    res = await db(AlertModel).get(query)
    print(f"Get by query: {[alert.to_primitive() for alert in res]}")

    to_update = {
        'location': "Russia",
        'alert_uuid': 22,
        'resolved': False,
        'created_time': datetime.now()
    }

    await db(AlertModel).update(filter, to_update)

    res = await db(AlertModel).get(query)
    print(f"Get by query after update: {[alert.to_primitive() for alert in res]}")

    _id = 2
    res = await db(AlertModel).get_by_id(_id)
    if res is not None:
        print(f"Get by id = {_id}: {res.to_primitive()}")

    await db(AlertModel).update_by_id(_id, to_update)

    updated_id = to_update['alert_uuid']
    res = await db(AlertModel).get_by_id(updated_id)
    if res is not None:
        print(f"Get by id after update = {_id}: {res.to_primitive()}")

    filter_obj = Or(Compare(AlertModel.primary_key, "=", 1), Compare(AlertModel.primary_key, "=", 2),
                    Compare(AlertModel.primary_key, "=", 4))
    res = await db(AlertModel).count(filter_obj)
    print(f"Count by filter: {res}")

    res = await db(AlertModel).delete(filter_obj)
    print(f"Deleted by filter: {res}")

    _id = 3
    is_deleted = await db(AlertModel).delete_by_id(_id)
    print(f"Object by id = {_id} was deleted: {is_deleted}")
Exemple #14
0
 async def retrieve_all(self, bundle_id) -> [SupportBundleModel]:
     query = Query().filter_by(
         Compare(SupportBundleModel.bundle_id, '=', bundle_id))
     return await self.db(SupportBundleModel).get(query)