@@ -35,27 +35,28 @@ class cloudConnection(object):
3535
3636 """ Connections to make API calls to the cloudstack management server
3737 """
38- def __init__ (self , mgmtDet ,asyncTimeout = 3600 , logging = None , scheme = 'http' ,path = 'client/api' ):
38+ def __init__ (self , mgmtDet , asyncTimeout = 3600 , logging = None ,
39+ scheme = 'http' , path = 'client/api' ):
3940 self .loglevel () # Turn off requests logs
4041 self .apiKey = mgmtDet .apiKey
4142 self .securityKey = mgmtDet .securityKey
4243 self .mgtSvr = mgmtDet .mgtSvrIp
4344 self .port = mgmtDet .port
4445 self .user = mgmtDet .user
4546 self .passwd = mgmtDet .passwd
46- self .certCAPath = mgmtDet .certCAPath
47- self .certPath = mgmtDet .certPath
47+ self .certCAPath = mgmtDet .certCAPath
48+ self .certPath = mgmtDet .certPath
4849 self .logging = logging
4950 self .path = path
5051 self .retries = 5
51- self .protocol = "http"
52+ self .protocol = scheme
5253 self .asyncTimeout = asyncTimeout
5354 self .auth = True
5455 if self .port == 8096 or \
5556 (self .apiKey is None and self .securityKey is None ):
5657 self .auth = False
5758 if mgmtDet .useHttps == "True" :
58- self .protocol = "https"
59+ self .protocol = "https"
5960 self .baseurl = "%s://%s:%d/%s" \
6061 % (self .protocol , self .mgtSvr , self .port , self .path )
6162
@@ -143,52 +144,53 @@ def request(self, command, auth=True, payload={}, method='GET'):
143144 payload ["signature" ] = signature
144145
145146 try :
146- '''
147- https_flag : Signifies whether to verify connection over http or https, if set to true uses https otherwise http
148- cert_path : Signifies ca and cert path required by requests library for the connection
149- '''
150- https_flag = False
151- cert_path = ()
152- if self .protocol == "https" :
153- https_flag = True
154- if self .certCAPath != "NA" and self .certPath != "NA" :
155- cert_path = ( self .certCAPath ,self .certPath )
156-
157- '''
158- Verify whether protocol is "http", then call the request over http
159- '''
160- if self .protocol == "http" :
161- if method == 'POST' :
162- response = requests .post (self .baseurl , params = payload , verify = https_flag )
163- else :
164- response = requests .get (self .baseurl , params = payload , verify = https_flag )
147+ #https_flag : whether https enabled or not
148+ #cert_path : ca and cert paths of the https connection
149+ https_flag = False
150+ cert_path = ()
151+ if self .protocol == "https" :
152+ https_flag = True
153+ if self .certCAPath != "NA" and self .certPath != "NA" :
154+ cert_path = (self .certCAPath , self .certPath )
155+ #Verify whether protocol is "http", then call the request over http
156+ if self .protocol == "http" :
157+ if method == 'POST' :
158+ response = requests .post (self .baseurl , params = payload ,
159+ verify = https_flag )
160+ else :
161+ response = requests .get (self .baseurl , params = payload ,
162+ verify = https_flag )
165163 else :
166- exception_check = False
167- exception_info = None
168- '''
169- If protocol is https, then request the url with user provided certificates provided as part of cert
170- '''
171- try :
172- if method == 'POST' :
173- response = requests .post (self .baseurl , params = payload , cert = cert_path , verify = https_flag )
174- else :
175- response = requests .get (self .baseurl , params = payload , cert = cert_path , verify = https_flag )
176- except Exception ,e :
177- '''
178- If an exception occurs with current CA certs, then try with default certs path, we dont need to mention here the cert path
179- '''
180- self .logging .debug ( "Creating CS connection over https didnt worked with user provided certs %s" % e )
181- exception_check = True
182- exception_info = e
183- if method == 'POST' :
184- response = requests .post (self .baseurl , params = payload , verify = https_flag )
185- else :
186- response = requests .get (self .baseurl , params = payload , verify = https_flag )
187- finally :
188- if exception_check == True and exception_info is not None :
189- raise exception_info
164+ exception_check = False
165+ exception_info = None
166+ #use user provided CA certs for request
167+ try :
168+ if method == 'POST' :
169+ response = requests .post (self .baseurl , params = payload ,
170+ cert = cert_path ,
171+ verify = https_flag )
172+ else :
173+ response = requests .get (self .baseurl , params = payload ,
174+ cert = cert_path ,
175+ verify = https_flag )
176+ except Exception , e :
177+ # attempt a connection using default certs
178+ self .logging .debug ("connection failed using provided certs"
179+ " because of %s" % e )
180+ exception_check = True
181+ exception_info = e
182+ if method == 'POST' :
183+ response = requests .post (self .baseurl , params = payload ,
184+ verify = https_flag )
185+ else :
186+ response = requests .get (self .baseurl , params = payload ,
187+ verify = https_flag )
188+ finally :
189+ if exception_check and exception_info is not None :
190+ raise exception_info
190191 except ConnectionError , c :
191- self .logging .debug ("Connection refused. Reason: %s : %s" % (self .baseurl , c ))
192+ self .logging .debug ("Connection refused."
193+ " Reason: %s : %s" % (self .baseurl , c ))
192194 raise c
193195 except HTTPError , h :
194196 self .logging .debug ("Server returned error code: %s" % h )
0 commit comments