<s> from django . conf import settings <EOL> from django . core . urlresolvers import reverse <EOL> from django . utils . translation import ugettext_lazy as _ <EOL> from django . utils . translation import ungettext_lazy <EOL> from horizon import tables <EOL> import six <EOL> from openstack_dashboard import api <EOL> from openstack_dashboard import policy <EOL> from openstack_dashboard . usage import quotas <EOL> from openstack_dashboard . utils import filters <EOL> POLICY_CHECK = getattr ( settings , "<STR_LIT>" , <EOL> lambda policy , request , target : True ) <EOL> class DeleteGroup ( policy . PolicyTargetMixin , tables . DeleteAction ) : <EOL> @ staticmethod <EOL> def action_present ( count ) : <EOL> return ungettext_lazy ( <EOL> u"<STR_LIT>" , <EOL> u"<STR_LIT>" , <EOL> count <EOL> ) <EOL> @ staticmethod <EOL> def action_past ( count ) : <EOL> return ungettext_lazy ( <EOL> u"<STR_LIT>" , <EOL> u"<STR_LIT>" , <EOL> count <EOL> ) <EOL> def allowed ( self , request , security_group = None ) : <EOL> policy_target = self . get_policy_target ( request , security_group ) <EOL> if api . base . is_service_enabled ( request , "<STR_LIT>" ) : <EOL> policy = ( ( "<STR_LIT>" , "<STR_LIT>" ) , ) <EOL> else : <EOL> policy = ( ( "<STR_LIT>" , "<STR_LIT>" ) , ) <EOL> if not POLICY_CHECK ( policy , request , policy_target ) : <EOL> return False <EOL> if not security_group : <EOL> return True <EOL> return security_group . name != '<STR_LIT:default>' <EOL> def delete ( self , request , obj_id ) : <EOL> api . network . security_group_delete ( request , obj_id ) <EOL> class CreateGroup ( tables . LinkAction ) : <EOL> name = "<STR_LIT>" <EOL> verbose_name = _ ( "<STR_LIT>" ) <EOL> url = "<STR_LIT>" <EOL> classes = ( "<STR_LIT>" , ) <EOL> icon = "<STR_LIT>" <EOL> def allowed ( self , request , security_group = None ) : <EOL> if api . base . is_service_enabled ( request , "<STR_LIT>" ) : <EOL> policy = ( ( "<STR_LIT>" , "<STR_LIT>" ) , ) <EOL> else : <EOL> policy = ( ( "<STR_LIT>" , "<STR_LIT>" ) , ) <EOL> usages = quotas . tenant_quota_usages ( request ) <EOL> if usages [ '<STR_LIT>' ] . get ( '<STR_LIT>' , <NUM_LIT:1> ) <= <NUM_LIT:0> : <EOL> if "<STR_LIT>" not in self . classes : <EOL> self . classes = [ c for c in self . classes ] + [ "<STR_LIT>" ] <EOL> self . verbose_name = _ ( "<STR_LIT>" ) <EOL> else : <EOL> self . verbose_name = _ ( "<STR_LIT>" ) <EOL> self . classes = [ c for c in self . classes if c != "<STR_LIT>" ] <EOL> return POLICY_CHECK ( policy , request , target = { } ) <EOL> class EditGroup ( policy . PolicyTargetMixin , tables . LinkAction ) : <EOL> name = "<STR_LIT>" <EOL> verbose_name = _ ( "<STR_LIT>" ) <EOL> url = "<STR_LIT>" <EOL> classes = ( "<STR_LIT>" , ) <EOL> icon = "<STR_LIT>" <EOL> def allowed ( self , request , security_group = None ) : <EOL> policy_target = self . get_policy_target ( request , security_group ) <EOL> if api . base . is_service_enabled ( request , "<STR_LIT>" ) : <EOL> policy = ( ( "<STR_LIT>" , "<STR_LIT>" ) , ) <EOL> else : <EOL> policy = ( ( "<STR_LIT>" , "<STR_LIT>" ) , ) <EOL> if not POLICY_CHECK ( policy , request , policy_target ) : <EOL> return False <EOL> if not security_group : <EOL> return True <EOL> return security_group . name != '<STR_LIT:default>' <EOL> class ManageRules ( policy . PolicyTargetMixin , tables . LinkAction ) : <EOL> name = "<STR_LIT>" <EOL> verbose_name = _ ( "<STR_LIT>" ) <EOL> url = "<STR_LIT>" <EOL> icon = "<STR_LIT>" <EOL> def allowed ( self , request , security_group = None ) : <EOL> policy_target = self . get_policy_target ( request , security_group ) <EOL> if api . base . is_service_enabled ( request , "<STR_LIT>" ) : <EOL> policy = ( ( "<STR_LIT>" , "<STR_LIT>" ) , ) <EOL> else : <EOL> policy = ( ( "<STR_LIT>" , "<STR_LIT>" ) , ) <EOL> return POLICY_CHECK ( policy , request , policy_target ) <EOL> class SecurityGroupsFilterAction ( tables . FilterAction ) : <EOL> def filter ( self , table , security_groups , filter_string ) : <EOL> """<STR_LIT>""" <EOL> query = filter_string . lower ( ) <EOL> return [ security_group for security_group in security_groups <EOL> if query in security_group . name . lower ( ) ] <EOL> class SecurityGroupsTable ( tables . DataTable ) : <EOL> name = tables . Column ( "<STR_LIT:name>" , verbose_name = _ ( "<STR_LIT:Name>" ) ) <EOL> description = tables . Column ( "<STR_LIT:description>" , verbose_name = _ ( "<STR_LIT>" ) ) <EOL> def sanitize_id ( self , obj_id ) : <EOL> return filters . get_int_or_uuid ( obj_id ) <EOL> class Meta ( object ) : <EOL> name = "<STR_LIT>" <EOL> verbose_name = _ ( "<STR_LIT>" ) <EOL> table_actions = ( CreateGroup , DeleteGroup , SecurityGroupsFilterAction ) <EOL> row_actions = ( ManageRules , EditGroup , DeleteGroup ) <EOL> class CreateRule ( tables . LinkAction ) : <EOL> name = "<STR_LIT>" <EOL> verbose_name = _ ( "<STR_LIT>" ) <EOL> url = "<STR_LIT>" <EOL> classes = ( "<STR_LIT>" , ) <EOL> icon = "<STR_LIT>" <EOL> def allowed ( self , request , security_group_rule = None ) : <EOL> if api . base . is_service_enabled ( request , "<STR_LIT>" ) : <EOL> policy = ( ( "<STR_LIT>" , "<STR_LIT>" ) , ) <EOL> else : <EOL> policy = ( ( "<STR_LIT>" , "<STR_LIT>" ) , ) <EOL> return POLICY_CHECK ( policy , request , target = { } ) <EOL> def get_link_url ( self ) : <EOL> return reverse ( self . url , args = [ self . table . kwargs [ '<STR_LIT>' ] ] ) <EOL> class DeleteRule ( tables . DeleteAction ) : <EOL> @ staticmethod <EOL> def action_present ( count ) : <EOL> return ungettext_lazy ( <EOL> u"<STR_LIT>" , <EOL> u"<STR_LIT>" , <EOL> count <EOL> ) <EOL> @ staticmethod <EOL> def action_past ( count ) : <EOL> return ungettext_lazy ( <EOL> u"<STR_LIT>" , <EOL> u"<STR_LIT>" , <EOL> count <EOL> ) <EOL> def allowed ( self , request , security_group_rule = None ) : <EOL> if api . base . is_service_enabled ( request , "<STR_LIT>" ) : <EOL> policy = ( ( "<STR_LIT>" , "<STR_LIT>" ) , ) <EOL> else : <EOL> policy = ( ( "<STR_LIT>" , "<STR_LIT>" ) , ) <EOL> return POLICY_CHECK ( policy , request , target = { } ) <EOL> def delete ( self , request , obj_id ) : <EOL> api . network . security_group_rule_delete ( request , obj_id ) <EOL> def get_success_url ( self , request ) : <EOL> sg_id = self . table . kwargs [ '<STR_LIT>' ] <EOL> return reverse ( "<STR_LIT>" <EOL> "<STR_LIT>" , args = [ sg_id ] ) <EOL> def get_remote_ip_prefix ( rule ) : <EOL> if '<STR_LIT>' in rule . ip_range : <EOL> if rule . ip_range [ '<STR_LIT>' ] is None : <EOL> range = '<STR_LIT>' if rule . ethertype == '<STR_LIT>' else '<STR_LIT>' <EOL> else : <EOL> range = rule . ip_range [ '<STR_LIT>' ] <EOL> return range <EOL> else : <EOL> return None <EOL> def get_remote_security_group ( rule ) : <EOL> return rule . group . get ( '<STR_LIT:name>' ) <EOL> def get_port_range ( rule ) : <EOL> if rule . from_port is None : <EOL> return _ ( '<STR_LIT>' ) <EOL> ip_proto = rule . ip_protocol <EOL> if rule . from_port == rule . to_port : <EOL> return check_rule_template ( rule . from_port , ip_proto ) <EOL> else : <EOL> return ( u"<STR_LIT>" % <EOL> { '<STR_LIT>' : check_rule_template ( rule . from_port , ip_proto ) , <EOL> '<STR_LIT:to>' : check_rule_template ( rule . to_port , ip_proto ) } ) <EOL> def filter_direction ( direction ) : <EOL> if direction is None or direction . lower ( ) == '<STR_LIT>' : <EOL> return _ ( '<STR_LIT>' ) <EOL> else : <EOL> return _ ( '<STR_LIT>' ) <EOL> def filter_protocol ( protocol ) : <EOL> if protocol is None : <EOL> return _ ( '<STR_LIT>' ) <EOL> return six . text_type . upper ( protocol ) <EOL> def check_rule_template ( port , ip_proto ) : <EOL> rules_dict = getattr ( settings , '<STR_LIT>' , { } ) <EOL> if not rules_dict : <EOL> return port <EOL> templ_rule = [ rule for rule in rules_dict . values ( ) <EOL> if ( str ( port ) == rule [ '<STR_LIT>' ] <EOL> and str ( port ) == rule [ '<STR_LIT>' ] <EOL> and ip_proto == rule [ '<STR_LIT>' ] ) ] <EOL> if templ_rule : <EOL> return u"<STR_LIT>" % templ_rule [ <NUM_LIT:0> ] <EOL> return port <EOL> class RulesTable ( tables . DataTable ) : <EOL> direction = tables . Column ( "<STR_LIT>" , <EOL> verbose_name = _ ( "<STR_LIT>" ) , <EOL> filters = ( filter_direction , ) ) <EOL> ethertype = tables . Column ( "<STR_LIT>" , <EOL> verbose_name = _ ( "<STR_LIT>" ) ) <EOL> protocol = tables . Column ( "<STR_LIT>" , <EOL> verbose_name = _ ( "<STR_LIT>" ) , <EOL> filters = ( filter_protocol , ) ) <EOL> port_range = tables . Column ( get_port_range , <EOL> verbose_name = _ ( "<STR_LIT>" ) ) <EOL> remote_ip_prefix = tables . Column ( get_remote_ip_prefix , <EOL> verbose_name = _ ( "<STR_LIT>" ) ) <EOL> remote_security_group = tables . Column ( get_remote_security_group , <EOL> verbose_name = _ ( "<STR_LIT>" <EOL> "<STR_LIT>" ) ) <EOL> def sanitize_id ( self , obj_id ) : <EOL> return filters . get_int_or_uuid ( obj_id ) <EOL> def get_object_display ( self , rule ) : <EOL> return six . text_type ( rule ) <EOL> class Meta ( object ) : <EOL> name = "<STR_LIT>" <EOL> verbose_name = _ ( "<STR_LIT>" ) <EOL> table_actions = ( CreateRule , DeleteRule ) <EOL> row_actions = ( DeleteRule , ) </s>
