From ac536c6114a4d9c429c2e47b9ad00b27b437fc77 Mon Sep 17 00:00:00 2001 From: Angus Salkeld Date: Wed, 4 Mar 2015 15:26:48 +1000 Subject: [PATCH] Convert all non-test imports of json to jsonutils As requested in: https://review.openstack.org/#/c/116336/24/heat/engine/stack_resource.py Change-Id: Icea97aa44d2a3461fdfa2324292fd3b2c2f000e1 --- heat/api/cfn/v1/stacks.py | 6 +++--- heat/api/cfn/versions.py | 4 ++-- heat/api/cloudwatch/versions.py | 4 ++-- heat/api/openstack/versions.py | 4 ++-- heat/common/heat_keystoneclient.py | 10 +++++----- heat/common/serializers.py | 6 +++--- heat/common/template_format.py | 4 ++-- heat/common/wsgi.py | 4 ++-- heat/db/sqlalchemy/types.py | 7 +++---- heat/engine/cfn/functions.py | 4 ++-- heat/engine/clients/os/nova.py | 6 +++--- heat/engine/parameters.py | 9 +++++---- heat/engine/resources/nova_utils.py | 6 +++--- heat/engine/resources/openstack/volume.py | 5 ++--- heat/engine/resources/openstack/wait_condition.py | 5 ++--- heat/engine/resources/swiftsignal.py | 6 +++--- heat/engine/resources/template_resource.py | 5 ++--- heat/engine/service.py | 4 ++-- heat/engine/service_software_config.py | 4 +--- heat/tests/test_engine_service.py | 5 ++--- heat/tests/test_parameters.py | 3 +-- 21 files changed, 52 insertions(+), 59 deletions(-) diff --git a/heat/api/cfn/v1/stacks.py b/heat/api/cfn/v1/stacks.py index 3f20e0d874..0eac238d0e 100644 --- a/heat/api/cfn/v1/stacks.py +++ b/heat/api/cfn/v1/stacks.py @@ -15,10 +15,10 @@ Stack endpoint for Heat CloudFormation v1 API. """ -import json import socket from oslo_log import log as logging +from oslo_serialization import jsonutils from heat.api.aws import exception from heat.api.aws import utils as api_utils @@ -510,8 +510,8 @@ class StackController(object): action = e[rpc_api.EVENT_RES_ACTION] status = e[rpc_api.EVENT_RES_STATUS] result['ResourceStatus'] = '_'.join((action, status)) - result['ResourceProperties'] = json.dumps(result[ - 'ResourceProperties']) + result['ResourceProperties'] = jsonutils.dumps(result[ + 'ResourceProperties']) return self._id_format(result) diff --git a/heat/api/cfn/versions.py b/heat/api/cfn/versions.py index c92a992b3d..6f5763cdf9 100644 --- a/heat/api/cfn/versions.py +++ b/heat/api/cfn/versions.py @@ -16,8 +16,8 @@ Controller that returns information on the heat API versions """ import httplib -import json +from oslo_serialization import jsonutils import webob.dec @@ -44,7 +44,7 @@ class Controller(object): }] }] - body = json.dumps(dict(versions=version_objs)) + body = jsonutils.dumps(dict(versions=version_objs)) response = webob.Response(request=req, status=httplib.MULTIPLE_CHOICES, diff --git a/heat/api/cloudwatch/versions.py b/heat/api/cloudwatch/versions.py index c773573d44..3e64ecdec4 100644 --- a/heat/api/cloudwatch/versions.py +++ b/heat/api/cloudwatch/versions.py @@ -14,8 +14,8 @@ """Controller that returns information on the heat API versions.""" import httplib -import json +from oslo_serialization import jsonutils import webob.dec @@ -40,7 +40,7 @@ class Controller(object): }] }] - body = json.dumps(dict(versions=version_objs)) + body = jsonutils.dumps(dict(versions=version_objs)) response = webob.Response(request=req, status=httplib.MULTIPLE_CHOICES, diff --git a/heat/api/openstack/versions.py b/heat/api/openstack/versions.py index c92a992b3d..6f5763cdf9 100644 --- a/heat/api/openstack/versions.py +++ b/heat/api/openstack/versions.py @@ -16,8 +16,8 @@ Controller that returns information on the heat API versions """ import httplib -import json +from oslo_serialization import jsonutils import webob.dec @@ -44,7 +44,7 @@ class Controller(object): }] }] - body = json.dumps(dict(versions=version_objs)) + body = jsonutils.dumps(dict(versions=version_objs)) response = webob.Response(request=req, status=httplib.MULTIPLE_CHOICES, diff --git a/heat/common/heat_keystoneclient.py b/heat/common/heat_keystoneclient.py index 5e8e4fda6f..0346a95885 100644 --- a/heat/common/heat_keystoneclient.py +++ b/heat/common/heat_keystoneclient.py @@ -14,7 +14,6 @@ """Keystone Client functionality for use by resources.""" import collections -import json import uuid import keystoneclient.exceptions as kc_exception @@ -22,6 +21,7 @@ from keystoneclient import session from keystoneclient.v3 import client as kc_v3 from oslo_config import cfg from oslo_log import log as logging +from oslo_serialization import jsonutils from oslo_utils import importutils from heat.common import context @@ -484,7 +484,7 @@ class KeystoneClientV3(object): # extensible-crud-manager-operations bp lands credentials = self.client.credentials.list() for cr in credentials: - ec2_creds = json.loads(cr.blob) + ec2_creds = jsonutils.loads(cr.blob) if ec2_creds.get('access') == access: return AccessKey(id=cr.id, access=ec2_creds['access'], @@ -514,7 +514,7 @@ class KeystoneClientV3(object): # then we'll have to do a brute-force lookup locally if credential_id: cred = self.client.credentials.get(credential_id) - ec2_creds = json.loads(cred.blob) + ec2_creds = jsonutils.loads(cred.blob) return AccessKey(id=cred.id, access=ec2_creds['access'], secret=ec2_creds['secret']) @@ -529,7 +529,7 @@ class KeystoneClientV3(object): data_blob = {'access': uuid.uuid4().hex, 'secret': uuid.uuid4().hex} ec2_creds = self.client.credentials.create( - user=user_id, type='ec2', data=json.dumps(data_blob), + user=user_id, type='ec2', data=jsonutils.dumps(data_blob), project=project_id) # Return a AccessKey namedtuple for easier access to the blob contents @@ -550,7 +550,7 @@ class KeystoneClientV3(object): data_blob = {'access': uuid.uuid4().hex, 'secret': uuid.uuid4().hex} creds = self.domain_admin_client.credentials.create( - user=user_id, type='ec2', data=json.dumps(data_blob), + user=user_id, type='ec2', data=jsonutils.dumps(data_blob), project=project_id) return AccessKey(id=creds.id, access=data_blob['access'], diff --git a/heat/common/serializers.py b/heat/common/serializers.py index 553d9a9309..9379ce8991 100644 --- a/heat/common/serializers.py +++ b/heat/common/serializers.py @@ -21,10 +21,10 @@ Utility methods for serializing responses """ import datetime -import json from lxml import etree from oslo_log import log as logging +from oslo_serialization import jsonutils import six LOG = logging.getLogger(__name__) @@ -38,7 +38,7 @@ class JSONResponseSerializer(object): return obj.isoformat() return obj - response = json.dumps(data, default=sanitizer) + response = jsonutils.dumps(data, default=sanitizer) LOG.debug("JSON response : %s" % response) return response @@ -67,7 +67,7 @@ class XMLResponseSerializer(object): # Need to use json.dumps for the JSON inside XML # otherwise quotes get mangled and json.loads breaks try: - subelement.text = json.dumps(value) + subelement.text = jsonutils.dumps(value) except TypeError: subelement.text = str(value) else: diff --git a/heat/common/template_format.py b/heat/common/template_format.py index c80c8edfd3..bd64c1ae72 100644 --- a/heat/common/template_format.py +++ b/heat/common/template_format.py @@ -12,10 +12,10 @@ # under the License. import itertools -import json import re from oslo_config import cfg +from oslo_serialization import jsonutils import six import yaml @@ -50,7 +50,7 @@ yaml_loader.add_constructor(u'tag:yaml.org,2002:timestamp', def simple_parse(tmpl_str): try: - tpl = json.loads(tmpl_str) + tpl = jsonutils.loads(tmpl_str) except ValueError: try: tpl = yaml.load(tmpl_str, Loader=yaml_loader) diff --git a/heat/common/wsgi.py b/heat/common/wsgi.py index ffb4930c66..21ef2d789b 100644 --- a/heat/common/wsgi.py +++ b/heat/common/wsgi.py @@ -22,7 +22,6 @@ Utility methods for working with WSGI servers import abc import errno -import json import logging import os import signal @@ -36,6 +35,7 @@ import eventlet.greenio import eventlet.wsgi from oslo_config import cfg import oslo_i18n as i18n +from oslo_serialization import jsonutils from oslo_utils import importutils from paste import deploy import routes @@ -590,7 +590,7 @@ class JSONRequestDeserializer(object): ) % {'len': len(datastring), 'limit': cfg.CONF.max_json_body_size} raise exception.RequestLimitExceeded(message=msg) - return json.loads(datastring) + return jsonutils.loads(datastring) except ValueError as ex: raise webob.exc.HTTPBadRequest(six.text_type(ex)) diff --git a/heat/db/sqlalchemy/types.py b/heat/db/sqlalchemy/types.py index 5fe89a2df4..79cc04b483 100644 --- a/heat/db/sqlalchemy/types.py +++ b/heat/db/sqlalchemy/types.py @@ -11,15 +11,14 @@ # License for the specific language governing permissions and limitations # under the License. -import json - +from oslo_serialization import jsonutils from sqlalchemy.dialects import mysql from sqlalchemy.ext import mutable from sqlalchemy import types -dumps = json.dumps -loads = json.loads +dumps = jsonutils.dumps +loads = jsonutils.loads class LongText(types.TypeDecorator): diff --git a/heat/engine/cfn/functions.py b/heat/engine/cfn/functions.py index 1d1c6880dc..bc46aea166 100644 --- a/heat/engine/cfn/functions.py +++ b/heat/engine/cfn/functions.py @@ -13,8 +13,8 @@ import collections import itertools -import json +from oslo_serialization import jsonutils import six from heat.api.aws import utils as aws_utils @@ -238,7 +238,7 @@ class Select(function.Function): if isinstance(strings, six.string_types): # might be serialized json. try: - strings = json.loads(strings) + strings = jsonutils.loads(strings) except ValueError as json_ex: fmt_data = {'fn_name': self.fn_name, 'err': json_ex} diff --git a/heat/engine/clients/os/nova.py b/heat/engine/clients/os/nova.py index 1a2bb3b888..5936a31698 100644 --- a/heat/engine/clients/os/nova.py +++ b/heat/engine/clients/os/nova.py @@ -15,7 +15,6 @@ import collections import email from email.mime import multipart from email.mime import text -import json import logging import os import pkgutil @@ -25,6 +24,7 @@ from novaclient import client as nc from novaclient import exceptions from novaclient import shell as novashell from oslo_config import cfg +from oslo_serialization import jsonutils from oslo_utils import uuidutils import six from six.moves.urllib import parse as urlparse @@ -259,7 +259,7 @@ echo -e '%s\tALL=(ALL)\tNOPASSWD: ALL' >> /etc/sudoers 'loguserdata.py', 'x-shellscript')) if metadata: - attachments.append((json.dumps(metadata), + attachments.append((jsonutils.dumps(metadata), 'cfn-init-data', 'x-cfninitdata')) attachments.append((cfg.CONF.heat_watch_server_url, @@ -386,7 +386,7 @@ echo -e '%s\tALL=(ALL)\tNOPASSWD: ALL' >> /etc/sudoers return dict((key, (value if isinstance(value, six.string_types) - else json.dumps(value)) + else jsonutils.dumps(value)) ) for (key, value) in metadata.items()) def meta_update(self, server, metadata): diff --git a/heat/engine/parameters.py b/heat/engine/parameters.py index 1e62aba2ed..7c7fc89120 100644 --- a/heat/engine/parameters.py +++ b/heat/engine/parameters.py @@ -13,8 +13,8 @@ import collections import itertools -import json +from oslo_serialization import jsonutils from oslo_utils import encodeutils from oslo_utils import strutils import six @@ -405,9 +405,10 @@ class JsonParam(Parameter): try: val = value if not isinstance(val, six.string_types): - val = json.dumps(val) + # turn off oslo_serialization's clever to_primitive() + val = jsonutils.dumps(val, default=None) if val: - return json.loads(val) + return jsonutils.loads(val) except (ValueError, TypeError) as err: message = _('Value must be valid JSON: %s') % err raise ValueError(message) @@ -431,7 +432,7 @@ class JsonParam(Parameter): def __str__(self): if self.hidden(): return super(JsonParam, self).__str__() - return encodeutils.safe_decode(json.dumps(self.value())) + return encodeutils.safe_decode(jsonutils.dumps(self.value())) def _validate(self, val, context): val = self.parse(val) diff --git a/heat/engine/resources/nova_utils.py b/heat/engine/resources/nova_utils.py index 116088b833..424f68ce1a 100644 --- a/heat/engine/resources/nova_utils.py +++ b/heat/engine/resources/nova_utils.py @@ -15,7 +15,6 @@ import email from email.mime import multipart from email.mime import text -import json import os import pkgutil import string @@ -24,6 +23,7 @@ import warnings from novaclient import exceptions as nova_exceptions from oslo_config import cfg from oslo_log import log as logging +from oslo_serialization import jsonutils import six from six.moves.urllib import parse as urlparse @@ -213,7 +213,7 @@ echo -e '%s\tALL=(ALL)\tNOPASSWD: ALL' >> /etc/sudoers metadata = resource.metadata_get() if metadata: - attachments.append((json.dumps(metadata), + attachments.append((jsonutils.dumps(metadata), 'cfn-init-data', 'x-cfninitdata')) attachments.append((cfg.CONF.heat_watch_server_url, @@ -354,7 +354,7 @@ def meta_serialize(metadata): 'Use self.client_plugin("nova").meta_serialize') return dict((key, (value if isinstance(value, six.string_types) - else json.dumps(value)) + else jsonutils.dumps(value)) ) for (key, value) in metadata.items()) diff --git a/heat/engine/resources/openstack/volume.py b/heat/engine/resources/openstack/volume.py index dc56d173bd..ac8d4725af 100644 --- a/heat/engine/resources/openstack/volume.py +++ b/heat/engine/resources/openstack/volume.py @@ -11,9 +11,8 @@ # License for the specific language governing permissions and limitations # under the License. -import json - from oslo_log import log as logging +from oslo_serialization import jsonutils import six from heat.common import exception @@ -213,7 +212,7 @@ class CinderVolume(aws_vol.Volume): cinder = self.client() vol = cinder.volumes.get(self.resource_id) if name == self.METADATA_ATTR: - return six.text_type(json.dumps(vol.metadata)) + return six.text_type(jsonutils.dumps(vol.metadata)) elif name == self.METADATA_VALUES_ATTR: return vol.metadata if cinder.volume_api_version >= 2: diff --git a/heat/engine/resources/openstack/wait_condition.py b/heat/engine/resources/openstack/wait_condition.py index dc0f275e59..1634e46650 100644 --- a/heat/engine/resources/openstack/wait_condition.py +++ b/heat/engine/resources/openstack/wait_condition.py @@ -11,9 +11,8 @@ # License for the specific language governing permissions and limitations # under the License. -import json - from oslo_log import log as logging +from oslo_serialization import jsonutils from oslo_utils import timeutils import six @@ -142,7 +141,7 @@ class HeatWaitCondition(resource.Resource): 'key': key, 'res': res}) - return six.text_type(json.dumps(res)) + return six.text_type(jsonutils.dumps(res)) def resource_mapping(): diff --git a/heat/engine/resources/swiftsignal.py b/heat/engine/resources/swiftsignal.py index 45bdb05977..7ab39960e7 100644 --- a/heat/engine/resources/swiftsignal.py +++ b/heat/engine/resources/swiftsignal.py @@ -11,10 +11,10 @@ # License for the specific language governing permissions and limitations # under the License. -import json import urlparse from oslo_log import log as logging +from oslo_serialization import jsonutils from oslo_utils import timeutils import six @@ -262,7 +262,7 @@ class SwiftSignal(resource.Resource): obj_bodies.append({}) continue try: - obj_bodies.append(json.loads(body)) + obj_bodies.append(jsonutils.loads(body)) except ValueError: raise exception.Error(_("Failed to parse JSON data: %s") % body) @@ -330,7 +330,7 @@ class SwiftSignal(resource.Resource): def _resolve_attribute(self, key): if key == self.DATA: - return six.text_type(json.dumps(self.get_data())) + return six.text_type(jsonutils.dumps(self.get_data())) def resource_mapping(): diff --git a/heat/engine/resources/template_resource.py b/heat/engine/resources/template_resource.py index e13a48c39a..e45d2538ec 100644 --- a/heat/engine/resources/template_resource.py +++ b/heat/engine/resources/template_resource.py @@ -11,8 +11,7 @@ # License for the specific language governing permissions and limitations # under the License. -import json - +from oslo_serialization import jsonutils from requests import exceptions import six @@ -175,7 +174,7 @@ class TemplateResource(stack_resource.StackResource): if t_data is None: if self.nested() is not None: - t_data = json.dumps(self.nested().t.t) + t_data = jsonutils.dumps(self.nested().t.t) if t_data is not None: self.stack.t.files[self.template_name] = t_data diff --git a/heat/engine/service.py b/heat/engine/service.py index ada86c2c8e..676b6945af 100644 --- a/heat/engine/service.py +++ b/heat/engine/service.py @@ -12,7 +12,6 @@ # under the License. import collections -import json import os import socket import warnings @@ -21,6 +20,7 @@ import eventlet from oslo_config import cfg from oslo_log import log as logging import oslo_messaging as messaging +from oslo_serialization import jsonutils from oslo_utils import uuidutils from osprofiler import profiler import six @@ -1039,7 +1039,7 @@ class EngineService(service.Service): # fall back to looking for EC2 credentials in the context try: - ec2_creds = json.loads(cnxt.aws_creds).get('ec2Credentials') + ec2_creds = jsonutils.loads(cnxt.aws_creds).get('ec2Credentials') except (TypeError, AttributeError): ec2_creds = None diff --git a/heat/engine/service_software_config.py b/heat/engine/service_software_config.py index b95f7e3aaa..fa7abd615d 100644 --- a/heat/engine/service_software_config.py +++ b/heat/engine/service_software_config.py @@ -11,8 +11,6 @@ # License for the specific language governing permissions and limitations # under the License. -import json - from oslo_log import log as logging from oslo_serialization import jsonutils from oslo_utils import timeutils @@ -127,7 +125,7 @@ class SoftwareConfigService(service.Service): raise ex if obj: self.signal_software_deployment( - cnxt, sd.id, json.loads(obj), + cnxt, sd.id, jsonutils.loads(obj), timeutils.strtime(last_modified)) return db_api.software_deployment_get(cnxt, sd.id) diff --git a/heat/tests/test_engine_service.py b/heat/tests/test_engine_service.py index b27eddb566..a0bbe5e738 100644 --- a/heat/tests/test_engine_service.py +++ b/heat/tests/test_engine_service.py @@ -12,7 +12,6 @@ # under the License. import datetime -import json import sys import uuid @@ -22,7 +21,7 @@ import mock import mox from oslo_config import cfg from oslo_messaging.rpc import dispatcher -from oslo_serialization import jsonutils +from oslo_serialization import jsonutils as json from oslo_utils import timeutils import six @@ -3974,7 +3973,7 @@ class SoftwareConfigServiceTest(common.HeatTestCase): {'rsrc_metadata': result_metadata}) put.assert_called_once_with( - 'http://192.168.2.2/foo/bar', jsonutils.dumps(result_metadata)) + 'http://192.168.2.2/foo/bar', json.dumps(result_metadata)) @mock.patch.object(service_software_config.SoftwareConfigService, 'signal_software_deployment') diff --git a/heat/tests/test_parameters.py b/heat/tests/test_parameters.py index 573b5fb325..5d9d2f2c96 100644 --- a/heat/tests/test_parameters.py +++ b/heat/tests/test_parameters.py @@ -11,8 +11,7 @@ # License for the specific language governing permissions and limitations # under the License. -import json - +from oslo_serialization import jsonutils as json import six import testtools