@@ -35,29 +35,27 @@ class cloudConnection(object):
3535
3636 """ Connections to make API calls to the cloudstack management server
3737 """
38-
39- def __init__ (self , mgtSvr , port = 8096 , user = None , passwd = None ,
40- apiKey = None , securityKey = None ,
41- asyncTimeout = 3600 , logging = None , scheme = 'http' ,
42- path = 'client/api' ):
38+ def __init__ (self , mgmtDet ,asyncTimeout = 3600 , logging = None , scheme = 'http' ,path = 'client/api' ):
4339 self .loglevel () # Turn off requests logs
44- self .apiKey = apiKey
45- self .securityKey = securityKey
46- self .mgtSvr = mgtSvr
47- self .port = port
48- self .user = user
49- self .passwd = passwd
40+ self .apiKey = mgmtDet .apiKey
41+ self .securityKey = mgmtDet .securityKey
42+ self .mgtSvr = mgmtDet .mgtSvrIp
43+ self .port = mgmtDet .port
44+ self .user = mgmtDet .user
45+ self .passwd = mgmtDet .passwd
46+ self .certCAPath = mgmtDet .certCAPath
47+ self .certPath = mgmtDet .certPath
5048 self .logging = logging
5149 self .path = path
5250 self .retries = 5
51+ self .protocol = "http"
5352 self .asyncTimeout = asyncTimeout
5453 self .auth = True
55- if port == 8096 or \
54+ if self . port == 8096 or \
5655 (self .apiKey is None and self .securityKey is None ):
5756 self .auth = False
58- if scheme not in ['http' , 'https' ]:
59- raise RequestException ("Protocol must be HTTP" )
60- self .protocol = scheme
57+ if mgmtDet .useHttps == "True" :
58+ self .protocol = "https"
6159 self .baseurl = "%s://%s:%d/%s" \
6260 % (self .protocol , self .mgtSvr , self .port , self .path )
6361
@@ -145,15 +143,52 @@ def request(self, command, auth=True, payload={}, method='GET'):
145143 payload ["signature" ] = signature
146144
147145 try :
148- if method == 'POST' :
149- response = requests .post (
150- self .baseurl , params = payload , verify = False )
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 )
151165 else :
152- response = requests .get (
153- self .baseurl , params = payload , verify = False )
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
154190 except ConnectionError , c :
155- self .logging .debug ("Connection refused. Reason: %s : %s" %
156- (self .baseurl , c ))
191+ self .logging .debug ("Connection refused. Reason: %s : %s" % (self .baseurl , c ))
157192 raise c
158193 except HTTPError , h :
159194 self .logging .debug ("Server returned error code: %s" % h )
0 commit comments