From daf34d9df804d592fd4aa7b5473f166cc8876a81 Mon Sep 17 00:00:00 2001 From: Erik Olof Gunnar Andersson Date: Tue, 15 Jan 2019 19:18:18 -0800 Subject: [PATCH] Use oslo_serialization instead of the json module directly * Always use oslo jsonutils. * Consistently import jsonutils as-is. * Use dump_as_bytes instead of dumps. https://wiki.openstack.org/wiki/Python3#Serialization:_base64.2C_JSON.2C_etc. Change-Id: I2b65faa7df43a1d58205e8ff106ff62f73d78198 --- magnum/api/attr_validator.py | 4 ++-- magnum/api/http_error.py | 7 +++---- magnum/api/middleware/parsable_error.py | 7 +++---- magnum/db/sqlalchemy/models.py | 7 +++---- .../drivers/common/templates/swarm/fragments/make-cert.py | 1 + .../tests/functional/api/v1/models/baymodelpatch_model.py | 4 ++-- magnum/tests/functional/api/v1/models/baypatch_model.py | 4 ++-- .../api/v1/models/cluster_templatepatch_model.py | 4 ++-- .../tests/functional/api/v1/models/clusterpatch_model.py | 4 ++-- magnum/tests/functional/common/models.py | 6 +++--- 10 files changed, 23 insertions(+), 25 deletions(-) diff --git a/magnum/api/attr_validator.py b/magnum/api/attr_validator.py index 9bf400dc4d..fc2f44dda9 100644 --- a/magnum/api/attr_validator.py +++ b/magnum/api/attr_validator.py @@ -14,7 +14,7 @@ from glanceclient import exc as glance_exception from novaclient import exceptions as nova_exception -from oslo_serialization import jsonutils as json +from oslo_serialization import jsonutils from magnum.api import utils as api_utils from magnum.common import clients @@ -165,7 +165,7 @@ def validate_labels_executor_env_variables(labels): mesos_slave_executor_env_val = labels.get( 'mesos_slave_executor_env_variables') try: - json.loads(mesos_slave_executor_env_val) + jsonutils.loads(mesos_slave_executor_env_val) except ValueError: err = (_("Json format error")) raise exception.InvalidParameterValue(err) diff --git a/magnum/api/http_error.py b/magnum/api/http_error.py index 04167b75b0..1246e3dfeb 100644 --- a/magnum/api/http_error.py +++ b/magnum/api/http_error.py @@ -12,8 +12,7 @@ # License for the specific language governing permissions and limitations # under the License. -import json -import six +from oslo_serialization import jsonutils from webob import exc @@ -50,7 +49,7 @@ class HTTPNotAcceptableAPIVersion(exc.HTTPNotAcceptable): for err_str in self.app_iter: err = {} try: - err = json.loads(err_str.decode('utf-8')) + err = jsonutils.loads(err_str.decode('utf-8')) except ValueError: pass @@ -63,7 +62,7 @@ class HTTPNotAcceptableAPIVersion(exc.HTTPNotAcceptable): err['links'] = [links] err['title'] = "Requested microversion is unsupported" - self.app_iter = [six.b(json.dumps(err))] + self.app_iter = [jsonutils.dump_as_bytes(err)] self.headers['Content-Length'] = str(len(self.app_iter[0])) return super(HTTPNotAcceptableAPIVersion, self).__call__( diff --git a/magnum/api/middleware/parsable_error.py b/magnum/api/middleware/parsable_error.py index e3cff0475d..06d0095ff9 100644 --- a/magnum/api/middleware/parsable_error.py +++ b/magnum/api/middleware/parsable_error.py @@ -18,8 +18,7 @@ response with one formatted so the client can parse it. Based on pecan.middleware.errordocument """ -import json -import six +from oslo_serialization import jsonutils from magnum.i18n import _ @@ -34,7 +33,7 @@ class ParsableErrorMiddleware(object): for err_str in app_iter: err = {} try: - err = json.loads(err_str.decode('utf-8')) + err = jsonutils.loads(err_str.decode('utf-8')) except ValueError: pass @@ -96,7 +95,7 @@ class ParsableErrorMiddleware(object): if (state['status_code'] // 100) not in (2, 3): errs = self._update_errors(app_iter, state['status_code']) - body = [six.b(json.dumps({'errors': errs}))] + body = [jsonutils.dump_as_bytes({'errors': errs})] state['headers'].append(('Content-Type', 'application/json')) state['headers'].append(('Content-Length', str(len(body[0])))) diff --git a/magnum/db/sqlalchemy/models.py b/magnum/db/sqlalchemy/models.py index 378f4b42d1..8a18868b41 100644 --- a/magnum/db/sqlalchemy/models.py +++ b/magnum/db/sqlalchemy/models.py @@ -16,10 +16,9 @@ SQLAlchemy models for container service """ -import json - from oslo_db.sqlalchemy import models from oslo_db.sqlalchemy.types import String +from oslo_serialization import jsonutils import six.moves.urllib.parse as urlparse from sqlalchemy import Boolean from sqlalchemy import Column @@ -61,12 +60,12 @@ class JsonEncodedType(TypeDecorator): "given" % {'class': self.__class__.__name__, 'type': self.type.__name__, 'value': type(value).__name__}) - serialized_value = json.dumps(value) + serialized_value = jsonutils.dumps(value) return serialized_value def process_result_value(self, value, dialect): if value is not None: - value = json.loads(value) + value = jsonutils.loads(value) return value diff --git a/magnum/drivers/common/templates/swarm/fragments/make-cert.py b/magnum/drivers/common/templates/swarm/fragments/make-cert.py index 54f06a28e1..ec00e6cc97 100644 --- a/magnum/drivers/common/templates/swarm/fragments/make-cert.py +++ b/magnum/drivers/common/templates/swarm/fragments/make-cert.py @@ -21,6 +21,7 @@ import sys import requests + HEAT_PARAMS_PATH = '/etc/sysconfig/heat-params' PUBLIC_IP_URL = 'http://169.254.169.254/latest/meta-data/public-ipv4' CERT_DIR = '/etc/docker' diff --git a/magnum/tests/functional/api/v1/models/baymodelpatch_model.py b/magnum/tests/functional/api/v1/models/baymodelpatch_model.py index 74fcbfd450..1623cb366e 100644 --- a/magnum/tests/functional/api/v1/models/baymodelpatch_model.py +++ b/magnum/tests/functional/api/v1/models/baymodelpatch_model.py @@ -10,7 +10,7 @@ # License for the specific language governing permissions and limitations # under the License. -import json +from oslo_serialization import jsonutils from magnum.tests.functional.common import models @@ -47,7 +47,7 @@ class BayModelPatchCollection(models.CollectionModel): collection = [] for d in data: collection.append(d.to_dict()) - return json.dumps(collection) + return jsonutils.dumps(collection) @classmethod def from_dict(cls, data): diff --git a/magnum/tests/functional/api/v1/models/baypatch_model.py b/magnum/tests/functional/api/v1/models/baypatch_model.py index 4a0f137446..717f65cd79 100644 --- a/magnum/tests/functional/api/v1/models/baypatch_model.py +++ b/magnum/tests/functional/api/v1/models/baypatch_model.py @@ -10,7 +10,7 @@ # License for the specific language governing permissions and limitations # under the License. -import json +from oslo_serialization import jsonutils from magnum.tests.functional.common import models @@ -47,7 +47,7 @@ class BayPatchCollection(models.CollectionModel): collection = [] for d in data: collection.append(d.to_dict()) - return json.dumps(collection) + return jsonutils.dumps(collection) @classmethod def from_dict(cls, data): diff --git a/magnum/tests/functional/api/v1/models/cluster_templatepatch_model.py b/magnum/tests/functional/api/v1/models/cluster_templatepatch_model.py index 83a6b6744b..9d6081a04f 100644 --- a/magnum/tests/functional/api/v1/models/cluster_templatepatch_model.py +++ b/magnum/tests/functional/api/v1/models/cluster_templatepatch_model.py @@ -10,7 +10,7 @@ # License for the specific language governing permissions and limitations # under the License. -import json +from oslo_serialization import jsonutils from magnum.tests.functional.common import models @@ -48,7 +48,7 @@ class ClusterTemplatePatchCollection(models.CollectionModel): collection = [] for d in data: collection.append(d.to_dict()) - return json.dumps(collection) + return jsonutils.dumps(collection) @classmethod def from_dict(cls, data): diff --git a/magnum/tests/functional/api/v1/models/clusterpatch_model.py b/magnum/tests/functional/api/v1/models/clusterpatch_model.py index 6e93f377af..73b1258c3d 100644 --- a/magnum/tests/functional/api/v1/models/clusterpatch_model.py +++ b/magnum/tests/functional/api/v1/models/clusterpatch_model.py @@ -10,7 +10,7 @@ # License for the specific language governing permissions and limitations # under the License. -import json +from oslo_serialization import jsonutils from magnum.tests.functional.common import models @@ -47,7 +47,7 @@ class ClusterPatchCollection(models.CollectionModel): collection = [] for d in data: collection.append(d.to_dict()) - return json.dumps(collection) + return jsonutils.dumps(collection) @classmethod def from_dict(cls, data): diff --git a/magnum/tests/functional/common/models.py b/magnum/tests/functional/common/models.py index 81706b4e63..ff7c9856b0 100644 --- a/magnum/tests/functional/common/models.py +++ b/magnum/tests/functional/common/models.py @@ -10,7 +10,7 @@ # License for the specific language governing permissions and limitations # under the License. -import json +from oslo_serialization import jsonutils class BaseModel(object): @@ -18,10 +18,10 @@ class BaseModel(object): @classmethod def from_json(cls, json_str): - return cls.from_dict(json.loads(json_str)) + return cls.from_dict(jsonutils.loads(json_str)) def to_json(self): - return json.dumps(self.to_dict()) + return jsonutils.dumps(self.to_dict()) @classmethod def from_dict(cls, data):