<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 openstack_dashboard import api <EOL> LOG = logging . getLogger ( __name__ ) <EOL> PROVIDER_TYPES = [ ( '<STR_LIT>' , _ ( '<STR_LIT>' ) ) , ( '<STR_LIT>' , _ ( '<STR_LIT>' ) ) , <EOL> ( '<STR_LIT>' , _ ( '<STR_LIT>' ) ) , ( '<STR_LIT>' , _ ( '<STR_LIT>' ) ) , <EOL> ( '<STR_LIT>' , _ ( '<STR_LIT>' ) ) ] <EOL> SEGMENTATION_ID_RANGE = { '<STR_LIT>' : [ <NUM_LIT:1> , <NUM_LIT> ] , '<STR_LIT>' : [ <NUM_LIT:0> , ( <NUM_LIT:2> ** <NUM_LIT:32> ) - <NUM_LIT:1> ] , <EOL> '<STR_LIT>' : [ <NUM_LIT:0> , ( <NUM_LIT:2> ** <NUM_LIT> ) - <NUM_LIT:1> ] } <EOL> class CreateNetwork ( forms . SelfHandlingForm ) : <EOL> name = forms . CharField ( max_length = <NUM_LIT:255> , <EOL> label = _ ( "<STR_LIT:Name>" ) , <EOL> required = False ) <EOL> tenant_id = forms . ChoiceField ( label = _ ( "<STR_LIT>" ) ) <EOL> if api . neutron . is_port_profiles_supported ( ) : <EOL> widget = None <EOL> else : <EOL> widget = forms . HiddenInput ( ) <EOL> net_profile_id = forms . ChoiceField ( label = _ ( "<STR_LIT>" ) , <EOL> required = False , <EOL> widget = widget ) <EOL> network_type = forms . ChoiceField ( <EOL> label = _ ( "<STR_LIT>" ) , <EOL> help_text = _ ( "<STR_LIT>" <EOL> "<STR_LIT>" ) , <EOL> widget = forms . Select ( attrs = { <EOL> '<STR_LIT:class>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' <EOL> } ) ) <EOL> physical_network = forms . CharField ( <EOL> max_length = <NUM_LIT:255> , <EOL> label = _ ( "<STR_LIT>" ) , <EOL> help_text = _ ( "<STR_LIT>" <EOL> "<STR_LIT>" ) , <EOL> initial = '<STR_LIT:default>' , <EOL> widget = forms . TextInput ( attrs = { <EOL> '<STR_LIT:class>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : _ ( '<STR_LIT>' ) , <EOL> '<STR_LIT>' : _ ( '<STR_LIT>' ) <EOL> } ) ) <EOL> segmentation_id = forms . IntegerField ( <EOL> label = _ ( "<STR_LIT>" ) , <EOL> widget = forms . TextInput ( attrs = { <EOL> '<STR_LIT:class>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : _ ( '<STR_LIT>' ) , <EOL> '<STR_LIT>' : _ ( '<STR_LIT>' ) , <EOL> '<STR_LIT>' : _ ( '<STR_LIT>' ) <EOL> } ) ) <EOL> admin_state = forms . ChoiceField ( choices = [ ( True , _ ( '<STR_LIT>' ) ) , <EOL> ( False , _ ( '<STR_LIT>' ) ) ] , <EOL> label = _ ( "<STR_LIT>" ) ) <EOL> shared = forms . BooleanField ( label = _ ( "<STR_LIT>" ) , <EOL> initial = False , required = False ) <EOL> external = forms . BooleanField ( label = _ ( "<STR_LIT>" ) , <EOL> initial = False , required = False ) <EOL> @ classmethod <EOL> def _instantiate ( cls , request , * args , ** kwargs ) : <EOL> return cls ( request , * args , ** kwargs ) <EOL> def __init__ ( self , request , * args , ** kwargs ) : <EOL> super ( CreateNetwork , self ) . __init__ ( request , * args , ** kwargs ) <EOL> tenant_choices = [ ( '<STR_LIT>' , _ ( "<STR_LIT>" ) ) ] <EOL> tenants , has_more = api . keystone . tenant_list ( request ) <EOL> for tenant in tenants : <EOL> if tenant . enabled : <EOL> tenant_choices . append ( ( tenant . id , tenant . name ) ) <EOL> self . fields [ '<STR_LIT>' ] . choices = tenant_choices <EOL> if api . neutron . is_port_profiles_supported ( ) : <EOL> self . fields [ '<STR_LIT>' ] . choices = ( <EOL> self . get_network_profile_choices ( request ) ) <EOL> if api . neutron . is_extension_supported ( request , '<STR_LIT>' ) : <EOL> neutron_settings = getattr ( settings , <EOL> '<STR_LIT>' , { } ) <EOL> seg_id_range = neutron_settings . get ( '<STR_LIT>' , { } ) <EOL> self . seg_id_range = { <EOL> '<STR_LIT>' : seg_id_range . get ( '<STR_LIT>' , <EOL> SEGMENTATION_ID_RANGE . get ( '<STR_LIT>' ) ) , <EOL> '<STR_LIT>' : seg_id_range . get ( '<STR_LIT>' , <EOL> SEGMENTATION_ID_RANGE . get ( '<STR_LIT>' ) ) , <EOL> '<STR_LIT>' : seg_id_range . get ( '<STR_LIT>' , <EOL> SEGMENTATION_ID_RANGE . get ( '<STR_LIT>' ) ) <EOL> } <EOL> seg_id_help = ( <EOL> _ ( "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> % { '<STR_LIT>' : self . seg_id_range [ '<STR_LIT>' ] [ <NUM_LIT:0> ] , <EOL> '<STR_LIT>' : self . seg_id_range [ '<STR_LIT>' ] [ <NUM_LIT:1> ] , <EOL> '<STR_LIT>' : self . seg_id_range [ '<STR_LIT>' ] [ <NUM_LIT:0> ] , <EOL> '<STR_LIT>' : self . seg_id_range [ '<STR_LIT>' ] [ <NUM_LIT:1> ] , <EOL> '<STR_LIT>' : self . seg_id_range [ '<STR_LIT>' ] [ <NUM_LIT:0> ] , <EOL> '<STR_LIT>' : self . seg_id_range [ '<STR_LIT>' ] [ <NUM_LIT:1> ] } ) <EOL> self . fields [ '<STR_LIT>' ] . help_text = seg_id_help <EOL> supported_provider_types = neutron_settings . get ( <EOL> '<STR_LIT>' , [ '<STR_LIT:*>' ] ) <EOL> if supported_provider_types == [ '<STR_LIT:*>' ] : <EOL> network_type_choices = PROVIDER_TYPES <EOL> else : <EOL> network_type_choices = [ <EOL> net_type for net_type in PROVIDER_TYPES <EOL> if net_type [ <NUM_LIT:0> ] in supported_provider_types ] <EOL> if len ( network_type_choices ) == <NUM_LIT:0> : <EOL> self . _hide_provider_network_type ( ) <EOL> else : <EOL> self . fields [ '<STR_LIT>' ] . choices = network_type_choices <EOL> else : <EOL> self . _hide_provider_network_type ( ) <EOL> def get_network_profile_choices ( self , request ) : <EOL> profile_choices = [ ( '<STR_LIT>' , _ ( "<STR_LIT>" ) ) ] <EOL> for profile in self . _get_profiles ( request , '<STR_LIT>' ) : <EOL> profile_choices . append ( ( profile . id , profile . name ) ) <EOL> return profile_choices <EOL> def _get_profiles ( self , request , type_p ) : <EOL> profiles = [ ] <EOL> try : <EOL> profiles = api . neutron . profile_list ( request , type_p ) <EOL> except Exception : <EOL> msg = _ ( '<STR_LIT>' ) <EOL> exceptions . handle ( request , msg ) <EOL> return profiles <EOL> def _hide_provider_network_type ( self ) : <EOL> self . fields [ '<STR_LIT>' ] . widget = forms . HiddenInput ( ) <EOL> self . fields [ '<STR_LIT>' ] . widget = forms . HiddenInput ( ) <EOL> self . fields [ '<STR_LIT>' ] . widget = forms . HiddenInput ( ) <EOL> self . fields [ '<STR_LIT>' ] . required = False <EOL> self . fields [ '<STR_LIT>' ] . required = False <EOL> self . fields [ '<STR_LIT>' ] . required = False <EOL> def handle ( self , request , data ) : <EOL> try : <EOL> params = { '<STR_LIT:name>' : data [ '<STR_LIT:name>' ] , <EOL> '<STR_LIT>' : data [ '<STR_LIT>' ] , <EOL> '<STR_LIT>' : ( data [ '<STR_LIT>' ] == '<STR_LIT:True>' ) , <EOL> '<STR_LIT>' : data [ '<STR_LIT>' ] , <EOL> '<STR_LIT>' : data [ '<STR_LIT>' ] } <EOL> if api . neutron . is_port_profiles_supported ( ) : <EOL> params [ '<STR_LIT>' ] = data [ '<STR_LIT>' ] <EOL> if api . neutron . is_extension_supported ( request , '<STR_LIT>' ) : <EOL> network_type = data [ '<STR_LIT>' ] <EOL> params [ '<STR_LIT>' ] = network_type <EOL> if network_type in [ '<STR_LIT>' , '<STR_LIT>' ] : <EOL> params [ '<STR_LIT>' ] = ( <EOL> data [ '<STR_LIT>' ] ) <EOL> if network_type in [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] : <EOL> params [ '<STR_LIT>' ] = ( <EOL> data [ '<STR_LIT>' ] ) <EOL> network = api . neutron . network_create ( request , ** params ) <EOL> msg = _ ( '<STR_LIT>' ) % data [ '<STR_LIT:name>' ] <EOL> LOG . debug ( msg ) <EOL> messages . success ( request , msg ) <EOL> return network <EOL> except Exception : <EOL> redirect = reverse ( '<STR_LIT>' ) <EOL> msg = _ ( '<STR_LIT>' ) % data [ '<STR_LIT:name>' ] <EOL> exceptions . handle ( request , msg , redirect = redirect ) <EOL> def clean ( self ) : <EOL> cleaned_data = super ( CreateNetwork , self ) . clean ( ) <EOL> self . _clean_physical_network ( cleaned_data ) <EOL> self . _clean_segmentation_id ( cleaned_data ) <EOL> return cleaned_data <EOL> def _clean_physical_network ( self , data ) : <EOL> network_type = data . get ( '<STR_LIT>' ) <EOL> if '<STR_LIT>' in self . _errors and ( <EOL> network_type in [ '<STR_LIT>' , '<STR_LIT>' ] ) : <EOL> del self . _errors [ '<STR_LIT>' ] <EOL> def _clean_segmentation_id ( self , data ) : <EOL> network_type = data . get ( '<STR_LIT>' ) <EOL> if '<STR_LIT>' in self . _errors : <EOL> if network_type in [ '<STR_LIT>' , '<STR_LIT>' ] : <EOL> del self . _errors [ '<STR_LIT>' ] <EOL> elif network_type in [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] : <EOL> seg_id = data . get ( '<STR_LIT>' ) <EOL> seg_id_range = { '<STR_LIT>' : self . seg_id_range [ network_type ] [ <NUM_LIT:0> ] , <EOL> '<STR_LIT>' : self . seg_id_range [ network_type ] [ <NUM_LIT:1> ] } <EOL> if seg_id < seg_id_range [ '<STR_LIT>' ] or seg_id > seg_id_range [ '<STR_LIT>' ] : <EOL> if network_type == '<STR_LIT>' : <EOL> msg = _ ( '<STR_LIT>' <EOL> '<STR_LIT>' ) % seg_id_range <EOL> elif network_type == '<STR_LIT>' : <EOL> msg = _ ( '<STR_LIT>' <EOL> '<STR_LIT>' ) % seg_id_range <EOL> elif network_type == '<STR_LIT>' : <EOL> msg = _ ( '<STR_LIT>' <EOL> '<STR_LIT>' ) % seg_id_range <EOL> self . _errors [ '<STR_LIT>' ] = self . error_class ( [ msg ] ) <EOL> class UpdateNetwork ( forms . SelfHandlingForm ) : <EOL> name = forms . CharField ( label = _ ( "<STR_LIT:Name>" ) , required = False ) <EOL> tenant_id = forms . CharField ( widget = forms . HiddenInput ) <EOL> admin_state = forms . ChoiceField ( choices = [ ( True , _ ( '<STR_LIT>' ) ) , <EOL> ( False , _ ( '<STR_LIT>' ) ) ] , <EOL> label = _ ( "<STR_LIT>" ) ) <EOL> shared = forms . BooleanField ( label = _ ( "<STR_LIT>" ) , required = False ) <EOL> external = forms . BooleanField ( label = _ ( "<STR_LIT>" ) , required = False ) <EOL> failure_url = '<STR_LIT>' <EOL> def handle ( self , request , data ) : <EOL> try : <EOL> params = { '<STR_LIT:name>' : data [ '<STR_LIT:name>' ] , <EOL> '<STR_LIT>' : ( data [ '<STR_LIT>' ] == '<STR_LIT:True>' ) , <EOL> '<STR_LIT>' : data [ '<STR_LIT>' ] , <EOL> '<STR_LIT>' : data [ '<STR_LIT>' ] } <EOL> network = api . neutron . network_update ( request , <EOL> self . initial [ '<STR_LIT>' ] , <EOL> ** params ) <EOL> msg = _ ( '<STR_LIT>' ) % data [ '<STR_LIT:name>' ] <EOL> LOG . debug ( msg ) <EOL> messages . success ( request , msg ) <EOL> return network <EOL> except Exception : <EOL> msg = _ ( '<STR_LIT>' ) % data [ '<STR_LIT:name>' ] <EOL> LOG . info ( msg ) <EOL> redirect = reverse ( self . failure_url ) <EOL> exceptions . handle ( request , msg , redirect = redirect ) </s>
