1*9c5db199SXin Li""" 2*9c5db199SXin LiThis module provides utility functions useful when writing clients for the RPC 3*9c5db199SXin Liserver. 4*9c5db199SXin Li""" 5*9c5db199SXin Li 6*9c5db199SXin Li__author__ = '[email protected] (Steve Howard)' 7*9c5db199SXin Li 8*9c5db199SXin Liimport getpass, os 9*9c5db199SXin Lifrom autotest_lib.frontend.afe.json_rpc import proxy 10*9c5db199SXin Lifrom autotest_lib.client.common_lib import utils 11*9c5db199SXin Li 12*9c5db199SXin Li 13*9c5db199SXin Liclass AuthError(Exception): 14*9c5db199SXin Li pass 15*9c5db199SXin Li 16*9c5db199SXin Lidef add_protocol(hostname): 17*9c5db199SXin Li """Constructs a normalized URL to make RPC calls 18*9c5db199SXin Li 19*9c5db199SXin Li This function exists because our configuration files 20*9c5db199SXin Li (global_config/shadow_config) include only the hostname of the RPC server to 21*9c5db199SXin Li hit, not the protocol (http/https). 22*9c5db199SXin Li To support endpoints that require a specific protocol, we allow the hostname 23*9c5db199SXin Li to include the protocol prefix, and respect the protocol. If no protocol is 24*9c5db199SXin Li provided, http is used, viz, 25*9c5db199SXin Li 26*9c5db199SXin Li add_protocol('cautotest') --> 'http://cautotest' 27*9c5db199SXin Li add_protocol('http://cautotest') --> 'http://cautotest' 28*9c5db199SXin Li add_protocol('https://cautotest') --> 'https://cautotest' 29*9c5db199SXin Li 30*9c5db199SXin Li @param hostname: hostname or url prefix of the RPC server. 31*9c5db199SXin Li @returns: A string URL for the RPC server with the protocl prefix. 32*9c5db199SXin Li """ 33*9c5db199SXin Li if (not hostname.startswith('http://') and 34*9c5db199SXin Li not hostname.startswith('https://')): 35*9c5db199SXin Li return 'http://' + hostname 36*9c5db199SXin Li return hostname 37*9c5db199SXin Li 38*9c5db199SXin Li 39*9c5db199SXin Lidef get_proxy(*args, **kwargs): 40*9c5db199SXin Li """Use this to access the AFE or TKO RPC interfaces.""" 41*9c5db199SXin Li return proxy.ServiceProxy(*args, **kwargs) 42*9c5db199SXin Li 43*9c5db199SXin Li 44*9c5db199SXin Lidef _base_authorization_headers(username, server): 45*9c5db199SXin Li """ 46*9c5db199SXin Li Don't call this directly, call authorization_headers(). 47*9c5db199SXin Li This implementation may be overridden by site code. 48*9c5db199SXin Li 49*9c5db199SXin Li @returns A dictionary of authorization headers to pass in to get_proxy(). 50*9c5db199SXin Li """ 51*9c5db199SXin Li if not username: 52*9c5db199SXin Li if 'AUTOTEST_USER' in os.environ: 53*9c5db199SXin Li username = os.environ['AUTOTEST_USER'] 54*9c5db199SXin Li else: 55*9c5db199SXin Li username = getpass.getuser() 56*9c5db199SXin Li return {'AUTHORIZATION' : username} 57*9c5db199SXin Li 58*9c5db199SXin Li 59*9c5db199SXin Liauthorization_headers = utils.import_site_function( 60*9c5db199SXin Li __file__, 'autotest_lib.frontend.afe.site_rpc_client_lib', 61*9c5db199SXin Li 'authorization_headers', _base_authorization_headers) 62