working cli dbupdate (poetry run halfapi dbupdate)

This commit is contained in:
Maxime Alves LIRMM@home 2020-07-04 02:18:40 +02:00
parent 5cad163a39
commit 37104948ca
1 changed files with 102 additions and 82 deletions

View File

@ -97,7 +97,17 @@ def run(host, port, debug, dev, dbname, dbhost, dbport, dbuser, dbpassword):
log_level=log_level,
reload=reload)
def dropdb(dbname, host, port, user, password):
@click.option('--dbname', default='api')
@click.option('--host', default='127.0.0.1')
@click.option('--port', default=5432)
@click.option('--user', default='api')
@click.option('--password', default='')
@click.option('--domain', default='organigramme')
@click.option('--drop', is_flag=True, default=False)
@cli.command()
def dbupdate(dbname, host, port, user, password, domain, drop):
def dropdb():
if not click.confirm(f'Will now drop database {dbname}', default=True):
return False
@ -118,7 +128,7 @@ def dropdb(dbname, host, port, user, password):
return True
def delete_domain(domain):
def delete_domain():
d = Domain(name=domain)
if len(d) < 1:
return False
@ -136,72 +146,82 @@ def delete_domain(domain):
return True
def add_acl_fct(version, domain_name, fct):
acl = Acl()
def add_acl_fct(fct):
acl = AclFunction()
acl.version = version
acl.domain = domain_name
acl.domain = domain
acl.name = fct.__name__
if len(acl) == 0:
acl.insert()
def add_route(version, domain_name, name, **kwargs):
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_name
route.domain = domain
route.path = kwargs['path']
if len(route) == 0:
route.insert()
def add_routes(version, domain_name, routes):
def add_routes_and_acl(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)
add_route(name, **route_params)
add_acl(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()
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()
@click.option('--dbname', default='api')
@click.option('--host', default='127.0.0.1')
@click.option('--port', default=5432)
@click.option('--user', default='api')
@click.option('--password', default='')
@click.option('--domain', default='organigramme')
@click.option('--drop', is_flag=True, default=False)
@cli.command()
def dbupdate(dbname, host, port, user, password, domain, drop):
if drop:
dropdb(dbname, host, port, user, password)
dropdb()
delete_domain(domain)
delete_domain()
acl_set = set()
add_route_acl = lambda routes: [
[ acl_set.add(acl) for acl in route['acl'] ]
for route in routes.keys() ]
try:
# module retrieval
dom_mod = importlib.import_module(domain)
API_VERSION = dom_mod.API_VERSION
add_domain(API_VERSION, domain)
version = dom_mod.API_VERSION
add_domain()
# add main routes
ROUTES = dom_mod.ROUTES
add_route_acl(ROUTES)
add_routes(API_VERSION, domain, dom_mod.ROUTES)
add_routes_and_acl(dom_mod.ROUTES)
# add sub routers
ROUTERS = dom_mod.ROUTERS
for router_name in dom_mod.ROUTERS:
router_mod = importlib.import_module(f'.routers.{router_name}', domain)
add_route_acl(router_mod.ROUTES)
add_routes(API_VERSION, domain, router_mod.ROUTES)
for acl in acl_set:
add_acl_fct(API_VERSION, domain, acl)
add_routes_and_acl(router_mod.ROUTES)
except ImportError:
click.echo(f'The domain {domain} has no *ROUTES* variable', err=True)