<s> import logging <EOL> from django . conf import settings <EOL> from django . core . urlresolvers import reverse <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 memoized <EOL> from horizon import workflows <EOL> from openstack_dashboard import api <EOL> from openstack_dashboard . api import cinder <EOL> from openstack_dashboard . api import keystone <EOL> from openstack_dashboard . api import nova <EOL> from openstack_dashboard . usage import quotas <EOL> from openstack_dashboard . utils . identity import IdentityMixIn <EOL> LOG = logging . getLogger ( __name__ ) <EOL> INDEX_URL = "<STR_LIT>" <EOL> ADD_USER_URL = "<STR_LIT>" <EOL> PROJECT_GROUP_ENABLED = keystone . VERSIONS . active >= <NUM_LIT:3> <EOL> PROJECT_USER_MEMBER_SLUG = "<STR_LIT>" <EOL> PROJECT_GROUP_MEMBER_SLUG = "<STR_LIT>" <EOL> COMMON_HORIZONTAL_TEMPLATE = "<STR_LIT>" <EOL> class ProjectQuotaAction ( workflows . Action ) : <EOL> ifcb_label = _ ( "<STR_LIT>" ) <EOL> metadata_items = forms . IntegerField ( min_value = - <NUM_LIT:1> , <EOL> label = _ ( "<STR_LIT>" ) ) <EOL> cores = forms . IntegerField ( min_value = - <NUM_LIT:1> , label = _ ( "<STR_LIT>" ) ) <EOL> instances = forms . IntegerField ( min_value = - <NUM_LIT:1> , label = _ ( "<STR_LIT>" ) ) <EOL> injected_files = forms . IntegerField ( min_value = - <NUM_LIT:1> , <EOL> label = _ ( "<STR_LIT>" ) ) <EOL> injected_file_content_bytes = forms . IntegerField ( min_value = - <NUM_LIT:1> , <EOL> label = ifcb_label ) <EOL> volumes = forms . IntegerField ( min_value = - <NUM_LIT:1> , label = _ ( "<STR_LIT>" ) ) <EOL> snapshots = forms . IntegerField ( min_value = - <NUM_LIT:1> , label = _ ( "<STR_LIT>" ) ) <EOL> gigabytes = forms . IntegerField ( <EOL> min_value = - <NUM_LIT:1> , label = _ ( "<STR_LIT>" ) ) <EOL> ram = forms . IntegerField ( min_value = - <NUM_LIT:1> , label = _ ( "<STR_LIT>" ) ) <EOL> floating_ips = forms . IntegerField ( min_value = - <NUM_LIT:1> , label = _ ( "<STR_LIT>" ) ) <EOL> fixed_ips = forms . IntegerField ( min_value = - <NUM_LIT:1> , label = _ ( "<STR_LIT>" ) ) <EOL> security_groups = forms . IntegerField ( min_value = - <NUM_LIT:1> , <EOL> label = _ ( "<STR_LIT>" ) ) <EOL> security_group_rules = forms . IntegerField ( min_value = - <NUM_LIT:1> , <EOL> label = _ ( "<STR_LIT>" ) ) <EOL> security_group = forms . IntegerField ( min_value = - <NUM_LIT:1> , <EOL> label = _ ( "<STR_LIT>" ) ) <EOL> security_group_rule = forms . IntegerField ( min_value = - <NUM_LIT:1> , <EOL> label = _ ( "<STR_LIT>" ) ) <EOL> floatingip = forms . IntegerField ( min_value = - <NUM_LIT:1> , label = _ ( "<STR_LIT>" ) ) <EOL> network = forms . IntegerField ( min_value = - <NUM_LIT:1> , label = _ ( "<STR_LIT>" ) ) <EOL> port = forms . IntegerField ( min_value = - <NUM_LIT:1> , label = _ ( "<STR_LIT>" ) ) <EOL> router = forms . IntegerField ( min_value = - <NUM_LIT:1> , label = _ ( "<STR_LIT>" ) ) <EOL> subnet = forms . IntegerField ( min_value = - <NUM_LIT:1> , label = _ ( "<STR_LIT>" ) ) <EOL> def __init__ ( self , request , * args , ** kwargs ) : <EOL> super ( ProjectQuotaAction , self ) . __init__ ( request , <EOL> * args , <EOL> ** kwargs ) <EOL> disabled_quotas = quotas . get_disabled_quotas ( request ) <EOL> for field in disabled_quotas : <EOL> if field in self . fields : <EOL> self . fields [ field ] . required = False <EOL> self . fields [ field ] . widget = forms . HiddenInput ( ) <EOL> class UpdateProjectQuotaAction ( ProjectQuotaAction ) : <EOL> def clean ( self ) : <EOL> cleaned_data = super ( UpdateProjectQuotaAction , self ) . clean ( ) <EOL> usages = quotas . tenant_quota_usages ( <EOL> self . request , tenant_id = self . initial [ '<STR_LIT>' ] ) <EOL> bad_values = [ ] <EOL> for key , value in cleaned_data . items ( ) : <EOL> used = usages [ key ] . get ( '<STR_LIT>' , <NUM_LIT:0> ) <EOL> if value is not None and value >= <NUM_LIT:0> and used > value : <EOL> bad_values . append ( _ ( '<STR_LIT>' ) % <EOL> { '<STR_LIT>' : used , <EOL> '<STR_LIT:key>' : quotas . QUOTA_NAMES . get ( key , key ) } ) <EOL> if bad_values : <EOL> value_str = "<STR_LIT:U+002CU+0020>" . join ( bad_values ) <EOL> msg = ( _ ( '<STR_LIT>' <EOL> '<STR_LIT>' ) % <EOL> value_str ) <EOL> raise forms . ValidationError ( msg ) <EOL> return cleaned_data <EOL> class Meta ( object ) : <EOL> name = _ ( "<STR_LIT>" ) <EOL> slug = '<STR_LIT>' <EOL> help_text = _ ( "<STR_LIT>" ) <EOL> permissions = ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> class CreateProjectQuotaAction ( ProjectQuotaAction ) : <EOL> class Meta ( object ) : <EOL> name = _ ( "<STR_LIT>" ) <EOL> slug = '<STR_LIT>' <EOL> help_text = _ ( "<STR_LIT>" ) <EOL> permissions = ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> class UpdateProjectQuota ( workflows . Step ) : <EOL> action_class = UpdateProjectQuotaAction <EOL> template_name = COMMON_HORIZONTAL_TEMPLATE <EOL> depends_on = ( "<STR_LIT>" , ) <EOL> contributes = quotas . QUOTA_FIELDS <EOL> class CreateProjectQuota ( workflows . Step ) : <EOL> action_class = CreateProjectQuotaAction <EOL> template_name = COMMON_HORIZONTAL_TEMPLATE <EOL> depends_on = ( "<STR_LIT>" , ) <EOL> contributes = quotas . QUOTA_FIELDS <EOL> class CreateProjectInfoAction ( workflows . Action ) : <EOL> domain_id = forms . CharField ( label = _ ( "<STR_LIT>" ) , <EOL> required = False , <EOL> widget = forms . HiddenInput ( ) ) <EOL> domain_name = forms . CharField ( label = _ ( "<STR_LIT>" ) , <EOL> required = False , <EOL> widget = forms . HiddenInput ( ) ) <EOL> name = forms . CharField ( label = _ ( "<STR_LIT:Name>" ) , <EOL> max_length = <NUM_LIT:64> ) <EOL> description = forms . CharField ( widget = forms . widgets . Textarea ( <EOL> attrs = { '<STR_LIT>' : <NUM_LIT:4> } ) , <EOL> label = _ ( "<STR_LIT>" ) , <EOL> required = False ) <EOL> enabled = forms . BooleanField ( label = _ ( "<STR_LIT>" ) , <EOL> required = False , <EOL> initial = True ) <EOL> def __init__ ( self , request , * args , ** kwargs ) : <EOL> super ( CreateProjectInfoAction , self ) . __init__ ( request , <EOL> * args , <EOL> ** kwargs ) <EOL> if keystone . VERSIONS . active >= <NUM_LIT:3> : <EOL> readonlyInput = forms . TextInput ( attrs = { '<STR_LIT>' : '<STR_LIT>' } ) <EOL> self . fields [ "<STR_LIT>" ] . widget = readonlyInput <EOL> self . fields [ "<STR_LIT>" ] . widget = readonlyInput <EOL> class Meta ( object ) : <EOL> name = _ ( "<STR_LIT>" ) <EOL> help_text = _ ( "<STR_LIT>" ) <EOL> class CreateProjectInfo ( workflows . Step ) : <EOL> action_class = CreateProjectInfoAction <EOL> template_name = COMMON_HORIZONTAL_TEMPLATE <EOL> contributes = ( "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT:name>" , <EOL> "<STR_LIT:description>" , <EOL> "<STR_LIT>" ) <EOL> class UpdateProjectMembersAction ( workflows . MembershipAction ) : <EOL> def __init__ ( self , request , * args , ** kwargs ) : <EOL> super ( UpdateProjectMembersAction , self ) . __init__ ( request , <EOL> * args , <EOL> ** kwargs ) <EOL> err_msg = _ ( '<STR_LIT>' ) <EOL> domain_id = self . initial . get ( "<STR_LIT>" , None ) <EOL> project_id = '<STR_LIT>' <EOL> if '<STR_LIT>' in self . initial : <EOL> project_id = self . initial [ '<STR_LIT>' ] <EOL> try : <EOL> default_role = keystone . get_default_role ( self . request ) <EOL> if default_role is None : <EOL> default = getattr ( settings , <EOL> "<STR_LIT>" , None ) <EOL> msg = ( _ ( '<STR_LIT>' ) % <EOL> default ) <EOL> raise exceptions . NotFound ( msg ) <EOL> except Exception : <EOL> exceptions . handle ( self . request , <EOL> err_msg , <EOL> redirect = reverse ( INDEX_URL ) ) <EOL> default_role_name = self . get_default_role_field_name ( ) <EOL> self . fields [ default_role_name ] = forms . CharField ( required = False ) <EOL> self . fields [ default_role_name ] . initial = default_role . id <EOL> all_users = [ ] <EOL> try : <EOL> all_users = api . keystone . user_list ( request , <EOL> domain = domain_id ) <EOL> except Exception : <EOL> exceptions . handle ( request , err_msg ) <EOL> users_list = [ ( user . id , user . name ) for user in all_users ] <EOL> role_list = [ ] <EOL> try : <EOL> role_list = api . keystone . role_list ( request ) <EOL> except Exception : <EOL> exceptions . handle ( request , <EOL> err_msg , <EOL> redirect = reverse ( INDEX_URL ) ) <EOL> for role in role_list : <EOL> field_name = self . get_member_field_name ( role . id ) <EOL> label = role . name <EOL> self . fields [ field_name ] = forms . MultipleChoiceField ( required = False , <EOL> label = label ) <EOL> self . fields [ field_name ] . choices = users_list <EOL> self . fields [ field_name ] . initial = [ ] <EOL> if project_id : <EOL> try : <EOL> users_roles = api . keystone . get_project_users_roles ( request , <EOL> project_id ) <EOL> except Exception : <EOL> exceptions . handle ( request , <EOL> err_msg , <EOL> redirect = reverse ( INDEX_URL ) ) <EOL> for user_id in users_roles : <EOL> roles_ids = users_roles [ user_id ] <EOL> for role_id in roles_ids : <EOL> field_name = self . get_member_field_name ( role_id ) <EOL> self . fields [ field_name ] . initial . append ( user_id ) <EOL> class Meta ( object ) : <EOL> name = _ ( "<STR_LIT>" ) <EOL> slug = PROJECT_USER_MEMBER_SLUG <EOL> class UpdateProjectMembers ( workflows . UpdateMembersStep ) : <EOL> action_class = UpdateProjectMembersAction <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> def contribute ( self , data , context ) : <EOL> if data : <EOL> try : <EOL> roles = api . keystone . role_list ( self . workflow . request ) <EOL> except Exception : <EOL> exceptions . handle ( self . workflow . request , <EOL> _ ( '<STR_LIT>' ) ) <EOL> post = self . workflow . request . POST <EOL> for role in roles : <EOL> field = self . get_member_field_name ( role . id ) <EOL> context [ field ] = post . getlist ( field ) <EOL> return context <EOL> class UpdateProjectGroupsAction ( workflows . MembershipAction ) : <EOL> def __init__ ( self , request , * args , ** kwargs ) : <EOL> super ( UpdateProjectGroupsAction , self ) . __init__ ( request , <EOL> * args , <EOL> ** kwargs ) <EOL> err_msg = _ ( '<STR_LIT>' ) <EOL> domain_id = self . initial . get ( "<STR_LIT>" , None ) <EOL> project_id = '<STR_LIT>' <EOL> if '<STR_LIT>' in self . initial : <EOL> project_id = self . initial [ '<STR_LIT>' ] <EOL> try : <EOL> default_role = api . keystone . get_default_role ( self . request ) <EOL> if default_role is None : <EOL> default = getattr ( settings , <EOL> "<STR_LIT>" , None ) <EOL> msg = ( _ ( '<STR_LIT>' ) % <EOL> default ) <EOL> raise exceptions . NotFound ( msg ) <EOL> except Exception : <EOL> exceptions . handle ( self . request , <EOL> err_msg , <EOL> redirect = reverse ( INDEX_URL ) ) <EOL> default_role_name = self . get_default_role_field_name ( ) <EOL> self . fields [ default_role_name ] = forms . CharField ( required = False ) <EOL> self . fields [ default_role_name ] . initial = default_role . id <EOL> all_groups = [ ] <EOL> try : <EOL> all_groups = api . keystone . group_list ( request , <EOL> domain = domain_id ) <EOL> except Exception : <EOL> exceptions . handle ( request , err_msg ) <EOL> groups_list = [ ( group . id , group . name ) for group in all_groups ] <EOL> role_list = [ ] <EOL> try : <EOL> role_list = api . keystone . role_list ( request ) <EOL> except Exception : <EOL> exceptions . handle ( request , <EOL> err_msg , <EOL> redirect = reverse ( INDEX_URL ) ) <EOL> for role in role_list : <EOL> field_name = self . get_member_field_name ( role . id ) <EOL> label = role . name <EOL> self . fields [ field_name ] = forms . MultipleChoiceField ( required = False , <EOL> label = label ) <EOL> self . fields [ field_name ] . choices = groups_list <EOL> self . fields [ field_name ] . initial = [ ] <EOL> if project_id : <EOL> try : <EOL> groups_roles = api . keystone . get_project_groups_roles ( <EOL> request , project_id ) <EOL> except Exception : <EOL> exceptions . handle ( request , <EOL> err_msg , <EOL> redirect = reverse ( INDEX_URL ) ) <EOL> for group_id in groups_roles : <EOL> roles_ids = groups_roles [ group_id ] <EOL> for role_id in roles_ids : <EOL> field_name = self . get_member_field_name ( role_id ) <EOL> self . fields [ field_name ] . initial . append ( group_id ) <EOL> class Meta ( object ) : <EOL> name = _ ( "<STR_LIT>" ) <EOL> slug = PROJECT_GROUP_MEMBER_SLUG <EOL> class UpdateProjectGroups ( workflows . UpdateMembersStep ) : <EOL> action_class = UpdateProjectGroupsAction <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> def contribute ( self , data , context ) : <EOL> if data : <EOL> try : <EOL> roles = api . keystone . role_list ( self . workflow . request ) <EOL> except Exception : <EOL> exceptions . handle ( self . workflow . request , <EOL> _ ( '<STR_LIT>' ) ) <EOL> post = self . workflow . request . POST <EOL> for role in roles : <EOL> field = self . get_member_field_name ( role . id ) <EOL> context [ field ] = post . getlist ( field ) <EOL> return context <EOL> class CommonQuotaWorkflow ( workflows . Workflow ) : <EOL> def _update_project_quota ( self , request , data , project_id ) : <EOL> nova_data = dict ( <EOL> [ ( key , data [ key ] ) for key in quotas . NOVA_QUOTA_FIELDS ] ) <EOL> nova . tenant_quota_update ( request , project_id , ** nova_data ) <EOL> if cinder . is_volume_service_enabled ( request ) : <EOL> cinder_data = dict ( [ ( key , data [ key ] ) for key in <EOL> quotas . CINDER_QUOTA_FIELDS ] ) <EOL> cinder . tenant_quota_update ( request , <EOL> project_id , <EOL> ** cinder_data ) <EOL> if api . base . is_service_enabled ( request , '<STR_LIT>' ) and api . neutron . is_quotas_extension_supported ( request ) : <EOL> neutron_data = { } <EOL> disabled_quotas = quotas . get_disabled_quotas ( request ) <EOL> for key in quotas . NEUTRON_QUOTA_FIELDS : <EOL> if key not in disabled_quotas : <EOL> neutron_data [ key ] = data [ key ] <EOL> api . neutron . tenant_quota_update ( request , <EOL> project_id , <EOL> ** neutron_data ) <EOL> class CreateProject ( CommonQuotaWorkflow ) : <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 = ( CreateProjectInfo , <EOL> UpdateProjectMembers , <EOL> CreateProjectQuota ) <EOL> def __init__ ( self , request = None , context_seed = None , entry_point = None , <EOL> * args , ** kwargs ) : <EOL> if PROJECT_GROUP_ENABLED : <EOL> self . default_steps = ( CreateProjectInfo , <EOL> UpdateProjectMembers , <EOL> UpdateProjectGroups , <EOL> CreateProjectQuota ) <EOL> super ( CreateProject , self ) . __init__ ( request = request , <EOL> context_seed = context_seed , <EOL> entry_point = entry_point , <EOL> * args , <EOL> ** kwargs ) <EOL> def format_status_message ( self , message ) : <EOL> if "<STR_LIT:%s>" in message : <EOL> return message % self . context . get ( '<STR_LIT:name>' , '<STR_LIT>' ) <EOL> else : <EOL> return message <EOL> def _create_project ( self , request , data ) : <EOL> domain_id = data [ '<STR_LIT>' ] <EOL> try : <EOL> desc = data [ '<STR_LIT:description>' ] <EOL> self . object = api . keystone . tenant_create ( request , <EOL> name = data [ '<STR_LIT:name>' ] , <EOL> description = desc , <EOL> enabled = data [ '<STR_LIT>' ] , <EOL> domain = domain_id ) <EOL> return self . object <EOL> except exceptions . Conflict : <EOL> msg = _ ( '<STR_LIT>' ) % data [ '<STR_LIT:name>' ] <EOL> self . failure_message = msg <EOL> return <EOL> except Exception : <EOL> exceptions . handle ( request , ignore = True ) <EOL> return <EOL> def _update_project_members ( self , request , data , project_id ) : <EOL> users_to_add = <NUM_LIT:0> <EOL> try : <EOL> available_roles = api . keystone . role_list ( request ) <EOL> member_step = self . get_step ( PROJECT_USER_MEMBER_SLUG ) <EOL> for role in available_roles : <EOL> field_name = member_step . get_member_field_name ( role . id ) <EOL> role_list = data [ field_name ] <EOL> users_to_add += len ( role_list ) <EOL> for role in available_roles : <EOL> field_name = member_step . get_member_field_name ( role . id ) <EOL> role_list = data [ field_name ] <EOL> users_added = <NUM_LIT:0> <EOL> for user in role_list : <EOL> api . keystone . add_tenant_user_role ( request , <EOL> project = project_id , <EOL> user = user , <EOL> role = role . id ) <EOL> users_added += <NUM_LIT:1> <EOL> users_to_add -= users_added <EOL> except Exception : <EOL> if PROJECT_GROUP_ENABLED : <EOL> group_msg = _ ( "<STR_LIT>" ) <EOL> else : <EOL> group_msg = "<STR_LIT>" <EOL> exceptions . handle ( request , <EOL> _ ( '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> % { '<STR_LIT>' : users_to_add , <EOL> '<STR_LIT>' : group_msg } ) <EOL> def _update_project_groups ( self , request , data , project_id ) : <EOL> groups_to_add = <NUM_LIT:0> <EOL> try : <EOL> available_roles = api . keystone . role_list ( request ) <EOL> member_step = self . get_step ( PROJECT_GROUP_MEMBER_SLUG ) <EOL> for role in available_roles : <EOL> field_name = member_step . get_member_field_name ( role . id ) <EOL> role_list = data [ field_name ] <EOL> groups_to_add += len ( role_list ) <EOL> for role in available_roles : <EOL> field_name = member_step . get_member_field_name ( role . id ) <EOL> role_list = data [ field_name ] <EOL> groups_added = <NUM_LIT:0> <EOL> for group in role_list : <EOL> api . keystone . add_group_role ( request , <EOL> role = role . id , <EOL> group = group , <EOL> project = project_id ) <EOL> groups_added += <NUM_LIT:1> <EOL> groups_to_add -= groups_added <EOL> except Exception : <EOL> exceptions . handle ( request , <EOL> _ ( '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> % groups_to_add ) <EOL> def _update_project_quota ( self , request , data , project_id ) : <EOL> try : <EOL> super ( CreateProject , self ) . _update_project_quota ( <EOL> request , data , project_id ) <EOL> except Exception : <EOL> exceptions . handle ( request , _ ( '<STR_LIT>' ) ) <EOL> def handle ( self , request , data ) : <EOL> project = self . _create_project ( request , data ) <EOL> if not project : <EOL> return False <EOL> project_id = project . id <EOL> self . _update_project_members ( request , data , project_id ) <EOL> if PROJECT_GROUP_ENABLED : <EOL> self . _update_project_groups ( request , data , project_id ) <EOL> if keystone . is_cloud_admin ( request ) : <EOL> self . _update_project_quota ( request , data , project_id ) <EOL> return True <EOL> class CreateProjectNoQuota ( CreateProject ) : <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 = ( CreateProjectInfo , UpdateProjectMembers ) <EOL> def __init__ ( self , request = None , context_seed = None , entry_point = None , <EOL> * args , ** kwargs ) : <EOL> if PROJECT_GROUP_ENABLED : <EOL> self . default_steps = ( CreateProjectInfo , <EOL> UpdateProjectMembers , <EOL> UpdateProjectGroups , ) <EOL> super ( CreateProject , self ) . __init__ ( request = request , <EOL> context_seed = context_seed , <EOL> entry_point = entry_point , <EOL> * args , <EOL> ** kwargs ) <EOL> class UpdateProjectInfoAction ( CreateProjectInfoAction ) : <EOL> enabled = forms . BooleanField ( required = False , label = _ ( "<STR_LIT>" ) ) <EOL> domain_name = forms . CharField ( label = _ ( "<STR_LIT>" ) , <EOL> required = False , <EOL> widget = forms . HiddenInput ( ) ) <EOL> def __init__ ( self , request , initial , * args , ** kwargs ) : <EOL> super ( UpdateProjectInfoAction , self ) . __init__ ( <EOL> request , initial , * args , ** kwargs ) <EOL> if initial [ '<STR_LIT>' ] == request . user . project_id : <EOL> self . fields [ '<STR_LIT>' ] . widget . attrs [ '<STR_LIT>' ] = True <EOL> self . fields [ '<STR_LIT>' ] . help_text = _ ( <EOL> '<STR_LIT>' ) <EOL> def clean ( self ) : <EOL> cleaned_data = super ( UpdateProjectInfoAction , self ) . clean ( ) <EOL> if self . fields [ '<STR_LIT>' ] . widget . attrs . get ( '<STR_LIT>' , False ) : <EOL> cleaned_data [ '<STR_LIT>' ] = True <EOL> return cleaned_data <EOL> class Meta ( object ) : <EOL> name = _ ( "<STR_LIT>" ) <EOL> slug = '<STR_LIT>' <EOL> help_text = _ ( "<STR_LIT>" ) <EOL> class UpdateProjectInfo ( workflows . Step ) : <EOL> action_class = UpdateProjectInfoAction <EOL> template_name = COMMON_HORIZONTAL_TEMPLATE <EOL> depends_on = ( "<STR_LIT>" , ) <EOL> contributes = ( "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT:name>" , <EOL> "<STR_LIT:description>" , <EOL> "<STR_LIT>" ) <EOL> class UpdateProject ( CommonQuotaWorkflow , IdentityMixIn ) : <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 = ( UpdateProjectInfo , <EOL> UpdateProjectMembers , <EOL> UpdateProjectQuota ) <EOL> def __init__ ( self , request = None , context_seed = None , entry_point = None , <EOL> * args , ** kwargs ) : <EOL> if PROJECT_GROUP_ENABLED : <EOL> self . default_steps = ( UpdateProjectInfo , <EOL> UpdateProjectMembers , <EOL> UpdateProjectGroups , <EOL> UpdateProjectQuota ) <EOL> super ( UpdateProject , self ) . __init__ ( request = request , <EOL> context_seed = context_seed , <EOL> entry_point = entry_point , <EOL> * args , <EOL> ** kwargs ) <EOL> def format_status_message ( self , message ) : <EOL> if "<STR_LIT:%s>" in message : <EOL> return message % self . context . get ( '<STR_LIT:name>' , '<STR_LIT>' ) <EOL> else : <EOL> return message <EOL> @ memoized . memoized_method <EOL> def _get_available_roles ( self , request ) : <EOL> return api . keystone . role_list ( request ) <EOL> def _update_project ( self , request , data ) : <EOL> """<STR_LIT>""" <EOL> domain_id = api . keystone . get_effective_domain_id ( self . request ) <EOL> try : <EOL> project_id = data [ '<STR_LIT>' ] <EOL> return api . keystone . tenant_update ( <EOL> request , <EOL> project_id , <EOL> name = data [ '<STR_LIT:name>' ] , <EOL> description = data [ '<STR_LIT:description>' ] , <EOL> enabled = data [ '<STR_LIT>' ] , <EOL> domain = domain_id ) <EOL> except exceptions . Conflict : <EOL> msg = _ ( '<STR_LIT>' ) % data [ '<STR_LIT:name>' ] <EOL> self . failure_message = msg <EOL> return <EOL> except Exception as e : <EOL> LOG . debug ( '<STR_LIT>' % e ) <EOL> exceptions . handle ( request , ignore = True ) <EOL> return <EOL> def _add_roles_to_users ( self , request , data , project_id , user_id , <EOL> role_ids , available_roles ) : <EOL> member_step = self . get_step ( PROJECT_USER_MEMBER_SLUG ) <EOL> current_role_ids = list ( role_ids ) <EOL> for role in available_roles : <EOL> field_name = member_step . get_member_field_name ( role . id ) <EOL> if user_id in data [ field_name ] : <EOL> if role . id not in current_role_ids : <EOL> api . keystone . add_tenant_user_role ( <EOL> request , <EOL> project = project_id , <EOL> user = user_id , <EOL> role = role . id ) <EOL> else : <EOL> index = current_role_ids . index ( role . id ) <EOL> current_role_ids . pop ( index ) <EOL> return current_role_ids <EOL> def _remove_roles_from_user ( self , request , project_id , user_id , <EOL> current_role_ids ) : <EOL> for id_to_delete in current_role_ids : <EOL> api . keystone . remove_tenant_user_role ( <EOL> request , <EOL> project = project_id , <EOL> user = user_id , <EOL> role = id_to_delete ) <EOL> def _is_removing_self_admin_role ( self , request , project_id , user_id , <EOL> available_roles , current_role_ids ) : <EOL> is_current_user = user_id == request . user . id <EOL> is_current_project = project_id == request . user . tenant_id <EOL> _admin_roles = self . get_admin_roles ( ) <EOL> available_admin_role_ids = [ role . id for role in available_roles <EOL> if role . name . lower ( ) in _admin_roles ] <EOL> admin_roles = [ role for role in current_role_ids <EOL> if role in available_admin_role_ids ] <EOL> if len ( admin_roles ) : <EOL> removing_admin = any ( [ role in current_role_ids <EOL> for role in admin_roles ] ) <EOL> else : <EOL> removing_admin = False <EOL> if is_current_user and is_current_project and removing_admin : <EOL> msg = _ ( '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> messages . warning ( request , msg ) <EOL> return True <EOL> else : <EOL> return False <EOL> def _update_project_members ( self , request , data , project_id ) : <EOL> users_to_modify = <NUM_LIT:0> <EOL> member_step = self . get_step ( PROJECT_USER_MEMBER_SLUG ) <EOL> try : <EOL> available_roles = self . _get_available_roles ( request ) <EOL> users_roles = api . keystone . get_project_users_roles ( <EOL> request , project = project_id ) <EOL> users_to_modify = len ( users_roles ) <EOL> all_users = api . keystone . user_list ( request , <EOL> domain = data [ '<STR_LIT>' ] ) <EOL> users_dict = { user . id : user . name for user in all_users } <EOL> for user_id in users_roles . keys ( ) : <EOL> if user_id not in users_dict : <EOL> users_to_modify -= <NUM_LIT:1> <EOL> continue <EOL> current_role_ids = list ( users_roles [ user_id ] ) <EOL> modified_role_ids = self . _add_roles_to_users ( <EOL> request , data , project_id , user_id , <EOL> current_role_ids , available_roles ) <EOL> removing_admin = self . _is_removing_self_admin_role ( <EOL> request , project_id , user_id , available_roles , <EOL> modified_role_ids ) <EOL> if not removing_admin : <EOL> self . _remove_roles_from_user ( request , project_id , user_id , <EOL> modified_role_ids ) <EOL> users_to_modify -= <NUM_LIT:1> <EOL> for role in available_roles : <EOL> field_name = member_step . get_member_field_name ( role . id ) <EOL> users_to_modify += len ( data [ field_name ] ) <EOL> for role in available_roles : <EOL> users_added = <NUM_LIT:0> <EOL> field_name = member_step . get_member_field_name ( role . id ) <EOL> for user_id in data [ field_name ] : <EOL> if user_id not in users_roles : <EOL> api . keystone . add_tenant_user_role ( request , <EOL> project = project_id , <EOL> user = user_id , <EOL> role = role . id ) <EOL> users_added += <NUM_LIT:1> <EOL> users_to_modify -= users_added <EOL> return True <EOL> except Exception : <EOL> if PROJECT_GROUP_ENABLED : <EOL> group_msg = _ ( "<STR_LIT>" ) <EOL> else : <EOL> group_msg = "<STR_LIT>" <EOL> exceptions . handle ( request , <EOL> _ ( '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> % { '<STR_LIT>' : users_to_modify , <EOL> '<STR_LIT>' : group_msg } ) <EOL> return False <EOL> def _update_project_groups ( self , request , data , project_id , domain_id ) : <EOL> groups_to_modify = <NUM_LIT:0> <EOL> member_step = self . get_step ( PROJECT_GROUP_MEMBER_SLUG ) <EOL> try : <EOL> available_roles = self . _get_available_roles ( request ) <EOL> project_groups = api . keystone . group_list ( request , <EOL> domain = domain_id , <EOL> project = project_id ) <EOL> groups_to_modify = len ( project_groups ) <EOL> for group in project_groups : <EOL> current_roles = api . keystone . roles_for_group ( <EOL> self . request , <EOL> group = group . id , <EOL> project = project_id ) <EOL> current_role_ids = [ role . id for role in current_roles ] <EOL> for role in available_roles : <EOL> field_name = member_step . get_member_field_name ( role . id ) <EOL> if group . id in data [ field_name ] : <EOL> if role . id not in current_role_ids : <EOL> api . keystone . add_group_role ( <EOL> request , <EOL> role = role . id , <EOL> group = group . id , <EOL> project = project_id ) <EOL> else : <EOL> index = current_role_ids . index ( role . id ) <EOL> current_role_ids . pop ( index ) <EOL> for id_to_delete in current_role_ids : <EOL> api . keystone . remove_group_role ( request , <EOL> role = id_to_delete , <EOL> group = group . id , <EOL> project = project_id ) <EOL> groups_to_modify -= <NUM_LIT:1> <EOL> for role in available_roles : <EOL> field_name = member_step . get_member_field_name ( role . id ) <EOL> groups_to_modify += len ( data [ field_name ] ) <EOL> for role in available_roles : <EOL> groups_added = <NUM_LIT:0> <EOL> field_name = member_step . get_member_field_name ( role . id ) <EOL> for group_id in data [ field_name ] : <EOL> if not filter ( lambda x : group_id == x . id , <EOL> project_groups ) : <EOL> api . keystone . add_group_role ( request , <EOL> role = role . id , <EOL> group = group_id , <EOL> project = project_id ) <EOL> groups_added += <NUM_LIT:1> <EOL> groups_to_modify -= groups_added <EOL> return True <EOL> except Exception : <EOL> exceptions . handle ( request , <EOL> _ ( '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> % groups_to_modify ) <EOL> return False <EOL> def _update_project_quota ( self , request , data , project_id ) : <EOL> try : <EOL> super ( UpdateProject , self ) . _update_project_quota ( <EOL> request , data , project_id ) <EOL> return True <EOL> except Exception : <EOL> exceptions . handle ( request , _ ( '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' ) ) <EOL> return False <EOL> def handle ( self , request , data ) : <EOL> project = self . _update_project ( request , data ) <EOL> if not project : <EOL> return False <EOL> project_id = data [ '<STR_LIT>' ] <EOL> domain_id = getattr ( project , "<STR_LIT>" , '<STR_LIT>' ) <EOL> ret = self . _update_project_members ( request , data , project_id ) <EOL> if not ret : <EOL> return False <EOL> if PROJECT_GROUP_ENABLED : <EOL> ret = self . _update_project_groups ( request , data , <EOL> project_id , domain_id ) <EOL> if not ret : <EOL> return False <EOL> if api . keystone . is_cloud_admin ( request ) : <EOL> ret = self . _update_project_quota ( request , data , project_id ) <EOL> if not ret : <EOL> return False <EOL> return True <EOL> class UpdateProjectNoQuota ( UpdateProject ) : <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 = ( UpdateProjectInfo , UpdateProjectMembers ) <EOL> def __init__ ( self , request = None , context_seed = None , entry_point = None , <EOL> * args , ** kwargs ) : <EOL> if PROJECT_GROUP_ENABLED : <EOL> self . default_steps = ( UpdateProjectInfo , <EOL> UpdateProjectMembers , <EOL> UpdateProjectGroups ) <EOL> super ( UpdateProject , self ) . __init__ ( request = request , <EOL> context_seed = context_seed , <EOL> entry_point = entry_point , <EOL> * args , <EOL> ** kwargs ) </s>
