<s> """<STR_LIT>""" <EOL> import json <EOL> import time <EOL> from ovs . log . logHandler import LogHandler <EOL> from ovs . extensions . generic . system import System <EOL> from ovs . extensions . db . etcd . configuration import EtcdConfiguration <EOL> from ovs . extensions . api . client import OVSClient <EOL> from django . views . generic import View <EOL> from django . views . decorators . csrf import csrf_exempt <EOL> from django . http import HttpResponse , HttpResponseBadRequest <EOL> from django . conf import settings <EOL> from oauth2 . decorators import auto_response , limit , authenticated <EOL> from backend . decorators import required_roles , load <EOL> from ovs . dal . lists . bearertokenlist import BearerTokenList <EOL> from ovs . dal . lists . storagerouterlist import StorageRouterList <EOL> from ovs . dal . lists . backendtypelist import BackendTypeList <EOL> logger = LogHandler . get ( '<STR_LIT>' , name = '<STR_LIT>' ) <EOL> class MetadataView ( View ) : <EOL> """<STR_LIT>""" <EOL> @ auto_response ( ) <EOL> @ limit ( amount = <NUM_LIT> , per = <NUM_LIT> , timeout = <NUM_LIT> ) <EOL> def get ( self , request , * args , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> _ = args , kwargs <EOL> data = { '<STR_LIT>' : False , <EOL> '<STR_LIT>' : None , <EOL> '<STR_LIT>' : { } , <EOL> '<STR_LIT:username>' : None , <EOL> '<STR_LIT>' : None , <EOL> '<STR_LIT>' : [ ] , <EOL> '<STR_LIT>' : { } , <EOL> '<STR_LIT>' : [ sr . ip for sr in StorageRouterList . get_storagerouters ( ) ] , <EOL> '<STR_LIT>' : list ( settings . VERSION ) , <EOL> '<STR_LIT>' : { } } <EOL> try : <EOL> plugins = { } <EOL> for backend_type in BackendTypeList . get_backend_types ( ) : <EOL> if backend_type . has_plugin is True : <EOL> if backend_type . code not in plugins : <EOL> plugins [ backend_type . code ] = [ ] <EOL> plugins [ backend_type . code ] += [ '<STR_LIT>' , '<STR_LIT>' ] <EOL> generic_plugins = EtcdConfiguration . get ( '<STR_LIT>' ) <EOL> for plugin_name in generic_plugins : <EOL> if plugin_name not in plugins : <EOL> plugins [ plugin_name ] = [ ] <EOL> plugins [ plugin_name ] += [ '<STR_LIT>' ] <EOL> data [ '<STR_LIT>' ] = plugins <EOL> data [ '<STR_LIT>' ] = { '<STR_LIT>' : EtcdConfiguration . get ( '<STR_LIT>' ) } <EOL> authentication_metadata = { '<STR_LIT>' : System . get_my_storagerouter ( ) . ip } <EOL> for key in [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] : <EOL> if EtcdConfiguration . exists ( '<STR_LIT>' . format ( key ) ) : <EOL> authentication_metadata [ key ] = EtcdConfiguration . get ( '<STR_LIT>' . format ( key ) ) <EOL> data [ '<STR_LIT>' ] = authentication_metadata <EOL> if '<STR_LIT>' not in request . META : <EOL> return HttpResponse , dict ( data . items ( ) + { '<STR_LIT>' : '<STR_LIT>' } . items ( ) ) <EOL> authorization_type , access_token = request . META [ '<STR_LIT>' ] . split ( '<STR_LIT:U+0020>' ) <EOL> if authorization_type != '<STR_LIT>' : <EOL> return HttpResponse , dict ( data . items ( ) + { '<STR_LIT>' : '<STR_LIT>' } . items ( ) ) <EOL> tokens = BearerTokenList . get_by_access_token ( access_token ) <EOL> if len ( tokens ) != <NUM_LIT:1> : <EOL> return HttpResponse , dict ( data . items ( ) + { '<STR_LIT>' : '<STR_LIT>' } . items ( ) ) <EOL> token = tokens [ <NUM_LIT:0> ] <EOL> if token . expiration < time . time ( ) : <EOL> for junction in token . roles . itersafe ( ) : <EOL> junction . delete ( ) <EOL> token . delete ( ) <EOL> return HttpResponse , dict ( data . items ( ) + { '<STR_LIT>' : '<STR_LIT>' } . items ( ) ) <EOL> user = token . client . user <EOL> if not user . is_active : <EOL> return HttpResponse , dict ( data . items ( ) + { '<STR_LIT>' : '<STR_LIT>' } . items ( ) ) <EOL> roles = [ j . role . code for j in token . roles ] <EOL> return HttpResponse , dict ( data . items ( ) + { '<STR_LIT>' : True , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT:username>' : user . username , <EOL> '<STR_LIT>' : user . guid , <EOL> '<STR_LIT>' : roles , <EOL> '<STR_LIT>' : plugins } . items ( ) ) <EOL> except Exception as ex : <EOL> logger . exception ( '<STR_LIT>' . format ( ex ) ) <EOL> return HttpResponse , dict ( data . items ( ) + { '<STR_LIT>' : '<STR_LIT>' } . items ( ) ) <EOL> @ csrf_exempt <EOL> def dispatch ( self , request , * args , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> return super ( MetadataView , self ) . dispatch ( request , * args , ** kwargs ) <EOL> def relay ( * args , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> @ authenticated ( ) <EOL> @ required_roles ( [ '<STR_LIT>' ] ) <EOL> @ load ( ) <EOL> def _relay ( _ , ip , port , client_id , client_secret , version , request ) : <EOL> path = '<STR_LIT>' . format ( request . path . replace ( '<STR_LIT>' , '<STR_LIT>' ) ) <EOL> method = request . META [ '<STR_LIT>' ] . lower ( ) <EOL> client = OVSClient ( ip , port , credentials = ( client_id , client_secret ) , version = version , raw_response = True ) <EOL> if not hasattr ( client , method ) : <EOL> return HttpResponseBadRequest , '<STR_LIT>' <EOL> client_kwargs = { '<STR_LIT>' : request . GET } <EOL> if method != '<STR_LIT>' : <EOL> client_kwargs [ '<STR_LIT:data>' ] = request . POST <EOL> call_response = getattr ( client , method ) ( path , ** client_kwargs ) <EOL> response = HttpResponse ( call_response . text , <EOL> content_type = '<STR_LIT:application/json>' , <EOL> status = call_response . status_code ) <EOL> for header , value in call_response . headers . iteritems ( ) : <EOL> response [ header ] = value <EOL> return response <EOL> try : <EOL> return _relay ( * args , ** kwargs ) <EOL> except Exception as ex : <EOL> message = str ( ex ) <EOL> status_code = <NUM_LIT> <EOL> if hasattr ( ex , '<STR_LIT>' ) : <EOL> message = ex . detail <EOL> if hasattr ( ex , '<STR_LIT>' ) : <EOL> status_code = ex . status_code <EOL> logger . exception ( '<STR_LIT>' . format ( message ) ) <EOL> return HttpResponse ( json . dumps ( { '<STR_LIT:error>' : message } ) , content_type = '<STR_LIT:application/json>' , status = status_code ) </s>
