@ -21,12 +21,12 @@ from elasticsearch import helpers as es_helpers
import logging
import uuid
from freezer_api . common . _i18n import _ , _LI
from freezer_api . common import _i18n
from freezer_api . common import exceptions as freezer_api_exc
from freezer_api . common . utils import ActionDoc
from freezer_api . common . utils import BackupMetadataDoc
from freezer_api . common . utils import JobDoc
from freezer_api . common . utils import ActionDoc
from freezer_api . common . utils import SessionDoc
from freezer_api . common import exceptions as freezer_api_exc
class TypeManager :
@ -53,9 +53,9 @@ class TypeManager:
base_filter = TypeManager . get_base_search_filter ( user_id , search )
query_filter = { " filter " : { " bool " : { " must " : base_filter } } }
return { ' query ' : { ' filtered ' : query_filter } }
except :
except Exception :
raise freezer_api_exc . StorageEngineError (
message = _ ( ' search operation failed: query not valid ' ) )
message = _ i18n. _ ( ' search operation failed: query not valid ' ) )
def get ( self , user_id , doc_id ) :
try :
@ -65,13 +65,13 @@ class TypeManager:
doc = res [ ' _source ' ]
except elasticsearch . TransportError :
raise freezer_api_exc . DocumentNotFound (
message = _ ( ' No document found with ID %s ' ) % doc_id )
message = _ i18n. _ ( ' No document found with ID %s ' ) % doc_id )
except Exception as e :
raise freezer_api_exc . StorageEngineError (
message = _ ( ' Get operation failed: %s ' ) % e )
message = _ i18n. _ ( ' Get operation failed: %s ' ) % e )
if doc [ ' user_id ' ] != user_id :
raise freezer_api_exc . AccessForbidden (
_ ( " Document access forbidden " ) )
_ i18n. _ ( " Document access forbidden " ) )
if ' _version ' in res :
doc [ ' _version ' ] = res [ ' _version ' ]
return doc
@ -84,10 +84,10 @@ class TypeManager:
size = limit , from_ = offset , body = query_dsl )
except elasticsearch . ConnectionError :
raise freezer_api_exc . StorageEngineError (
message = _ ( ' unable to connect to db server ' ) )
message = _ i18n. _ ( ' unable to connect to db server ' ) )
except Exception as e :
raise freezer_api_exc . StorageEngineError (
message = _ ( ' search operation failed: %s ' ) % e )
message = _ i18n. _ ( ' search operation failed: %s ' ) % e )
hit_list = res [ ' hits ' ] [ ' hits ' ]
return [ x [ ' _source ' ] for x in hit_list ]
@ -102,10 +102,10 @@ class TypeManager:
if e . status_code == 409 :
raise freezer_api_exc . DocumentExists ( message = e . error )
raise freezer_api_exc . StorageEngineError (
message = _ ( ' index operation failed %s ' ) % e )
message = _ i18n. _ ( ' index operation failed %s ' ) % e )
except Exception as e :
raise freezer_api_exc . StorageEngineError (
message = _ ( ' index operation failed %s ' ) % e )
message = _ i18n. _ ( ' index operation failed %s ' ) % e )
return ( created , version )
def delete ( self , user_id , doc_id ) :
@ -115,7 +115,7 @@ class TypeManager:
doc_type = self . doc_type , query = query_dsl )
except Exception as e :
raise freezer_api_exc . StorageEngineError (
message = _ ( ' Scan operation failed: %s ' ) % e )
message = _ i18n. _ ( ' Scan operation failed: %s ' ) % e )
id = None
for res in results :
id = res . get ( ' _id ' )
@ -123,7 +123,7 @@ class TypeManager:
self . es . delete ( index = self . index , doc_type = self . doc_type , id = id )
except Exception as e :
raise freezer_api_exc . StorageEngineError (
message = _ ( ' Delete operation failed: %s ' ) % e )
message = _ i18n. _ ( ' Delete operation failed: %s ' ) % e )
return id
@ -189,11 +189,11 @@ class JobTypeManager(TypeManager):
if e . status_code == 409 :
raise freezer_api_exc . DocumentExists ( message = e . error )
raise freezer_api_exc . DocumentNotFound (
message = _ ( ' Unable to find job to update '
message = _ i18n. _ ( ' Unable to find job to update '
' with id %(id)s . %(e)s ' ) % { ' id ' : job_id , ' e ' : e } )
except Exception :
raise freezer_api_exc . StorageEngineError (
message = _ ( ' Unable to update job with id %s ' ) % job_id )
message = _ i18n. _ ( ' Unable to update job with id %s ' ) % job_id )
return version
@ -222,11 +222,11 @@ class ActionTypeManager(TypeManager):
if e . status_code == 409 :
raise freezer_api_exc . DocumentExists ( message = e . error )
raise freezer_api_exc . DocumentNotFound (
message = _ ( ' Unable to find action to update '
message = _ i18n. _ ( ' Unable to find action to update '
' with id %s ' ) % action_id )
except Exception :
raise freezer_api_exc . StorageEngineError (
message = _ ( ' Unable to update action with id %s ' ) % action_id )
message = _ i18n. _ ( ' Unable to update action with id %s ' ) % action_id )
return version
@ -255,12 +255,12 @@ class SessionTypeManager(TypeManager):
if e . status_code == 409 :
raise freezer_api_exc . DocumentExists ( message = e . error )
raise freezer_api_exc . DocumentNotFound (
message = _ ( ( ' Unable to update session '
message = _ i18n. _ ( ( ' Unable to update session '
' %s . %s ' ) % ( session_id , e ) ) )
except Exception :
raise freezer_api_exc . StorageEngineError (
message = _ ( ' Unable to update session with id %s ' ) % session_id )
message = _ i18n. _ ( ' Unable to update session with id %s ' ) % session_id )
return version
@ -269,7 +269,7 @@ class ElasticSearchEngine(object):
def __init__ ( self , index = ' freezer ' , * * kwargs ) :
self . index = index
self . es = elasticsearch . Elasticsearch ( * * kwargs )
logging . info ( _ LI( ' Storage backend: Elasticsearch '
logging . info ( _ i18n. _ LI( ' Storage backend: Elasticsearch '
' at %s ' ) % kwargs [ ' hosts ' ] )
self . backup_manager = BackupTypeManager ( self . es , ' backups ' )
self . client_manager = ClientTypeManager ( self . es , ' clients ' )
@ -290,12 +290,12 @@ class ElasticSearchEngine(object):
# raises if data is malformed (HTTP_400) or already present (HTTP_409)
backup_metadata_doc = BackupMetadataDoc ( user_id , user_name , doc )
if not backup_metadata_doc . is_valid ( ) :
raise freezer_api_exc . BadDataFormat ( message = _ ( ' Bad Data Format ' ) )
raise freezer_api_exc . BadDataFormat ( message = _ i18n. _ ( ' Bad Data Format ' ) )
backup_id = backup_metadata_doc . backup_id
existing = self . backup_manager . search ( user_id , backup_id )
if existing :
raise freezer_api_exc . DocumentExists (
message = _ ( ' Backup data already existing with ID %s ' ) %
message = _ i18n. _ ( ' Backup data already existing with ID %s ' ) %
backup_id )
self . backup_manager . insert ( backup_metadata_doc . serialize ( ) )
return backup_id
@ -315,16 +315,16 @@ class ElasticSearchEngine(object):
def add_client ( self , user_id , doc ) :
client_id = doc . get ( ' client_id ' , None )
if client_id is None :
raise freezer_api_exc . BadDataFormat ( message = _ ( ' Missing client ID ' ) )
raise freezer_api_exc . BadDataFormat ( message = _ i18n. _ ( ' Missing client ID ' ) )
existing = self . client_manager . search ( user_id , client_id )
if existing :
raise freezer_api_exc . DocumentExists (
message = _ ( ' Client already registered with ID %s ' ) % client_id )
message = _ i18n. _ ( ' Client already registered with ID %s ' ) % client_id )
client_doc = { ' client ' : doc ,
' user_id ' : user_id ,
' uuid ' : uuid . uuid4 ( ) . hex }
self . client_manager . insert ( client_doc )
logging . info ( _ LI( ' Client registered, client_id: %s ' ) % client_id )
logging . info ( _ i18n. _ LI( ' Client registered, client_id: %s ' ) % client_id )
return client_id
def delete_client ( self , user_id , client_id ) :
@ -344,7 +344,7 @@ class ElasticSearchEngine(object):
jobdoc = JobDoc . create ( doc , user_id )
job_id = jobdoc [ ' job_id ' ]
self . job_manager . insert ( jobdoc , job_id )
logging . info ( _ LI( ' Job registered, job id: %s ' ) % job_id )
logging . info ( _ i18n. _ LI( ' Job registered, job id: %s ' ) % job_id )
return job_id
def delete_job ( self , user_id , job_id ) :
@ -357,7 +357,7 @@ class ElasticSearchEngine(object):
assert ( self . job_manager . get ( user_id , job_id ) )
version = self . job_manager . update ( job_id , valid_patch )
logging . info ( _ LI( ' Job %(id)s updated to version %(version)s ' ) %
logging . info ( _ i18n. _ LI( ' Job %(id)s updated to version %(version)s ' ) %
{ ' id ' : job_id , ' version ' : version } )
return version
@ -373,9 +373,9 @@ class ElasticSearchEngine(object):
( created , version ) = self . job_manager . insert ( valid_doc , job_id )
if created :
logging . info ( _ LI( ' Job %s created ' ) % job_id )
logging . info ( _ i18n. _ LI( ' Job %s created ' ) % job_id )
else :
logging . info ( _ LI( ' Job %(id)s replaced with version %(version)s ' %
logging . info ( _ i18n. _ LI( ' Job %(id)s replaced with version %(version)s ' %
{ ' id ' : job_id , ' version ' : version } ) )
return version
@ -393,7 +393,7 @@ class ElasticSearchEngine(object):
actiondoc = ActionDoc . create ( doc , user_id )
action_id = actiondoc [ ' action_id ' ]
self . action_manager . insert ( actiondoc , action_id )
logging . info ( _ LI( ' Action registered, action id: %s ' ) % action_id )
logging . info ( _ i18n. _ LI( ' Action registered, action id: %s ' ) % action_id )
return action_id
def delete_action ( self , user_id , action_id ) :
@ -406,7 +406,7 @@ class ElasticSearchEngine(object):
assert ( self . action_manager . get ( user_id , action_id ) )
version = self . action_manager . update ( action_id , valid_patch )
logging . info ( _ LI( ' Action %(id)s updated to version %(version)s ' %
logging . info ( _ i18n. _ LI( ' Action %(id)s updated to version %(version)s ' %
{ ' id ' : action_id , ' version ' : version } ) )
return version
@ -422,9 +422,9 @@ class ElasticSearchEngine(object):
( created , version ) = self . action_manager . insert ( valid_doc , action_id )
if created :
logging . info ( _ LI( ' Action %s created ' ) % action_id )
logging . info ( _ i18n. _ LI( ' Action %s created ' ) % action_id )
else :
logging . info ( _ LI( ' Action %(id)s replaced with version %(version)s '
logging . info ( _ i18n. _ LI( ' Action %(id)s replaced with version %(version)s '
% { ' id ' : action_id , ' version ' : version } ) )
return version
@ -442,7 +442,7 @@ class ElasticSearchEngine(object):
session_doc = SessionDoc . create ( doc , user_id )
session_id = session_doc [ ' session_id ' ]
self . session_manager . insert ( session_doc , session_id )
logging . info ( _ LI( ' Session registered, session id: %s ' ) % session_id )
logging . info ( _ i18n. _ LI( ' Session registered, session id: %s ' ) % session_id )
return session_id
def delete_session ( self , user_id , session_id ) :
@ -455,7 +455,7 @@ class ElasticSearchEngine(object):
assert ( self . session_manager . get ( user_id , session_id ) )
version = self . session_manager . update ( session_id , valid_patch )
logging . info ( _ LI( ' Session %(id)s updated to version %(version)s ' %
logging . info ( _ i18n. _ LI( ' Session %(id)s updated to version %(version)s ' %
{ ' id ' : session_id , ' version ' : version } ) )
return version
@ -471,8 +471,8 @@ class ElasticSearchEngine(object):
( created , version ) = self . session_manager . insert ( valid_doc , session_id )
if created :
logging . info ( _ LI( ' Session %s created ' ) % session_id )
logging . info ( _ i18n. _ LI( ' Session %s created ' ) % session_id )
else :
logging . info ( _ LI( ' Session %(id)s replaced with version %(version)s '
logging . info ( _ i18n. _ LI( ' Session %(id)s replaced with version %(version)s '
% { ' id ' : session_id , ' version ' : version } ) )
return version