<s> """<STR_LIT>""" <EOL> from django . conf import settings <EOL> from django . core . urlresolvers import reverse <EOL> from django . forms import ValidationError <EOL> from django . template . defaultfilters import filesizeformat <EOL> from django . utils . translation import pgettext_lazy <EOL> from django . utils . translation import ugettext_lazy as _ <EOL> from horizon import exceptions <EOL> from horizon import forms <EOL> from horizon import messages <EOL> from horizon . utils import functions <EOL> from horizon . utils . memoized import memoized <EOL> from openstack_dashboard import api <EOL> from openstack_dashboard . api import cinder <EOL> from openstack_dashboard . api import glance <EOL> from openstack_dashboard . api import nova <EOL> from openstack_dashboard . dashboards . project . images import utils <EOL> from openstack_dashboard . dashboards . project . instances import tables <EOL> from openstack_dashboard . usage import quotas <EOL> IMAGE_BACKEND_SETTINGS = getattr ( settings , '<STR_LIT>' , { } ) <EOL> IMAGE_FORMAT_CHOICES = IMAGE_BACKEND_SETTINGS . get ( '<STR_LIT>' , [ ] ) <EOL> VALID_DISK_FORMATS = ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) <EOL> DEFAULT_CONTAINER_FORMAT = '<STR_LIT>' <EOL> class CreateForm ( forms . SelfHandlingForm ) : <EOL> name = forms . CharField ( max_length = <NUM_LIT:255> , label = _ ( "<STR_LIT>" ) , <EOL> required = False ) <EOL> description = forms . CharField ( max_length = <NUM_LIT:255> , widget = forms . Textarea ( <EOL> attrs = { '<STR_LIT:class>' : '<STR_LIT>' , '<STR_LIT>' : <NUM_LIT:4> } ) , <EOL> label = _ ( "<STR_LIT>" ) , required = False ) <EOL> volume_source_type = forms . ChoiceField ( label = _ ( "<STR_LIT>" ) , <EOL> required = False , <EOL> widget = forms . Select ( attrs = { <EOL> '<STR_LIT:class>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT:source>' } ) ) <EOL> snapshot_source = forms . ChoiceField ( <EOL> label = _ ( "<STR_LIT>" ) , <EOL> widget = forms . SelectWidget ( <EOL> attrs = { '<STR_LIT:class>' : '<STR_LIT>' } , <EOL> data_attrs = ( '<STR_LIT:size>' , '<STR_LIT:name>' ) , <EOL> transform = lambda x : "<STR_LIT>" % ( x . name , x . size ) ) , <EOL> required = False ) <EOL> image_source = forms . ChoiceField ( <EOL> label = _ ( "<STR_LIT>" ) , <EOL> widget = forms . SelectWidget ( <EOL> attrs = { '<STR_LIT:class>' : '<STR_LIT>' } , <EOL> data_attrs = ( '<STR_LIT:size>' , '<STR_LIT:name>' , '<STR_LIT>' ) , <EOL> transform = lambda x : "<STR_LIT>" % ( x . name , filesizeformat ( x . bytes ) ) ) , <EOL> required = False ) <EOL> volume_source = forms . ChoiceField ( <EOL> label = _ ( "<STR_LIT>" ) , <EOL> widget = forms . SelectWidget ( <EOL> attrs = { '<STR_LIT:class>' : '<STR_LIT>' } , <EOL> data_attrs = ( '<STR_LIT:size>' , '<STR_LIT:name>' ) , <EOL> transform = lambda x : "<STR_LIT>" % ( <EOL> x . name , filesizeformat ( x . size * <NUM_LIT> * <NUM_LIT> * <NUM_LIT> ) ) ) , <EOL> required = False ) <EOL> type = forms . ChoiceField ( <EOL> label = _ ( "<STR_LIT>" ) , <EOL> required = False , <EOL> widget = forms . Select ( <EOL> attrs = { '<STR_LIT:class>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT:source>' , <EOL> '<STR_LIT>' : _ ( '<STR_LIT>' ) , <EOL> '<STR_LIT>' : _ ( '<STR_LIT>' ) } ) ) <EOL> size = forms . IntegerField ( min_value = <NUM_LIT:1> , initial = <NUM_LIT:1> , label = _ ( "<STR_LIT>" ) ) <EOL> availability_zone = forms . ChoiceField ( <EOL> label = _ ( "<STR_LIT>" ) , <EOL> required = False , <EOL> widget = forms . Select ( <EOL> attrs = { '<STR_LIT:class>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT:source>' , <EOL> '<STR_LIT>' : _ ( '<STR_LIT>' ) , <EOL> '<STR_LIT>' : _ ( '<STR_LIT>' ) } ) ) <EOL> def prepare_source_fields_if_snapshot_specified ( self , request ) : <EOL> try : <EOL> snapshot = self . get_snapshot ( request , <EOL> request . GET [ "<STR_LIT>" ] ) <EOL> self . fields [ '<STR_LIT:name>' ] . initial = snapshot . name <EOL> self . fields [ '<STR_LIT:size>' ] . initial = snapshot . size <EOL> self . fields [ '<STR_LIT>' ] . choices = ( ( snapshot . id , <EOL> snapshot ) , ) <EOL> try : <EOL> orig_volume = cinder . volume_get ( request , <EOL> snapshot . volume_id ) <EOL> self . fields [ '<STR_LIT:type>' ] . initial = orig_volume . volume_type <EOL> except Exception : <EOL> pass <EOL> self . fields [ '<STR_LIT:size>' ] . help_text = ( <EOL> _ ( '<STR_LIT>' <EOL> '<STR_LIT>' ) % snapshot . size ) <EOL> del self . fields [ '<STR_LIT>' ] <EOL> del self . fields [ '<STR_LIT>' ] <EOL> del self . fields [ '<STR_LIT>' ] <EOL> del self . fields [ '<STR_LIT>' ] <EOL> except Exception : <EOL> exceptions . handle ( request , <EOL> _ ( '<STR_LIT>' ) ) <EOL> def prepare_source_fields_if_image_specified ( self , request ) : <EOL> self . fields [ '<STR_LIT>' ] . choices = self . availability_zones ( request ) <EOL> try : <EOL> image = self . get_image ( request , <EOL> request . GET [ "<STR_LIT>" ] ) <EOL> image . bytes = image . size <EOL> self . fields [ '<STR_LIT:name>' ] . initial = image . name <EOL> min_vol_size = functions . bytes_to_gigabytes ( <EOL> image . size ) <EOL> size_help_text = ( _ ( '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> % filesizeformat ( image . size ) ) <EOL> properties = getattr ( image , '<STR_LIT>' , { } ) <EOL> min_disk_size = ( getattr ( image , '<STR_LIT>' , <NUM_LIT:0> ) or <EOL> properties . get ( '<STR_LIT>' , <NUM_LIT:0> ) ) <EOL> if ( min_disk_size > min_vol_size ) : <EOL> min_vol_size = min_disk_size <EOL> size_help_text = ( _ ( '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> % min_disk_size ) <EOL> self . fields [ '<STR_LIT:size>' ] . initial = min_vol_size <EOL> self . fields [ '<STR_LIT:size>' ] . help_text = size_help_text <EOL> self . fields [ '<STR_LIT>' ] . choices = ( ( image . id , image ) , ) <EOL> del self . fields [ '<STR_LIT>' ] <EOL> del self . fields [ '<STR_LIT>' ] <EOL> del self . fields [ '<STR_LIT>' ] <EOL> except Exception : <EOL> msg = _ ( '<STR_LIT>' ) <EOL> exceptions . handle ( request , msg % request . GET [ '<STR_LIT>' ] ) <EOL> def prepare_source_fields_if_volume_specified ( self , request ) : <EOL> self . fields [ '<STR_LIT>' ] . choices = self . availability_zones ( request ) <EOL> volume = None <EOL> try : <EOL> volume = self . get_volume ( request , request . GET [ "<STR_LIT>" ] ) <EOL> except Exception : <EOL> msg = _ ( '<STR_LIT>' ) <EOL> exceptions . handle ( request , msg % request . GET [ '<STR_LIT>' ] ) <EOL> if volume is not None : <EOL> self . fields [ '<STR_LIT:name>' ] . initial = volume . name <EOL> self . fields [ '<STR_LIT:description>' ] . initial = volume . description <EOL> min_vol_size = volume . size <EOL> size_help_text = ( _ ( '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> % filesizeformat ( volume . size ) ) <EOL> self . fields [ '<STR_LIT:size>' ] . initial = min_vol_size <EOL> self . fields [ '<STR_LIT:size>' ] . help_text = size_help_text <EOL> self . fields [ '<STR_LIT>' ] . choices = ( ( volume . id , volume ) , ) <EOL> self . fields [ '<STR_LIT:type>' ] . initial = volume . type <EOL> del self . fields [ '<STR_LIT>' ] <EOL> del self . fields [ '<STR_LIT>' ] <EOL> del self . fields [ '<STR_LIT>' ] <EOL> def prepare_source_fields_default ( self , request ) : <EOL> source_type_choices = [ ] <EOL> self . fields [ '<STR_LIT>' ] . choices = self . availability_zones ( request ) <EOL> try : <EOL> available = api . cinder . VOLUME_STATE_AVAILABLE <EOL> snapshots = cinder . volume_snapshot_list ( <EOL> request , search_opts = dict ( status = available ) ) <EOL> if snapshots : <EOL> source_type_choices . append ( ( "<STR_LIT>" , <EOL> _ ( "<STR_LIT>" ) ) ) <EOL> choices = [ ( '<STR_LIT>' , _ ( "<STR_LIT>" ) ) ] + [ ( s . id , s ) for s in snapshots ] <EOL> self . fields [ '<STR_LIT>' ] . choices = choices <EOL> else : <EOL> del self . fields [ '<STR_LIT>' ] <EOL> except Exception : <EOL> exceptions . handle ( request , <EOL> _ ( "<STR_LIT>" ) ) <EOL> images = utils . get_available_images ( request , <EOL> request . user . tenant_id ) <EOL> if images : <EOL> source_type_choices . append ( ( "<STR_LIT>" , _ ( "<STR_LIT>" ) ) ) <EOL> choices = [ ( '<STR_LIT>' , _ ( "<STR_LIT>" ) ) ] <EOL> for image in images : <EOL> image . bytes = image . size <EOL> image . size = functions . bytes_to_gigabytes ( image . bytes ) <EOL> choices . append ( ( image . id , image ) ) <EOL> self . fields [ '<STR_LIT>' ] . choices = choices <EOL> else : <EOL> del self . fields [ '<STR_LIT>' ] <EOL> volumes = self . get_volumes ( request ) <EOL> if volumes : <EOL> source_type_choices . append ( ( "<STR_LIT>" , _ ( "<STR_LIT>" ) ) ) <EOL> choices = [ ( '<STR_LIT>' , _ ( "<STR_LIT>" ) ) ] <EOL> for volume in volumes : <EOL> choices . append ( ( volume . id , volume ) ) <EOL> self . fields [ '<STR_LIT>' ] . choices = choices <EOL> else : <EOL> del self . fields [ '<STR_LIT>' ] <EOL> if source_type_choices : <EOL> choices = ( [ ( '<STR_LIT>' , <EOL> _ ( "<STR_LIT>" ) ) ] + <EOL> source_type_choices ) <EOL> self . fields [ '<STR_LIT>' ] . choices = choices <EOL> else : <EOL> del self . fields [ '<STR_LIT>' ] <EOL> def __init__ ( self , request , * args , ** kwargs ) : <EOL> super ( CreateForm , self ) . __init__ ( request , * args , ** kwargs ) <EOL> volume_types = cinder . volume_type_list ( request ) <EOL> self . fields [ '<STR_LIT:type>' ] . choices = [ ( "<STR_LIT>" , _ ( "<STR_LIT>" ) ) ] + [ ( type . name , type . name ) <EOL> for type in volume_types ] <EOL> if "<STR_LIT>" in request . GET : <EOL> self . prepare_source_fields_if_snapshot_specified ( request ) <EOL> elif '<STR_LIT>' in request . GET : <EOL> self . prepare_source_fields_if_image_specified ( request ) <EOL> elif '<STR_LIT>' in request . GET : <EOL> self . prepare_source_fields_if_volume_specified ( request ) <EOL> else : <EOL> self . prepare_source_fields_default ( request ) <EOL> def clean ( self ) : <EOL> cleaned_data = super ( CreateForm , self ) . clean ( ) <EOL> source_type = self . cleaned_data . get ( '<STR_LIT>' ) <EOL> if ( source_type == '<STR_LIT>' and <EOL> not cleaned_data . get ( '<STR_LIT>' ) ) : <EOL> msg = _ ( '<STR_LIT>' ) <EOL> self . _errors [ '<STR_LIT>' ] = self . error_class ( [ msg ] ) <EOL> elif ( source_type == '<STR_LIT>' and <EOL> not cleaned_data . get ( '<STR_LIT>' ) ) : <EOL> msg = _ ( '<STR_LIT>' ) <EOL> self . _errors [ '<STR_LIT>' ] = self . error_class ( [ msg ] ) <EOL> elif ( source_type == '<STR_LIT>' and <EOL> not cleaned_data . get ( '<STR_LIT>' ) ) : <EOL> msg = _ ( '<STR_LIT>' ) <EOL> self . _errors [ '<STR_LIT>' ] = self . error_class ( [ msg ] ) <EOL> return cleaned_data <EOL> def cinder_az_supported ( self , request ) : <EOL> try : <EOL> return cinder . extension_supported ( request , '<STR_LIT>' ) <EOL> except Exception : <EOL> exceptions . handle ( request , _ ( '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' ) ) <EOL> return False <EOL> def availability_zones ( self , request ) : <EOL> zone_list = [ ] <EOL> if self . cinder_az_supported ( request ) : <EOL> try : <EOL> zones = api . cinder . availability_zone_list ( request ) <EOL> zone_list = [ ( zone . zoneName , zone . zoneName ) <EOL> for zone in zones if zone . zoneState [ '<STR_LIT>' ] ] <EOL> zone_list . sort ( ) <EOL> except Exception : <EOL> exceptions . handle ( request , _ ( '<STR_LIT>' <EOL> '<STR_LIT>' ) ) <EOL> if not zone_list : <EOL> zone_list . insert ( <NUM_LIT:0> , ( "<STR_LIT>" , _ ( "<STR_LIT>" ) ) ) <EOL> elif len ( zone_list ) > <NUM_LIT:0> : <EOL> zone_list . insert ( <NUM_LIT:0> , ( "<STR_LIT>" , _ ( "<STR_LIT>" ) ) ) <EOL> return zone_list <EOL> def get_volumes ( self , request ) : <EOL> volumes = [ ] <EOL> try : <EOL> available = api . cinder . VOLUME_STATE_AVAILABLE <EOL> volumes = cinder . volume_list ( self . request , <EOL> search_opts = dict ( status = available ) ) <EOL> except Exception : <EOL> exceptions . handle ( request , <EOL> _ ( '<STR_LIT>' ) ) <EOL> return volumes <EOL> def handle ( self , request , data ) : <EOL> try : <EOL> usages = quotas . tenant_limit_usages ( self . request ) <EOL> availableGB = usages [ '<STR_LIT>' ] - usages [ '<STR_LIT>' ] <EOL> availableVol = usages [ '<STR_LIT>' ] - usages [ '<STR_LIT>' ] <EOL> snapshot_id = None <EOL> image_id = None <EOL> volume_id = None <EOL> source_type = data . get ( '<STR_LIT>' , None ) <EOL> az = data . get ( '<STR_LIT>' , None ) or None <EOL> if ( data . get ( "<STR_LIT>" , None ) and <EOL> source_type in [ None , '<STR_LIT>' ] ) : <EOL> snapshot = self . get_snapshot ( request , <EOL> data [ "<STR_LIT>" ] ) <EOL> snapshot_id = snapshot . id <EOL> if ( data [ '<STR_LIT:size>' ] < snapshot . size ) : <EOL> error_message = ( _ ( '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> % snapshot . size ) <EOL> raise ValidationError ( error_message ) <EOL> az = None <EOL> elif ( data . get ( "<STR_LIT>" , None ) and <EOL> source_type in [ None , '<STR_LIT>' ] ) : <EOL> image = self . get_image ( request , <EOL> data [ "<STR_LIT>" ] ) <EOL> image_id = image . id <EOL> image_size = functions . bytes_to_gigabytes ( image . size ) <EOL> if ( data [ '<STR_LIT:size>' ] < image_size ) : <EOL> error_message = ( _ ( '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> % filesizeformat ( image . size ) ) <EOL> raise ValidationError ( error_message ) <EOL> properties = getattr ( image , '<STR_LIT>' , { } ) <EOL> min_disk_size = ( getattr ( image , '<STR_LIT>' , <NUM_LIT:0> ) or <EOL> properties . get ( '<STR_LIT>' , <NUM_LIT:0> ) ) <EOL> if ( min_disk_size > <NUM_LIT:0> and data [ '<STR_LIT:size>' ] < min_disk_size ) : <EOL> error_message = ( _ ( '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> % min_disk_size ) <EOL> raise ValidationError ( error_message ) <EOL> elif ( data . get ( "<STR_LIT>" , None ) and <EOL> source_type in [ None , '<STR_LIT>' ] ) : <EOL> volume = self . get_volume ( request , data [ "<STR_LIT>" ] ) <EOL> volume_id = volume . id <EOL> if data [ '<STR_LIT:size>' ] < volume . size : <EOL> error_message = ( _ ( '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> % volume . size ) <EOL> raise ValidationError ( error_message ) <EOL> else : <EOL> if type ( data [ '<STR_LIT:size>' ] ) is str : <EOL> data [ '<STR_LIT:size>' ] = int ( data [ '<STR_LIT:size>' ] ) <EOL> if availableGB < data [ '<STR_LIT:size>' ] : <EOL> error_message = _ ( '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> params = { '<STR_LIT>' : data [ '<STR_LIT:size>' ] , <EOL> '<STR_LIT>' : availableGB } <EOL> raise ValidationError ( error_message % params ) <EOL> elif availableVol <= <NUM_LIT:0> : <EOL> error_message = _ ( '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> raise ValidationError ( error_message ) <EOL> metadata = { } <EOL> volume = cinder . volume_create ( request , <EOL> data [ '<STR_LIT:size>' ] , <EOL> data [ '<STR_LIT:name>' ] , <EOL> data [ '<STR_LIT:description>' ] , <EOL> data [ '<STR_LIT:type>' ] , <EOL> snapshot_id = snapshot_id , <EOL> image_id = image_id , <EOL> metadata = metadata , <EOL> availability_zone = az , <EOL> source_volid = volume_id ) <EOL> message = _ ( '<STR_LIT>' ) % data [ '<STR_LIT:name>' ] <EOL> messages . info ( request , message ) <EOL> return volume <EOL> except ValidationError as e : <EOL> self . api_error ( e . messages [ <NUM_LIT:0> ] ) <EOL> return False <EOL> except Exception : <EOL> exceptions . handle ( request , ignore = True ) <EOL> self . api_error ( _ ( "<STR_LIT>" ) ) <EOL> return False <EOL> @ memoized <EOL> def get_snapshot ( self , request , id ) : <EOL> return cinder . volume_snapshot_get ( request , id ) <EOL> @ memoized <EOL> def get_image ( self , request , id ) : <EOL> return glance . image_get ( request , id ) <EOL> @ memoized <EOL> def get_volume ( self , request , id ) : <EOL> return cinder . volume_get ( request , id ) <EOL> class AttachForm ( forms . SelfHandlingForm ) : <EOL> instance = forms . ChoiceField ( label = _ ( "<STR_LIT>" ) , <EOL> help_text = _ ( "<STR_LIT>" <EOL> "<STR_LIT>" ) ) <EOL> device = forms . CharField ( label = _ ( "<STR_LIT>" ) , <EOL> widget = forms . TextInput ( attrs = { '<STR_LIT>' : <EOL> '<STR_LIT>' } ) , <EOL> required = False , <EOL> help_text = _ ( "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) ) <EOL> def __init__ ( self , * args , ** kwargs ) : <EOL> super ( AttachForm , self ) . __init__ ( * args , ** kwargs ) <EOL> if not nova . can_set_mount_point ( ) : <EOL> self . fields [ '<STR_LIT>' ] . widget = forms . widgets . HiddenInput ( ) <EOL> volume = kwargs . get ( '<STR_LIT>' , { } ) . get ( "<STR_LIT>" , None ) <EOL> if volume : <EOL> volume_id = volume . id <EOL> else : <EOL> volume_id = None <EOL> self . fields [ '<STR_LIT>' ] = forms . CharField ( widget = forms . HiddenInput ( ) , <EOL> initial = volume_id ) <EOL> instance_list = kwargs . get ( '<STR_LIT>' , { } ) . get ( '<STR_LIT>' , [ ] ) <EOL> instances = [ ] <EOL> for instance in instance_list : <EOL> if instance . status in tables . VOLUME_ATTACH_READY_STATES and not any ( instance . id == att [ "<STR_LIT>" ] <EOL> for att in volume . attachments ) : <EOL> instances . append ( ( instance . id , '<STR_LIT>' % ( instance . name , <EOL> instance . id ) ) ) <EOL> if instances : <EOL> instances . insert ( <NUM_LIT:0> , ( "<STR_LIT>" , _ ( "<STR_LIT>" ) ) ) <EOL> else : <EOL> instances = ( ( "<STR_LIT>" , _ ( "<STR_LIT>" ) ) , ) <EOL> self . fields [ '<STR_LIT>' ] . choices = instances <EOL> def handle ( self , request , data ) : <EOL> instance_choices = dict ( self . fields [ '<STR_LIT>' ] . choices ) <EOL> instance_name = instance_choices . get ( data [ '<STR_LIT>' ] , <EOL> _ ( "<STR_LIT>" ) ) <EOL> instance_name = instance_name . rsplit ( "<STR_LIT>" ) [ <NUM_LIT:0> ] <EOL> try : <EOL> attach = api . nova . instance_volume_attach ( request , <EOL> data [ '<STR_LIT>' ] , <EOL> data [ '<STR_LIT>' ] , <EOL> data . get ( '<STR_LIT>' , '<STR_LIT>' ) ) <EOL> volume = cinder . volume_get ( request , data [ '<STR_LIT>' ] ) <EOL> message = _ ( '<STR_LIT>' <EOL> '<STR_LIT>' ) % { "<STR_LIT>" : volume . name , <EOL> "<STR_LIT>" : instance_name , <EOL> "<STR_LIT>" : attach . device } <EOL> messages . info ( request , message ) <EOL> return True <EOL> except Exception : <EOL> redirect = reverse ( "<STR_LIT>" ) <EOL> exceptions . handle ( request , <EOL> _ ( '<STR_LIT>' ) , <EOL> redirect = redirect ) <EOL> class CreateSnapshotForm ( forms . SelfHandlingForm ) : <EOL> name = forms . CharField ( max_length = <NUM_LIT:255> , label = _ ( "<STR_LIT>" ) ) <EOL> description = forms . CharField ( max_length = <NUM_LIT:255> , <EOL> widget = forms . Textarea ( attrs = { '<STR_LIT>' : <NUM_LIT:4> } ) , <EOL> label = _ ( "<STR_LIT>" ) , <EOL> required = False ) <EOL> def __init__ ( self , request , * args , ** kwargs ) : <EOL> super ( CreateSnapshotForm , self ) . __init__ ( request , * args , ** kwargs ) <EOL> volume_id = kwargs . get ( '<STR_LIT>' , { } ) . get ( '<STR_LIT>' , [ ] ) <EOL> self . fields [ '<STR_LIT>' ] = forms . CharField ( widget = forms . HiddenInput ( ) , <EOL> initial = volume_id ) <EOL> def handle ( self , request , data ) : <EOL> try : <EOL> volume = cinder . volume_get ( request , <EOL> data [ '<STR_LIT>' ] ) <EOL> force = False <EOL> message = _ ( '<STR_LIT>' ) % data [ '<STR_LIT:name>' ] <EOL> if volume . status == '<STR_LIT>' : <EOL> force = True <EOL> message = _ ( '<STR_LIT>' <EOL> '<STR_LIT>' ) % data [ '<STR_LIT:name>' ] <EOL> snapshot = cinder . volume_snapshot_create ( request , <EOL> data [ '<STR_LIT>' ] , <EOL> data [ '<STR_LIT:name>' ] , <EOL> data [ '<STR_LIT:description>' ] , <EOL> force = force ) <EOL> messages . info ( request , message ) <EOL> return snapshot <EOL> except Exception : <EOL> redirect = reverse ( "<STR_LIT>" ) <EOL> exceptions . handle ( request , <EOL> _ ( '<STR_LIT>' ) , <EOL> redirect = redirect ) <EOL> class UpdateForm ( forms . SelfHandlingForm ) : <EOL> name = forms . CharField ( max_length = <NUM_LIT:255> , label = _ ( "<STR_LIT>" ) , <EOL> required = False ) <EOL> description = forms . CharField ( max_length = <NUM_LIT:255> , <EOL> widget = forms . Textarea ( attrs = { '<STR_LIT>' : <NUM_LIT:4> } ) , <EOL> label = _ ( "<STR_LIT>" ) , <EOL> required = False ) <EOL> def handle ( self , request , data ) : <EOL> volume_id = self . initial [ '<STR_LIT>' ] <EOL> try : <EOL> cinder . volume_update ( request , volume_id , data [ '<STR_LIT:name>' ] , <EOL> data [ '<STR_LIT:description>' ] ) <EOL> message = _ ( '<STR_LIT>' ) % data [ '<STR_LIT:name>' ] <EOL> messages . info ( request , message ) <EOL> return True <EOL> except Exception : <EOL> redirect = reverse ( "<STR_LIT>" ) <EOL> exceptions . handle ( request , <EOL> _ ( '<STR_LIT>' ) , <EOL> redirect = redirect ) <EOL> class UploadToImageForm ( forms . SelfHandlingForm ) : <EOL> name = forms . CharField ( label = _ ( '<STR_LIT>' ) , <EOL> widget = forms . TextInput ( <EOL> attrs = { '<STR_LIT>' : '<STR_LIT>' } ) ) <EOL> image_name = forms . CharField ( max_length = <NUM_LIT:255> , label = _ ( '<STR_LIT>' ) ) <EOL> disk_format = forms . ChoiceField ( label = _ ( '<STR_LIT>' ) , <EOL> widget = forms . Select ( ) , <EOL> required = False ) <EOL> force = forms . BooleanField ( <EOL> label = pgettext_lazy ( "<STR_LIT>" , <EOL> u"<STR_LIT>" ) , <EOL> widget = forms . CheckboxInput ( ) , <EOL> required = False ) <EOL> def __init__ ( self , request , * args , ** kwargs ) : <EOL> super ( UploadToImageForm , self ) . __init__ ( request , * args , ** kwargs ) <EOL> disk_format_choices = [ ( value , name ) for value , name <EOL> in IMAGE_FORMAT_CHOICES <EOL> if value in VALID_DISK_FORMATS ] <EOL> self . fields [ '<STR_LIT>' ] . choices = disk_format_choices <EOL> self . fields [ '<STR_LIT>' ] . initial = '<STR_LIT>' <EOL> if self . initial [ '<STR_LIT:status>' ] != '<STR_LIT>' : <EOL> self . fields [ '<STR_LIT>' ] . widget = forms . widgets . HiddenInput ( ) <EOL> def handle ( self , request , data ) : <EOL> volume_id = self . initial [ '<STR_LIT:id>' ] <EOL> try : <EOL> cinder . volume_upload_to_image ( request , <EOL> volume_id , <EOL> data [ '<STR_LIT>' ] , <EOL> data [ '<STR_LIT>' ] , <EOL> DEFAULT_CONTAINER_FORMAT , <EOL> data [ '<STR_LIT>' ] ) <EOL> message = _ ( <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' ) % data [ '<STR_LIT:name>' ] <EOL> messages . info ( request , message ) <EOL> return True <EOL> except Exception : <EOL> error_message = _ ( <EOL> '<STR_LIT>' ) % data [ '<STR_LIT:name>' ] <EOL> exceptions . handle ( request , error_message ) <EOL> return False <EOL> class ExtendForm ( forms . SelfHandlingForm ) : <EOL> name = forms . CharField ( <EOL> label = _ ( "<STR_LIT>" ) , <EOL> widget = forms . TextInput ( attrs = { '<STR_LIT>' : '<STR_LIT>' } ) , <EOL> required = False , <EOL> ) <EOL> orig_size = forms . IntegerField ( <EOL> label = _ ( "<STR_LIT>" ) , <EOL> widget = forms . TextInput ( attrs = { '<STR_LIT>' : '<STR_LIT>' } ) , <EOL> required = False , <EOL> ) <EOL> new_size = forms . IntegerField ( label = _ ( "<STR_LIT>" ) ) <EOL> def clean ( self ) : <EOL> cleaned_data = super ( ExtendForm , self ) . clean ( ) <EOL> new_size = cleaned_data . get ( '<STR_LIT>' ) <EOL> orig_size = self . initial [ '<STR_LIT>' ] <EOL> if new_size <= orig_size : <EOL> raise ValidationError ( <EOL> _ ( "<STR_LIT>" ) ) <EOL> usages = quotas . tenant_limit_usages ( self . request ) <EOL> availableGB = usages [ '<STR_LIT>' ] - usages [ '<STR_LIT>' ] <EOL> if availableGB < ( new_size - orig_size ) : <EOL> message = _ ( '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> params = { '<STR_LIT>' : new_size , '<STR_LIT>' : availableGB } <EOL> self . _errors [ "<STR_LIT>" ] = self . error_class ( [ message % params ] ) <EOL> return cleaned_data <EOL> def handle ( self , request , data ) : <EOL> volume_id = self . initial [ '<STR_LIT:id>' ] <EOL> try : <EOL> volume = cinder . volume_extend ( request , <EOL> volume_id , <EOL> data [ '<STR_LIT>' ] ) <EOL> message = _ ( '<STR_LIT>' ) % data [ '<STR_LIT:name>' ] <EOL> messages . info ( request , message ) <EOL> return volume <EOL> except Exception : <EOL> redirect = reverse ( "<STR_LIT>" ) <EOL> exceptions . handle ( request , <EOL> _ ( '<STR_LIT>' ) , <EOL> redirect = redirect ) <EOL> class RetypeForm ( forms . SelfHandlingForm ) : <EOL> name = forms . CharField ( label = _ ( '<STR_LIT>' ) , <EOL> widget = forms . TextInput ( <EOL> attrs = { '<STR_LIT>' : '<STR_LIT>' } ) ) <EOL> volume_type = forms . ChoiceField ( label = _ ( '<STR_LIT>' ) ) <EOL> MIGRATION_POLICY_CHOICES = [ ( '<STR_LIT>' , _ ( '<STR_LIT>' ) ) , <EOL> ( '<STR_LIT>' , _ ( '<STR_LIT>' ) ) ] <EOL> migration_policy = forms . ChoiceField ( label = _ ( '<STR_LIT>' ) , <EOL> widget = forms . Select ( ) , <EOL> choices = ( MIGRATION_POLICY_CHOICES ) , <EOL> initial = '<STR_LIT>' , <EOL> required = False ) <EOL> def __init__ ( self , request , * args , ** kwargs ) : <EOL> super ( RetypeForm , self ) . __init__ ( request , * args , ** kwargs ) <EOL> try : <EOL> volume_types = cinder . volume_type_list ( request ) <EOL> self . fields [ '<STR_LIT>' ] . choices = [ ( t . name , t . name ) <EOL> for t in volume_types ] <EOL> self . fields [ '<STR_LIT>' ] . initial = self . initial [ '<STR_LIT>' ] <EOL> except Exception : <EOL> redirect_url = reverse ( "<STR_LIT>" ) <EOL> error_message = _ ( '<STR_LIT>' ) <EOL> exceptions . handle ( request , error_message , redirect = redirect_url ) <EOL> def clean_volume_type ( self ) : <EOL> cleaned_volume_type = self . cleaned_data [ '<STR_LIT>' ] <EOL> origin_type = self . initial [ '<STR_LIT>' ] <EOL> if cleaned_volume_type == origin_type : <EOL> error_message = _ ( <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' ) % cleaned_volume_type <EOL> raise ValidationError ( error_message ) <EOL> return cleaned_volume_type <EOL> def handle ( self , request , data ) : <EOL> volume_id = self . initial [ '<STR_LIT:id>' ] <EOL> try : <EOL> cinder . volume_retype ( request , <EOL> volume_id , <EOL> data [ '<STR_LIT>' ] , <EOL> data [ '<STR_LIT>' ] ) <EOL> message = _ ( <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> params = { '<STR_LIT:name>' : data [ '<STR_LIT:name>' ] , <EOL> '<STR_LIT>' : data [ '<STR_LIT>' ] } <EOL> messages . info ( request , message % params ) <EOL> return True <EOL> except Exception : <EOL> error_message = _ ( <EOL> '<STR_LIT>' ) % data [ '<STR_LIT:name>' ] <EOL> exceptions . handle ( request , error_message ) <EOL> return False </s>
