Fix JSON parsing for Python3

Fix corrects the parsing of JSON output in Python 3
environment by using to_text API.

Fixes: #26489

Signed-off-by: Abhijeet Kasurde <akasurde@redhat.com>
This commit is contained in:
Abhijeet Kasurde 2017-07-10 16:54:14 +05:30 committed by Toshio Kuratomi
commit b266204afa

View file

@ -108,6 +108,7 @@ import os
from ansible.module_utils.basic import AnsibleModule from ansible.module_utils.basic import AnsibleModule
from ansible.module_utils.urls import fetch_url from ansible.module_utils.urls import fetch_url
from ansible.module_utils._text import to_text
class Response(object): class Response(object):
@ -122,10 +123,10 @@ class Response(object):
def json(self): def json(self):
if not self.body: if not self.body:
if "body" in self.info: if "body" in self.info:
return json.loads(self.info["body"]) return json.loads(to_text(self.info["body"], errors='surrogate_or_strict'))
return None return None
try: try:
return json.loads(self.body) return json.loads(to_text(self.body, errors='surrogate_or_strict'))
except ValueError: except ValueError:
return None return None
@ -213,6 +214,8 @@ def core(module):
# Check if resource is already tagged or not # Check if resource is already tagged or not
found = False found = False
url = "{0}?tag_name={1}".format(resource_type, name) url = "{0}?tag_name={1}".format(resource_type, name)
if resource_type == 'droplet':
url = "droplets?tag_name={0}".format(name)
response = rest.get(url) response = rest.get(url)
status_code = response.status_code status_code = response.status_code
resp_json = response.json resp_json = response.json