2 import xml.parsers.expat
7 from Exceptions import *
10 def create_auth_structure( vars, call_params ):
12 create and return an authentication structure for a Boot API
13 call. Vars contains the boot manager runtime variables, and
14 call_params is a tuple of the parameters that will be passed to the
15 API call. Return None if unable to (typically due to missing
16 keys in vars, such as node_id or node_key)
20 auth['AuthMethod']= 'hmac'
23 network= vars['NETWORK_SETTINGS']
25 auth['node_id']= vars['NODE_ID']
26 auth['node_ip']= network['ip']
27 node_key= vars['NODE_KEY']
31 msg= serialize_params(call_params)
32 node_hmac= hmac.new(node_key,msg,sha).hexdigest()
33 auth['value']= node_hmac
39 def serialize_params( call_params ):
41 convert a list of parameters into a format that will be used in the
42 hmac generation. both the boot manager and plc must have a common
43 format. full documentation is in the boot manager technical document,
44 but essentially we are going to take all the values (and keys for
45 dictionary objects), and put them into a list. sort them, and combine
46 them into one long string encased in a set of braces.
49 # if there are no parameters, just return empty paren set
50 if len(call_params) == 0:
55 for param in call_params:
56 if isinstance(param,list) or isinstance(param,tuple):
57 values= values + map(str,param)
58 elif isinstance(param,dict):
59 values= values + collapse_dict(param)
61 values.append( str(param) )
64 values= "[" + string.join(values,"") + "]"
68 def collapse_dict( value ):
70 given a dictionary, return a list of all the keys and values as strings,
71 in no particular order
76 if not isinstance(value,dict):
79 for key in value.keys():
81 if isinstance(key_value,list) or isinstance(key_value,tuple):
82 item_list= item_list + map(str,key_value)
83 elif isinstance(key_value,dict):
84 item_list= item_list + collapse_dict(key_value)
86 item_list.append( str(key_value) )
92 def call_api_function( vars, function, user_params ):
94 call the named api function with params, and return the
95 value to the caller. the authentication structure is handled
96 automatically, and doesn't need to be passed in with params.
98 If the call fails, a BootManagerException is raised.
102 api_server= vars['API_SERVER_INST']
104 raise BootManagerException, "No connection to the API server exists."
106 auth= create_auth_structure(vars,user_params)
108 raise BootManagerException, \
109 "Could not create auth structure, missing values."
112 params= params + user_params
115 exec( "rc= api_server.%s(*params)" % function )
117 except xmlrpclib.Fault, fault:
118 raise BootManagerException, "API Fault: %s" % fault
119 except xmlrpclib.ProtocolError, err:
120 raise BootManagerException,"XML RPC protocol error: %s" % err
121 except xml.parsers.expat.ExpatError, err:
122 raise BootManagerException,"XML parsing error: %s" % err