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


import elasticsearch
import logging
import os
from freezer_api.common import elasticv2_utils as utils
from freezer_api.common import exceptions as freezer_api_exc
from oslo_config import cfg
from oslo_log import log


class JobTypeManagerV2(TypeManagerV2):
    def __init__(self, es, doc_type, index='freezer'):
        TypeManagerV2.__init__(self, es, doc_type, index=index)

    @staticmethod
    def get_search_query(project_id, doc_id, user_id=None, search=None):
        search = search or {}
        base_filter = TypeManagerV2.get_base_search_filter(
            project_id=project_id,
            user_id=user_id,
            search=search
        )
        if doc_id is not None:
            base_filter.append({"term": {"job_id": doc_id}})
        query_filter = {"filter": {"bool": {"must": base_filter}}}
        return {'query': {'filtered': query_filter}}

    def update(self, job_id, job_update_doc):
        # remove _version from the document
        job_update_doc.pop('_version', 0)
        update_doc = {"doc": job_update_doc}
        try:
            res = self.es.update(index=self.index, doc_type=self.doc_type,
                                 id=job_id, body=update_doc)
            version = res['_version']
            self.es.indices.refresh(index=self.index)
        except elasticsearch.TransportError as e:
            if e.status_code == 409:

# 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/storage/elasticv2.py
import elasticsearch
import logging
import os
from freezer_api.common import elasticv2_utils as utils
from freezer_api.common import exceptions as freezer_api_exc
from oslo_config import cfg
from oslo_log import log



class JobTypeManagerV2(TypeManagerV2):
    def __init__(self, es, doc_type, index='freezer'):
        TypeManagerV2.__init__(self, es, doc_type, index=index)

    @staticmethod
    def get_search_query(project_id, doc_id, user_id=None, search=None):
        search = search or {}
        base_filter = TypeManagerV2.get_base_search_filter(
            project_id=project_id,
            user_id=user_id,
            search=search
        )
        if doc_id is not None:
            base_filter.append({"term": {"job_id": doc_id}})
        query_filter = {"filter": {"bool": {"must": base_filter}}}
        return {'query': {'filtered': query_filter}}

    def update(self, job_id, job_update_doc):
        # remove _version from the document
        job_update_doc.pop('_version', 0)
        update_doc = {"doc": job_update_doc}
        try:
            res = self.es.update(index=self.index, doc_type=self.doc_type,
                                 id=job_id, body=update_doc)
            version = res['_version']
            self.es.indices.refresh(index=self.index)
        except elasticsearch.TransportError as e:
            if e.status_code == 409:

                raise freezer_api_exc.DocumentExists(message=e.error)