'Tests a sqla connection'

@api
@has_access_api
@expose(u'/testconn', methods=[u'POST', u'GET'])
def testconn(self):

    try:
        uri = request.json.get(u'uri')
        db_name = request.json.get(u'name')
        if db_name:
            database = db.session.query(models.Database).filter_by(database_name=db_name).first()
            if (database and (uri == database.safe_sqlalchemy_uri())):
                uri = database.sqlalchemy_uri_decrypted
        connect_args = request.json.get(u'extras', {}).get(u'engine_params', {}).get(u'connect_args', {})
        engine = create_engine(uri, connect_args=connect_args)
        engine.connect()
        return json_success(json.dumps(engine.table_names(), indent=4))
    except Exception as e:
        logging.exception(e)
        return json_error_response(u'Connection    failed!\n\nThe    error    message    returned    was:\n{}'.format(e))
