<s> from django . utils . translation import ugettext_lazy as _ <EOL> from horizon import exceptions <EOL> from horizon import forms <EOL> from horizon . utils import validators <EOL> from horizon import workflows <EOL> from openstack_dashboard import api <EOL> port_validator = validators . validate_port_or_colon_separated_port_range <EOL> class AddRuleAction ( workflows . Action ) : <EOL> name = forms . CharField ( <EOL> max_length = <NUM_LIT> , <EOL> label = _ ( "<STR_LIT:Name>" ) , <EOL> required = False ) <EOL> description = forms . CharField ( <EOL> max_length = <NUM_LIT> , <EOL> label = _ ( "<STR_LIT>" ) , <EOL> required = False ) <EOL> protocol = forms . ChoiceField ( <EOL> label = _ ( "<STR_LIT>" ) , <EOL> choices = [ ( '<STR_LIT>' , _ ( '<STR_LIT>' ) ) , <EOL> ( '<STR_LIT>' , _ ( '<STR_LIT>' ) ) , <EOL> ( '<STR_LIT>' , _ ( '<STR_LIT>' ) ) , <EOL> ( '<STR_LIT>' , _ ( '<STR_LIT>' ) ) ] , ) <EOL> action = forms . ChoiceField ( <EOL> label = _ ( "<STR_LIT>" ) , <EOL> choices = [ ( '<STR_LIT>' , _ ( '<STR_LIT>' ) ) , <EOL> ( '<STR_LIT>' , _ ( '<STR_LIT>' ) ) ] , ) <EOL> source_ip_address = forms . IPField ( <EOL> label = _ ( "<STR_LIT>" ) , <EOL> version = forms . IPv4 | forms . IPv6 , <EOL> required = False , mask = True ) <EOL> destination_ip_address = forms . IPField ( <EOL> label = _ ( "<STR_LIT>" ) , <EOL> version = forms . IPv4 | forms . IPv6 , <EOL> required = False , mask = True ) <EOL> source_port = forms . CharField ( <EOL> max_length = <NUM_LIT> , <EOL> label = _ ( "<STR_LIT>" ) , <EOL> required = False , <EOL> validators = [ port_validator ] ) <EOL> destination_port = forms . CharField ( <EOL> max_length = <NUM_LIT> , <EOL> label = _ ( "<STR_LIT>" ) , <EOL> required = False , <EOL> validators = [ port_validator ] ) <EOL> shared = forms . BooleanField ( <EOL> label = _ ( "<STR_LIT>" ) , initial = False , required = False ) <EOL> enabled = forms . BooleanField ( <EOL> label = _ ( "<STR_LIT>" ) , initial = True , required = False ) <EOL> def __init__ ( self , request , * args , ** kwargs ) : <EOL> super ( AddRuleAction , self ) . __init__ ( request , * args , ** kwargs ) <EOL> class Meta : <EOL> name = _ ( "<STR_LIT>" ) <EOL> permissions = ( '<STR_LIT>' , ) <EOL> help_text = _ ( "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> class AddRuleStep ( workflows . Step ) : <EOL> action_class = AddRuleAction <EOL> contributes = ( "<STR_LIT:name>" , "<STR_LIT:description>" , "<STR_LIT>" , "<STR_LIT:action>" , <EOL> "<STR_LIT>" , "<STR_LIT>" , <EOL> "<STR_LIT>" , "<STR_LIT>" , <EOL> "<STR_LIT>" , "<STR_LIT>" ) <EOL> def contribute ( self , data , context ) : <EOL> context = super ( AddRuleStep , self ) . contribute ( data , context ) <EOL> if data : <EOL> if context [ '<STR_LIT>' ] == '<STR_LIT>' : <EOL> del context [ '<STR_LIT>' ] <EOL> for field in [ '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' ] : <EOL> if not context [ field ] : <EOL> del context [ field ] <EOL> return context <EOL> class AddRule ( 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 = ( AddRuleStep , ) <EOL> def format_status_message ( self , message ) : <EOL> return message % self . context . get ( '<STR_LIT:name>' ) <EOL> def handle ( self , request , context ) : <EOL> try : <EOL> api . fwaas . rule_create ( request , ** context ) <EOL> return True <EOL> except Exception as e : <EOL> msg = self . format_status_message ( self . failure_message ) + str ( e ) <EOL> exceptions . handle ( request , msg ) <EOL> return False <EOL> class SelectRulesAction ( workflows . Action ) : <EOL> rule = forms . MultipleChoiceField ( <EOL> label = _ ( "<STR_LIT>" ) , <EOL> required = False , <EOL> widget = forms . CheckboxSelectMultiple ( ) , <EOL> help_text = _ ( "<STR_LIT>" ) ) <EOL> class Meta : <EOL> name = _ ( "<STR_LIT>" ) <EOL> permissions = ( '<STR_LIT>' , ) <EOL> help_text = _ ( "<STR_LIT>" ) <EOL> def populate_rule_choices ( self , request , context ) : <EOL> try : <EOL> tenant_id = self . request . user . tenant_id <EOL> rules = api . fwaas . rule_list ( request , tenant_id = tenant_id ) <EOL> rules = sorted ( rules , <EOL> key = lambda rule : rule . name_or_id ) <EOL> rule_list = [ ( rule . id , rule . name_or_id ) for rule in rules <EOL> if not rule . firewall_policy_id ] <EOL> except Exception as e : <EOL> rule_list = [ ] <EOL> exceptions . handle ( request , <EOL> _ ( '<STR_LIT>' ) % { <EOL> '<STR_LIT:error>' : str ( e ) } ) <EOL> return rule_list <EOL> class SelectRulesStep ( workflows . Step ) : <EOL> action_class = SelectRulesAction <EOL> template_name = "<STR_LIT>" <EOL> contributes = ( "<STR_LIT>" , ) <EOL> def contribute ( self , data , context ) : <EOL> if data : <EOL> rules = self . workflow . request . POST . getlist ( "<STR_LIT>" ) <EOL> if rules : <EOL> rules = [ r for r in rules if r != '<STR_LIT>' ] <EOL> context [ '<STR_LIT>' ] = rules <EOL> return context <EOL> class AddPolicyAction ( workflows . Action ) : <EOL> name = forms . CharField ( max_length = <NUM_LIT> , <EOL> label = _ ( "<STR_LIT:Name>" ) ) <EOL> description = forms . CharField ( max_length = <NUM_LIT> , <EOL> label = _ ( "<STR_LIT>" ) , <EOL> required = False ) <EOL> shared = forms . BooleanField ( label = _ ( "<STR_LIT>" ) , <EOL> initial = False , <EOL> required = False ) <EOL> audited = forms . BooleanField ( label = _ ( "<STR_LIT>" ) , <EOL> initial = False , <EOL> required = False ) <EOL> def __init__ ( self , request , * args , ** kwargs ) : <EOL> super ( AddPolicyAction , self ) . __init__ ( request , * args , ** kwargs ) <EOL> class Meta : <EOL> name = _ ( "<STR_LIT>" ) <EOL> permissions = ( '<STR_LIT>' , ) <EOL> help_text = _ ( "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> class AddPolicyStep ( workflows . Step ) : <EOL> action_class = AddPolicyAction <EOL> contributes = ( "<STR_LIT:name>" , "<STR_LIT:description>" , "<STR_LIT>" , "<STR_LIT>" ) <EOL> def contribute ( self , data , context ) : <EOL> context = super ( AddPolicyStep , self ) . contribute ( data , context ) <EOL> if data : <EOL> return context <EOL> class AddPolicy ( 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 = ( AddPolicyStep , SelectRulesStep ) <EOL> def format_status_message ( self , message ) : <EOL> return message % self . context . get ( '<STR_LIT:name>' ) <EOL> def handle ( self , request , context ) : <EOL> try : <EOL> api . fwaas . policy_create ( request , ** context ) <EOL> return True <EOL> except Exception as e : <EOL> msg = self . format_status_message ( self . failure_message ) + str ( e ) <EOL> exceptions . handle ( request , msg ) <EOL> return False <EOL> class AddFirewallAction ( workflows . Action ) : <EOL> name = forms . CharField ( max_length = <NUM_LIT> , <EOL> label = _ ( "<STR_LIT:Name>" ) , <EOL> required = False ) <EOL> description = forms . CharField ( max_length = <NUM_LIT> , <EOL> label = _ ( "<STR_LIT>" ) , <EOL> required = False ) <EOL> firewall_policy_id = forms . ChoiceField ( label = _ ( "<STR_LIT>" ) ) <EOL> shared = forms . BooleanField ( label = _ ( "<STR_LIT>" ) , <EOL> initial = False , <EOL> required = False ) <EOL> admin_state_up = forms . ChoiceField ( choices = [ ( True , _ ( '<STR_LIT>' ) ) , <EOL> ( False , _ ( '<STR_LIT>' ) ) ] , <EOL> label = _ ( "<STR_LIT>" ) ) <EOL> def __init__ ( self , request , * args , ** kwargs ) : <EOL> super ( AddFirewallAction , self ) . __init__ ( request , * args , ** kwargs ) <EOL> firewall_policy_id_choices = [ ( '<STR_LIT>' , _ ( "<STR_LIT>" ) ) ] <EOL> try : <EOL> tenant_id = self . request . user . tenant_id <EOL> policies = api . fwaas . policy_list ( request , tenant_id = tenant_id ) <EOL> policies = sorted ( policies , key = lambda policy : policy . name ) <EOL> except Exception as e : <EOL> exceptions . handle ( <EOL> request , <EOL> _ ( '<STR_LIT>' ) % { <EOL> '<STR_LIT:error>' : str ( e ) } ) <EOL> policies = [ ] <EOL> for p in policies : <EOL> firewall_policy_id_choices . append ( ( p . id , p . name_or_id ) ) <EOL> self . fields [ '<STR_LIT>' ] . choices = firewall_policy_id_choices <EOL> if not request . user . is_superuser : <EOL> self . fields [ '<STR_LIT>' ] . widget . attrs [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> class Meta : <EOL> name = _ ( "<STR_LIT>" ) <EOL> permissions = ( '<STR_LIT>' , ) <EOL> help_text = _ ( "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> class AddFirewallStep ( workflows . Step ) : <EOL> action_class = AddFirewallAction <EOL> contributes = ( "<STR_LIT:name>" , "<STR_LIT>" , "<STR_LIT:description>" , <EOL> "<STR_LIT>" , "<STR_LIT>" ) <EOL> def contribute ( self , data , context ) : <EOL> context = super ( AddFirewallStep , self ) . contribute ( data , context ) <EOL> context [ '<STR_LIT>' ] = ( context [ '<STR_LIT>' ] == '<STR_LIT:True>' ) <EOL> return context <EOL> class AddFirewall ( 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 = ( AddFirewallStep , ) <EOL> def format_status_message ( self , message ) : <EOL> return message % self . context . get ( '<STR_LIT:name>' ) <EOL> def handle ( self , request , context ) : <EOL> try : <EOL> api . fwaas . firewall_create ( request , ** context ) <EOL> return True <EOL> except Exception as e : <EOL> msg = self . format_status_message ( self . failure_message ) + str ( e ) <EOL> exceptions . handle ( request , msg ) <EOL> return False </s>
