Use jsonutils instead of stdlib json

jsonutils provides multiple benefits in comparison to pure stdlib json
(like using simplejson on Python 2.6).

Similar patch was already merged before [1], but since it lacked hacking
rule to enforce jsonutils usage, new occurrences of stdlib json module
usage were introduced.

This patch switches all the code to using jsonutils and adds a hacking
rule to enforce the rule.

The hacking rule requires that jsonutils module does not mimic as 'json'
thru using import renames, so the code was updated not to rename the
module when doing import.

The hacking rule was shamelessly copied from the corresponding nova
review [2].

[1]: https://review.openstack.org/#/c/99760/
[2]: https://review.openstack.org/111296/

Change-Id: Ie7a5bb76445e15cde9fbf9ff3d2101a014637b37
This commit is contained in:
Ihar Hrachyshka 2014-08-08 00:04:44 +02:00
parent 4b3547c28c
commit 0a75865b1f
28 changed files with 214 additions and 151 deletions

View File

@ -9,6 +9,7 @@ Neutron Specific Commandments
--------------------------
- [N320] Validate that LOG messages, except debug ones, have translations
- [N321] Validate that jsonutils module is used instead of json
Creating Unit Tests
-------------------

View File

@ -46,5 +46,27 @@ def validate_log_translations(logical_line, physical_line, filename):
yield (0, msg)
def use_jsonutils(logical_line, filename):
msg = "N321: jsonutils.%(fun)s must be used instead of json.%(fun)s"
# Some files in the tree are not meant to be run from inside Neutron
# itself, so we should not complain about them not using jsonutils
json_check_skipped_patterns = [
"neutron/plugins/openvswitch/agent/xenapi/etc/xapi.d/plugins/netwrap",
]
for pattern in json_check_skipped_patterns:
if pattern in filename:
return
if "json." in logical_line:
json_funcs = ['dumps(', 'dump(', 'loads(', 'load(']
for f in json_funcs:
pos = logical_line.find('json.%s' % f)
if pos != -1:
yield (pos, msg % {'fun': f[:-1]})
def factory(register):
register(validate_log_translations)
register(use_jsonutils)

View File

@ -44,7 +44,7 @@ from oslo.config import cfg
from neutron.common import exceptions
from neutron.common import utils
from neutron.openstack.common import excutils
from neutron.openstack.common import jsonutils as json
from neutron.openstack.common import jsonutils
from neutron.openstack.common import log as logging
from neutron.plugins.bigswitch.db import consistency_db as cdb
@ -112,7 +112,7 @@ class ServerProxy(object):
def get_capabilities(self):
try:
body = self.rest_call('GET', CAPABILITIES_PATH)[2]
self.capabilities = json.loads(body)
self.capabilities = jsonutils.loads(body)
except Exception:
LOG.exception(_("Couldn't retrieve capabilities. "
"Newer API calls won't be supported."))
@ -124,7 +124,7 @@ class ServerProxy(object):
def rest_call(self, action, resource, data='', headers={}, timeout=False,
reconnect=False, hash_handler=None):
uri = self.base_uri + resource
body = json.dumps(data)
body = jsonutils.dumps(data)
if not headers:
headers = {}
headers['Content-type'] = 'application/json'
@ -192,7 +192,7 @@ class ServerProxy(object):
if hash_value is not None:
hash_handler.put_hash(hash_value)
try:
respdata = json.loads(respstr)
respdata = jsonutils.loads(respstr)
except ValueError:
# response was not JSON, ignore the exception
pass

View File

@ -27,7 +27,7 @@ import re
from six import moves
from wsgiref import simple_server
from neutron.openstack.common import jsonutils as json
from neutron.openstack.common import jsonutils
class TestNetworkCtrl(object):
@ -123,7 +123,7 @@ class TestNetworkCtrl(object):
request_data = environ.get('wsgi.input').read(content_len)
if request_data:
try:
request_data = json.loads(request_data)
request_data = jsonutils.loads(request_data)
except Exception:
# OK for it not to be json! Ignore it
pass
@ -138,13 +138,14 @@ class TestNetworkCtrl(object):
print('%s %s' % (method, uri))
if request_data:
print('%s' %
json.dumps(request_data, sort_keys=True, indent=4))
jsonutils.dumps(
request_data, sort_keys=True, indent=4))
status, body = self.request_handler(method, uri, None)
body_data = None
if body:
try:
body_data = json.loads(body)
body_data = jsonutils.loads(body)
except Exception:
# OK for it not to be json! Ignore it
pass
@ -153,7 +154,8 @@ class TestNetworkCtrl(object):
if self.debug:
if self.debug_env:
print('%s: %s' % ('Response',
json.dumps(body_data, sort_keys=True, indent=4)))
jsonutils.dumps(
body_data, sort_keys=True, indent=4)))
return body
return simple_server.make_server(self.host, self.port, app)

View File

@ -14,9 +14,9 @@
#
# @author: Hareesh Puthalath, Cisco Systems, Inc.
import json
import logging
from neutron.openstack.common import jsonutils
from neutron.plugins.cisco.cfg_agent.device_drivers import devicedriver_api
LOG = logging.getLogger(__name__)
@ -33,7 +33,7 @@ class DummyRoutingDriver(devicedriver_api.RoutingDriverBase):
# Datetime values causes json decoding errors. So removing it locally
if my_device_params.get('created_at'):
del my_device_params['created_at']
LOG.debug(json.dumps(my_device_params, sort_keys=True, indent=4))
LOG.debug(jsonutils.dumps(my_device_params, sort_keys=True, indent=4))
###### Public Functions ########
def router_added(self, ri):
@ -44,7 +44,7 @@ class DummyRoutingDriver(devicedriver_api.RoutingDriverBase):
def internal_network_added(self, ri, port):
LOG.debug("DummyDriver internal_network_added() called.")
LOG.debug("Int port data: " + json.dumps(port, sort_keys=True,
LOG.debug("Int port data: " + jsonutils.dumps(port, sort_keys=True,
indent=4))
def internal_network_removed(self, ri, port):
@ -52,8 +52,9 @@ class DummyRoutingDriver(devicedriver_api.RoutingDriverBase):
def external_gateway_added(self, ri, ex_gw_port):
LOG.debug("DummyDriver external_gateway_added() called.")
LOG.debug("Ext port data: " + json.dumps(ex_gw_port, sort_keys=True,
indent=4))
LOG.debug("Ext port data: " + jsonutils.dumps(ex_gw_port,
sort_keys=True,
indent=4))
def external_gateway_removed(self, ri, ex_gw_port):
LOG.debug("DummyDriver external_gateway_removed() called.")

View File

@ -21,7 +21,7 @@ import time
import requests
import requests.exceptions
from neutron.openstack.common import jsonutils as json
from neutron.openstack.common import jsonutils
from neutron.openstack.common import log as logging
from neutron.plugins.ml2.drivers.cisco.apic import exceptions as cexc
@ -184,7 +184,7 @@ class ApicSession(object):
@staticmethod
def _make_data(key, **attrs):
"""Build the body for a msg out of a key and some attributes."""
return json.dumps({key: {'attributes': attrs}})
return jsonutils.dumps({key: {'attributes': attrs}})
def _api_url(self, api):
"""Create the URL for a generic API."""

View File

@ -18,7 +18,7 @@ import time
import requests
from neutron.openstack.common import excutils
from neutron.openstack.common import jsonutils as json
from neutron.openstack.common import jsonutils
from neutron.openstack.common import log as logging
from neutron.plugins.nec.common import config
from neutron.plugins.nec.common import exceptions as nexc
@ -80,7 +80,7 @@ class OFCClient(object):
{'host': self.host, 'port': self.port,
'method': method, 'action': action, 'body': body})
if type(body) is dict:
body = json.dumps(body)
body = jsonutils.dumps(body)
try:
res = self._get_response(method, action, body)
data = res.text
@ -90,7 +90,7 @@ class OFCClient(object):
# Try to decode JSON data if possible.
try:
data = json.loads(data)
data = jsonutils.loads(data)
except (ValueError, TypeError):
pass

View File

@ -17,7 +17,7 @@
"""Intermidiate NVSD Library."""
from neutron.openstack.common import excutils
from neutron.openstack.common import jsonutils as json
from neutron.openstack.common import jsonutils
from neutron.openstack.common import log as logging
import neutron.plugins.oneconvergence.lib.exception as nvsdexception
from neutron.plugins.oneconvergence.lib import plugin_helper
@ -101,7 +101,8 @@ class NVSDApi(object):
uri = NETWORKS_URI % tenant_id
response = self.send_request("POST", uri, body=json.dumps(network_obj),
response = self.send_request("POST", uri,
body=jsonutils.dumps(network_obj),
resource='network', tenant_id=tenant_id)
nvsd_net = response.json()
@ -119,7 +120,7 @@ class NVSDApi(object):
uri = NETWORK_URI % (tenant_id, network_id)
self.send_request("PUT", uri,
body=json.dumps(network_update),
body=jsonutils.dumps(network_update),
resource='network', tenant_id=tenant_id,
resource_id=network_id)
@ -154,7 +155,7 @@ class NVSDApi(object):
uri = SUBNETS_URI % (tenant_id, network_id)
self.send_request("POST", uri, body=json.dumps(subnet),
self.send_request("POST", uri, body=jsonutils.dumps(subnet),
resource='subnet', tenant_id=tenant_id)
LOG.debug(_("Subnet %(id)s created under tenant %(tenant_id)s"),
@ -183,7 +184,7 @@ class NVSDApi(object):
uri = SUBNET_URI % (tenant_id, network_id, subnet_id)
self.send_request("PUT", uri,
body=json.dumps(subnet_update),
body=jsonutils.dumps(subnet_update),
resource='subnet', tenant_id=tenant_id,
resource_id=subnet_id)
@ -216,7 +217,7 @@ class NVSDApi(object):
path = PORTS_URI % (tenant_id, network_id)
self.send_request("POST", path, body=json.dumps(lport),
self.send_request("POST", path, body=jsonutils.dumps(lport),
resource='port', tenant_id=tenant_id)
LOG.debug(_("Port %(id)s created under tenant %(tenant_id)s"),
@ -239,7 +240,7 @@ class NVSDApi(object):
uri = PORT_URI % (tenant_id, network_id, port_id)
self.send_request("PUT", uri, body=json.dumps(lport),
self.send_request("PUT", uri, body=jsonutils.dumps(lport),
resource='port', tenant_id=tenant_id,
resource_id=port_id)
@ -274,7 +275,7 @@ class NVSDApi(object):
uri = FLOATING_IPS_URI % tenant_id
self.send_request("POST", uri, body=json.dumps(floating_ip),
self.send_request("POST", uri, body=jsonutils.dumps(floating_ip),
resource='floating_ip',
tenant_id=tenant_id)
@ -290,7 +291,8 @@ class NVSDApi(object):
uri = FLOATING_IP_URI % (tenant_id, floating_ip_id)
self.send_request("PUT", uri,
body=json.dumps(floating_ip_update['floatingip']),
body=jsonutils.dumps(
floating_ip_update['floatingip']),
resource='floating_ip',
tenant_id=tenant_id,
resource_id=floating_ip_id)
@ -318,7 +320,7 @@ class NVSDApi(object):
uri = ROUTERS_URI % tenant_id
self.send_request("POST", uri, body=json.dumps(router),
self.send_request("POST", uri, body=jsonutils.dumps(router),
resource='router',
tenant_id=tenant_id)
@ -334,7 +336,7 @@ class NVSDApi(object):
uri = ROUTER_URI % (tenant_id, router_id)
self.send_request("PUT", uri,
body=json.dumps(router),
body=jsonutils.dumps(router),
resource='router', tenant_id=tenant_id,
resource_id=router_id)

View File

@ -23,7 +23,7 @@ from oslo.config import cfg
import requests
from six.moves.urllib import parse
from neutron.openstack.common import jsonutils as json
from neutron.openstack.common import jsonutils
from neutron.openstack.common import log as logging
import neutron.plugins.oneconvergence.lib.exception as exception
@ -68,7 +68,8 @@ class NVSDController(object):
login_url = parse.urljoin(self.api_url,
"/pluginhandler/ocplugin/authmgmt/login")
data = json.dumps({"user_name": self._user, "passwd": self._password})
data = jsonutils.dumps({"user_name": self._user,
"passwd": self._password})
attempts = 0
@ -98,7 +99,7 @@ class NVSDController(object):
LOG.debug(_("Login Successful %(uri)s "
"%(status)s"), {'uri': self.api_url,
'status': response.status_code})
self.auth_token = json.loads(response.content)["session_uuid"]
self.auth_token = jsonutils.loads(response.content)["session_uuid"]
LOG.debug(_("AuthToken = %s"), self.auth_token)
else:
LOG.error(_("login failed"))

View File

@ -18,7 +18,7 @@ import eventlet
import httplib
import urllib
from neutron.openstack.common import jsonutils as json
from neutron.openstack.common import jsonutils
from neutron.openstack.common import log as logging
from neutron.plugins.vmware.api_client import request
@ -199,7 +199,7 @@ class GetApiProvidersRequestEventlet(EventletApiRequest):
try:
if self.successful():
ret = []
body = json.loads(self.value.body)
body = jsonutils.loads(self.value.body)
for node in body.get('results', []):
for role in node.get('roles', []):
if role.get('role') == 'api_provider':

View File

@ -14,7 +14,7 @@
# under the License.
from neutron.common import exceptions as exception
from neutron.openstack.common import jsonutils as json
from neutron.openstack.common import jsonutils
from neutron.openstack.common import log
from neutron.plugins.vmware.api_client import exception as api_exc
from neutron.plugins.vmware.common import exceptions as nsx_exc
@ -97,7 +97,7 @@ def do_request(*args, **kwargs):
try:
res = cluster.api_client.request(*args)
if res:
return json.loads(res)
return jsonutils.loads(res)
except api_exc.ResourceNotFound:
raise exception.NotFound()
except api_exc.ReadOnlyMode:
@ -141,4 +141,4 @@ def mk_body(**kwargs):
:param kwargs: the key/value pirs to be dumped into a json string.
:returns: a json string.
"""
return json.dumps(kwargs, ensure_ascii=False)
return jsonutils.dumps(kwargs, ensure_ascii=False)

View File

@ -14,7 +14,7 @@
# under the License.
#
from neutron.openstack.common import jsonutils as json
from neutron.openstack.common import jsonutils
from neutron.openstack.common import log
from neutron.plugins.vmware.api_client import exception as api_exc
from neutron.plugins.vmware.common import exceptions as nsx_exc
@ -59,7 +59,7 @@ def create_l2_gw_service(cluster, tenant_id, display_name, devices):
}
return nsxlib.do_request(
HTTP_POST, nsxlib._build_uri_path(GWSERVICE_RESOURCE),
json.dumps(gwservice_obj), cluster=cluster)
jsonutils.dumps(gwservice_obj), cluster=cluster)
def plug_l2_gw_service(cluster, lswitch_id, lport_id,
@ -101,7 +101,7 @@ def update_l2_gw_service(cluster, gateway_id, display_name):
return nsxlib.do_request(HTTP_PUT,
nsxlib._build_uri_path(GWSERVICE_RESOURCE,
resource_id=gateway_id),
json.dumps(gwservice_obj), cluster=cluster)
jsonutils.dumps(gwservice_obj), cluster=cluster)
def delete_l2_gw_service(cluster, gateway_id):
@ -149,7 +149,7 @@ def create_gateway_device(cluster, tenant_id, display_name, neutron_id,
try:
return nsxlib.do_request(
HTTP_POST, nsxlib._build_uri_path(TRANSPORTNODE_RESOURCE),
json.dumps(body), cluster=cluster)
jsonutils.dumps(body), cluster=cluster)
except api_exc.InvalidSecurityCertificate:
raise nsx_exc.InvalidSecurityCertificate()
@ -166,7 +166,7 @@ def update_gateway_device(cluster, gateway_id, tenant_id,
HTTP_PUT,
nsxlib._build_uri_path(TRANSPORTNODE_RESOURCE,
resource_id=gateway_id),
json.dumps(body), cluster=cluster)
jsonutils.dumps(body), cluster=cluster)
except api_exc.InvalidSecurityCertificate:
raise nsx_exc.InvalidSecurityCertificate()

View File

@ -14,7 +14,7 @@
# under the License.
from neutron.common import exceptions as exception
from neutron.openstack.common import jsonutils as json
from neutron.openstack.common import jsonutils
from neutron.openstack.common import log
from neutron.plugins.vmware.api_client import exception as api_exc
from neutron.plugins.vmware.common import exceptions as nsx_exc
@ -56,7 +56,7 @@ def lsn_for_network_create(cluster, network_id):
}
return nsxlib.do_request(HTTP_POST,
nsxlib._build_uri_path(LSERVICESNODE_RESOURCE),
json.dumps(lsn_obj),
jsonutils.dumps(lsn_obj),
cluster=cluster)["uuid"]
@ -88,7 +88,7 @@ def lsn_port_host_entries_update(
parent_resource_id=lsn_id,
resource_id=lsn_port_id,
extra_action=conf),
json.dumps(hosts_obj),
jsonutils.dumps(hosts_obj),
cluster=cluster)
@ -103,7 +103,7 @@ def lsn_port_create(cluster, lsn_id, port_data):
return nsxlib.do_request(HTTP_POST,
nsxlib._build_uri_path(LSERVICESNODEPORT_RESOURCE,
parent_resource_id=lsn_id),
json.dumps(port_obj),
jsonutils.dumps(port_obj),
cluster=cluster)["uuid"]
@ -164,7 +164,7 @@ def lsn_port_plug_network(cluster, lsn_id, lsn_port_id, lswitch_port_id):
parent_resource_id=lsn_id,
resource_id=lsn_port_id,
is_attachment=True),
json.dumps(patch_obj),
jsonutils.dumps(patch_obj),
cluster=cluster)
except api_exc.Conflict:
# This restriction might be lifted at some point
@ -184,7 +184,7 @@ def _lsn_configure_action(
nsxlib._build_uri_path(LSERVICESNODE_RESOURCE,
resource_id=lsn_id,
extra_action=action),
json.dumps(lsn_obj),
jsonutils.dumps(lsn_obj),
cluster=cluster)
@ -194,14 +194,14 @@ def _lsn_port_configure_action(
nsxlib._build_uri_path(LSERVICESNODE_RESOURCE,
resource_id=lsn_id,
extra_action=action),
json.dumps({"enabled": is_enabled}),
jsonutils.dumps({"enabled": is_enabled}),
cluster=cluster)
nsxlib.do_request(HTTP_PUT,
nsxlib._build_uri_path(LSERVICESNODEPORT_RESOURCE,
parent_resource_id=lsn_id,
resource_id=lsn_port_id,
extra_action=action),
json.dumps(obj),
jsonutils.dumps(obj),
cluster=cluster)
@ -244,7 +244,7 @@ def _lsn_port_host_action(
resource_id=lsn_port_id,
extra_action=extra_action,
filters={"action": action}),
json.dumps(host_obj),
jsonutils.dumps(host_obj),
cluster=cluster)

View File

@ -16,7 +16,7 @@
from neutron.common import constants
from neutron.common import exceptions
from neutron.openstack.common import excutils
from neutron.openstack.common import jsonutils as json
from neutron.openstack.common import jsonutils
from neutron.openstack.common import log
from neutron.plugins.vmware.common import utils
from neutron.plugins.vmware import nsxlib
@ -37,7 +37,7 @@ def mk_body(**kwargs):
:param kwargs: the key/value pirs to be dumped into a json string.
:returns: a json string.
"""
return json.dumps(kwargs, ensure_ascii=False)
return jsonutils.dumps(kwargs, ensure_ascii=False)
def query_security_profiles(cluster, fields=None, filters=None):
@ -125,7 +125,7 @@ def update_security_profile(cluster, spid, name):
return nsxlib.do_request(
HTTP_PUT,
nsxlib._build_uri_path(SECPROF_RESOURCE, resource_id=spid),
json.dumps({"display_name": utils.check_and_truncate(name)}),
jsonutils.dumps({"display_name": utils.check_and_truncate(name)}),
cluster=cluster)

View File

@ -18,7 +18,7 @@ from oslo.config import cfg
from neutron.common import constants
from neutron.common import exceptions as exception
from neutron.openstack.common import jsonutils as json
from neutron.openstack.common import jsonutils
from neutron.openstack.common import log
from neutron.plugins.vmware.api_client import exception as api_exc
from neutron.plugins.vmware.common import exceptions as nsx_exc
@ -125,7 +125,7 @@ def create_lswitch(cluster, neutron_net_id, tenant_id, display_name,
if "tags" in kwargs:
lswitch_obj["tags"].extend(kwargs["tags"])
uri = nsxlib._build_uri_path(LSWITCH_RESOURCE)
lswitch = nsxlib.do_request(HTTP_POST, uri, json.dumps(lswitch_obj),
lswitch = nsxlib.do_request(HTTP_POST, uri, jsonutils.dumps(lswitch_obj),
cluster=cluster)
LOG.debug(_("Created logical switch: %s"), lswitch['uuid'])
return lswitch
@ -144,7 +144,7 @@ def update_lswitch(cluster, lswitch_id, display_name,
if tags:
lswitch_obj['tags'] = tags
try:
return nsxlib.do_request(HTTP_PUT, uri, json.dumps(lswitch_obj),
return nsxlib.do_request(HTTP_PUT, uri, jsonutils.dumps(lswitch_obj),
cluster=cluster)
except exception.NotFound as e:
LOG.error(_("Network not found, Error: %s"), str(e))
@ -319,7 +319,7 @@ def update_port(cluster, lswitch_uuid, lport_uuid, neutron_port_id, tenant_id,
path = "/ws.v1/lswitch/" + lswitch_uuid + "/lport/" + lport_uuid
try:
result = nsxlib.do_request(HTTP_PUT, path, json.dumps(lport_obj),
result = nsxlib.do_request(HTTP_PUT, path, jsonutils.dumps(lport_obj),
cluster=cluster)
LOG.debug(_("Updated logical port %(result)s "
"on logical switch %(uuid)s"),
@ -353,7 +353,7 @@ def create_lport(cluster, lswitch_uuid, tenant_id, neutron_port_id,
path = nsxlib._build_uri_path(LSWITCHPORT_RESOURCE,
parent_resource_id=lswitch_uuid)
result = nsxlib.do_request(HTTP_POST, path, json.dumps(lport_obj),
result = nsxlib.do_request(HTTP_POST, path, jsonutils.dumps(lport_obj),
cluster=cluster)
LOG.debug(_("Created logical port %(result)s on logical switch %(uuid)s"),
@ -382,7 +382,7 @@ def plug_interface(cluster, lswitch_id, lport_id, att_obj):
nsxlib._build_uri_path(LSWITCHPORT_RESOURCE,
lport_id, lswitch_id,
is_attachment=True),
json.dumps(att_obj),
jsonutils.dumps(att_obj),
cluster=cluster)

View File

@ -20,7 +20,7 @@ import base64
import httplib2
from oslo.config import cfg
from neutron.openstack.common import jsonutils as json
from neutron.openstack.common import jsonutils
from neutron.openstack.common import log as logging
from neutron.services.firewall.agents.varmour import varmour_utils as va_utils
@ -87,7 +87,7 @@ class vArmourRestAPI(object):
def rest_api(self, method, url, body=None, headers=None):
url = REST_URL_PREFIX + url
if body:
body_data = json.dumps(body)
body_data = jsonutils.dumps(body)
else:
body_data = ''
if not headers:
@ -118,7 +118,7 @@ class vArmourRestAPI(object):
if resp.status == 200:
return {'status': resp.status,
'reason': resp.reason,
'body': json.loads(resp_str)}
'body': jsonutils.loads(resp_str)}
except Exception:
LOG.error(_('vArmourRestAPI: Could not establish HTTP connection'))

View File

@ -34,7 +34,7 @@ from neutron import context
from neutron.db.loadbalancer import loadbalancer_db as lb_db
from neutron.extensions import loadbalancer
from neutron.openstack.common import excutils
from neutron.openstack.common import jsonutils as json
from neutron.openstack.common import jsonutils
from neutron.openstack.common import log as logging
from neutron.plugins.common import constants
from neutron.services.loadbalancer.drivers import abstract_driver
@ -728,7 +728,7 @@ class vDirectRESTClient:
if binary:
body = data
else:
body = json.dumps(data)
body = jsonutils.dumps(data)
debug_data = 'binary' if binary else body
debug_data = debug_data if debug_data else 'EMPTY'
@ -758,7 +758,7 @@ class vDirectRESTClient:
respstr = response.read()
respdata = respstr
try:
respdata = json.loads(respstr)
respdata = jsonutils.loads(respstr)
except ValueError:
# response was not JSON, ignore the exception
pass

View File

@ -15,7 +15,7 @@
# @author: Kevin Benton, <kevin.benton@bigswitch.com>
#
from neutron.openstack.common import jsonutils as json
from neutron.openstack.common import jsonutils
from neutron.openstack.common import log as logging
from neutron.plugins.bigswitch import servermanager
@ -119,7 +119,7 @@ class VerifyMultiTenantFloatingIP(HTTPConnectionMock):
def request(self, action, uri, body, headers):
# Only handle network update requests
if 'network' in uri and 'tenant' in uri and 'ports' not in uri:
req = json.loads(body)
req = jsonutils.loads(body)
if 'network' not in req or 'floatingips' not in req['network']:
msg = _("No floating IPs in request"
"uri=%(uri)s, body=%(body)s") % {'uri': uri,

View File

@ -18,7 +18,7 @@ import mock
from oslo.config import cfg
import requests
from neutron.openstack.common import jsonutils as json
from neutron.openstack.common import jsonutils
from neutron.plugins.nec.common import config
from neutron.plugins.nec.common import exceptions as nexc
from neutron.plugins.nec.common import ofc_client
@ -64,7 +64,7 @@ class OFCClientTest(base.BaseTestCase):
headers=headers)
def test_do_request_200_json_value(self):
self._test_do_request(200, json.dumps([1, 2, 3]), [1, 2, 3])
self._test_do_request(200, jsonutils.dumps([1, 2, 3]), [1, 2, 3])
def test_do_request_200_string(self):
self._test_do_request(200, 'abcdef', 'abcdef')
@ -78,7 +78,7 @@ class OFCClientTest(base.BaseTestCase):
def test_do_request_with_path_prefix(self):
config.CONF.set_override('path_prefix', '/dummy', group='OFC')
self._test_do_request(200, json.dumps([1, 2, 3]), [1, 2, 3],
self._test_do_request(200, jsonutils.dumps([1, 2, 3]), [1, 2, 3],
path_prefix='/dummy')
def test_do_request_returns_404(self):
@ -100,7 +100,7 @@ class OFCClientTest(base.BaseTestCase):
exc_checks)
def test_do_request_error_json_body(self):
resbody = json.dumps({'err_code': 40022,
resbody = jsonutils.dumps({'err_code': 40022,
'err_msg': 'This is an error.'})
errmsg = _("An OFC exception has occurred: Operation on OFC failed")
exc_checks = {'status': 400, 'err_code': 40022,

View File

@ -15,7 +15,7 @@
import mock
from neutron.openstack.common import jsonutils as json
from neutron.openstack.common import jsonutils
from neutron.plugins.oneconvergence.lib import nvsdlib
from neutron.tests import base
@ -65,10 +65,11 @@ class TestNVSDApi(base.BaseTestCase):
return_value=resp) as send_request:
uri = NETWORKS_URI % TEST_TENANT
net = self.nvsdlib.create_network(network_obj)
send_request.assert_called_once_with("POST", uri,
body=json.dumps(network_obj),
resource='network',
tenant_id=TEST_TENANT)
send_request.assert_called_once_with(
"POST", uri,
body=jsonutils.dumps(network_obj),
resource='network',
tenant_id=TEST_TENANT)
self.assertEqual(net, {'id': 'uuid'})
def test_update_network(self):
@ -79,7 +80,7 @@ class TestNVSDApi(base.BaseTestCase):
with mock.patch.object(self.nvsdlib, 'send_request') as send_request:
self.nvsdlib.update_network(network, update_network)
send_request.assert_called_once_with(
"PUT", uri, body=json.dumps(update_network),
"PUT", uri, body=jsonutils.dumps(update_network),
resource='network', tenant_id=TEST_TENANT,
resource_id=TEST_NET)
@ -123,10 +124,11 @@ class TestNVSDApi(base.BaseTestCase):
"admin_state_up": True,
"network_id": TEST_NET,
"status": 'ACTIVE'}
send_request.assert_called_once_with("POST", path,
body=json.dumps(expected),
resource='port',
tenant_id=TEST_TENANT)
send_request.assert_called_once_with(
"POST", path,
body=jsonutils.dumps(expected),
resource='port',
tenant_id=TEST_TENANT)
def test_update_port(self):
port = {'id': TEST_PORT,
@ -137,11 +139,12 @@ class TestNVSDApi(base.BaseTestCase):
with mock.patch.object(self.nvsdlib, 'send_request') as send_request:
self.nvsdlib.update_port(TEST_TENANT, port, port_update)
send_request.assert_called_once_with("PUT", uri,
body=json.dumps(port_update),
resource='port',
resource_id='test-port',
tenant_id=TEST_TENANT)
send_request.assert_called_once_with(
"PUT", uri,
body=jsonutils.dumps(port_update),
resource='port',
resource_id='test-port',
tenant_id=TEST_TENANT)
def test_delete_port(self):
port = {'network_id': TEST_NET,
@ -164,7 +167,7 @@ class TestNVSDApi(base.BaseTestCase):
with mock.patch.object(self.nvsdlib, 'send_request') as send_request:
self.nvsdlib.create_subnet(subnet)
send_request.assert_called_once_with("POST", uri,
body=json.dumps(subnet),
body=jsonutils.dumps(subnet),
resource='subnet',
tenant_id=TEST_TENANT)
@ -178,7 +181,8 @@ class TestNVSDApi(base.BaseTestCase):
with mock.patch.object(self.nvsdlib, 'send_request') as send_request:
self.nvsdlib.update_subnet(subnet, subnet_update)
send_request.assert_called_once_with(
"PUT", uri, body=json.dumps(subnet_update), resource='subnet',
"PUT", uri,
body=jsonutils.dumps(subnet_update), resource='subnet',
tenant_id=TEST_TENANT, resource_id=TEST_SUBNET)
def test_delete_subnet(self):
@ -201,10 +205,11 @@ class TestNVSDApi(base.BaseTestCase):
with mock.patch.object(self.nvsdlib, 'send_request') as send_request:
self.nvsdlib.create_floatingip(floatingip)
send_request.assert_called_once_with("POST", uri,
body=json.dumps(floatingip),
resource='floating_ip',
tenant_id=TEST_TENANT)
send_request.assert_called_once_with(
"POST", uri,
body=jsonutils.dumps(floatingip),
resource='floating_ip',
tenant_id=TEST_TENANT)
def test_update_floatingip(self):
floatingip = {'id': TEST_FIP,
@ -215,7 +220,8 @@ class TestNVSDApi(base.BaseTestCase):
with mock.patch.object(self.nvsdlib, 'send_request') as send_request:
self.nvsdlib.update_floatingip(floatingip, floatingip_update)
send_request.assert_called_once_with(
"PUT", uri, body=json.dumps(floatingip_update['floatingip']),
"PUT", uri,
body=jsonutils.dumps(floatingip_update['floatingip']),
resource='floating_ip', tenant_id=TEST_TENANT,
resource_id=TEST_FIP)
@ -237,7 +243,7 @@ class TestNVSDApi(base.BaseTestCase):
with mock.patch.object(self.nvsdlib, 'send_request') as send_request:
self.nvsdlib.create_router(router)
send_request.assert_called_once_with(
"POST", uri, body=json.dumps(router), resource='router',
"POST", uri, body=jsonutils.dumps(router), resource='router',
tenant_id=TEST_TENANT)
def test_update_router(self):
@ -247,7 +253,7 @@ class TestNVSDApi(base.BaseTestCase):
with mock.patch.object(self.nvsdlib, 'send_request') as send_request:
self.nvsdlib.update_router(router)
send_request.assert_called_once_with(
"PUT", uri, body=json.dumps(router),
"PUT", uri, body=jsonutils.dumps(router),
resource='router', tenant_id=TEST_TENANT,
resource_id=TEST_ROUTER)

View File

@ -16,7 +16,7 @@
import mock
import requests
from neutron.openstack.common import jsonutils as json
from neutron.openstack.common import jsonutils
from neutron.plugins.oneconvergence.lib import config # noqa
from neutron.plugins.oneconvergence.lib import plugin_helper as client
from neutron.tests import base
@ -30,14 +30,14 @@ class TestPluginHelper(base.BaseTestCase):
def get_response(self, *args, **kwargs):
response = mock.Mock()
response.status_code = requests.codes.ok
response.content = json.dumps({'session_uuid': 'new_auth_token'})
response.content = jsonutils.dumps({'session_uuid': 'new_auth_token'})
return response
def test_login(self):
login_url = ('http://127.0.0.1:8082/pluginhandler/ocplugin/'
'authmgmt/login')
headers = {'Content-Type': 'application/json'}
data = json.dumps({"user_name": "ocplugin", "passwd": "oc123"})
data = jsonutils.dumps({"user_name": "ocplugin", "passwd": "oc123"})
timeout = 30.0
with mock.patch.object(self.nvsdcontroller, 'do_request',

View File

@ -24,7 +24,7 @@ from six.moves import queue as Queue
from neutron import context
from neutron.extensions import loadbalancer
from neutron import manager
from neutron.openstack.common import jsonutils as json
from neutron.openstack.common import jsonutils
from neutron.plugins.common import constants
from neutron.services.loadbalancer.drivers.radware import driver
from neutron.services.loadbalancer.drivers.radware import exceptions as r_exc
@ -66,9 +66,9 @@ def rest_call_function_mock(action, resource, data, headers, binary=False):
def _get_handler(resource):
if resource == GET_200[2]:
if rest_call_function_mock.TEMPLATES_MISSING:
data = json.loads('[]')
data = jsonutils.loads('[]')
else:
data = json.loads(
data = jsonutils.loads(
'[{"name":"openstack_l2_l3"},{"name":"openstack_l4"}]'
)
return 200, '', '', data
@ -76,7 +76,7 @@ def _get_handler(resource):
if resource in GET_200:
return 200, '', '', ''
else:
data = json.loads('{"complete":"True", "success": "True"}')
data = jsonutils.loads('{"complete":"True", "success": "True"}')
return 202, '', '', data
@ -86,10 +86,10 @@ def _delete_handler(resource):
def _post_handler(resource, binary):
if re.search(r'/api/workflow/.+/action/.+', resource):
data = json.loads('{"uri":"some_uri"}')
data = jsonutils.loads('{"uri":"some_uri"}')
return 202, '', '', data
elif re.search(r'/api/service\?name=.+', resource):
data = json.loads('{"links":{"actions":{"provision":"someuri"}}}')
data = jsonutils.loads('{"links":{"actions":{"provision":"someuri"}}}')
return 201, '', '', data
elif binary:
return 201, '', '', ''

View File

@ -41,3 +41,29 @@ class HackingTestCase(base.BaseTestCase):
self.assertEqual(
0, len(list(checks.validate_log_translations(ok,
ok, 'f'))))
def test_use_jsonutils(self):
def __get_msg(fun):
msg = ("N321: jsonutils.%(fun)s must be used instead of "
"json.%(fun)s" % {'fun': fun})
return [(0, msg)]
for method in ('dump', 'dumps', 'load', 'loads'):
self.assertEqual(
__get_msg(method),
list(checks.use_jsonutils("json.%s(" % method,
"./neutron/common/rpc.py")))
self.assertEqual(0,
len(list(checks.use_jsonutils("jsonx.%s(" % method,
"./neutron/common/rpc.py"))))
self.assertEqual(0,
len(list(checks.use_jsonutils("json.%sx(" % method,
"./neutron/common/rpc.py"))))
self.assertEqual(0,
len(list(checks.use_jsonutils(
"json.%s" % method,
"./neutron/plugins/openvswitch/agent/xenapi/etc/xapi.d/"
"plugins/netwrap"))))

View File

@ -27,7 +27,7 @@ from neutron.common import exceptions
from neutron import context
from neutron import manager
from neutron.openstack.common import importutils
from neutron.openstack.common import jsonutils as json
from neutron.openstack.common import jsonutils
from neutron.openstack.common import policy as common_policy
from neutron import policy
from neutron.tests import base
@ -524,7 +524,7 @@ class NeutronPolicyTestCase(base.BaseTestCase):
def _test_set_rules_with_deprecated_policy(self, input_rules,
expected_rules):
policy._set_rules(json.dumps(input_rules))
policy._set_rules(jsonutils.dumps(input_rules))
# verify deprecated policy has been removed
for pol in input_rules.keys():
self.assertNotIn(pol, common_policy._rules)

View File

@ -14,7 +14,7 @@
import six.moves.urllib.parse as urlparse
from neutron.openstack.common import jsonutils as json
from neutron.openstack.common import jsonutils
from neutron.openstack.common import log as logging
from neutron.openstack.common import uuidutils
from neutron.plugins.vmware.api_client import exception as api_exc
@ -153,7 +153,7 @@ class FakeClient:
return (tag_filter, attr_filter, page_len, page_cursor)
def _add_lswitch(self, body):
fake_lswitch = json.loads(body)
fake_lswitch = jsonutils.loads(body)
fake_lswitch['uuid'] = uuidutils.generate_uuid()
self._fake_lswitch_dict[fake_lswitch['uuid']] = fake_lswitch
# put the tenant_id and the zone_uuid in the main dict
@ -167,7 +167,7 @@ class FakeClient:
return fake_lswitch
def _build_lrouter(self, body, uuid=None):
fake_lrouter = json.loads(body)
fake_lrouter = jsonutils.loads(body)
if uuid:
fake_lrouter['uuid'] = uuid
fake_lrouter['tenant_id'] = self._get_tag(fake_lrouter, 'os_tid')
@ -198,13 +198,13 @@ class FakeClient:
return fake_lrouter
def _add_lqueue(self, body):
fake_lqueue = json.loads(body)
fake_lqueue = jsonutils.loads(body)
fake_lqueue['uuid'] = uuidutils.generate_uuid()
self._fake_lqueue_dict[fake_lqueue['uuid']] = fake_lqueue
return fake_lqueue
def _add_lswitch_lport(self, body, ls_uuid):
fake_lport = json.loads(body)
fake_lport = jsonutils.loads(body)
new_uuid = uuidutils.generate_uuid()
fake_lport['uuid'] = new_uuid
# put the tenant_id and the ls_uuid in the main dict
@ -231,7 +231,7 @@ class FakeClient:
return fake_lport
def _build_lrouter_lport(self, body, new_uuid=None, lr_uuid=None):
fake_lport = json.loads(body)
fake_lport = jsonutils.loads(body)
if new_uuid:
fake_lport['uuid'] = new_uuid
# put the tenant_id and the le_uuid in the main dict
@ -243,7 +243,7 @@ class FakeClient:
'q_port_id')
# replace ip_address with its json dump
if 'ip_addresses' in fake_lport:
ip_addresses_json = json.dumps(fake_lport['ip_addresses'])
ip_addresses_json = jsonutils.dumps(fake_lport['ip_addresses'])
fake_lport['ip_addresses_json'] = ip_addresses_json
return fake_lport
@ -264,7 +264,7 @@ class FakeClient:
return fake_lport
def _add_securityprofile(self, body):
fake_securityprofile = json.loads(body)
fake_securityprofile = jsonutils.loads(body)
fake_securityprofile['uuid'] = uuidutils.generate_uuid()
fake_securityprofile['tenant_id'] = self._get_tag(
fake_securityprofile, 'os_tid')
@ -276,18 +276,18 @@ class FakeClient:
return fake_securityprofile
def _add_lrouter_nat(self, body, lr_uuid):
fake_nat = json.loads(body)
fake_nat = jsonutils.loads(body)
new_uuid = uuidutils.generate_uuid()
fake_nat['uuid'] = new_uuid
fake_nat['lr_uuid'] = lr_uuid
self._fake_lrouter_nat_dict[fake_nat['uuid']] = fake_nat
if 'match' in fake_nat:
match_json = json.dumps(fake_nat['match'])
match_json = jsonutils.dumps(fake_nat['match'])
fake_nat['match_json'] = match_json
return fake_nat
def _add_gatewayservice(self, body):
fake_gwservice = json.loads(body)
fake_gwservice = jsonutils.loads(body)
fake_gwservice['uuid'] = str(uuidutils.generate_uuid())
fake_gwservice['tenant_id'] = self._get_tag(
fake_gwservice, 'os_tid')
@ -428,7 +428,7 @@ class FakeClient:
return False
def _build_item(resource):
item = json.loads(response_template % resource)
item = jsonutils.loads(response_template % resource)
if relations: