# Path: src/myaas/settings.py
# HOSTNAME = config('MYAAS_HOSTNAME', default='localhost')
# 
# DEBUG = config('MYAAS_DEBUG', default=False, cast=bool)
# 
# CONTAINER_TTL = config('MYAAS_CONTAINER_TTL', cast=int, default=86400)
# 
# Path: src/myaas/utils/container.py
# def find_container(name):
# def list_containers(all=True):
# def translate_host_basedir(path):
# def get_random_cpuset(cores_to_assign):
# def get_mapped_cpuset():
# 
# Path: src/myaas/utils/database.py
# def get_myaas_containers():
#     return filter(_is_database_container, list_containers())
# 
# def get_enabled_backend():
#     return importlib.import_module(settings.BACKEND)
# 
# def list_databases():
#     containers = filter(_is_database_container, list_containers())
#     return [_get_database_name(c) for c in containers]
# 
# def list_database_templates():
#     containers = filter(_is_template_container, list_containers())
#     return [_get_database_name(c) for c in containers]
# 
# Path: src/myaas/backends/exceptions.py
# class NonExistentDatabase(Exception):
#     pass
# 
# class NonExistentTemplate(NonExistentDatabase):
#     pass
# 
# class ImportInProgress(Exception):
#     pass


import logging
import os
from flask import Flask, Response, request, jsonify, abort
from .settings import HOSTNAME, DEBUG, CONTAINER_TTL
from .utils.container import client
from .utils.database import (get_myaas_containers, get_enabled_backend,
                             list_databases, list_database_templates)
from .backends.exceptions import (NonExistentDatabase, NonExistentTemplate,
                                  ImportInProgress)

    if request.args.get('all'):
        return jsonify(container=db.inspect())

    result = dict(
        database=db.database,
        host=HOSTNAME,
        name=db.name,
        port=db.host_port,
        user=db.user,
        password=db.password,
        running=db.running(),
        status=db.container['Status'],
        created=db.container['Created'],
    )

    if 'com.myaas.expiresAt' in db.container['Labels']:
        result.update({'expires_at': db.container['Labels']['com.myaas.expiresAt']})

    return jsonify(result)


@app.route('/db/<template>/<name>', methods=['post'])
def create_database(template, name):
    logger.debug(f'requested create DB from "{template}" as "{name}"')
    form_ttl = request.form.get("ttl")
    json_ttl = request.get_json(silent=True)
    json_ttl = json_ttl.get("ttl") if json_ttl else None
    ttl = form_ttl or json_ttl
    ttl = int(ttl) if ttl else CONTAINER_TTL

# Path: src/myaas/settings.py
# HOSTNAME = config('MYAAS_HOSTNAME', default='localhost')
# 
# DEBUG = config('MYAAS_DEBUG', default=False, cast=bool)
# 
# CONTAINER_TTL = config('MYAAS_CONTAINER_TTL', cast=int, default=86400)
# 
# Path: src/myaas/utils/container.py
# def find_container(name):
# def list_containers(all=True):
# def translate_host_basedir(path):
# def get_random_cpuset(cores_to_assign):
# def get_mapped_cpuset():
# 
# Path: src/myaas/utils/database.py
# def get_myaas_containers():
#     return filter(_is_database_container, list_containers())
# 
# def get_enabled_backend():
#     return importlib.import_module(settings.BACKEND)
# 
# def list_databases():
#     containers = filter(_is_database_container, list_containers())
#     return [_get_database_name(c) for c in containers]
# 
# def list_database_templates():
#     containers = filter(_is_template_container, list_containers())
#     return [_get_database_name(c) for c in containers]
# 
# Path: src/myaas/backends/exceptions.py
# class NonExistentDatabase(Exception):
#     pass
# 
# class NonExistentTemplate(NonExistentDatabase):
#     pass
# 
# class ImportInProgress(Exception):
#     pass


# Path: src/myaas/server.py
import logging
import os
from flask import Flask, Response, request, jsonify, abort
from .settings import HOSTNAME, DEBUG, CONTAINER_TTL
from .utils.container import client
from .utils.database import (get_myaas_containers, get_enabled_backend,
                             list_databases, list_database_templates)
from .backends.exceptions import (NonExistentDatabase, NonExistentTemplate,
                                  ImportInProgress)


    if request.args.get('all'):
        return jsonify(container=db.inspect())

    result = dict(
        database=db.database,
        host=HOSTNAME,
        name=db.name,
        port=db.host_port,
        user=db.user,
        password=db.password,
        running=db.running(),
        status=db.container['Status'],
        created=db.container['Created'],
    )

    if 'com.myaas.expiresAt' in db.container['Labels']:
        result.update({'expires_at': db.container['Labels']['com.myaas.expiresAt']})

    return jsonify(result)


@app.route('/db/<template>/<name>', methods=['post'])
def create_database(template, name):
    logger.debug(f'requested create DB from "{template}" as "{name}"')
    form_ttl = request.form.get("ttl")
    json_ttl = request.get_json(silent=True)
    json_ttl = json_ttl.get("ttl") if json_ttl else None
    ttl = form_ttl or json_ttl
    ttl = int(ttl) if ttl else CONTAINER_TTL

    database_class = get_enabled_backend().Database