# Path: postgraas_server/backends/exceptions.py
# class PostgraasApiException(Exception):
#     """Base exception which should be used by backends instead of their own
#     backend specific exceptions.
# 
#     .. example::
# 
#         try:
#             docker_backend.do_something()
#         except docker.errors.APIError as e:
#             raise PostgraasApiException(str(e))
# 
#     """
# 
#     def __init__(self, message=None):
#         self.message = message
# 
#     def __str__(self):
#         return self.message


import datetime
import logging
import psycopg2
from flask import current_app
from flask_restful import fields, Resource, marshal_with, reqparse, abort
from flask_sqlalchemy import SQLAlchemy
from postgraas_server.backends.exceptions import PostgraasApiException
from contextlib import closing
                    args['postgraas_instance_name']
                )
            )

        db_credentials = {
            "db_name": args['db_name'],
            "db_username": args['db_username'],
            "db_pwd": args['db_pwd'],
            "host": current_app.postgraas_backend.hostname,
            "port": current_app.postgraas_backend.port
        }

        db_entry = DBInstance(
            postgraas_instance_name=args['postgraas_instance_name'],
            db_name=args['db_name'],
            username=db_credentials['db_username'],
            password="",
            hostname=db_credentials['host'],
            port=db_credentials['port']
        )
        if current_app.postgraas_backend.exists(db_entry):
            abort(
                409,
                description="database or user already exists {}, {}".format(
                    args['db_name'], args['db_username']
                )
            )

        try:
            db_entry.container_id = current_app.postgraas_backend.create(db_entry, db_credentials)

# Path: postgraas_server/backends/exceptions.py
# class PostgraasApiException(Exception):
#     """Base exception which should be used by backends instead of their own
#     backend specific exceptions.
# 
#     .. example::
# 
#         try:
#             docker_backend.do_something()
#         except docker.errors.APIError as e:
#             raise PostgraasApiException(str(e))
# 
#     """
# 
#     def __init__(self, message=None):
#         self.message = message
# 
#     def __str__(self):
#         return self.message


# Path: postgraas_server/management_resources.py
import datetime
import logging
import psycopg2
from flask import current_app
from flask_restful import fields, Resource, marshal_with, reqparse, abort
from flask_sqlalchemy import SQLAlchemy
from postgraas_server.backends.exceptions import PostgraasApiException
from contextlib import closing

                    args['postgraas_instance_name']
                )
            )

        db_credentials = {
            "db_name": args['db_name'],
            "db_username": args['db_username'],
            "db_pwd": args['db_pwd'],
            "host": current_app.postgraas_backend.hostname,
            "port": current_app.postgraas_backend.port
        }

        db_entry = DBInstance(
            postgraas_instance_name=args['postgraas_instance_name'],
            db_name=args['db_name'],
            username=db_credentials['db_username'],
            password="",
            hostname=db_credentials['host'],
            port=db_credentials['port']
        )
        if current_app.postgraas_backend.exists(db_entry):
            abort(
                409,
                description="database or user already exists {}, {}".format(
                    args['db_name'], args['db_username']
                )
            )

        try:
            db_entry.container_id = current_app.postgraas_backend.create(db_entry, db_credentials)

        except PostgraasApiException as e: