<s> import base64 <EOL> import json <EOL> import logging <EOL> from django . utils . translation import ugettext_lazy as _ <EOL> from saharaclient . api import base as api_base <EOL> from horizon import exceptions <EOL> from horizon import forms <EOL> from horizon import workflows <EOL> from openstack_dashboard . api import sahara as saharaclient <EOL> from openstack_dashboard . dashboards . project . data_processing . utils import helpers as helpers <EOL> from openstack_dashboard . dashboards . project . data_processing . utils import anti_affinity as aa <EOL> import openstack_dashboard . dashboards . project . data_processing . utils . workflow_helpers as whelpers <EOL> LOG = logging . getLogger ( __name__ ) <EOL> class SelectPluginAction ( workflows . Action ) : <EOL> hidden_create_field = forms . CharField ( <EOL> required = False , <EOL> widget = forms . HiddenInput ( attrs = { "<STR_LIT:class>" : "<STR_LIT>" } ) ) <EOL> def __init__ ( self , request , * args , ** kwargs ) : <EOL> super ( SelectPluginAction , self ) . __init__ ( request , * args , ** kwargs ) <EOL> try : <EOL> plugins = saharaclient . plugin_list ( request ) <EOL> except Exception : <EOL> plugins = [ ] <EOL> exceptions . handle ( request , <EOL> _ ( "<STR_LIT>" ) ) <EOL> plugin_choices = [ ( plugin . name , plugin . title ) for plugin in plugins ] <EOL> self . fields [ "<STR_LIT>" ] = forms . ChoiceField ( <EOL> label = _ ( "<STR_LIT>" ) , <EOL> choices = plugin_choices , <EOL> widget = forms . Select ( attrs = { "<STR_LIT:class>" : "<STR_LIT>" } ) ) <EOL> for plugin in plugins : <EOL> field_name = plugin . name + "<STR_LIT>" <EOL> choice_field = forms . ChoiceField ( <EOL> label = _ ( "<STR_LIT>" ) , <EOL> choices = [ ( version , version ) for version in plugin . versions ] , <EOL> widget = forms . Select ( <EOL> attrs = { "<STR_LIT:class>" : "<STR_LIT>" <EOL> + field_name + "<STR_LIT>" } ) <EOL> ) <EOL> self . fields [ field_name ] = choice_field <EOL> class Meta : <EOL> name = _ ( "<STR_LIT>" ) <EOL> help_text_template = ( "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> class SelectPlugin ( workflows . Step ) : <EOL> action_class = SelectPluginAction <EOL> class CreateClusterTemplate ( 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 = ( SelectPlugin , ) <EOL> class GeneralConfigAction ( workflows . Action ) : <EOL> hidden_configure_field = forms . CharField ( <EOL> required = False , <EOL> widget = forms . HiddenInput ( attrs = { "<STR_LIT:class>" : "<STR_LIT>" } ) ) <EOL> hidden_to_delete_field = forms . CharField ( <EOL> required = False , <EOL> widget = forms . HiddenInput ( attrs = { "<STR_LIT:class>" : "<STR_LIT>" } ) ) <EOL> cluster_template_name = forms . CharField ( label = _ ( "<STR_LIT>" ) ) <EOL> description = forms . CharField ( label = _ ( "<STR_LIT>" ) , <EOL> required = False , <EOL> widget = forms . Textarea ( attrs = { '<STR_LIT>' : <NUM_LIT:4> } ) ) <EOL> anti_affinity = aa . anti_affinity_field ( ) <EOL> def __init__ ( self , request , * args , ** kwargs ) : <EOL> super ( GeneralConfigAction , self ) . __init__ ( request , * args , ** kwargs ) <EOL> plugin , hadoop_version = whelpers . get_plugin_and_hadoop_version ( request ) <EOL> self . fields [ "<STR_LIT>" ] = forms . CharField ( <EOL> widget = forms . HiddenInput ( ) , <EOL> initial = plugin <EOL> ) <EOL> self . fields [ "<STR_LIT>" ] = forms . CharField ( <EOL> widget = forms . HiddenInput ( ) , <EOL> initial = hadoop_version <EOL> ) <EOL> populate_anti_affinity_choices = aa . populate_anti_affinity_choices <EOL> def get_help_text ( self ) : <EOL> extra = dict ( ) <EOL> plugin , hadoop_version = whelpers . get_plugin_and_hadoop_version ( self . request ) <EOL> extra [ "<STR_LIT>" ] = plugin <EOL> extra [ "<STR_LIT>" ] = hadoop_version <EOL> return super ( GeneralConfigAction , self ) . get_help_text ( extra ) <EOL> def clean ( self ) : <EOL> cleaned_data = super ( GeneralConfigAction , self ) . clean ( ) <EOL> if cleaned_data . get ( "<STR_LIT>" , None ) == "<STR_LIT>" : <EOL> self . _errors = dict ( ) <EOL> return cleaned_data <EOL> class Meta : <EOL> name = _ ( "<STR_LIT>" ) <EOL> help_text_template = ( "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> class GeneralConfig ( workflows . Step ) : <EOL> action_class = GeneralConfigAction <EOL> contributes = ( "<STR_LIT>" , ) <EOL> def contribute ( self , data , context ) : <EOL> for k , v in data . items ( ) : <EOL> context [ "<STR_LIT>" + k ] = v <EOL> post = self . workflow . request . POST <EOL> context [ '<STR_LIT>' ] = post . getlist ( "<STR_LIT>" ) <EOL> return context <EOL> class ConfigureNodegroupsAction ( workflows . Action ) : <EOL> hidden_nodegroups_field = forms . CharField ( <EOL> required = False , <EOL> widget = forms . HiddenInput ( attrs = { "<STR_LIT:class>" : "<STR_LIT>" } ) ) <EOL> forms_ids = forms . CharField ( <EOL> required = False , <EOL> widget = forms . HiddenInput ( ) ) <EOL> def __init__ ( self , request , * args , ** kwargs ) : <EOL> super ( ConfigureNodegroupsAction , self ) . __init__ ( request , * args , ** kwargs ) <EOL> plugin , hadoop_version = whelpers . get_plugin_and_hadoop_version ( request ) <EOL> self . templates = saharaclient . nodegroup_template_find ( <EOL> request , plugin_name = plugin , hadoop_version = hadoop_version ) <EOL> deletable = request . REQUEST . get ( "<STR_LIT>" , dict ( ) ) <EOL> if '<STR_LIT>' in request . POST : <EOL> self . groups = [ ] <EOL> for id in json . loads ( request . POST [ '<STR_LIT>' ] ) : <EOL> group_name = "<STR_LIT>" + str ( id ) <EOL> template_id = "<STR_LIT>" + str ( id ) <EOL> count = "<STR_LIT>" + str ( id ) <EOL> serialized = "<STR_LIT>" + str ( id ) <EOL> self . groups . append ( { "<STR_LIT:name>" : request . POST [ group_name ] , <EOL> "<STR_LIT>" : request . POST [ template_id ] , <EOL> "<STR_LIT:count>" : request . POST [ count ] , <EOL> "<STR_LIT:id>" : id , <EOL> "<STR_LIT>" : deletable . get ( <EOL> request . POST [ group_name ] , "<STR_LIT:true>" ) , <EOL> "<STR_LIT>" : request . POST [ serialized ] } ) <EOL> whelpers . build_node_group_fields ( self , <EOL> group_name , <EOL> template_id , <EOL> count , <EOL> serialized ) <EOL> def clean ( self ) : <EOL> cleaned_data = super ( ConfigureNodegroupsAction , self ) . clean ( ) <EOL> if cleaned_data . get ( "<STR_LIT>" , None ) == "<STR_LIT>" : <EOL> self . _errors = dict ( ) <EOL> return cleaned_data <EOL> class Meta : <EOL> name = _ ( "<STR_LIT>" ) <EOL> class ConfigureNodegroups ( workflows . Step ) : <EOL> action_class = ConfigureNodegroupsAction <EOL> contributes = ( "<STR_LIT>" , ) <EOL> template_name = ( "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> def contribute ( self , data , context ) : <EOL> for k , v in data . items ( ) : <EOL> context [ "<STR_LIT>" + k ] = v <EOL> return context <EOL> class ConfigureClusterTemplate ( whelpers . ServiceParametersWorkflow , <EOL> whelpers . StatusFormatMixin ) : <EOL> slug = "<STR_LIT>" <EOL> name = _ ( "<STR_LIT>" ) <EOL> finalize_button_name = _ ( "<STR_LIT>" ) <EOL> success_message = _ ( "<STR_LIT>" ) <EOL> name_property = "<STR_LIT>" <EOL> success_url = "<STR_LIT>" <EOL> default_steps = ( GeneralConfig , <EOL> ConfigureNodegroups ) <EOL> def __init__ ( self , request , context_seed , entry_point , * args , ** kwargs ) : <EOL> ConfigureClusterTemplate . _cls_registry = set ( [ ] ) <EOL> hlps = helpers . Helpers ( request ) <EOL> plugin , hadoop_version = whelpers . get_plugin_and_hadoop_version ( request ) <EOL> general_parameters = hlps . get_cluster_general_configs ( <EOL> plugin , <EOL> hadoop_version ) <EOL> service_parameters = hlps . get_targeted_cluster_configs ( <EOL> plugin , <EOL> hadoop_version ) <EOL> self . _populate_tabs ( general_parameters , service_parameters ) <EOL> super ( ConfigureClusterTemplate , self ) . __init__ ( request , <EOL> context_seed , <EOL> entry_point , <EOL> * args , ** kwargs ) <EOL> def is_valid ( self ) : <EOL> steps_valid = True <EOL> for step in self . steps : <EOL> if not step . action . is_valid ( ) : <EOL> steps_valid = False <EOL> step . has_errors = True <EOL> errors_fields = list ( step . action . errors . keys ( ) ) <EOL> step . action . errors_fields = errors_fields <EOL> if not steps_valid : <EOL> return steps_valid <EOL> return self . validate ( self . context ) <EOL> def handle ( self , request , context ) : <EOL> try : <EOL> node_groups = [ ] <EOL> configs_dict = whelpers . parse_configs_from_context ( context , <EOL> self . defaults ) <EOL> ids = json . loads ( context [ '<STR_LIT>' ] ) <EOL> for id in ids : <EOL> name = context [ '<STR_LIT>' + str ( id ) ] <EOL> template_id = context [ '<STR_LIT>' + str ( id ) ] <EOL> count = context [ '<STR_LIT>' + str ( id ) ] <EOL> raw_ng = context . get ( "<STR_LIT>" + str ( id ) ) <EOL> if raw_ng and raw_ng != '<STR_LIT:null>' : <EOL> ng = json . loads ( base64 . urlsafe_b64decode ( str ( raw_ng ) ) ) <EOL> else : <EOL> ng = dict ( ) <EOL> ng [ "<STR_LIT:name>" ] = name <EOL> ng [ "<STR_LIT:count>" ] = count <EOL> if template_id and template_id != u'<STR_LIT:None>' : <EOL> ng [ "<STR_LIT>" ] = template_id <EOL> node_groups . append ( ng ) <EOL> plugin , hadoop_version = whelpers . get_plugin_and_hadoop_version ( request ) <EOL> saharaclient . cluster_template_create ( <EOL> request , <EOL> context [ "<STR_LIT>" ] , <EOL> plugin , <EOL> hadoop_version , <EOL> context [ "<STR_LIT>" ] , <EOL> configs_dict , <EOL> node_groups , <EOL> context [ "<STR_LIT>" ] , <EOL> ) <EOL> return True <EOL> except api_base . APIException as e : <EOL> self . error_description = str ( e ) <EOL> return False <EOL> except Exception : <EOL> exceptions . handle ( request , <EOL> _ ( "<STR_LIT>" ) ) <EOL> return False </s>
