Consistently use jsonutils instead of specific implementation

jsonutils have several benefits in comparison to pure json
implementation, like enabling C boosted encoders and decoders for
Python2.6 by using simplejson when available.

Change-Id: I24d0cd442e8d9d89fac50e43fc97f7bb4a293c3d
Closes-Bug: 1329496
This commit is contained in:
Ihar Hrachyshka 2014-06-16 09:26:39 +02:00
parent d003a85520
commit 9f2658d8cf
15 changed files with 40 additions and 46 deletions

View File

@ -17,7 +17,6 @@
"""Instance Metadata information.""" """Instance Metadata information."""
import base64 import base64
import json
import os import os
import posixpath import posixpath
@ -33,6 +32,7 @@ from nova import network
from nova import objects from nova import objects
from nova.objects import base as obj_base from nova.objects import base as obj_base
from nova.openstack.common import importutils from nova.openstack.common import importutils
from nova.openstack.common import jsonutils
from nova.openstack.common import log as logging from nova.openstack.common import log as logging
from nova.openstack.common import timeutils from nova.openstack.common import timeutils
from nova import utils from nova import utils
@ -327,7 +327,7 @@ class InstanceMetadata():
metadata['random_seed'] = base64.b64encode(os.urandom(512)) metadata['random_seed'] = base64.b64encode(os.urandom(512))
self.set_mimetype(MIME_TYPE_APPLICATION_JSON) self.set_mimetype(MIME_TYPE_APPLICATION_JSON)
return json.dumps(metadata) return jsonutils.dumps(metadata)
def _handle_content(self, path_tokens): def _handle_content(self, path_tokens):
if len(path_tokens) == 1: if len(path_tokens) == 1:
@ -361,7 +361,7 @@ class InstanceMetadata():
def _vendor_data(self, version, path): def _vendor_data(self, version, path):
if self._check_os_version(HAVANA, version): if self._check_os_version(HAVANA, version):
self.set_mimetype(MIME_TYPE_APPLICATION_JSON) self.set_mimetype(MIME_TYPE_APPLICATION_JSON)
return json.dumps(self.vddriver.get()) return jsonutils.dumps(self.vddriver.get())
raise KeyError(path) raise KeyError(path)
def _check_version(self, required, requested, versions=VERSIONS): def _check_version(self, required, requested, versions=VERSIONS):
@ -440,7 +440,7 @@ class InstanceMetadata():
pass pass
filepath = os.path.join('ec2', version, 'meta-data.json') filepath = os.path.join('ec2', version, 'meta-data.json')
yield (filepath, json.dumps(data['meta-data'])) yield (filepath, jsonutils.dumps(data['meta-data']))
ALL_OPENSTACK_VERSIONS = OPENSTACK_VERSIONS + ["latest"] ALL_OPENSTACK_VERSIONS = OPENSTACK_VERSIONS + ["latest"]
for version in ALL_OPENSTACK_VERSIONS: for version in ALL_OPENSTACK_VERSIONS:

View File

@ -17,7 +17,6 @@
"""The security groups extension.""" """The security groups extension."""
import contextlib import contextlib
import json
import webob import webob
from webob import exc from webob import exc
@ -32,6 +31,7 @@ from nova import exception
from nova.i18n import _ from nova.i18n import _
from nova.network.security_group import neutron_driver from nova.network.security_group import neutron_driver
from nova.network.security_group import openstack_driver from nova.network.security_group import openstack_driver
from nova.openstack.common import jsonutils
from nova.openstack.common import log as logging from nova.openstack.common import log as logging
from nova.openstack.common import xmlutils from nova.openstack.common import xmlutils
from nova.virt import netutils from nova.virt import netutils
@ -565,7 +565,7 @@ class SecurityGroupsOutputController(wsgi.Controller):
else: else:
try: try:
# try converting to json # try converting to json
req_obj = json.loads(req.body) req_obj = jsonutils.loads(req.body)
# Add security group to server, if no security group was in # Add security group to server, if no security group was in
# request add default since that is the group it is part of # request add default since that is the group it is part of
servers[0][key] = req_obj['server'].get( servers[0][key] = req_obj['server'].get(

View File

@ -16,8 +16,6 @@
"""The security groups extension.""" """The security groups extension."""
import json
from nova.api.openstack.compute.schemas.v3 import security_groups as \ from nova.api.openstack.compute.schemas.v3 import security_groups as \
schema_security_groups schema_security_groups
from nova.api.openstack import extensions from nova.api.openstack import extensions
@ -27,6 +25,7 @@ from nova.compute import api as compute_api
from nova import exception from nova import exception
from nova.network.security_group import neutron_driver from nova.network.security_group import neutron_driver
from nova.network.security_group import openstack_driver from nova.network.security_group import openstack_driver
from nova.openstack.common import jsonutils
ALIAS = 'os-security-groups' ALIAS = 'os-security-groups'
@ -81,7 +80,7 @@ class SecurityGroupsOutputController(wsgi.Controller):
# one server in an API request. # one server in an API request.
else: else:
# try converting to json # try converting to json
req_obj = json.loads(req.body) req_obj = jsonutils.loads(req.body)
# Add security group to server, if no security group was in # Add security group to server, if no security group was in
# request add default since that is the group it is part of # request add default since that is the group it is part of
servers[0][ATTRIBUTE_NAME] = req_obj['server'].get( servers[0][ATTRIBUTE_NAME] = req_obj['server'].get(

View File

@ -19,7 +19,6 @@ from __future__ import absolute_import
import copy import copy
import itertools import itertools
import json
import random import random
import sys import sys
import time import time
@ -126,7 +125,7 @@ def generate_identity_headers(context, status='Confirmed'):
'X-Tenant-Id': getattr(context, 'tenant', None), 'X-Tenant-Id': getattr(context, 'tenant', None),
'X-Roles': ','.join(context.roles), 'X-Roles': ','.join(context.roles),
'X-Identity-Status': status, 'X-Identity-Status': status,
'X-Service-Catalog': json.dumps(context.service_catalog), 'X-Service-Catalog': jsonutils.dumps(context.service_catalog),
} }

View File

@ -21,13 +21,13 @@ dynamic configuration.
""" """
import datetime import datetime
import json
import os import os
from oslo.config import cfg from oslo.config import cfg
from nova.i18n import _ from nova.i18n import _
from nova.openstack.common import excutils from nova.openstack.common import excutils
from nova.openstack.common import jsonutils
from nova.openstack.common import log as logging from nova.openstack.common import log as logging
from nova.openstack.common import timeutils from nova.openstack.common import timeutils
@ -73,7 +73,7 @@ class SchedulerOptions(object):
def _load_file(self, handle): def _load_file(self, handle):
"""Decode the JSON file. Broken out for testing.""" """Decode the JSON file. Broken out for testing."""
try: try:
return json.load(handle) return jsonutils.load(handle)
except ValueError as e: except ValueError as e:
LOG.exception(_("Could not decode scheduler options: '%s'"), e) LOG.exception(_("Could not decode scheduler options: '%s'"), e)
return {} return {}

View File

@ -12,8 +12,6 @@
# License for the specific language governing permissions and limitations # License for the specific language governing permissions and limitations
# under the License. # under the License.
import json
import mock import mock
import webob import webob
@ -21,6 +19,7 @@ from nova.api.openstack.compute.contrib import server_external_events
from nova import context from nova import context
from nova import exception from nova import exception
from nova import objects from nova import objects
from nova.openstack.common import jsonutils
from nova import test from nova import test
fake_instances = { fake_instances = {
@ -71,7 +70,7 @@ class ServerExternalEventsTest(test.NoDBTestCase):
req.method = 'POST' req.method = 'POST'
req.headers['content-type'] = 'application/json' req.headers['content-type'] = 'application/json'
req.environ['nova.context'] = self.context req.environ['nova.context'] = self.context
req.body = json.dumps(body) req.body = jsonutils.dumps(body)
return req return req
def _assert_call(self, req, body, expected_uuids, expected_events): def _assert_call(self, req, body, expected_uuids, expected_events):

View File

@ -12,8 +12,6 @@
# License for the specific language governing permissions and limitations # License for the specific language governing permissions and limitations
# under the License. # under the License.
import json
import mock import mock
import webob import webob
@ -21,6 +19,7 @@ from nova.api.openstack.compute.plugins.v3 import server_external_events
from nova import context from nova import context
from nova import exception from nova import exception
from nova import objects from nova import objects
from nova.openstack.common import jsonutils
from nova import test from nova import test
fake_instances = { fake_instances = {
@ -66,7 +65,7 @@ class ServerExternalEventsTest(test.NoDBTestCase):
req.method = 'POST' req.method = 'POST'
req.headers['content-type'] = 'application/json' req.headers['content-type'] = 'application/json'
req.environ['nova.context'] = self.context req.environ['nova.context'] = self.context
req.body = json.dumps(body) req.body = jsonutils.dumps(body)
return req return req
def _assert_call(self, req, body, expected_uuids, expected_events): def _assert_call(self, req, body, expected_uuids, expected_events):

View File

@ -12,14 +12,13 @@
# License for the specific language governing permissions and limitations # License for the specific language governing permissions and limitations
# under the License. # under the License.
import json
from oslo.config import cfg from oslo.config import cfg
import webob import webob
import webob.exc import webob.exc
import nova.api.auth import nova.api.auth
from nova.i18n import _ from nova.i18n import _
from nova.openstack.common import jsonutils
from nova.openstack.common.middleware import request_id from nova.openstack.common.middleware import request_id
from nova import test from nova import test
@ -41,7 +40,7 @@ class TestNovaKeystoneContextMiddleware(test.NoDBTestCase):
self.request = webob.Request.blank('/') self.request = webob.Request.blank('/')
self.request.headers['X_TENANT_ID'] = 'testtenantid' self.request.headers['X_TENANT_ID'] = 'testtenantid'
self.request.headers['X_AUTH_TOKEN'] = 'testauthtoken' self.request.headers['X_AUTH_TOKEN'] = 'testauthtoken'
self.request.headers['X_SERVICE_CATALOG'] = json.dumps({}) self.request.headers['X_SERVICE_CATALOG'] = jsonutils.dumps({})
def test_no_user_or_user_id(self): def test_no_user_or_user_id(self):
response = self.request.get_response(self.middleware) response = self.request.get_response(self.middleware)
@ -102,7 +101,7 @@ class TestKeystoneMiddlewareRoles(test.NoDBTestCase):
self.request.headers['X_USER'] = 'testuser' self.request.headers['X_USER'] = 'testuser'
self.request.headers['X_TENANT_ID'] = 'testtenantid' self.request.headers['X_TENANT_ID'] = 'testtenantid'
self.request.headers['X_AUTH_TOKEN'] = 'testauthtoken' self.request.headers['X_AUTH_TOKEN'] = 'testauthtoken'
self.request.headers['X_SERVICE_CATALOG'] = json.dumps({}) self.request.headers['X_SERVICE_CATALOG'] = jsonutils.dumps({})
self.roles = "pawn, knight, rook" self.roles = "pawn, knight, rook"

View File

@ -17,7 +17,6 @@ import base64
import copy import copy
import datetime import datetime
import inspect import inspect
import json
import os import os
import re import re
import urllib import urllib
@ -2128,7 +2127,7 @@ class ConsoleAuthTokensSampleJsonTests(ServersSampleBase):
"Console_auth_tokens") "Console_auth_tokens")
def _get_console_url(self, data): def _get_console_url(self, data):
return json.loads(data)["console"]["url"] return jsonutils.loads(data)["console"]["url"]
def _get_console_token(self, uuid): def _get_console_token(self, uuid):
response = self._do_post('servers/%s/action' % uuid, response = self._do_post('servers/%s/action' % uuid,
@ -2600,7 +2599,7 @@ class OsNetworksJsonTests(ApiSampleTestBaseV2):
def test_delete_network(self): def test_delete_network(self):
response = self._do_post('os-tenant-networks', "networks-post-req", {}) response = self._do_post('os-tenant-networks', "networks-post-req", {})
net = json.loads(response.read()) net = jsonutils.loads(response.read())
response = self._do_delete('os-tenant-networks/%s' % response = self._do_delete('os-tenant-networks/%s' %
net["network"]["id"]) net["network"]["id"])
self.assertEqual(response.status, 202) self.assertEqual(response.status, 202)

View File

@ -12,9 +12,9 @@
# License for the specific language governing permissions and limitations # License for the specific language governing permissions and limitations
# under the License. # under the License.
import json
import re import re
from nova.openstack.common import jsonutils
from nova.tests.integrated.v3 import test_servers from nova.tests.integrated.v3 import test_servers
@ -23,7 +23,7 @@ class ConsoleAuthTokensSampleJsonTests(test_servers.ServersSampleBase):
extra_extensions_to_load = ["os-remote-consoles"] extra_extensions_to_load = ["os-remote-consoles"]
def _get_console_url(self, data): def _get_console_url(self, data):
return json.loads(data)["console"]["url"] return jsonutils.loads(data)["console"]["url"]
def _get_console_token(self, uuid): def _get_console_token(self, uuid):
response = self._do_post('servers/%s/action' % uuid, response = self._do_post('servers/%s/action' % uuid,

View File

@ -12,12 +12,12 @@
# License for the specific language governing permissions and limitations # License for the specific language governing permissions and limitations
# under the License. # under the License.
import json
import os import os
import fixtures import fixtures
from oslo.config import cfg from oslo.config import cfg
from nova.openstack.common import jsonutils
from nova.openstack.common import policy as common_policy from nova.openstack.common import policy as common_policy
import nova.policy import nova.policy
from nova.tests import fake_policy from nova.tests import fake_policy
@ -56,7 +56,7 @@ class RoleBasedPolicyFixture(fixtures.Fixture):
allow users of the specified role only allow users of the specified role only
""" """
super(RoleBasedPolicyFixture, self).setUp() super(RoleBasedPolicyFixture, self).setUp()
policy = json.load(open(CONF.policy_file)) policy = jsonutils.load(open(CONF.policy_file))
# Convert all actions to require specified role # Convert all actions to require specified role
for action, rule in policy.iteritems(): for action, rule in policy.iteritems():
@ -66,7 +66,7 @@ class RoleBasedPolicyFixture(fixtures.Fixture):
self.policy_file_name = os.path.join(self.policy_dir.path, self.policy_file_name = os.path.join(self.policy_dir.path,
'policy.json') 'policy.json')
with open(self.policy_file_name, 'w') as policy_file: with open(self.policy_file_name, 'w') as policy_file:
json.dump(policy, policy_file) jsonutils.dump(policy, policy_file)
CONF.set_override('policy_file', self.policy_file_name) CONF.set_override('policy_file', self.policy_file_name)
nova.policy.reset() nova.policy.reset()
nova.policy.init() nova.policy.init()

View File

@ -19,7 +19,6 @@
import base64 import base64
import hashlib import hashlib
import hmac import hmac
import json
import re import re
try: try:
@ -43,6 +42,7 @@ from nova.db.sqlalchemy import api
from nova import exception from nova import exception
from nova.network import api as network_api from nova.network import api as network_api
from nova import objects from nova import objects
from nova.openstack.common import jsonutils
from nova import test from nova import test
from nova.tests import fake_block_device from nova.tests import fake_block_device
from nova.tests import fake_instance from nova.tests import fake_instance
@ -417,7 +417,7 @@ class OpenStackMetadataTestCase(test.TestCase):
mdjson = mdinst.lookup("/openstack/2012-08-10/meta_data.json") mdjson = mdinst.lookup("/openstack/2012-08-10/meta_data.json")
mdjson = mdinst.lookup("/openstack/latest/meta_data.json") mdjson = mdinst.lookup("/openstack/latest/meta_data.json")
mddict = json.loads(mdjson) mddict = jsonutils.loads(mdjson)
self.assertEqual(mddict['uuid'], self.instance['uuid']) self.assertEqual(mddict['uuid'], self.instance['uuid'])
self.assertIn('files', mddict) self.assertIn('files', mddict)
@ -447,7 +447,7 @@ class OpenStackMetadataTestCase(test.TestCase):
mdinst = fake_InstanceMetadata(self.stubs, inst, extra_md=extra) mdinst = fake_InstanceMetadata(self.stubs, inst, extra_md=extra)
mdjson = mdinst.lookup("/openstack/2012-08-10/meta_data.json") mdjson = mdinst.lookup("/openstack/2012-08-10/meta_data.json")
mddict = json.loads(mdjson) mddict = jsonutils.loads(mdjson)
for key, val in extra.iteritems(): for key, val in extra.iteritems():
self.assertEqual(mddict[key], val) self.assertEqual(mddict[key], val)
@ -485,20 +485,21 @@ class OpenStackMetadataTestCase(test.TestCase):
# verify that 2013-04-04 has the 'random' field # verify that 2013-04-04 has the 'random' field
mdjson = mdinst.lookup("/openstack/2013-04-04/meta_data.json") mdjson = mdinst.lookup("/openstack/2013-04-04/meta_data.json")
mddict = json.loads(mdjson) mddict = jsonutils.loads(mdjson)
self.assertIn("random_seed", mddict) self.assertIn("random_seed", mddict)
self.assertEqual(len(base64.b64decode(mddict["random_seed"])), 512) self.assertEqual(len(base64.b64decode(mddict["random_seed"])), 512)
# verify that older version do not have it # verify that older version do not have it
mdjson = mdinst.lookup("/openstack/2012-08-10/meta_data.json") mdjson = mdinst.lookup("/openstack/2012-08-10/meta_data.json")
self.assertNotIn("random_seed", json.loads(mdjson)) self.assertNotIn("random_seed", jsonutils.loads(mdjson))
def test_no_dashes_in_metadata(self): def test_no_dashes_in_metadata(self):
# top level entries in meta_data should not contain '-' in their name # top level entries in meta_data should not contain '-' in their name
inst = self.instance.obj_clone() inst = self.instance.obj_clone()
mdinst = fake_InstanceMetadata(self.stubs, inst) mdinst = fake_InstanceMetadata(self.stubs, inst)
mdjson = json.loads(mdinst.lookup("/openstack/latest/meta_data.json")) mdjson = jsonutils.loads(
mdinst.lookup("/openstack/latest/meta_data.json"))
self.assertEqual([], [k for k in mdjson.keys() if k.find("-") != -1]) self.assertEqual([], [k for k in mdjson.keys() if k.find("-") != -1])
@ -534,7 +535,7 @@ class OpenStackMetadataTestCase(test.TestCase):
# verify that 2013-10-17 has the vendor_data.json file # verify that 2013-10-17 has the vendor_data.json file
vdpath = "/openstack/2013-10-17/vendor_data.json" vdpath = "/openstack/2013-10-17/vendor_data.json"
vd = json.loads(mdinst.lookup(vdpath)) vd = jsonutils.loads(mdinst.lookup(vdpath))
# the instance should be passed through, and our class copies the # the instance should be passed through, and our class copies the
# uuid through to 'inst_uuid'. # uuid through to 'inst_uuid'.

View File

@ -17,7 +17,6 @@
import contextlib import contextlib
import cStringIO import cStringIO
import hashlib import hashlib
import json
import os import os
import time import time
@ -26,6 +25,7 @@ from oslo.config import cfg
from nova import conductor from nova import conductor
from nova import db from nova import db
from nova.openstack.common import importutils from nova.openstack.common import importutils
from nova.openstack.common import jsonutils
from nova.openstack.common import log as logging from nova.openstack.common import log as logging
from nova.openstack.common import processutils from nova.openstack.common import processutils
from nova import test from nova import test
@ -501,7 +501,7 @@ class ImageCacheManagerTestCase(test.NoDBTestCase):
d = {'sha1': '21323454'} d = {'sha1': '21323454'}
with open('%s.info' % fname, 'w') as f: with open('%s.info' % fname, 'w') as f:
f.write(json.dumps(d)) f.write(jsonutils.dumps(d))
image_cache_manager = imagecache.ImageCacheManager() image_cache_manager = imagecache.ImageCacheManager()
image_cache_manager.unexplained_images = [fname] image_cache_manager.unexplained_images = [fname]

View File

@ -21,7 +21,6 @@ http://wiki.openstack.org/nova-image-cache-management.
""" """
import hashlib import hashlib
import json
import os import os
import re import re
import time import time
@ -206,7 +205,7 @@ def write_stored_info(target, field=None, value=None):
d['%s-timestamp' % field] = time.time() d['%s-timestamp' % field] = time.time()
with open(info_file, 'w') as f: with open(info_file, 'w') as f:
f.write(json.dumps(d)) f.write(jsonutils.dumps(d))
write_file(info_file, field, value) write_file(info_file, field, value)

View File

@ -13,13 +13,13 @@
# under the License. # under the License.
import json
import os import os
import time import time
from oslo.config import cfg from oslo.config import cfg
from nova.i18n import _ from nova.i18n import _
from nova.openstack.common import jsonutils
from nova.openstack.common import log as logging from nova.openstack.common import log as logging
from nova import utils from nova import utils
@ -59,7 +59,7 @@ def register_storage_use(storage_path, hostname):
if os.path.exists(id_path): if os.path.exists(id_path):
with open(id_path) as f: with open(id_path) as f:
try: try:
d = json.loads(f.read()) d = jsonutils.loads(f.read())
except ValueError: except ValueError:
LOG.warning(_("Cannot decode JSON from %(id_path)s"), LOG.warning(_("Cannot decode JSON from %(id_path)s"),
{"id_path": id_path}) {"id_path": id_path})
@ -67,7 +67,7 @@ def register_storage_use(storage_path, hostname):
d[hostname] = time.time() d[hostname] = time.time()
with open(id_path, 'w') as f: with open(id_path, 'w') as f:
f.write(json.dumps(d)) f.write(jsonutils.dumps(d))
return do_register_storage_use(storage_path, hostname) return do_register_storage_use(storage_path, hostname)
@ -97,7 +97,7 @@ def get_storage_users(storage_path):
if os.path.exists(id_path): if os.path.exists(id_path):
with open(id_path) as f: with open(id_path) as f:
try: try:
d = json.loads(f.read()) d = jsonutils.loads(f.read())
except ValueError: except ValueError:
LOG.warning(_("Cannot decode JSON from %(id_path)s"), LOG.warning(_("Cannot decode JSON from %(id_path)s"),
{"id_path": id_path}) {"id_path": id_path})