<s> import base64 <EOL> import json <EOL> import logging <EOL> from django . utils . translation import ugettext_lazy as _ <EOL> from horizon import exceptions <EOL> from openstack_dashboard . api import sahara as saharaclient <EOL> import openstack_dashboard . dashboards . project . data_processing . cluster_templates . workflows . create as clt_create_flow <EOL> import openstack_dashboard . dashboards . project . data_processing . clusters . workflows . create as cl_create_flow <EOL> from openstack_dashboard . dashboards . project . data_processing . utils import workflow_helpers <EOL> from saharaclient . api import base as api_base <EOL> LOG = logging . getLogger ( __name__ ) <EOL> class NodeGroupsStep ( clt_create_flow . ConfigureNodegroups ) : <EOL> pass <EOL> class ScaleCluster ( cl_create_flow . ConfigureCluster , <EOL> workflow_helpers . StatusFormatMixin ) : <EOL> slug = "<STR_LIT>" <EOL> name = _ ( "<STR_LIT>" ) <EOL> finalize_button_name = _ ( "<STR_LIT>" ) <EOL> success_url = "<STR_LIT>" <EOL> default_steps = ( NodeGroupsStep , ) <EOL> def __init__ ( self , request , context_seed , entry_point , * args , ** kwargs ) : <EOL> ScaleCluster . _cls_registry = set ( [ ] ) <EOL> self . success_message = _ ( "<STR_LIT>" ) <EOL> cluster_id = context_seed [ "<STR_LIT>" ] <EOL> try : <EOL> cluster = saharaclient . cluster_get ( request , cluster_id ) <EOL> plugin = cluster . plugin_name <EOL> hadoop_version = cluster . hadoop_version <EOL> deletable = dict ( ) <EOL> for group in cluster . node_groups : <EOL> deletable [ group [ "<STR_LIT:name>" ] ] = "<STR_LIT:false>" <EOL> request . GET = request . GET . copy ( ) <EOL> request . GET . update ( { <EOL> "<STR_LIT>" : cluster_id , <EOL> "<STR_LIT>" : plugin , <EOL> "<STR_LIT>" : hadoop_version , <EOL> "<STR_LIT>" : deletable <EOL> } ) <EOL> super ( ScaleCluster , self ) . __init__ ( request , context_seed , <EOL> entry_point , * args , <EOL> ** kwargs ) <EOL> for step in self . steps : <EOL> if not isinstance ( step , clt_create_flow . ConfigureNodegroups ) : <EOL> continue <EOL> ng_action = step . action <EOL> template_ngs = cluster . node_groups <EOL> if '<STR_LIT>' in request . POST : <EOL> continue <EOL> ng_action . groups = [ ] <EOL> for i , templ_ng in enumerate ( template_ngs ) : <EOL> group_name = "<STR_LIT>" % i <EOL> template_id = "<STR_LIT>" % i <EOL> count = "<STR_LIT>" % i <EOL> serialized = "<STR_LIT>" % i <EOL> serialized_val = base64 . urlsafe_b64encode ( json . dumps ( <EOL> workflow_helpers . clean_node_group ( templ_ng ) ) ) <EOL> ng_action . groups . append ( { <EOL> "<STR_LIT:name>" : templ_ng [ "<STR_LIT:name>" ] , <EOL> "<STR_LIT>" : templ_ng [ "<STR_LIT>" ] , <EOL> "<STR_LIT:count>" : templ_ng [ "<STR_LIT:count>" ] , <EOL> "<STR_LIT:id>" : i , <EOL> "<STR_LIT>" : "<STR_LIT:false>" , <EOL> "<STR_LIT>" : serialized_val <EOL> } ) <EOL> workflow_helpers . build_node_group_fields ( ng_action , <EOL> group_name , <EOL> template_id , <EOL> count , <EOL> serialized ) <EOL> except Exception : <EOL> exceptions . handle ( request , <EOL> _ ( "<STR_LIT>" ) ) <EOL> def format_status_message ( self , message ) : <EOL> error_description = getattr ( self , '<STR_LIT>' , None ) <EOL> if error_description : <EOL> return error_description <EOL> else : <EOL> return self . success_message <EOL> def handle ( self , request , context ) : <EOL> cluster_id = request . GET [ "<STR_LIT>" ] <EOL> try : <EOL> cluster = saharaclient . cluster_get ( request , cluster_id ) <EOL> existing_node_groups = set ( [ ] ) <EOL> for ng in cluster . node_groups : <EOL> existing_node_groups . add ( ng [ "<STR_LIT:name>" ] ) <EOL> scale_object = dict ( ) <EOL> ids = json . loads ( context [ "<STR_LIT>" ] ) <EOL> for _id in ids : <EOL> name = context [ "<STR_LIT>" % _id ] <EOL> template_id = context [ "<STR_LIT>" % _id ] <EOL> count = context [ "<STR_LIT>" % _id ] <EOL> if name not in existing_node_groups : <EOL> if "<STR_LIT>" not in scale_object : <EOL> scale_object [ "<STR_LIT>" ] = [ ] <EOL> scale_object [ "<STR_LIT>" ] . append ( <EOL> { "<STR_LIT:name>" : name , <EOL> "<STR_LIT>" : template_id , <EOL> "<STR_LIT:count>" : int ( count ) } ) <EOL> else : <EOL> old_count = None <EOL> for ng in cluster . node_groups : <EOL> if name == ng [ "<STR_LIT:name>" ] : <EOL> old_count = ng [ "<STR_LIT:count>" ] <EOL> break <EOL> if old_count != count : <EOL> if "<STR_LIT>" not in scale_object : <EOL> scale_object [ "<STR_LIT>" ] = [ ] <EOL> scale_object [ "<STR_LIT>" ] . append ( <EOL> { "<STR_LIT:name>" : name , <EOL> "<STR_LIT:count>" : int ( count ) } <EOL> ) <EOL> except Exception : <EOL> scale_object = { } <EOL> exceptions . handle ( request , <EOL> _ ( "<STR_LIT>" ) ) <EOL> try : <EOL> saharaclient . cluster_scale ( request , cluster_id , scale_object ) <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>
