Fixes on CDH for python 3 compatibility

Story: #2005914
Task: #34174
Change-Id: I5de5a2109f0b50adc209008738920fb11fd0fd76
This commit is contained in:
Telles Nobrega 2019-07-31 15:17:33 -03:00
parent 21a54be52f
commit 9d443815fc
5 changed files with 31 additions and 12 deletions

View File

@ -110,7 +110,12 @@ class HttpClient(object):
path=path))
data = None
if http_method in ("POST", "PUT"):
if data is not None:
data = data.encode('utf-8')
# Setup the request
request = urllib.request.Request(url, data)
# Hack/workaround because urllib2 only does GET and POST
request.get_method = lambda: http_method

View File

@ -85,8 +85,9 @@ class Resource(object):
LOG.debug("{method} got response: {body}".format(method=method,
body=body[:32]))
# Is the response application/json?
if (len(body) != 0 and resp.info().getmaintype() == "application"
and resp.info().getsubtype() == "json"):
if (len(body) != 0 and
self._get_content_maintype(resp.info()) == "application"
and self._get_content_subtype(resp.info()) == "json"):
try:
json_dict = json.loads(body)
return json_dict
@ -144,6 +145,7 @@ class Resource(object):
:return: A dictionary of the JSON result.
"""
return self.invoke("POST", relpath, params, data,
self._make_headers(contenttype))
@ -164,3 +166,15 @@ class Resource(object):
if contenttype:
return {'Content-Type': contenttype}
return None
def _get_content_maintype(self, info):
try:
return info.getmaintype()
except AttributeError:
return info.get_content_maintype()
def _get_content_subtype(self, info):
try:
return info.getsubtype()
except AttributeError:
return info.get_content_subtype()

View File

@ -142,7 +142,7 @@ def call(method, path, ret_type,
:param params: Optional query parameters for the call.
:param api_version: minimum API version for the call.
"""
check_api_version(method.im_self, api_version)
check_api_version(method.__self__, api_version)
if data is not None:
data = json.dumps(Attr(is_api_list=True).to_json(data, False))
ret = method(path, data=data, params=params)
@ -151,11 +151,11 @@ def call(method, path, ret_type,
if ret_type is None:
return
elif ret_is_list:
return ApiList.from_json_dict(ret, method.im_self, ret_type)
return ApiList.from_json_dict(ret, method.__self__, ret_type)
elif isinstance(ret, list):
return [ret_type.from_json_dict(x, method.im_self) for x in ret]
return [ret_type.from_json_dict(x, method.__self__) for x in ret]
else:
return ret_type.from_json_dict(ret, method.im_self)
return ret_type.from_json_dict(ret, method.__self__)
class BaseApiObject(object):

View File

@ -104,15 +104,15 @@ def get_sentry_db_password(cluster):
def create_hive_database(cluster, remote):
db_password = get_hive_db_password(cluster)
create_db_script = utils.get_file_text(
create_db_script = utils.try_get_file_text(
'plugins/cdh/db_resources/create_hive_db.sql', 'sahara_plugin_cdh')
create_db_script = create_db_script % db_password
create_db_script = create_db_script % db_password.encode('utf-8')
remote_execute_db_script(remote, create_db_script)
def create_sentry_database(cluster, remote):
db_password = get_sentry_db_password(cluster)
create_db_script = utils.get_file_text(
create_db_script = utils.try_get_file_text(
'plugins/cdh/db_resources/create_sentry_db.sql', 'sahara_plugin_cdh')
create_db_script = create_db_script % db_password
create_db_script = create_db_script % db_password.encode('utf-8')
remote_execute_db_script(remote, create_db_script)

View File

@ -177,7 +177,7 @@ class TestPluginUtils(b.SaharaTestCase):
'plugins/cdh/db_resources/create_hive_db.sql'
.format(version=self.version),
'sahara_plugin_cdh')
create_db_script = create_db_script % db_password
create_db_script = (create_db_script % db_password).encode('utf-8')
self.plug_utils.configure_hive(cluster)
@ -311,7 +311,7 @@ class TestPluginUtilsHigherThanV5(TestPluginUtils):
'plugins/cdh/db_resources/create_sentry_db.sql'
.format(version=self.version),
'sahara_plugin_cdh')
create_db_script = create_db_script % db_password
create_db_script = (create_db_script % db_password).encode('utf-8')
self.plug_utils.configure_sentry(cluster)