Пример #1
0
 async def get_bundle_status(command):
     """
     Initializes the process for Displaying the Status for Support Bundle.
     :param command: Csm_cli Command Object :type: command
     :return: None
     """
     try:
         bundle_id = command.options.get(const.SB_BUNDLE_ID)
         conf = GeneralConfig(database.DATABASE)
         db = DataBaseProvider(conf)
         repo = SupportBundleRepository(db)
         all_nodes_status = await repo.retrieve_all(bundle_id)
         response = {
             "status": [
                 each_status.to_primitive()
                 for each_status in all_nodes_status
             ]
         }
         return Response(output=response, rc=OPERATION_SUCESSFUL)
     except DataAccessExternalError as e:
         Log.warn(f"Failed to connect to elasticsearch: {e}")
         return Response(
             output=("Support Bundle status is not available currently"
                     " as required services are not running."
                     " Please wait and check the /tmp/support_bundle"
                     " folder for newly generated support bundle."),
             rc=str(errno.ECONNREFUSED))
     except Exception as e:
         Log.error(f"Failed to get bundle status: {e}")
         return Response(
             output=("Support Bundle status is not available currently"
                     " as required services are not running."
                     " Failed to get status of bundle."),
             rc=str(errno.ENOENT))
Пример #2
0
 def __init__(self) -> None:
     """
     Init load consul db for storing key in db
     """
     DbConf.init(general_const.CLUSTER_CONF)
     dict_conf = DbConf.export_database_conf()
     conf = GeneralConfig(dict_conf)
     self.storage = DataBaseProvider(conf)
Пример #3
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}")
Пример #5
0
 def __init__(self) -> None:
     """Init load consul db for storing key in db."""
     conf = GeneralConfig(database.DATABASE)
     self.storage = DataBaseProvider(conf)
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 * '-')