# 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)
    try:
        template_db = template_class(client, template)
        logger.debug(f'found template "{template}"')
        db = template_db.clone(name, ttl=ttl)
        logger.debug(f'starting database "{template}" => "{name}"')
        db.start()
    except ImportInProgress:
        logger.error(f'requested template "{template}" not available, import in progress')
        response = jsonify(status="Database not available, content is being imported.")
        response.status_code = 423
        return response
    except NonExistentTemplate:
        logger.error(f'requested template "{template}" not found')
        response = jsonify(status=f'Template "{template}" does not exist.')
        response.status_code = 404
        return response

    response = inspect_database(template, name)
    response.status_code = 201
    return response


@app.route('/db/<template>/<name>', methods=['delete'])
def remove_database(template, name):
    logger.debug(f'requested delete DB "{template}" => "{name}"')
    database_class = get_enabled_backend().Database
    try:
        db = database_class(client, template, name)
        db.remove()
        logger.debug("removed")

# 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)

    try:
        template_db = template_class(client, template)
        logger.debug(f'found template "{template}"')
        db = template_db.clone(name, ttl=ttl)
        logger.debug(f'starting database "{template}" => "{name}"')
        db.start()
    except ImportInProgress:
        logger.error(f'requested template "{template}" not available, import in progress')
        response = jsonify(status="Database not available, content is being imported.")
        response.status_code = 423
        return response
    except NonExistentTemplate:
        logger.error(f'requested template "{template}" not found')
        response = jsonify(status=f'Template "{template}" does not exist.')
        response.status_code = 404
        return response

    response = inspect_database(template, name)
    response.status_code = 201
    return response


@app.route('/db/<template>/<name>', methods=['delete'])
def remove_database(template, name):
    logger.debug(f'requested delete DB "{template}" => "{name}"')
    database_class = get_enabled_backend().Database
    try:
        db = database_class(client, template, name)
        db.remove()
        logger.debug("removed")

    except NonExistentDatabase: