<s> from functools import wraps <EOL> import time <EOL> import six <EOL> import pyrax <EOL> from pyrax . client import BaseClient <EOL> import pyrax . exceptions as exc <EOL> from pyrax . manager import BaseManager <EOL> from pyrax . resource import BaseResource <EOL> import pyrax . utils as utils <EOL> RETRY_INTERVAL = <NUM_LIT:5> <EOL> def _resolve_id ( val ) : <EOL> """<STR_LIT>""" <EOL> return val if isinstance ( val , six . string_types ) else val . id <EOL> def _resolve_name ( val ) : <EOL> """<STR_LIT>""" <EOL> return val if isinstance ( val , six . string_types ) else val . name <EOL> def assure_volume ( fnc ) : <EOL> """<STR_LIT>""" <EOL> @ wraps ( fnc ) <EOL> def _wrapped ( self , volume , * args , ** kwargs ) : <EOL> if not isinstance ( volume , CloudBlockStorageVolume ) : <EOL> volume = self . _manager . get ( volume ) <EOL> return fnc ( self , volume , * args , ** kwargs ) <EOL> return _wrapped <EOL> def assure_snapshot ( fnc ) : <EOL> """<STR_LIT>""" <EOL> @ wraps ( fnc ) <EOL> def _wrapped ( self , snapshot , * args , ** kwargs ) : <EOL> if not isinstance ( snapshot , CloudBlockStorageSnapshot ) : <EOL> snapshot = self . _snapshot_manager . get ( snapshot ) <EOL> return fnc ( self , snapshot , * args , ** kwargs ) <EOL> return _wrapped <EOL> class CloudBlockStorageSnapshot ( BaseResource ) : <EOL> """<STR_LIT>""" <EOL> def delete ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . status not in ( "<STR_LIT>" , "<STR_LIT:error>" ) : <EOL> raise exc . SnapshotNotAvailable ( "<STR_LIT>" <EOL> "<STR_LIT>" % <EOL> self . status ) <EOL> try : <EOL> super ( CloudBlockStorageSnapshot , self ) . delete ( ) <EOL> except exc . ClientException as e : <EOL> if "<STR_LIT>" in str ( e ) : <EOL> time . sleep ( RETRY_INTERVAL ) <EOL> super ( CloudBlockStorageSnapshot , self ) . delete ( ) <EOL> def update ( self , display_name = None , display_description = None ) : <EOL> """<STR_LIT>""" <EOL> return self . manager . update ( self , display_name = display_name , <EOL> display_description = display_description ) <EOL> def rename ( self , name ) : <EOL> """<STR_LIT>""" <EOL> return self . update ( display_name = name ) <EOL> def _get_name ( self ) : <EOL> return self . display_name <EOL> def _set_name ( self , val ) : <EOL> self . display_name = val <EOL> name = property ( _get_name , _set_name , None , <EOL> "<STR_LIT>" ) <EOL> def _get_description ( self ) : <EOL> return self . display_description <EOL> def _set_description ( self , val ) : <EOL> self . display_description = val <EOL> description = property ( _get_description , _set_description , None , <EOL> "<STR_LIT>" ) <EOL> class CloudBlockStorageVolumeType ( BaseResource ) : <EOL> """<STR_LIT>""" <EOL> pass <EOL> class CloudBlockStorageVolume ( BaseResource ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , * args , ** kwargs ) : <EOL> super ( CloudBlockStorageVolume , self ) . __init__ ( * args , ** kwargs ) <EOL> region = self . manager . api . region_name <EOL> context = self . manager . api . identity <EOL> cs = pyrax . connect_to_cloudservers ( region = region , context = context ) <EOL> self . _nova_volumes = cs . volumes <EOL> def attach_to_instance ( self , instance , mountpoint ) : <EOL> """<STR_LIT>""" <EOL> instance_id = _resolve_id ( instance ) <EOL> try : <EOL> resp = self . _nova_volumes . create_server_volume ( instance_id , <EOL> self . id , mountpoint ) <EOL> except Exception as e : <EOL> raise exc . VolumeAttachmentFailed ( "<STR_LIT:%s>" % e ) <EOL> def detach ( self ) : <EOL> """<STR_LIT>""" <EOL> attachments = self . attachments <EOL> if not attachments : <EOL> return <EOL> att = attachments [ <NUM_LIT:0> ] <EOL> instance_id = att [ "<STR_LIT>" ] <EOL> attachment_id = att [ "<STR_LIT:id>" ] <EOL> try : <EOL> self . _nova_volumes . delete_server_volume ( instance_id , attachment_id ) <EOL> except Exception as e : <EOL> raise exc . VolumeDetachmentFailed ( "<STR_LIT:%s>" % e ) <EOL> def delete ( self , force = False ) : <EOL> """<STR_LIT>""" <EOL> if force : <EOL> self . detach ( ) <EOL> self . delete_all_snapshots ( ) <EOL> try : <EOL> super ( CloudBlockStorageVolume , self ) . delete ( ) <EOL> except exc . VolumeNotAvailable : <EOL> raise <EOL> def update ( self , display_name = None , display_description = None ) : <EOL> """<STR_LIT>""" <EOL> return self . manager . update ( self , display_name = display_name , <EOL> display_description = display_description ) <EOL> def rename ( self , name ) : <EOL> """<STR_LIT>""" <EOL> return self . update ( display_name = name ) <EOL> def create_snapshot ( self , name = None , description = None , force = False ) : <EOL> """<STR_LIT>""" <EOL> name = name or "<STR_LIT>" <EOL> description = description or "<STR_LIT>" <EOL> return self . manager . create_snapshot ( volume = self , name = name , <EOL> description = description , force = force ) <EOL> def list_snapshots ( self ) : <EOL> """<STR_LIT>""" <EOL> return [ snap for snap in self . manager . list_snapshots ( ) <EOL> if snap . volume_id == self . id ] <EOL> def delete_all_snapshots ( self ) : <EOL> """<STR_LIT>""" <EOL> for snap in self . list_snapshots ( ) : <EOL> snap . delete ( ) <EOL> def _get_name ( self ) : <EOL> return self . display_name <EOL> def _set_name ( self , val ) : <EOL> self . display_name = val <EOL> name = property ( _get_name , _set_name , None , <EOL> "<STR_LIT>" ) <EOL> def _get_description ( self ) : <EOL> return self . display_description <EOL> def _set_description ( self , val ) : <EOL> self . display_description = val <EOL> description = property ( _get_description , _set_description , None , <EOL> "<STR_LIT>" ) <EOL> class CloudBlockStorageManager ( BaseManager ) : <EOL> """<STR_LIT>""" <EOL> def _create_body ( self , name , size = None , volume_type = None , description = None , <EOL> metadata = None , snapshot_id = None , clone_id = None , <EOL> availability_zone = None , image = None ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> int ( size ) <EOL> except : <EOL> raise exc . InvalidSize ( "<STR_LIT>" ) <EOL> if volume_type is None : <EOL> volume_type = "<STR_LIT>" <EOL> if description is None : <EOL> description = "<STR_LIT>" <EOL> if metadata is None : <EOL> metadata = { } <EOL> if image is not None : <EOL> image = utils . get_id ( image ) <EOL> body = { "<STR_LIT>" : { <EOL> "<STR_LIT:size>" : size , <EOL> "<STR_LIT>" : snapshot_id , <EOL> "<STR_LIT>" : clone_id , <EOL> "<STR_LIT>" : name , <EOL> "<STR_LIT>" : description , <EOL> "<STR_LIT>" : volume_type , <EOL> "<STR_LIT>" : metadata , <EOL> "<STR_LIT>" : availability_zone , <EOL> "<STR_LIT>" : image , <EOL> } } <EOL> return body <EOL> def create ( self , * args , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> return super ( CloudBlockStorageManager , self ) . create ( * args , <EOL> ** kwargs ) <EOL> except exc . BadRequest as e : <EOL> msg = e . message <EOL> if "<STR_LIT>" in msg : <EOL> raise exc . VolumeCloneTooSmall ( msg ) <EOL> else : <EOL> raise <EOL> def update ( self , volume , display_name = None , display_description = None ) : <EOL> """<STR_LIT>""" <EOL> uri = "<STR_LIT>" % ( self . uri_base , utils . get_id ( volume ) ) <EOL> param_dict = { } <EOL> if display_name : <EOL> param_dict [ "<STR_LIT>" ] = display_name <EOL> if display_description : <EOL> param_dict [ "<STR_LIT>" ] = display_description <EOL> if not param_dict : <EOL> return <EOL> body = { "<STR_LIT>" : param_dict } <EOL> resp , resp_body = self . api . method_put ( uri , body = body ) <EOL> def list_snapshots ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . api . list_snapshots ( ) <EOL> def create_snapshot ( self , volume , name , description = None , force = False ) : <EOL> """<STR_LIT>""" <EOL> return self . api . create_snapshot ( volume , name , description = description , <EOL> force = force ) <EOL> class CloudBlockStorageSnapshotManager ( BaseManager ) : <EOL> """<STR_LIT>""" <EOL> def _create_body ( self , name , description = None , volume = None , force = False ) : <EOL> """<STR_LIT>""" <EOL> body = { "<STR_LIT>" : { <EOL> "<STR_LIT>" : name , <EOL> "<STR_LIT>" : description , <EOL> "<STR_LIT>" : volume . id , <EOL> "<STR_LIT>" : str ( force ) . lower ( ) , <EOL> } } <EOL> return body <EOL> def create ( self , name , volume , description = None , force = False ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> snap = super ( CloudBlockStorageSnapshotManager , self ) . create ( <EOL> name = name , volume = volume , description = description , <EOL> force = force ) <EOL> except exc . BadRequest as e : <EOL> msg = str ( e ) <EOL> if "<STR_LIT>" in msg : <EOL> raise exc . VolumeNotAvailable ( "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> else : <EOL> raise <EOL> except exc . ClientException as e : <EOL> if e . code == <NUM_LIT> : <EOL> if "<STR_LIT>" in str ( e ) : <EOL> txt = ( "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> raise exc . VolumeNotAvailable ( txt ) <EOL> else : <EOL> raise <EOL> else : <EOL> raise <EOL> return snap <EOL> def update ( self , snapshot , display_name = None , display_description = None ) : <EOL> """<STR_LIT>""" <EOL> uri = "<STR_LIT>" % ( self . uri_base , utils . get_id ( snapshot ) ) <EOL> param_dict = { } <EOL> if display_name : <EOL> param_dict [ "<STR_LIT>" ] = display_name <EOL> if display_description : <EOL> param_dict [ "<STR_LIT>" ] = display_description <EOL> if not param_dict : <EOL> return <EOL> body = { "<STR_LIT>" : param_dict } <EOL> resp , resp_body = self . api . method_put ( uri , body = body ) <EOL> class CloudBlockStorageClient ( BaseClient ) : <EOL> """<STR_LIT>""" <EOL> name = "<STR_LIT>" <EOL> def _configure_manager ( self ) : <EOL> """<STR_LIT>""" <EOL> self . _manager = CloudBlockStorageManager ( self , <EOL> resource_class = CloudBlockStorageVolume , response_key = "<STR_LIT>" , <EOL> uri_base = "<STR_LIT>" ) <EOL> self . _types_manager = BaseManager ( self , <EOL> resource_class = CloudBlockStorageVolumeType , <EOL> response_key = "<STR_LIT>" , uri_base = "<STR_LIT>" ) <EOL> self . _snapshot_manager = CloudBlockStorageSnapshotManager ( self , <EOL> resource_class = CloudBlockStorageSnapshot , <EOL> response_key = "<STR_LIT>" , uri_base = "<STR_LIT>" ) <EOL> def list_types ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _types_manager . list ( ) <EOL> def list_snapshots ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _snapshot_manager . list ( ) <EOL> @ assure_volume <EOL> def attach_to_instance ( self , volume , instance , mountpoint ) : <EOL> """<STR_LIT>""" <EOL> return volume . attach_to_instance ( instance , mountpoint ) <EOL> @ assure_volume <EOL> def detach ( self , volume ) : <EOL> """<STR_LIT>""" <EOL> return volume . detach ( ) <EOL> @ assure_volume <EOL> def delete_volume ( self , volume , force = False ) : <EOL> """<STR_LIT>""" <EOL> return volume . delete ( force = force ) <EOL> @ assure_volume <EOL> def update ( self , volume , display_name = None , display_description = None ) : <EOL> """<STR_LIT>""" <EOL> return volume . update ( display_name = display_name , <EOL> display_description = display_description ) <EOL> def rename ( self , volume , name ) : <EOL> """<STR_LIT>""" <EOL> return self . update ( volume , display_name = name ) <EOL> @ assure_volume <EOL> def create_snapshot ( self , volume , name = None , description = None , force = False ) : <EOL> """<STR_LIT>""" <EOL> return self . _snapshot_manager . create ( volume = volume , name = name , <EOL> description = description , force = force ) <EOL> def get_snapshot ( self , snapshot ) : <EOL> """<STR_LIT>""" <EOL> return self . _snapshot_manager . get ( snapshot ) <EOL> @ assure_snapshot <EOL> def delete_snapshot ( self , snapshot ) : <EOL> """<STR_LIT>""" <EOL> return snapshot . delete ( ) <EOL> @ assure_snapshot <EOL> def update_snapshot ( self , snapshot , display_name = None , <EOL> display_description = None ) : <EOL> """<STR_LIT>""" <EOL> return snapshot . update ( display_name = display_name , <EOL> display_description = display_description ) <EOL> def rename_snapshot ( self , snapshot , name ) : <EOL> """<STR_LIT>""" <EOL> return self . update_snapshot ( snapshot , display_name = name ) </s>
