working cli dbupdate (poetry run halfapi dbupdate)
This commit is contained in:
parent
5cad163a39
commit
37104948ca
184
halfapi/cli.py
184
halfapi/cli.py
|
@ -97,73 +97,6 @@ def run(host, port, debug, dev, dbname, dbhost, dbport, dbuser, dbpassword):
|
||||||
log_level=log_level,
|
log_level=log_level,
|
||||||
reload=reload)
|
reload=reload)
|
||||||
|
|
||||||
def dropdb(dbname, host, port, user, password):
|
|
||||||
if not click.confirm(f'Will now drop database {dbname}', default=True):
|
|
||||||
return False
|
|
||||||
|
|
||||||
conn = psycopg2.connect({
|
|
||||||
'dbname': dbname,
|
|
||||||
'host': host,
|
|
||||||
'port': port,
|
|
||||||
'user': user,
|
|
||||||
'password': password
|
|
||||||
})
|
|
||||||
|
|
||||||
cur = conn.cursor()
|
|
||||||
|
|
||||||
cur.execute(f'DROP DATABASE {dbname};')
|
|
||||||
conn.commit()
|
|
||||||
cur.close()
|
|
||||||
conn.close()
|
|
||||||
|
|
||||||
return True
|
|
||||||
|
|
||||||
def delete_domain(domain):
|
|
||||||
d = Domain(name=domain)
|
|
||||||
if len(d) < 1:
|
|
||||||
return False
|
|
||||||
|
|
||||||
acl = Acl(domain=domain)
|
|
||||||
acl.delete()
|
|
||||||
|
|
||||||
fct = AclFunction(domain=domain)
|
|
||||||
fct.delete()
|
|
||||||
|
|
||||||
route = Route(domain=domain)
|
|
||||||
route.delete()
|
|
||||||
|
|
||||||
d.delete()
|
|
||||||
|
|
||||||
return True
|
|
||||||
|
|
||||||
def add_acl_fct(version, domain_name, fct):
|
|
||||||
acl = Acl()
|
|
||||||
acl.version = version
|
|
||||||
acl.domain = domain_name
|
|
||||||
acl.name = fct.__name__
|
|
||||||
acl.insert()
|
|
||||||
|
|
||||||
def add_route(version, domain_name, name, **kwargs):
|
|
||||||
route = Route()
|
|
||||||
route.version = version
|
|
||||||
route.domain = domain_name
|
|
||||||
route.path = kwargs['path']
|
|
||||||
route.insert()
|
|
||||||
|
|
||||||
|
|
||||||
def add_routes(version, domain_name, routes):
|
|
||||||
for name, route_params in routes.items():
|
|
||||||
print(f'Adding route {version}/{domain_name}/{function_name}')
|
|
||||||
add_route(version, domain_name, name, **route_params)
|
|
||||||
|
|
||||||
|
|
||||||
def add_domain(version, domain_name):
|
|
||||||
domain = Domain(name=domain_name)
|
|
||||||
domain.version = version
|
|
||||||
if len(domain) == 0:
|
|
||||||
print(f'New domain {domain_name}')
|
|
||||||
domain.insert()
|
|
||||||
|
|
||||||
@click.option('--dbname', default='api')
|
@click.option('--dbname', default='api')
|
||||||
@click.option('--host', default='127.0.0.1')
|
@click.option('--host', default='127.0.0.1')
|
||||||
@click.option('--port', default=5432)
|
@click.option('--port', default=5432)
|
||||||
|
@ -173,35 +106,122 @@ def add_domain(version, domain_name):
|
||||||
@click.option('--drop', is_flag=True, default=False)
|
@click.option('--drop', is_flag=True, default=False)
|
||||||
@cli.command()
|
@cli.command()
|
||||||
def dbupdate(dbname, host, port, user, password, domain, drop):
|
def dbupdate(dbname, host, port, user, password, domain, drop):
|
||||||
if drop:
|
|
||||||
dropdb(dbname, host, port, user, password)
|
|
||||||
|
|
||||||
delete_domain(domain)
|
def dropdb():
|
||||||
|
if not click.confirm(f'Will now drop database {dbname}', default=True):
|
||||||
|
return False
|
||||||
|
|
||||||
|
conn = psycopg2.connect({
|
||||||
|
'dbname': dbname,
|
||||||
|
'host': host,
|
||||||
|
'port': port,
|
||||||
|
'user': user,
|
||||||
|
'password': password
|
||||||
|
})
|
||||||
|
|
||||||
|
cur = conn.cursor()
|
||||||
|
|
||||||
|
cur.execute(f'DROP DATABASE {dbname};')
|
||||||
|
conn.commit()
|
||||||
|
cur.close()
|
||||||
|
conn.close()
|
||||||
|
|
||||||
|
return True
|
||||||
|
|
||||||
|
def delete_domain():
|
||||||
|
d = Domain(name=domain)
|
||||||
|
if len(d) < 1:
|
||||||
|
return False
|
||||||
|
|
||||||
|
acl = Acl(domain=domain)
|
||||||
|
acl.delete()
|
||||||
|
|
||||||
|
fct = AclFunction(domain=domain)
|
||||||
|
fct.delete()
|
||||||
|
|
||||||
|
route = Route(domain=domain)
|
||||||
|
route.delete()
|
||||||
|
|
||||||
|
d.delete()
|
||||||
|
|
||||||
|
return True
|
||||||
|
|
||||||
|
def add_acl_fct(fct):
|
||||||
|
acl = AclFunction()
|
||||||
|
acl.version = version
|
||||||
|
acl.domain = domain
|
||||||
|
acl.name = fct.__name__
|
||||||
|
if len(acl) == 0:
|
||||||
|
acl.insert()
|
||||||
|
|
||||||
|
def add_acl(name, **kwargs):
|
||||||
|
acl = Acl()
|
||||||
|
acl.version = version
|
||||||
|
acl.domain = domain
|
||||||
|
acl.name = name
|
||||||
|
acl.path = kwargs['path']
|
||||||
|
acl.http_verb = kwargs['verb']
|
||||||
|
for fct in kwargs['acl']:
|
||||||
|
acl.function = fct.__name__
|
||||||
|
|
||||||
|
if len(acl) == 0:
|
||||||
|
if fct is not None:
|
||||||
|
add_acl_fct(fct)
|
||||||
|
|
||||||
|
acl.insert()
|
||||||
|
|
||||||
|
elif fct is None:
|
||||||
|
acl.delete()
|
||||||
|
|
||||||
|
|
||||||
|
def add_route(name, **kwargs):
|
||||||
|
print(f'Adding route {version}/{domain}/{name}')
|
||||||
|
route = Route()
|
||||||
|
route.version = version
|
||||||
|
route.domain = domain
|
||||||
|
route.path = kwargs['path']
|
||||||
|
if len(route) == 0:
|
||||||
|
route.insert()
|
||||||
|
|
||||||
|
def add_routes_and_acl(routes):
|
||||||
|
for name, route_params in routes.items():
|
||||||
|
add_route(name, **route_params)
|
||||||
|
add_acl(name, **route_params)
|
||||||
|
|
||||||
|
|
||||||
|
def add_domain():
|
||||||
|
new_domain = Domain(name=domain)
|
||||||
|
new_domain.version = version
|
||||||
|
if len(new_domain) == 0:
|
||||||
|
print(f'New domain {domain}')
|
||||||
|
new_domain.insert()
|
||||||
|
|
||||||
|
|
||||||
|
if drop:
|
||||||
|
dropdb()
|
||||||
|
|
||||||
|
delete_domain()
|
||||||
|
|
||||||
acl_set = set()
|
acl_set = set()
|
||||||
add_route_acl = lambda routes: [
|
|
||||||
[ acl_set.add(acl) for acl in route['acl'] ]
|
|
||||||
for route in routes.keys() ]
|
|
||||||
|
|
||||||
try:
|
try:
|
||||||
|
|
||||||
|
# module retrieval
|
||||||
dom_mod = importlib.import_module(domain)
|
dom_mod = importlib.import_module(domain)
|
||||||
|
|
||||||
API_VERSION = dom_mod.API_VERSION
|
version = dom_mod.API_VERSION
|
||||||
add_domain(API_VERSION, domain)
|
add_domain()
|
||||||
|
|
||||||
|
# add main routes
|
||||||
ROUTES = dom_mod.ROUTES
|
ROUTES = dom_mod.ROUTES
|
||||||
add_route_acl(ROUTES)
|
add_routes_and_acl(dom_mod.ROUTES)
|
||||||
add_routes(API_VERSION, domain, dom_mod.ROUTES)
|
|
||||||
|
|
||||||
|
# add sub routers
|
||||||
ROUTERS = dom_mod.ROUTERS
|
ROUTERS = dom_mod.ROUTERS
|
||||||
|
|
||||||
for router_name in dom_mod.ROUTERS:
|
for router_name in dom_mod.ROUTERS:
|
||||||
router_mod = importlib.import_module(f'.routers.{router_name}', domain)
|
router_mod = importlib.import_module(f'.routers.{router_name}', domain)
|
||||||
add_route_acl(router_mod.ROUTES)
|
add_routes_and_acl(router_mod.ROUTES)
|
||||||
add_routes(API_VERSION, domain, router_mod.ROUTES)
|
|
||||||
|
|
||||||
for acl in acl_set:
|
|
||||||
add_acl_fct(API_VERSION, domain, acl)
|
|
||||||
|
|
||||||
except ImportError:
|
except ImportError:
|
||||||
click.echo(f'The domain {domain} has no *ROUTES* variable', err=True)
|
click.echo(f'The domain {domain} has no *ROUTES* variable', err=True)
|
||||||
|
|
Loading…
Reference in New Issue