<s> import logging <EOL> from util import full_stack <EOL> from dbaas_nfsaas . provider import NfsaasProvider <EOL> from dbaas_nfsaas . models import HostAttr as nfs_HostAttr <EOL> from workflow . steps . util . base import BaseStep <EOL> from workflow . exceptions . error_codes import DBAAS_0021 <EOL> from workflow . steps . util . restore_snapshot import destroy_unused_export <EOL> LOG = logging . getLogger ( __name__ ) <EOL> class RestoreSnapshot ( BaseStep ) : <EOL> def __unicode__ ( self ) : <EOL> return "<STR_LIT>" <EOL> def do ( self , workflow_dict ) : <EOL> try : <EOL> workflow_dict [ '<STR_LIT>' ] = [ ] <EOL> databaseinfra = workflow_dict [ '<STR_LIT>' ] <EOL> snapshot_id = workflow_dict [ '<STR_LIT>' ] <EOL> nfsaas_export_id = workflow_dict [ '<STR_LIT>' ] <EOL> provider = NfsaasProvider ( ) <EOL> restore_result = provider . restore_snapshot ( environment = databaseinfra . environment , <EOL> export_id = nfsaas_export_id , <EOL> snapshot_id = snapshot_id ) <EOL> job_result = provider . check_restore_nfsaas_job ( environment = databaseinfra . environment , <EOL> job_id = restore_result [ '<STR_LIT>' ] ) <EOL> if '<STR_LIT:id>' in job_result [ '<STR_LIT:result>' ] : <EOL> new_export_id = job_result [ '<STR_LIT:result>' ] [ '<STR_LIT:id>' ] <EOL> new_export_path = job_result [ '<STR_LIT:result>' ] [ '<STR_LIT:path>' ] <EOL> else : <EOL> raise Exception ( '<STR_LIT>' ) <EOL> host = workflow_dict [ '<STR_LIT:host>' ] <EOL> workflow_dict [ '<STR_LIT>' ] . append ( { <EOL> '<STR_LIT:host>' : host , <EOL> '<STR_LIT>' : workflow_dict [ '<STR_LIT>' ] , <EOL> '<STR_LIT>' : workflow_dict [ '<STR_LIT>' ] , <EOL> '<STR_LIT>' : new_export_id , <EOL> '<STR_LIT>' : new_export_path , <EOL> } ) <EOL> old_host_attr = nfs_HostAttr . objects . get ( nfsaas_export_id = nfsaas_export_id ) <EOL> new_host_attr = nfs_HostAttr ( ) <EOL> new_host_attr . host = old_host_attr . host <EOL> new_host_attr . nfsaas_export_id = new_export_id <EOL> new_host_attr . nfsaas_path = new_export_path <EOL> new_host_attr . is_active = False <EOL> new_host_attr . nfsaas_team_id = old_host_attr . nfsaas_team_id <EOL> new_host_attr . nfsaas_project_id = old_host_attr . nfsaas_project_id <EOL> new_host_attr . nfsaas_environment_id = old_host_attr . nfsaas_environment_id <EOL> new_host_attr . nfsaas_size_id = old_host_attr . nfsaas_size_id <EOL> new_host_attr . save ( ) <EOL> restore_result = provider . restore_snapshot ( environment = databaseinfra . environment , <EOL> export_id = nfsaas_export_id , <EOL> snapshot_id = snapshot_id ) <EOL> job_result = provider . check_restore_nfsaas_job ( environment = databaseinfra . environment , <EOL> job_id = restore_result [ '<STR_LIT>' ] ) <EOL> if '<STR_LIT:id>' in job_result [ '<STR_LIT:result>' ] : <EOL> new_export_id = job_result [ '<STR_LIT:result>' ] [ '<STR_LIT:id>' ] <EOL> new_export_path = job_result [ '<STR_LIT:result>' ] [ '<STR_LIT:path>' ] <EOL> else : <EOL> raise Exception ( '<STR_LIT>' ) <EOL> host = workflow_dict [ '<STR_LIT>' ] [ <NUM_LIT:0> ] <EOL> nfs_host_attr = nfs_HostAttr . objects . get ( host = host , is_active = True ) <EOL> workflow_dict [ '<STR_LIT>' ] . append ( { <EOL> '<STR_LIT:host>' : host , <EOL> '<STR_LIT>' : nfs_host_attr . nfsaas_export_id , <EOL> '<STR_LIT>' : nfs_host_attr . nfsaas_path , <EOL> '<STR_LIT>' : new_export_id , <EOL> '<STR_LIT>' : new_export_path , <EOL> } ) <EOL> new_host_attr = nfs_HostAttr ( ) <EOL> new_host_attr . host = old_host_attr . host <EOL> new_host_attr . nfsaas_export_id = new_export_id <EOL> new_host_attr . nfsaas_path = new_export_path <EOL> new_host_attr . is_active = False <EOL> new_host_attr . nfsaas_team_id = old_host_attr . nfsaas_team_id <EOL> new_host_attr . nfsaas_project_id = old_host_attr . nfsaas_project_id <EOL> new_host_attr . nfsaas_environment_id = old_host_attr . nfsaas_environment_id <EOL> new_host_attr . nfsaas_size_id = old_host_attr . nfsaas_size_id <EOL> new_host_attr . save ( ) <EOL> return True <EOL> except Exception : <EOL> traceback = full_stack ( ) <EOL> workflow_dict [ '<STR_LIT>' ] [ '<STR_LIT>' ] . append ( DBAAS_0021 ) <EOL> workflow_dict [ '<STR_LIT>' ] [ '<STR_LIT>' ] . append ( traceback ) <EOL> return False <EOL> def undo ( self , workflow_dict ) : <EOL> LOG . info ( "<STR_LIT>" ) <EOL> try : <EOL> for host_and_export in workflow_dict [ '<STR_LIT>' ] : <EOL> destroy_unused_export ( export_id = host_and_export [ '<STR_LIT>' ] , <EOL> export_path = host_and_export [ <EOL> '<STR_LIT>' ] , <EOL> host = host_and_export [ '<STR_LIT:host>' ] , <EOL> databaseinfra = workflow_dict [ '<STR_LIT>' ] ) <EOL> new_host_attr = nfs_HostAttr . objects . get ( nfsaas_path = host_and_export [ '<STR_LIT>' ] ) <EOL> new_host_attr . delete ( ) <EOL> return True <EOL> except Exception : <EOL> traceback = full_stack ( ) <EOL> workflow_dict [ '<STR_LIT>' ] [ '<STR_LIT>' ] . append ( DBAAS_0021 ) <EOL> workflow_dict [ '<STR_LIT>' ] [ '<STR_LIT>' ] . append ( traceback ) <EOL> return False </s>
