diff --git a/.gitignore b/.gitignore index e921d7a..2489b8c 100644 --- a/.gitignore +++ b/.gitignore @@ -2,4 +2,5 @@ __pycache__/ ENV api/config/dbconfig.ini api/config/authservice.pub +cli/config/dbconfig.ini diff --git a/cli/db.py b/cli/db.py new file mode 100644 index 0000000..17fb05c --- /dev/null +++ b/cli/db.py @@ -0,0 +1,46 @@ +import psycopg2 +import psycopg2.extras +from loguru import logger + + +class NoDataFoundException(Exception): pass + +class TooMuchDataFoundException(Exception): pass + + +def execDatabaseOperation(dbh, func, params): + cur = None + try: + with dbh.cursor(cursor_factory = psycopg2.extras.RealDictCursor) as cur: + params["params"] = [ v if not v=='' else None for v in params["params"] ] + logger.debug("edo: {}".format(str(params))) + return func(cur, params) + except psycopg2.Error as err: + raise Exception("Error when working on cursor: {}".format(err)) + + + +def _opGetMany(cursor, params): + items = [] + cursor.execute(params["statement"], params["params"]) + for itemObj in cursor: + logger.debug("item received {}".format(str(itemObj))) + items.append(itemObj) + return items + +def dbGetMany(dbh, params): + return execDatabaseOperation(dbh, _opGetMany, params) + +def _opGetOne(cursor, params): + cursor.execute(params["statement"], params["params"]) + itemObj = cursor.fetchone() + logger.debug(f"item received: {itemObj}") + if not itemObj: + raise NoDataFoundException + dummyObj = cursor.fetchone() + if dummyObj: + raise TooMuchDataFoundException + return itemObj + +def dbGetOne(dbh, params): + return execDatabaseOperation(dbh, _opGetOne, params) diff --git a/cli/hv2cli.py b/cli/hv2cli.py new file mode 100644 index 0000000..8733fe5 --- /dev/null +++ b/cli/hv2cli.py @@ -0,0 +1,62 @@ +import psycopg2 +import psycopg2.extras +from loguru import logger +import os +import configparser +import json +import argparse +import importlib + + +DB_USER = "" +DB_PASS = "" +DB_HOST = "" +DB_NAME = "" +try: + DB_USER = os.environ["DB_USER"] + DB_PASS = os.environ["DB_PASS"] + DB_HOST = os.environ["DB_HOST"] + DB_NAME = os.environ["DB_NAME"] +except KeyError: + config = configparser.ConfigParser() + config.read('./config/dbconfig.ini') + DB_USER = config["database"]["user"] + DB_PASS = config["database"]["pass"] + DB_HOST = config["database"]["host"] + DB_NAME = config["database"]["name"] + + +parser = argparse.ArgumentParser(description="hv2cli.py") +parser.add_argument('--operation', '-o', + help='Operation to perform.', + required=True) +parser.add_argument('--params', '-p', + help='JSON string with parameter for the selected operation, default: {}', + required=False, + default="{}") +args = parser.parse_args() +operation = args.operation +params = json.loads(args.params) + + + + +try: + opMod = importlib.import_module(operation) + + dbh = psycopg2.connect(user = DB_USER, password = DB_PASS, + host = DB_HOST, database = DB_NAME, + sslmode = 'require') + dbh.autocommit = False + + with dbh: + opMod.perform(dbh, params) +except psycopg2.Error as err: + raise Exception("Error when working on the database: {}".format(err)) +except Exception as err: + raise err +finally: + if dbh: + dbh.close() + + diff --git a/cli/listTenants.py b/cli/listTenants.py new file mode 100644 index 0000000..6c6e47d --- /dev/null +++ b/cli/listTenants.py @@ -0,0 +1,5 @@ +from db import dbGetMany + +def perform(dbh, params): + tenants = dbGetMany(dbh, { "statement": "SELECT * FROM tenant_t", "params": () }) + print(tenants)