예제 #1
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)
예제 #2
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
    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
        ]
예제 #4
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 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
예제 #6
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)
예제 #7
0
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}")
예제 #9
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)
예제 #10
0
async def example():
    conf = GeneralConfig({
        "databases": {
            "openldap": {
                "import_path": "OpenLdap",
                "config": {
                    "hosts": ["127.0.0.1"],
                    "port": 389,
                    "login": "******",
                    "password": "******"
                }
            }
        },
        "models": [
            {
                "import_path": "cortx.utils.data.db.examples.openldap.cortxuser_model.CortxUser",
                "database": "openldap",
                "config": {
                    "openldap": {
                        "collection": "ou=accounts,dc=csm,dc=seagate,dc=com"
                    }
                }
            }]
    })

    SAMPLE_USER = {
        "user_id": "eos22623demo",
        'user_type': 'csm',
        'email': '*****@*****.**',
        'password_hash': 'demopasswd',
        'user_role': 'manage',
        'created_time': datetime.now(),
        'updated_time': datetime.now(),
        'alert_notification': True
    }

    SAMPLE_USER2 = {
        "user_id": "test1",
        'user_type': 'csm',
        'email': '*****@*****.**',
        'password_hash': '987dfsg231sdg234GG',
        'user_role': 'manage',
        'created_time': datetime.strptime("20210801045500Z", '%Y%m%d%H%M%SZ'),
        'updated_time': datetime.now(),
        'alert_notification': True
    }

    SAMPLE_USER3 = {
        "user_id": "test2",
        'user_type': 'csm',
        'email': '*****@*****.**',
        'password_hash': '12358ZHGJDhhasdfG',
        'user_role': 'manage',
        'created_time': datetime.strptime("20200103125530Z", '%Y%m%d%H%M%SZ'),
        'updated_time': datetime.now(),
        'alert_notification': True
    }

    SAMPLE_USER4 = {
        "user_id": "s3sampleuser",
        'user_type': 's3',
        'email': '*****@*****.**',
        'password_hash': 'asghuoanmnxcbg',
        'user_role': 'monitor',
        'created_time': datetime.strptime("20210803120030Z", '%Y%m%d%H%M%SZ'),
        'updated_time': datetime.now(),
        'alert_notification': False
    }

    sample_users = [SAMPLE_USER, SAMPLE_USER2, SAMPLE_USER3, SAMPLE_USER4]

    db = DataBaseProvider(conf)

    items = await db(CortxUser).get(Query())
    print('Users collection')
    print_items(items)
    print(20 * '-')

    for user in sample_users:
        user_obj = CortxUser(user)
        await db(CortxUser).store(user_obj)

    print('Collection status after addition')
    updated_items = await db(CortxUser).get(Query())
    print_items(updated_items)
    print(20 * '-')

    print('Query search')
    filter_obj = And(
        Or(
            Compare(CortxUser.user_role, '=', 'manage'),
            Compare(CortxUser.user_role, "=", "admin")),
        Compare(CortxUser.user_type, '=', 'csm')
    )
    query = Query().filter_by(filter_obj).order_by(CortxUser.user_id, SortOrder.DESC)
    queried_items = await db(CortxUser).get(query)
    print_items(queried_items)
    print(20 * '-')

    print('Query generalized')
    filter_obj = And(
        Compare(CortxUser.created_time, '>=', datetime.strptime('20210827000005Z', '%Y%m%d%H%M%SZ')),
        Compare(CortxUser.alert_notification, '=', True)
    )
    query = Query().filter_by(filter_obj)
    queried_items = await db(CortxUser).get(query)
    print_items(queried_items)
    print(20 * '-')

    filter_obj = Compare(CortxUser.user_type, '=', 's3')
    to_update = {
        'user_role': 'admin'
    }
    num = await db(CortxUser).update(filter_obj, to_update)
    items = await db(CortxUser).get(Query())
    print(f'Updated - {num}')
    print_items(items)
    print(20 * '-')

    filter_obj = Or(
        Compare(CortxUser.user_type, '=', 's3'),
        Compare(CortxUser.user_id, 'like', 'est'))
    cnt = await db(CortxUser).count(filter_obj)
    print(f'Counted - {cnt}')
    print(20 * '-')

    num = await db(CortxUser).delete(None)
    items = await db(CortxUser).get(Query())
    print(f'Deleted - {num}')
    print_items(items)
    print(20 * '-')