begin cli work
This commit is contained in:
parent
2ec6311ca9
commit
966ad7aee8
1
.gitignore
vendored
1
.gitignore
vendored
@ -2,4 +2,5 @@ __pycache__/
|
||||
ENV
|
||||
api/config/dbconfig.ini
|
||||
api/config/authservice.pub
|
||||
cli/config/dbconfig.ini
|
||||
|
||||
|
46
cli/db.py
Normal file
46
cli/db.py
Normal file
@ -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)
|
62
cli/hv2cli.py
Normal file
62
cli/hv2cli.py
Normal file
@ -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()
|
||||
|
||||
|
5
cli/listTenants.py
Normal file
5
cli/listTenants.py
Normal file
@ -0,0 +1,5 @@
|
||||
from db import dbGetMany
|
||||
|
||||
def perform(dbh, params):
|
||||
tenants = dbGetMany(dbh, { "statement": "SELECT * FROM tenant_t", "params": () })
|
||||
print(tenants)
|
Loading…
x
Reference in New Issue
Block a user