def _create_connection(self, url, method, cacerts=False, ssl_thumbprint=None): _urlparse = urlparse.urlparse(url) scheme, netloc, path, params, query, fragment = _urlparse if scheme == 'http': conn = httplib.HTTPConnection(netloc) elif scheme == 'https': conn = httplib.HTTPSConnection(netloc) cert_reqs = None # cacerts can be either True or False or contain # actual certificates. If it is a boolean, then # we need to set cert_reqs and clear the cacerts if isinstance(cacerts, bool): if cacerts: cert_reqs = ssl.CERT_REQUIRED else: cert_reqs = ssl.CERT_NONE cacerts = None conn.set_cert(ca_certs=cacerts, cert_reqs=cert_reqs, assert_fingerprint=ssl_thumbprint) else: excep_msg = _("Invalid scheme: %s.") % scheme LOG.error(excep_msg) raise ValueError(excep_msg) if query: path = path + '?' + query conn.putrequest(method, path) return conn
def create_write_connection(url, file_size=None, method=None, cookies=None, overwrite=None, content_type=None, cacerts=False, ssl_thumbprint=None): """ Create HTTP connection write to VMDK file. """ LOG.debug("Creating https connection with vCenter") if not method: method = "POST" if not content_type: content_type = 'application/x-vnd.vmware-streamVmdk' _urlparse = urlparse.urlparse(url) scheme, netloc, path, _, query, _ = _urlparse if scheme == 'http': conn = httplib.HTTPConnection(netloc) elif scheme == 'https': conn = httplib.HTTPSConnection(netloc) cert_reqs = None # cacerts can be either True or False or contain # actual certificates. If it is a boolean, then # we need to set cert_reqs and clear the cacerts if isinstance(cacerts, bool): if cacerts: cert_reqs = ssl.CERT_REQUIRED else: cert_reqs = ssl.CERT_NONE cacerts = None conn.set_cert(ca_certs=cacerts, cert_reqs=cert_reqs, assert_fingerprint=ssl_thumbprint) else: excep_msg = _("Invalid scheme: %s.") % scheme raise ValueError(excep_msg) if query: path = path + '?' + query conn.putrequest(method, path) try: headers = {'User-Agent': "EON"} if file_size: headers.update({'Content-Length': str(file_size)}) if overwrite: headers.update({'Overwrite': overwrite}) if cookies: headers.update({}) if content_type: headers.update({'Content-Type': content_type}) for key, value in six.iteritems(headers): conn.putheader(key, value) conn.endheaders() return conn except requests.RequestException as excep: excep_msg = ("Error occurred while creating HTTP connection " "to write to VMDK file with URL = %s.") % url raise exception.UploadOVAFailure(error=excep)
def _create_write_connection(self, url, file_size=None, cookies=None, overwrite=None, content_type=None, cacerts=False): """Create HTTP connection to write to VMDK file.""" LOG.debug( "Creating HTTP connection to write to file with " "size = %(file_size)d and URL = %(url)s.", { 'file_size': file_size, 'url': url }) _urlparse = urlparse.urlparse(url) scheme, netloc, path, params, query, fragment = _urlparse try: if scheme == 'http': conn = httplib.HTTPConnection(netloc) elif scheme == 'https': conn = httplib.HTTPSConnection(netloc) cert_reqs = None # cacerts can be either True or False or contain # actual certificates. If it is a boolean, then # we need to set cert_reqs and clear the cacerts if isinstance(cacerts, bool): if cacerts: cert_reqs = ssl.CERT_REQUIRED else: cert_reqs = ssl.CERT_NONE cacerts = None conn.set_cert(ca_certs=cacerts, cert_reqs=cert_reqs) else: excep_msg = _("Invalid scheme: %s.") % scheme LOG.error(excep_msg) raise ValueError(excep_msg) if query: path = path + '?' + query headers = {'User-Agent': USER_AGENT} if file_size: headers.update({'Content-Length': str(file_size)}) if overwrite: headers.update({'Overwrite': overwrite}) if cookies: headers.update( {'Cookie': self._build_vim_cookie_header(cookies)}) if content_type: headers.update({'Content-Type': content_type}) conn.putrequest('PUT', path) for key, value in six.iteritems(headers): conn.putheader(key, value) conn.endheaders() return conn except requests.RequestException as excep: excep_msg = _("Error occurred while creating HTTP connection " "to write to VMDK file with URL = %s.") % url LOG.exception(excep_msg) raise exceptions.VimConnectionException(excep_msg, excep)