<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 workflows <EOL> from openstack_dashboard import api <EOL> from openstack_dashboard . dashboards . admin . domains import constants <EOL> LOG = logging . getLogger ( __name__ ) <EOL> class CreateDomainInfoAction ( workflows . Action ) : <EOL> name = forms . CharField ( label = _ ( "<STR_LIT:Name>" ) , <EOL> required = True ) <EOL> description = forms . CharField ( widget = forms . widgets . Textarea ( ) , <EOL> label = _ ( "<STR_LIT>" ) , <EOL> required = False ) <EOL> enabled = forms . BooleanField ( label = _ ( "<STR_LIT>" ) , <EOL> required = False , <EOL> initial = True ) <EOL> class Meta : <EOL> name = _ ( "<STR_LIT>" ) <EOL> slug = "<STR_LIT>" <EOL> help_text = _ ( "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> class CreateDomainInfo ( workflows . Step ) : <EOL> action_class = CreateDomainInfoAction <EOL> contributes = ( "<STR_LIT>" , <EOL> "<STR_LIT:name>" , <EOL> "<STR_LIT:description>" , <EOL> "<STR_LIT>" ) <EOL> class UpdateDomainGroupsAction ( workflows . MembershipAction ) : <EOL> def __init__ ( self , request , * args , ** kwargs ) : <EOL> super ( UpdateDomainGroupsAction , self ) . __init__ ( request , <EOL> * args , <EOL> ** kwargs ) <EOL> err_msg = _ ( '<STR_LIT>' ) <EOL> domain_id = '<STR_LIT>' <EOL> if '<STR_LIT>' in args [ <NUM_LIT:0> ] : <EOL> domain_id = args [ <NUM_LIT:0> ] [ '<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>' ) % default <EOL> raise exceptions . NotFound ( msg ) <EOL> except Exception : <EOL> exceptions . handle ( self . request , <EOL> err_msg , <EOL> redirect = reverse ( constants . DOMAINS_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 ( constants . DOMAINS_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 domain_id : <EOL> for group in all_groups : <EOL> try : <EOL> roles = api . keystone . roles_for_group ( self . request , <EOL> group = group . id , <EOL> domain = domain_id ) <EOL> except Exception : <EOL> exceptions . handle ( request , <EOL> err_msg , <EOL> redirect = reverse ( <EOL> constants . DOMAINS_INDEX_URL ) ) <EOL> for role in roles : <EOL> field_name = self . get_member_field_name ( role . id ) <EOL> self . fields [ field_name ] . initial . append ( group . id ) <EOL> class Meta : <EOL> name = _ ( "<STR_LIT>" ) <EOL> slug = constants . DOMAIN_GROUP_MEMBER_SLUG <EOL> class UpdateDomainGroups ( workflows . UpdateMembersStep ) : <EOL> action_class = UpdateDomainGroupsAction <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 CreateDomain ( 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 = constants . DOMAINS_INDEX_URL <EOL> default_steps = ( CreateDomainInfo , ) <EOL> def format_status_message ( self , message ) : <EOL> return message % self . context . get ( '<STR_LIT:name>' , '<STR_LIT>' ) <EOL> def handle ( self , request , data ) : <EOL> try : <EOL> LOG . info ( '<STR_LIT>' % data [ '<STR_LIT:name>' ] ) <EOL> desc = data [ '<STR_LIT:description>' ] <EOL> api . keystone . domain_create ( request , <EOL> name = data [ '<STR_LIT:name>' ] , <EOL> description = desc , <EOL> enabled = data [ '<STR_LIT>' ] ) <EOL> except Exception : <EOL> exceptions . handle ( request , ignore = True ) <EOL> return False <EOL> return True <EOL> class UpdateDomainInfoAction ( CreateDomainInfoAction ) : <EOL> class Meta : <EOL> name = _ ( "<STR_LIT>" ) <EOL> slug = '<STR_LIT>' <EOL> help_text = _ ( "<STR_LIT>" ) <EOL> class UpdateDomainInfo ( workflows . Step ) : <EOL> action_class = UpdateDomainInfoAction <EOL> depends_on = ( "<STR_LIT>" , ) <EOL> contributes = ( "<STR_LIT:name>" , <EOL> "<STR_LIT:description>" , <EOL> "<STR_LIT>" ) <EOL> class UpdateDomain ( 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 = constants . DOMAINS_INDEX_URL <EOL> default_steps = ( UpdateDomainInfo , <EOL> UpdateDomainGroups ) <EOL> def format_status_message ( self , message ) : <EOL> return message % self . context . get ( '<STR_LIT:name>' , '<STR_LIT>' ) <EOL> def handle ( self , request , data ) : <EOL> domain_id = data . pop ( '<STR_LIT>' ) <EOL> try : <EOL> LOG . info ( '<STR_LIT>' % data [ '<STR_LIT:name>' ] ) <EOL> api . keystone . domain_update ( request , <EOL> domain_id = domain_id , <EOL> name = data [ '<STR_LIT:name>' ] , <EOL> description = data [ '<STR_LIT:description>' ] , <EOL> enabled = data [ '<STR_LIT>' ] ) <EOL> except Exception : <EOL> exceptions . handle ( request , ignore = True ) <EOL> return False <EOL> groups_to_modify = <NUM_LIT:0> <EOL> member_step = self . get_step ( constants . DOMAIN_GROUP_MEMBER_SLUG ) <EOL> try : <EOL> available_roles = api . keystone . role_list ( request ) <EOL> domain_groups = api . keystone . group_list ( request , <EOL> domain = domain_id ) <EOL> groups_to_modify = len ( domain_groups ) <EOL> for group in domain_groups : <EOL> current_roles = api . keystone . roles_for_group ( <EOL> self . request , <EOL> group = group . id , <EOL> domain = domain_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> domain = domain_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> domain = domain_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 , domain_groups ) : <EOL> api . keystone . add_group_role ( request , <EOL> role = role . id , <EOL> group = group_id , <EOL> domain = domain_id ) <EOL> groups_added += <NUM_LIT:1> <EOL> groups_to_modify -= groups_added <EOL> except Exception : <EOL> exceptions . handle ( request , _ ( '<STR_LIT>' <EOL> % groups_to_modify ) ) <EOL> return True <EOL> return True </s>
