# Path: freezer_api/api/common/utils.py
# LOG = log.getLogger(__name__)
# def inject_context(req, resp, params):
# def before_hooks():
# def json_encode(obj):
# def json_decode(binary):
#     def __init__(self, func):
#     def process_resource(self, req, resp, resource, params=None):
# class FuncMiddleware(object):
# 
# Path: freezer_api/common/_i18n.py

# 
# Path: freezer_api/common/elasticv2_utils.py
# LOG = log.getLogger(__name__)
# class BackupMetadataDoc(object):
# class JobDoc(object):
# class ActionDoc(object):
# class SessionDoc(object):
# class ClientDoc(object):
#     def __init__(self, project_id='', user_id='', user_name='', data={}):
#     def is_valid(self):
#     def serialize(self):
#     def validate(doc):
#     def validate_patch(doc):
#     def create_patch(doc):
#     def create(doc, project_id, user_id):
#     def update(doc, project_id, user_id, job_id):
#     def validate(doc):
#     def validate_patch(doc):
#     def create_patch(doc):
#     def create(doc, user_id, project_id):
#     def update(doc, user_id, action_id, project_id):
#     def validate(doc):
#     def validate_patch(doc):
#     def create_patch(doc):
#     def create(doc, user_id, project_id, hold_off=30):
#     def update(doc, user_id, session_id, project_id):
#     def validate(doc):
#     def create(doc, project_id, user_id):
# 
# Path: freezer_api/common/exceptions.py
# class FreezerAPIException(falcon.HTTPError):
# class BadDataFormat(FreezerAPIException):
# class DocumentExists(FreezerAPIException):
# class StorageEngineError(FreezerAPIException):
# class DocumentNotFound(FreezerAPIException):
# class AccessForbidden(FreezerAPIException):
# class MethodNotImplemented(FreezerAPIException):
#     def __init__(self, message=''):
#     def handle(ex, req, resp, params):
#     def handle(ex, req, resp, params):
#     def handle(ex, req, resp, params):
#     def handle(ex, req, resp, params):
#     def handle(ex, req, resp, params):
#     def handle(ex, req, resp, params):
#     def handle(ex, req, resp, params):
# 
# Path: freezer_api/common/utils.py
# class BackupMetadataDoc(object):
# class JobDoc(object):
# class ActionDoc(object):
# class SessionDoc(object):
# class ClientDoc(object):
#     def __init__(self, user_id='', user_name='', data={}):
#     def is_valid(self):
#     def serialize(self):
#     def validate(doc):
#     def validate_patch(doc):
#     def create_patch(doc):
#     def create(doc, user_id):
#     def update(doc, user_id, job_id):
#     def validate(doc):
#     def validate_patch(doc):
#     def create_patch(doc):
#     def create(doc, user_id):
#     def update(doc, user_id, action_id):
#     def validate(doc):
#     def validate_patch(doc):
#     def create_patch(doc):
#     def create(doc, user_id, hold_off=30):
#     def update(doc, user_id, session_id):
#     def validate(doc):
#     def create(doc, user_id):
# 
# Path: freezer_api/db/sqlalchemy/models.py
# BASE = declarative_base()
# class FreezerBase(models.TimestampMixin,
#                   models.ModelBase):
# class Client(BASE, FreezerBase):
# class Action(BASE, FreezerBase):
# class Session(BASE, FreezerBase):
# class Job(BASE, FreezerBase):
# class ActionReport(BASE, FreezerBase):
# class Backup(BASE, FreezerBase):
#     def delete_values():
#     def delete(self, session):
# def register_models(engine):
# def unregister_models(engine):
# def get_tables(engine):


import sys
from oslo_config import cfg
from oslo_db.sqlalchemy import enginefacade
from oslo_db.sqlalchemy import utils as sqlalchemyutils
from oslo_log import log
from freezer_api.api.common import utils as json_utils
from freezer_api.common._i18n import _
from freezer_api.common import elasticv2_utils as utilsv2
from freezer_api.common import exceptions as freezer_api_exc
from freezer_api.common import utils as utilsv1
from freezer_api.db.sqlalchemy import models

def search_job(user_id, project_id=None, offset=0,
               limit=100, search=None):
    jobs = []
    result, search_key = search_tuple(tablename=models.Job, user_id=user_id,
                                      project_id=project_id, offset=offset,
                                      limit=limit, search=search)
    for job in result:
        jobmap = {}
        jobmap['job_id'] = job.get('id')
        jobmap['project_id'] = job.get('project_id')
        jobmap['user_id'] = job.get('user_id')
        jobmap['job_schedule'] = json_utils.json_decode(job.get('schedule'))
        jobmap['client_id'] = job.get('client_id')
        jobmap['session_id'] = job.get('session_id')
        jobmap['session_tag'] = job.get('session_tag')
        jobmap['description'] = job.get('description')
        jobmap['job_actions'] = json_utils.json_decode(
            job.get('job_actions'))

        jobs.append(jobmap)
    # If search opt is wrong, filter will not work,
    # return all tuples.
    jobs = filter_tuple_by_search_opt(jobs, offset=offset, limit=limit,
                                      search=search_key)
    return jobs


def update_job(user_id, job_id, patch_doc, project_id=None):
    if CONF.enable_v1_api:

# Path: freezer_api/api/common/utils.py
# LOG = log.getLogger(__name__)
# def inject_context(req, resp, params):
# def before_hooks():
# def json_encode(obj):
# def json_decode(binary):
#     def __init__(self, func):
#     def process_resource(self, req, resp, resource, params=None):
# class FuncMiddleware(object):
# 
# Path: freezer_api/common/_i18n.py

# 
# Path: freezer_api/common/elasticv2_utils.py
# LOG = log.getLogger(__name__)
# class BackupMetadataDoc(object):
# class JobDoc(object):
# class ActionDoc(object):
# class SessionDoc(object):
# class ClientDoc(object):
#     def __init__(self, project_id='', user_id='', user_name='', data={}):
#     def is_valid(self):
#     def serialize(self):
#     def validate(doc):
#     def validate_patch(doc):
#     def create_patch(doc):
#     def create(doc, project_id, user_id):
#     def update(doc, project_id, user_id, job_id):
#     def validate(doc):
#     def validate_patch(doc):
#     def create_patch(doc):
#     def create(doc, user_id, project_id):
#     def update(doc, user_id, action_id, project_id):
#     def validate(doc):
#     def validate_patch(doc):
#     def create_patch(doc):
#     def create(doc, user_id, project_id, hold_off=30):
#     def update(doc, user_id, session_id, project_id):
#     def validate(doc):
#     def create(doc, project_id, user_id):
# 
# Path: freezer_api/common/exceptions.py
# class FreezerAPIException(falcon.HTTPError):
# class BadDataFormat(FreezerAPIException):
# class DocumentExists(FreezerAPIException):
# class StorageEngineError(FreezerAPIException):
# class DocumentNotFound(FreezerAPIException):
# class AccessForbidden(FreezerAPIException):
# class MethodNotImplemented(FreezerAPIException):
#     def __init__(self, message=''):
#     def handle(ex, req, resp, params):
#     def handle(ex, req, resp, params):
#     def handle(ex, req, resp, params):
#     def handle(ex, req, resp, params):
#     def handle(ex, req, resp, params):
#     def handle(ex, req, resp, params):
#     def handle(ex, req, resp, params):
# 
# Path: freezer_api/common/utils.py
# class BackupMetadataDoc(object):
# class JobDoc(object):
# class ActionDoc(object):
# class SessionDoc(object):
# class ClientDoc(object):
#     def __init__(self, user_id='', user_name='', data={}):
#     def is_valid(self):
#     def serialize(self):
#     def validate(doc):
#     def validate_patch(doc):
#     def create_patch(doc):
#     def create(doc, user_id):
#     def update(doc, user_id, job_id):
#     def validate(doc):
#     def validate_patch(doc):
#     def create_patch(doc):
#     def create(doc, user_id):
#     def update(doc, user_id, action_id):
#     def validate(doc):
#     def validate_patch(doc):
#     def create_patch(doc):
#     def create(doc, user_id, hold_off=30):
#     def update(doc, user_id, session_id):
#     def validate(doc):
#     def create(doc, user_id):
# 
# Path: freezer_api/db/sqlalchemy/models.py
# BASE = declarative_base()
# class FreezerBase(models.TimestampMixin,
#                   models.ModelBase):
# class Client(BASE, FreezerBase):
# class Action(BASE, FreezerBase):
# class Session(BASE, FreezerBase):
# class Job(BASE, FreezerBase):
# class ActionReport(BASE, FreezerBase):
# class Backup(BASE, FreezerBase):
#     def delete_values():
#     def delete(self, session):
# def register_models(engine):
# def unregister_models(engine):
# def get_tables(engine):


# Path: freezer_api/db/sqlalchemy/api.py
import sys
from oslo_config import cfg
from oslo_db.sqlalchemy import enginefacade
from oslo_db.sqlalchemy import utils as sqlalchemyutils
from oslo_log import log
from freezer_api.api.common import utils as json_utils
from freezer_api.common._i18n import _
from freezer_api.common import elasticv2_utils as utilsv2
from freezer_api.common import exceptions as freezer_api_exc
from freezer_api.common import utils as utilsv1
from freezer_api.db.sqlalchemy import models


def search_job(user_id, project_id=None, offset=0,
               limit=100, search=None):
    jobs = []
    result, search_key = search_tuple(tablename=models.Job, user_id=user_id,
                                      project_id=project_id, offset=offset,
                                      limit=limit, search=search)
    for job in result:
        jobmap = {}
        jobmap['job_id'] = job.get('id')
        jobmap['project_id'] = job.get('project_id')
        jobmap['user_id'] = job.get('user_id')
        jobmap['job_schedule'] = json_utils.json_decode(job.get('schedule'))
        jobmap['client_id'] = job.get('client_id')
        jobmap['session_id'] = job.get('session_id')
        jobmap['session_tag'] = job.get('session_tag')
        jobmap['description'] = job.get('description')
        jobmap['job_actions'] = json_utils.json_decode(
            job.get('job_actions'))

        jobs.append(jobmap)
    # If search opt is wrong, filter will not work,
    # return all tuples.
    jobs = filter_tuple_by_search_opt(jobs, offset=offset, limit=limit,
                                      search=search_key)
    return jobs


def update_job(user_id, job_id, patch_doc, project_id=None):
    if CONF.enable_v1_api:

        valid_patch = utilsv1.JobDoc.create_patch(patch_doc)