From 9d443815fcfdd3fe11b6cbe3c47660920a2a8fc5 Mon Sep 17 00:00:00 2001 From: Telles Nobrega Date: Wed, 31 Jul 2019 15:17:33 -0300 Subject: [PATCH] Fixes on CDH for python 3 compatibility Story: #2005914 Task: #34174 Change-Id: I5de5a2109f0b50adc209008738920fb11fd0fd76 --- .../plugins/cdh/client/http_client.py | 5 +++++ .../plugins/cdh/client/resource.py | 18 ++++++++++++++++-- sahara_plugin_cdh/plugins/cdh/client/types.py | 8 ++++---- sahara_plugin_cdh/plugins/cdh/db_helper.py | 8 ++++---- .../unit/plugins/cdh/base_plugin_utils_test.py | 4 ++-- 5 files changed, 31 insertions(+), 12 deletions(-) diff --git a/sahara_plugin_cdh/plugins/cdh/client/http_client.py b/sahara_plugin_cdh/plugins/cdh/client/http_client.py index 38e0936..4efff55 100644 --- a/sahara_plugin_cdh/plugins/cdh/client/http_client.py +++ b/sahara_plugin_cdh/plugins/cdh/client/http_client.py @@ -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 diff --git a/sahara_plugin_cdh/plugins/cdh/client/resource.py b/sahara_plugin_cdh/plugins/cdh/client/resource.py index fdd2325..9dd1ebd 100644 --- a/sahara_plugin_cdh/plugins/cdh/client/resource.py +++ b/sahara_plugin_cdh/plugins/cdh/client/resource.py @@ -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() diff --git a/sahara_plugin_cdh/plugins/cdh/client/types.py b/sahara_plugin_cdh/plugins/cdh/client/types.py index cd71edc..1a59150 100644 --- a/sahara_plugin_cdh/plugins/cdh/client/types.py +++ b/sahara_plugin_cdh/plugins/cdh/client/types.py @@ -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): diff --git a/sahara_plugin_cdh/plugins/cdh/db_helper.py b/sahara_plugin_cdh/plugins/cdh/db_helper.py index 2cb6415..179b222 100644 --- a/sahara_plugin_cdh/plugins/cdh/db_helper.py +++ b/sahara_plugin_cdh/plugins/cdh/db_helper.py @@ -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) diff --git a/sahara_plugin_cdh/tests/unit/plugins/cdh/base_plugin_utils_test.py b/sahara_plugin_cdh/tests/unit/plugins/cdh/base_plugin_utils_test.py index d93eaf1..055c0f0 100644 --- a/sahara_plugin_cdh/tests/unit/plugins/cdh/base_plugin_utils_test.py +++ b/sahara_plugin_cdh/tests/unit/plugins/cdh/base_plugin_utils_test.py @@ -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)