<s> from django . utils . translation import ugettext_lazy as _ <EOL> from horizon import exceptions <EOL> from horizon import forms <EOL> from horizon import workflows <EOL> from openstack_dashboard import api <EOL> class CreateFlavorInfoAction ( workflows . Action ) : <EOL> _flavor_id_regex = ( r'<STR_LIT>' <EOL> r'<STR_LIT>' ) <EOL> _flavor_id_help_text = _ ( "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> name = forms . RegexField ( label = _ ( "<STR_LIT:Name>" ) , <EOL> max_length = <NUM_LIT> , <EOL> regex = r'<STR_LIT>' , <EOL> error_messages = { '<STR_LIT>' : _ ( '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' ) } ) <EOL> flavor_id = forms . RegexField ( label = _ ( "<STR_LIT>" ) , <EOL> regex = _flavor_id_regex , <EOL> required = False , <EOL> initial = '<STR_LIT>' , <EOL> help_text = _flavor_id_help_text ) <EOL> vcpus = forms . IntegerField ( label = _ ( "<STR_LIT>" ) ) <EOL> memory_mb = forms . IntegerField ( label = _ ( "<STR_LIT>" ) ) <EOL> disk_gb = forms . IntegerField ( label = _ ( "<STR_LIT>" ) ) <EOL> eph_gb = forms . IntegerField ( label = _ ( "<STR_LIT>" ) ) <EOL> swap_mb = forms . IntegerField ( label = _ ( "<STR_LIT>" ) ) <EOL> class Meta : <EOL> name = _ ( "<STR_LIT>" ) <EOL> help_text = _ ( "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> def clean ( self ) : <EOL> cleaned_data = super ( CreateFlavorInfoAction , self ) . clean ( ) <EOL> name = cleaned_data . get ( '<STR_LIT:name>' ) <EOL> flavor_id = cleaned_data . get ( '<STR_LIT>' ) <EOL> try : <EOL> flavors = api . nova . flavor_list ( self . request , None ) <EOL> except Exception : <EOL> flavors = [ ] <EOL> msg = _ ( '<STR_LIT>' ) <EOL> exceptions . check_message ( [ "<STR_LIT>" , "<STR_LIT>" ] , msg ) <EOL> raise <EOL> if flavors is not None : <EOL> for flavor in flavors : <EOL> if flavor . name == name : <EOL> raise forms . ValidationError ( <EOL> _ ( '<STR_LIT>' ) <EOL> % name <EOL> ) <EOL> if flavor . id == flavor_id : <EOL> raise forms . ValidationError ( <EOL> _ ( '<STR_LIT>' ) <EOL> % flavor_id <EOL> ) <EOL> return cleaned_data <EOL> class CreateFlavorInfo ( workflows . Step ) : <EOL> action_class = CreateFlavorInfoAction <EOL> contributes = ( "<STR_LIT>" , <EOL> "<STR_LIT:name>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" ) <EOL> class UpdateFlavorAccessAction ( workflows . MembershipAction ) : <EOL> def __init__ ( self , request , * args , ** kwargs ) : <EOL> super ( UpdateFlavorAccessAction , self ) . __init__ ( request , <EOL> * args , <EOL> ** kwargs ) <EOL> err_msg = _ ( '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> context = args [ <NUM_LIT:0> ] <EOL> default_role_field_name = self . get_default_role_field_name ( ) <EOL> self . fields [ default_role_field_name ] = forms . CharField ( required = False ) <EOL> self . fields [ default_role_field_name ] . initial = '<STR_LIT>' <EOL> field_name = self . get_member_field_name ( '<STR_LIT>' ) <EOL> self . fields [ field_name ] = forms . MultipleChoiceField ( required = False ) <EOL> all_projects = [ ] <EOL> try : <EOL> all_projects , has_more = api . keystone . tenant_list ( request ) <EOL> except Exception : <EOL> exceptions . handle ( request , err_msg ) <EOL> projects_list = [ ( project . id , project . name ) <EOL> for project in all_projects ] <EOL> self . fields [ field_name ] . choices = projects_list <EOL> if request . method == '<STR_LIT:POST>' : <EOL> return <EOL> flavor_id = context . get ( '<STR_LIT>' ) <EOL> flavor_access = [ ] <EOL> try : <EOL> if flavor_id : <EOL> flavor = api . nova . flavor_get ( request , flavor_id ) <EOL> if not flavor . is_public : <EOL> flavor_access = [ project . tenant_id for project in <EOL> api . nova . flavor_access_list ( request , flavor_id ) ] <EOL> except Exception : <EOL> exceptions . handle ( request , err_msg ) <EOL> self . fields [ field_name ] . initial = flavor_access <EOL> class Meta : <EOL> name = _ ( "<STR_LIT>" ) <EOL> slug = "<STR_LIT>" <EOL> class UpdateFlavorAccess ( workflows . UpdateMembersStep ) : <EOL> action_class = UpdateFlavorAccessAction <EOL> help_text = _ ( "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> available_list_title = _ ( "<STR_LIT>" ) <EOL> members_list_title = _ ( "<STR_LIT>" ) <EOL> no_available_text = _ ( "<STR_LIT>" ) <EOL> no_members_text = _ ( "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> show_roles = False <EOL> depends_on = ( "<STR_LIT>" , ) <EOL> contributes = ( "<STR_LIT>" , ) <EOL> def contribute ( self , data , context ) : <EOL> if data : <EOL> member_field_name = self . get_member_field_name ( '<STR_LIT>' ) <EOL> context [ '<STR_LIT>' ] = data . get ( member_field_name , [ ] ) <EOL> return context <EOL> class CreateFlavor ( workflows . Workflow ) : <EOL> slug = "<STR_LIT>" <EOL> name = _ ( "<STR_LIT>" ) <EOL> finalize_button_name = _ ( "<STR_LIT>" ) <EOL> success_message = _ ( '<STR_LIT>' ) <EOL> failure_message = _ ( '<STR_LIT>' ) <EOL> success_url = "<STR_LIT>" <EOL> default_steps = ( CreateFlavorInfo , <EOL> UpdateFlavorAccess ) <EOL> def format_status_message ( self , message ) : <EOL> return message % self . context [ '<STR_LIT:name>' ] <EOL> def handle ( self , request , data ) : <EOL> flavor_id = data . get ( '<STR_LIT>' ) or '<STR_LIT>' <EOL> flavor_access = data [ '<STR_LIT>' ] <EOL> is_public = not flavor_access <EOL> try : <EOL> self . object = api . nova . flavor_create ( request , <EOL> name = data [ '<STR_LIT:name>' ] , <EOL> memory = data [ '<STR_LIT>' ] , <EOL> vcpu = data [ '<STR_LIT>' ] , <EOL> disk = data [ '<STR_LIT>' ] , <EOL> ephemeral = data [ '<STR_LIT>' ] , <EOL> swap = data [ '<STR_LIT>' ] , <EOL> flavorid = flavor_id , <EOL> is_public = is_public ) <EOL> except Exception : <EOL> exceptions . handle ( request , _ ( '<STR_LIT>' ) ) <EOL> return False <EOL> flavor_id = self . object . id <EOL> for project in flavor_access : <EOL> try : <EOL> api . nova . add_tenant_to_flavor ( <EOL> request , flavor_id , project ) <EOL> except Exception : <EOL> exceptions . handle ( request , <EOL> _ ( '<STR_LIT>' ) % project ) <EOL> return True <EOL> class UpdateFlavorInfoAction ( CreateFlavorInfoAction ) : <EOL> flavor_id = forms . CharField ( widget = forms . widgets . HiddenInput ) <EOL> class Meta : <EOL> name = _ ( "<STR_LIT>" ) <EOL> slug = '<STR_LIT>' <EOL> help_text = _ ( "<STR_LIT>" ) <EOL> def clean ( self ) : <EOL> name = self . cleaned_data . get ( '<STR_LIT:name>' ) <EOL> flavor_id = self . cleaned_data . get ( '<STR_LIT>' ) <EOL> try : <EOL> flavors = api . nova . flavor_list ( self . request , None ) <EOL> except Exception : <EOL> flavors = [ ] <EOL> msg = _ ( '<STR_LIT>' ) <EOL> exceptions . check_message ( [ "<STR_LIT>" , "<STR_LIT>" ] , msg ) <EOL> raise <EOL> if flavors is not None : <EOL> for flavor in flavors : <EOL> if flavor . name == name and flavor . id != flavor_id : <EOL> raise forms . ValidationError ( <EOL> _ ( '<STR_LIT>' <EOL> '<STR_LIT>' ) % name ) <EOL> return self . cleaned_data <EOL> class UpdateFlavorInfo ( workflows . Step ) : <EOL> action_class = UpdateFlavorInfoAction <EOL> depends_on = ( "<STR_LIT>" , ) <EOL> contributes = ( "<STR_LIT:name>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" ) <EOL> class UpdateFlavor ( workflows . Workflow ) : <EOL> slug = "<STR_LIT>" <EOL> name = _ ( "<STR_LIT>" ) <EOL> finalize_button_name = _ ( "<STR_LIT>" ) <EOL> success_message = _ ( '<STR_LIT>' ) <EOL> failure_message = _ ( '<STR_LIT>' ) <EOL> success_url = "<STR_LIT>" <EOL> default_steps = ( UpdateFlavorInfo , <EOL> UpdateFlavorAccess ) <EOL> def format_status_message ( self , message ) : <EOL> return message % self . context [ '<STR_LIT:name>' ] <EOL> def handle ( self , request , data ) : <EOL> flavor_projects = data [ "<STR_LIT>" ] <EOL> is_public = not flavor_projects <EOL> try : <EOL> flavor_id = data [ '<STR_LIT>' ] <EOL> extras_dict = api . nova . flavor_get_extras ( self . request , <EOL> flavor_id , <EOL> raw = True ) <EOL> api . nova . flavor_delete ( request , flavor_id ) <EOL> flavor = api . nova . flavor_create ( request , <EOL> data [ '<STR_LIT:name>' ] , <EOL> data [ '<STR_LIT>' ] , <EOL> data [ '<STR_LIT>' ] , <EOL> data [ '<STR_LIT>' ] , <EOL> ephemeral = data [ '<STR_LIT>' ] , <EOL> swap = data [ '<STR_LIT>' ] , <EOL> flavorid = flavor_id , <EOL> is_public = is_public ) <EOL> if ( extras_dict ) : <EOL> api . nova . flavor_extra_set ( request , flavor . id , extras_dict ) <EOL> except Exception : <EOL> exceptions . handle ( request , ignore = True ) <EOL> return False <EOL> for project in flavor_projects : <EOL> try : <EOL> api . nova . add_tenant_to_flavor ( request , flavor . id , project ) <EOL> except Exception : <EOL> exceptions . handle ( request , _ ( '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' ) ) <EOL> return True </s>
