Browse Source

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
changes/31/631331/10
Erik Olof Gunnar Andersson 3 years ago
parent
commit
daf34d9df8
  1. 4
      magnum/api/attr_validator.py
  2. 7
      magnum/api/http_error.py
  3. 7
      magnum/api/middleware/parsable_error.py
  4. 7
      magnum/db/sqlalchemy/models.py
  5. 1
      magnum/drivers/common/templates/swarm/fragments/make-cert.py
  6. 4
      magnum/tests/functional/api/v1/models/baymodelpatch_model.py
  7. 4
      magnum/tests/functional/api/v1/models/baypatch_model.py
  8. 4
      magnum/tests/functional/api/v1/models/cluster_templatepatch_model.py
  9. 4
      magnum/tests/functional/api/v1/models/clusterpatch_model.py
  10. 6
      magnum/tests/functional/common/models.py

4
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)

7
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__(

7
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]))))

7
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

1
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'

4
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):

4
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):

4
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):

4
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):

6
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):

Loading…
Cancel
Save