<s> import copy <EOL> import datetime <EOL> import time <EOL> from oslo_serialization import jsonutils as json <EOL> from oslo_utils import reflection <EOL> import six <EOL> from sahara import context <EOL> from sahara . i18n import _ <EOL> from sahara . plugins . cdh import exceptions as ex <EOL> class Attr ( object ) : <EOL> """<STR_LIT>""" <EOL> DATE_FMT = "<STR_LIT>" <EOL> def __init__ ( self , atype = None , rw = True , is_api_list = False ) : <EOL> self . _atype = atype <EOL> self . _is_api_list = is_api_list <EOL> self . rw = rw <EOL> def to_json ( self , value , preserve_ro ) : <EOL> """<STR_LIT>""" <EOL> if hasattr ( value , '<STR_LIT>' ) : <EOL> return value . to_json_dict ( preserve_ro ) <EOL> elif isinstance ( value , dict ) and self . _atype == ApiConfig : <EOL> return config_to_api_list ( value ) <EOL> elif isinstance ( value , datetime . datetime ) : <EOL> return value . strftime ( self . DATE_FMT ) <EOL> elif isinstance ( value , list ) or isinstance ( value , tuple ) : <EOL> if self . _is_api_list : <EOL> return ApiList ( value ) . to_json_dict ( ) <EOL> else : <EOL> return [ self . to_json ( x , preserve_ro ) for x in value ] <EOL> else : <EOL> return value <EOL> def from_json ( self , resource_root , data ) : <EOL> """<STR_LIT>""" <EOL> if data is None : <EOL> return None <EOL> if self . _atype == datetime . datetime : <EOL> return datetime . datetime . strptime ( data , self . DATE_FMT ) <EOL> elif self . _atype == ApiConfig : <EOL> if not data [ '<STR_LIT>' ] : <EOL> return { } <EOL> first = data [ '<STR_LIT>' ] [ <NUM_LIT:0> ] <EOL> return json_to_config ( data , len ( first ) == <NUM_LIT:2> ) <EOL> elif self . _is_api_list : <EOL> return ApiList . from_json_dict ( data , resource_root , self . _atype ) <EOL> elif isinstance ( data , list ) : <EOL> return [ self . from_json ( resource_root , x ) for x in data ] <EOL> elif hasattr ( self . _atype , '<STR_LIT>' ) : <EOL> return self . _atype . from_json_dict ( data , resource_root ) <EOL> else : <EOL> return data <EOL> class ROAttr ( Attr ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , atype = None , is_api_list = False ) : <EOL> Attr . __init__ ( self , atype = atype , rw = False , is_api_list = is_api_list ) <EOL> def check_api_version ( resource_root , min_version ) : <EOL> """<STR_LIT>""" <EOL> if resource_root . version < min_version : <EOL> raise ex . CMApiVersionError ( <EOL> _ ( "<STR_LIT>" ) <EOL> % { '<STR_LIT>' : min_version , '<STR_LIT>' : resource_root . version } ) <EOL> def call ( method , path , ret_type , <EOL> ret_is_list = False , data = None , params = None , api_version = <NUM_LIT:1> ) : <EOL> """<STR_LIT>""" <EOL> check_api_version ( method . im_self , api_version ) <EOL> if data is not None : <EOL> data = json . dumps ( Attr ( is_api_list = True ) . to_json ( data , False ) ) <EOL> ret = method ( path , data = data , params = params ) <EOL> else : <EOL> ret = method ( path , params = params ) <EOL> if ret_type is None : <EOL> return <EOL> elif ret_is_list : <EOL> return ApiList . from_json_dict ( ret , method . im_self , ret_type ) <EOL> elif isinstance ( ret , list ) : <EOL> return [ ret_type . from_json_dict ( x , method . im_self ) for x in ret ] <EOL> else : <EOL> return ret_type . from_json_dict ( ret , method . im_self ) <EOL> class BaseApiObject ( object ) : <EOL> """<STR_LIT>""" <EOL> _ATTRIBUTES = { } <EOL> _WHITELIST = ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> @ classmethod <EOL> def _get_attributes ( cls ) : <EOL> """<STR_LIT>""" <EOL> return cls . _ATTRIBUTES <EOL> @ staticmethod <EOL> def init ( obj , resource_root , attrs = None ) : <EOL> """<STR_LIT>""" <EOL> str_attrs = { } <EOL> if attrs : <EOL> for k , v in six . iteritems ( attrs ) : <EOL> if k not in ( '<STR_LIT>' , '<STR_LIT>' ) : <EOL> str_attrs [ k ] = v <EOL> BaseApiObject . __init__ ( obj , resource_root , ** str_attrs ) <EOL> def __init__ ( self , resource_root , ** attrs ) : <EOL> """<STR_LIT>""" <EOL> self . _resource_root = resource_root <EOL> for name , attr in six . iteritems ( self . _get_attributes ( ) ) : <EOL> object . __setattr__ ( self , name , None ) <EOL> if attrs : <EOL> self . _set_attrs ( attrs , from_json = False ) <EOL> def _set_attrs ( self , attrs , allow_ro = False , from_json = True ) : <EOL> """<STR_LIT>""" <EOL> for k , v in six . iteritems ( attrs ) : <EOL> attr = self . _check_attr ( k , allow_ro ) <EOL> if attr and from_json : <EOL> v = attr . from_json ( self . _get_resource_root ( ) , v ) <EOL> object . __setattr__ ( self , k , v ) <EOL> def __setattr__ ( self , name , val ) : <EOL> if name not in BaseApiObject . _WHITELIST : <EOL> self . _check_attr ( name , False ) <EOL> object . __setattr__ ( self , name , val ) <EOL> def _check_attr ( self , name , allow_ro ) : <EOL> cls_name = reflection . get_class_name ( self , fully_qualified = False ) <EOL> if name not in self . _get_attributes ( ) : <EOL> raise ex . CMApiAttributeError ( <EOL> _ ( '<STR_LIT>' ) <EOL> % { '<STR_LIT>' : name , '<STR_LIT>' : cls_name } ) <EOL> attr = self . _get_attributes ( ) [ name ] <EOL> if not allow_ro and attr and not attr . rw : <EOL> raise ex . CMApiAttributeError ( <EOL> _ ( '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> % { '<STR_LIT>' : name , '<STR_LIT>' : cls_name } ) <EOL> return attr <EOL> def _get_resource_root ( self ) : <EOL> return self . _resource_root <EOL> def _update ( self , api_obj ) : <EOL> """<STR_LIT>""" <EOL> if not isinstance ( self , api_obj . __class__ ) : <EOL> raise ex . CMApiValueError ( <EOL> _ ( "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> % { '<STR_LIT>' : self . __class__ , '<STR_LIT>' : api_obj . __class__ } ) <EOL> for name in self . _get_attributes ( ) . keys ( ) : <EOL> try : <EOL> val = getattr ( api_obj , name ) <EOL> setattr ( self , name , val ) <EOL> except AttributeError : <EOL> pass <EOL> def to_json_dict ( self , preserve_ro = False ) : <EOL> dic = { } <EOL> for name , attr in six . iteritems ( self . _get_attributes ( ) ) : <EOL> if not preserve_ro and attr and not attr . rw : <EOL> continue <EOL> try : <EOL> value = getattr ( self , name ) <EOL> if value is not None : <EOL> if attr : <EOL> dic [ name ] = attr . to_json ( value , preserve_ro ) <EOL> else : <EOL> dic [ name ] = value <EOL> except AttributeError : <EOL> pass <EOL> return dic <EOL> def __str__ ( self ) : <EOL> """<STR_LIT>""" <EOL> cls_name = reflection . get_class_name ( self , fully_qualified = False ) <EOL> name = list ( self . _get_attributes ( ) . keys ( ) ) [ <NUM_LIT:0> ] <EOL> value = getattr ( self , name , None ) <EOL> return "<STR_LIT>" % ( cls_name , name , value ) <EOL> @ classmethod <EOL> def from_json_dict ( cls , dic , resource_root ) : <EOL> obj = cls ( resource_root ) <EOL> obj . _set_attrs ( dic , allow_ro = True ) <EOL> return obj <EOL> class BaseApiResource ( BaseApiObject ) : <EOL> """<STR_LIT>""" <EOL> def _api_version ( self ) : <EOL> """<STR_LIT>""" <EOL> return <NUM_LIT:1> <EOL> def _path ( self ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError <EOL> def _require_min_api_version ( self , version ) : <EOL> """<STR_LIT>""" <EOL> actual_version = self . _get_resource_root ( ) . version <EOL> version = max ( version , self . _api_version ( ) ) <EOL> if actual_version < version : <EOL> raise ex . CMApiVersionError ( <EOL> _ ( "<STR_LIT>" ) <EOL> % { '<STR_LIT>' : version , '<STR_LIT>' : actual_version } ) <EOL> def _cmd ( self , command , data = None , params = None , api_version = <NUM_LIT:1> ) : <EOL> """<STR_LIT>""" <EOL> return self . _post ( "<STR_LIT>" + command , ApiCommand , <EOL> data = data , params = params , api_version = api_version ) <EOL> def _get_config ( self , rel_path , view , api_version = <NUM_LIT:1> ) : <EOL> """<STR_LIT>""" <EOL> self . _require_min_api_version ( api_version ) <EOL> params = dict ( view = view ) if view else None <EOL> resp = self . _get_resource_root ( ) . get ( self . _path ( ) + '<STR_LIT:/>' + rel_path , <EOL> params = params ) <EOL> return json_to_config ( resp , view == '<STR_LIT>' ) <EOL> def _update_config ( self , rel_path , config , api_version = <NUM_LIT:1> ) : <EOL> self . _require_min_api_version ( api_version ) <EOL> resp = self . _get_resource_root ( ) . put ( self . _path ( ) + '<STR_LIT:/>' + rel_path , <EOL> data = config_to_json ( config ) ) <EOL> return json_to_config ( resp , False ) <EOL> def _delete ( self , rel_path , ret_type , ret_is_list = False , params = None , <EOL> api_version = <NUM_LIT:1> ) : <EOL> return self . _call ( '<STR_LIT>' , rel_path , ret_type , ret_is_list , None , <EOL> params , api_version ) <EOL> def _get ( self , rel_path , ret_type , ret_is_list = False , params = None , <EOL> api_version = <NUM_LIT:1> ) : <EOL> return self . _call ( '<STR_LIT>' , rel_path , ret_type , ret_is_list , None , <EOL> params , api_version ) <EOL> def _post ( self , rel_path , ret_type , ret_is_list = False , data = None , <EOL> params = None , api_version = <NUM_LIT:1> ) : <EOL> return self . _call ( '<STR_LIT>' , rel_path , ret_type , ret_is_list , data , <EOL> params , api_version ) <EOL> def _put ( self , rel_path , ret_type , ret_is_list = False , data = None , <EOL> params = None , api_version = <NUM_LIT:1> ) : <EOL> return self . _call ( '<STR_LIT>' , rel_path , ret_type , ret_is_list , data , <EOL> params , api_version ) <EOL> def _call ( self , method , rel_path , ret_type , ret_is_list = False , data = None , <EOL> params = None , api_version = <NUM_LIT:1> ) : <EOL> path = self . _path ( ) <EOL> if rel_path : <EOL> path += '<STR_LIT:/>' + rel_path <EOL> return call ( getattr ( self . _get_resource_root ( ) , method ) , <EOL> path , <EOL> ret_type , <EOL> ret_is_list , <EOL> data , <EOL> params , <EOL> api_version ) <EOL> class ApiList ( BaseApiObject ) : <EOL> """<STR_LIT>""" <EOL> LIST_KEY = "<STR_LIT>" <EOL> def __init__ ( self , objects , resource_root = None , ** attrs ) : <EOL> BaseApiObject . __init__ ( self , resource_root , ** attrs ) <EOL> object . __setattr__ ( self , '<STR_LIT>' , objects ) <EOL> def __str__ ( self ) : <EOL> return ( "<STR_LIT>" % ( len ( self . objects ) , <EOL> "<STR_LIT:U+002CU+0020>" . join ( [ str ( item ) for item in self . objects ] ) ) ) <EOL> def to_json_dict ( self , preserve_ro = False ) : <EOL> ret = BaseApiObject . to_json_dict ( self , preserve_ro ) <EOL> attr = Attr ( ) <EOL> ret [ ApiList . LIST_KEY ] = [ attr . to_json ( x , preserve_ro ) <EOL> for x in self . objects ] <EOL> return ret <EOL> def __len__ ( self ) : <EOL> return self . objects . __len__ ( ) <EOL> def __iter__ ( self ) : <EOL> return self . objects . __iter__ ( ) <EOL> def __getitem__ ( self , i ) : <EOL> return self . objects . __getitem__ ( i ) <EOL> def __getslice__ ( self , i , j ) : <EOL> return self . objects . __getslice__ ( i , j ) <EOL> @ classmethod <EOL> def from_json_dict ( cls , dic , resource_root , member_cls = None ) : <EOL> if not member_cls : <EOL> member_cls = cls . _MEMBER_CLASS <EOL> attr = Attr ( atype = member_cls ) <EOL> items = [ ] <EOL> if ApiList . LIST_KEY in dic : <EOL> items = [ attr . from_json ( resource_root , x ) <EOL> for x in dic [ ApiList . LIST_KEY ] ] <EOL> ret = cls ( items ) <EOL> if cls . _ATTRIBUTES : <EOL> if ApiList . LIST_KEY in dic : <EOL> dic = copy . copy ( dic ) <EOL> del dic [ ApiList . LIST_KEY ] <EOL> ret . _set_attrs ( dic , allow_ro = True ) <EOL> return ret <EOL> class ApiHostRef ( BaseApiObject ) : <EOL> _ATTRIBUTES = { <EOL> '<STR_LIT>' : None , <EOL> } <EOL> def __init__ ( self , resource_root , hostId = None ) : <EOL> BaseApiObject . init ( self , resource_root , locals ( ) ) <EOL> def __str__ ( self ) : <EOL> return "<STR_LIT>" % ( self . hostId ) <EOL> class ApiServiceRef ( BaseApiObject ) : <EOL> _ATTRIBUTES = { <EOL> '<STR_LIT>' : None , <EOL> '<STR_LIT>' : None , <EOL> '<STR_LIT>' : None , <EOL> } <EOL> def __init__ ( self , resource_root , serviceName = None , clusterName = None , <EOL> peerName = None ) : <EOL> BaseApiObject . init ( self , resource_root , locals ( ) ) <EOL> class ApiClusterRef ( BaseApiObject ) : <EOL> _ATTRIBUTES = { <EOL> '<STR_LIT>' : None , <EOL> } <EOL> def __init__ ( self , resource_root , clusterName = None ) : <EOL> BaseApiObject . init ( self , resource_root , locals ( ) ) <EOL> class ApiRoleRef ( BaseApiObject ) : <EOL> _ATTRIBUTES = { <EOL> '<STR_LIT>' : None , <EOL> '<STR_LIT>' : None , <EOL> '<STR_LIT>' : None , <EOL> } <EOL> def __init__ ( self , resource_root , serviceName = None , roleName = None , <EOL> clusterName = None ) : <EOL> BaseApiObject . init ( self , resource_root , locals ( ) ) <EOL> class ApiRoleConfigGroupRef ( BaseApiObject ) : <EOL> _ATTRIBUTES = { <EOL> '<STR_LIT>' : None , <EOL> } <EOL> def __init__ ( self , resource_root , roleConfigGroupName = None ) : <EOL> BaseApiObject . init ( self , resource_root , locals ( ) ) <EOL> class ApiCommand ( BaseApiObject ) : <EOL> SYNCHRONOUS_COMMAND_ID = - <NUM_LIT:1> <EOL> @ classmethod <EOL> def _get_attributes ( cls ) : <EOL> if not ( '<STR_LIT>' in cls . __dict__ ) : <EOL> cls . _ATTRIBUTES = { <EOL> '<STR_LIT:id>' : ROAttr ( ) , <EOL> '<STR_LIT:name>' : ROAttr ( ) , <EOL> '<STR_LIT>' : ROAttr ( datetime . datetime ) , <EOL> '<STR_LIT>' : ROAttr ( datetime . datetime ) , <EOL> '<STR_LIT>' : ROAttr ( ) , <EOL> '<STR_LIT:success>' : ROAttr ( ) , <EOL> '<STR_LIT>' : ROAttr ( ) , <EOL> '<STR_LIT>' : ROAttr ( ApiClusterRef ) , <EOL> '<STR_LIT>' : ROAttr ( ApiServiceRef ) , <EOL> '<STR_LIT>' : ROAttr ( ApiRoleRef ) , <EOL> '<STR_LIT>' : ROAttr ( ApiHostRef ) , <EOL> '<STR_LIT>' : ROAttr ( ApiCommand , is_api_list = True ) , <EOL> '<STR_LIT>' : ROAttr ( ApiCommand ) , <EOL> '<STR_LIT>' : ROAttr ( ) , <EOL> } <EOL> return cls . _ATTRIBUTES <EOL> def __str__ ( self ) : <EOL> return ( "<STR_LIT>" <EOL> % ( self . name , self . id , self . active , self . success ) ) <EOL> def _path ( self ) : <EOL> return '<STR_LIT>' % self . id <EOL> def fetch ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . id == ApiCommand . SYNCHRONOUS_COMMAND_ID : <EOL> return self <EOL> resp = self . _get_resource_root ( ) . get ( self . _path ( ) ) <EOL> return ApiCommand . from_json_dict ( resp , self . _get_resource_root ( ) ) <EOL> def wait ( self , timeout = None ) : <EOL> """<STR_LIT>""" <EOL> if self . id == ApiCommand . SYNCHRONOUS_COMMAND_ID : <EOL> return self <EOL> SLEEP_SEC = <NUM_LIT:5> <EOL> if timeout is None : <EOL> deadline = None <EOL> else : <EOL> deadline = time . time ( ) + timeout <EOL> while True : <EOL> cmd = self . fetch ( ) <EOL> if not cmd . active : <EOL> return cmd <EOL> if deadline is not None : <EOL> now = time . time ( ) <EOL> if deadline < now : <EOL> return cmd <EOL> else : <EOL> context . sleep ( min ( SLEEP_SEC , deadline - now ) ) <EOL> else : <EOL> context . sleep ( SLEEP_SEC ) <EOL> def abort ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . id == ApiCommand . SYNCHRONOUS_COMMAND_ID : <EOL> return self <EOL> path = self . _path ( ) + '<STR_LIT>' <EOL> resp = self . _get_resource_root ( ) . post ( path ) <EOL> return ApiCommand . from_json_dict ( resp , self . _get_resource_root ( ) ) <EOL> class ApiBulkCommandList ( ApiList ) : <EOL> _ATTRIBUTES = { <EOL> '<STR_LIT>' : ROAttr ( ) , <EOL> } <EOL> _MEMBER_CLASS = ApiCommand <EOL> class ApiConfig ( BaseApiObject ) : <EOL> _ATTRIBUTES = { <EOL> '<STR_LIT:name>' : None , <EOL> '<STR_LIT:value>' : None , <EOL> '<STR_LIT>' : ROAttr ( ) , <EOL> '<STR_LIT:default>' : ROAttr ( ) , <EOL> '<STR_LIT>' : ROAttr ( ) , <EOL> '<STR_LIT:description>' : ROAttr ( ) , <EOL> '<STR_LIT>' : ROAttr ( ) , <EOL> '<STR_LIT>' : ROAttr ( ) , <EOL> '<STR_LIT>' : ROAttr ( ) , <EOL> } <EOL> def __init__ ( self , resource_root , name = None , value = None ) : <EOL> BaseApiObject . init ( self , resource_root , locals ( ) ) <EOL> def __str__ ( self ) : <EOL> return "<STR_LIT>" % ( self . name , self . value ) <EOL> def config_to_api_list ( dic ) : <EOL> """<STR_LIT>""" <EOL> config = [ ] <EOL> for k , v in six . iteritems ( dic ) : <EOL> config . append ( { '<STR_LIT:name>' : k , '<STR_LIT:value>' : v } ) <EOL> return { ApiList . LIST_KEY : config } <EOL> def config_to_json ( dic ) : <EOL> """<STR_LIT>""" <EOL> return json . dumps ( config_to_api_list ( dic ) ) <EOL> def json_to_config ( dic , full = False ) : <EOL> """<STR_LIT>""" <EOL> config = { } <EOL> for entry in dic [ '<STR_LIT>' ] : <EOL> k = entry [ '<STR_LIT:name>' ] <EOL> if full : <EOL> config [ k ] = ApiConfig . from_json_dict ( entry , None ) <EOL> else : <EOL> config [ k ] = entry . get ( '<STR_LIT:value>' ) <EOL> return config </s>
