From f999509bcaf6c87d1a981af6b67790aa326e9224 Mon Sep 17 00:00:00 2001 From: Sun Austin Date: Wed, 21 Nov 2018 17:30:25 +0800 Subject: [PATCH] Fix unicode issue for Python 2/3 compatible code. replace unicode with six.text_type Story: 2003433 Task: 27695 Change-Id: Ie7bc1b649b94fb1695eac51b65294d30e01f26f2 Signed-off-by: Sun Austin --- .../cgts-client/cgts-client/cgtsclient/exc.py | 3 +- .../sysinv/sysinv/api/controllers/v1/host.py | 30 +++++++++---------- .../sysinv/api/controllers/v1/interface.py | 2 +- .../sysinv/sysinv/api/controllers/v1/load.py | 9 +++--- .../api/controllers/v1/network_infra.py | 3 +- .../sysinv/api/controllers/v1/profile.py | 18 +++++------ .../sysinv/api/controllers/v1/sensorgroup.py | 2 +- .../sysinv/api/controllers/v1/service.py | 4 +-- .../api/controllers/v1/service_parameter.py | 3 +- .../sysinv/api/controllers/v1/servicegroup.py | 3 +- .../sysinv/api/controllers/v1/servicenode.py | 3 +- .../sysinv/api/controllers/v1/system.py | 2 +- .../sysinv/api/controllers/v1/upgrade.py | 5 ++-- .../sysinv/sysinv/sysinv/common/exception.py | 3 +- sysinv/sysinv/sysinv/sysinv/common/utils.py | 2 +- 15 files changed, 50 insertions(+), 42 deletions(-) diff --git a/sysinv/cgts-client/cgts-client/cgtsclient/exc.py b/sysinv/cgts-client/cgts-client/cgtsclient/exc.py index 988f4df04b..099388e981 100644 --- a/sysinv/cgts-client/cgts-client/cgtsclient/exc.py +++ b/sysinv/cgts-client/cgts-client/cgtsclient/exc.py @@ -10,6 +10,7 @@ # License for the specific language governing permissions and limitations # under the License. +import six import sys @@ -207,7 +208,7 @@ class CgtsclientException(Exception): if self.__class__.__name__.endswith('_Remote'): return self.args[0] else: - return unicode(self) + return six.text_type(self) class AmbiguousEndpoints(CgtsclientException): diff --git a/sysinv/sysinv/sysinv/sysinv/api/controllers/v1/host.py b/sysinv/sysinv/sysinv/sysinv/api/controllers/v1/host.py index 897b05ba28..1986070022 100644 --- a/sysinv/sysinv/sysinv/sysinv/api/controllers/v1/host.py +++ b/sysinv/sysinv/sysinv/sysinv/api/controllers/v1/host.py @@ -140,7 +140,7 @@ class HostProvisionState(state.State): class HostProvisionStateController(rest.RestController): # GET ihosts//state/provision - @wsme_pecan.wsexpose(HostProvisionState, unicode) + @wsme_pecan.wsexpose(HostProvisionState, six.text_type) def get(self, ihost_id): ihost = objects.host.get_by_uuid(pecan.request.context, ihost_id) @@ -149,7 +149,7 @@ class HostProvisionStateController(rest.RestController): return provision_state # PUT ihosts//state/provision - @wsme_pecan.wsexpose(HostProvisionState, unicode, unicode, status=202) + @wsme_pecan.wsexpose(HostProvisionState, six.text_type, six.text_type, status=202) def put(self, ihost_id, target): """Set the provision state of the machine.""" # TODO(lucasagomes): Test if target is a valid state and if it's able @@ -188,7 +188,7 @@ class HostStatesController(rest.RestController): } # GET ihosts//state - @wsme_pecan.wsexpose(HostStates, unicode) + @wsme_pecan.wsexpose(HostStates, six.text_type) def get(self, ihost_id): """List or update the state of a ihost.""" ihost = objects.host.get_by_uuid(pecan.request.context, @@ -207,7 +207,7 @@ class HostStatesController(rest.RestController): # PUT ihosts//state/update_install_uuid @cutils.synchronized(LOCK_NAME_STATE) - @wsme_pecan.wsexpose(HostStates, types.uuid, body=unicode) + @wsme_pecan.wsexpose(HostStates, types.uuid, body=six.text_type) def update_install_uuid(self, host_uuid, install_uuid): """ Update install_uuid in /etc/platform/platform.conf on the specified host. @@ -223,7 +223,7 @@ class HostStatesController(rest.RestController): # PUT ihosts//state/host_cpus_modify @cutils.synchronized(cpu_api.LOCK_NAME) - @wsme_pecan.wsexpose(cpu_api.CPUCollection, types.uuid, body=[unicode]) + @wsme_pecan.wsexpose(cpu_api.CPUCollection, types.uuid, body=[six.text_type]) def host_cpus_modify(self, host_uuid, capabilities): """ Perform bulk host cpus modify. :param host_uuid: UUID of the host @@ -1170,8 +1170,8 @@ class HostController(rest.RestController): activity = 'Controller-Standby' host['capabilities'].update({'Personality': activity}) - @wsme_pecan.wsexpose(HostCollection, unicode, unicode, int, unicode, - unicode, unicode) + @wsme_pecan.wsexpose(HostCollection, six.text_type, six.text_type, int, six.text_type, + six.text_type, six.text_type) def get_all(self, isystem_id=None, marker=None, limit=None, personality=None, sort_key='id', sort_dir='asc'): @@ -1182,7 +1182,7 @@ class HostController(rest.RestController): sort_key=sort_key, sort_dir=sort_dir) - @wsme_pecan.wsexpose(unicode, unicode, body=unicode) + @wsme_pecan.wsexpose(six.text_type, six.text_type, body=six.text_type) def install_progress(self, uuid, install_state, install_state_info=None): """ Update the install status for the given host.""" @@ -1200,8 +1200,8 @@ class HostController(rest.RestController): 'install_state_info': install_state_info}) - @wsme_pecan.wsexpose(HostCollection, unicode, unicode, int, unicode, - unicode, unicode) + @wsme_pecan.wsexpose(HostCollection, six.text_type, six.text_type, int, six.text_type, + six.text_type, six.text_type) def detail(self, isystem_id=None, marker=None, limit=None, personality=None, sort_key='id', sort_dir='asc'): @@ -1220,7 +1220,7 @@ class HostController(rest.RestController): sort_key=sort_key, sort_dir=sort_dir) - @wsme_pecan.wsexpose(Host, unicode) + @wsme_pecan.wsexpose(Host, six.text_type) def get_one(self, uuid): """Retrieve information about the given ihost.""" if self._from_isystem: @@ -1708,7 +1708,7 @@ class HostController(rest.RestController): return self._do_post(ihost_dict) - @wsme_pecan.wsexpose(Host, unicode, body=[unicode]) + @wsme_pecan.wsexpose(Host, six.text_type, body=[six.text_type]) def patch(self, uuid, patch): """ Update an existing ihost. """ @@ -2230,7 +2230,7 @@ class HostController(rest.RestController): return True @cutils.synchronized(LOCK_NAME) - @wsme_pecan.wsexpose(None, unicode, status_code=204) + @wsme_pecan.wsexpose(None, six.text_type, status_code=204) def delete(self, ihost_id): """Delete an ihost. """ @@ -2487,7 +2487,7 @@ class HostController(rest.RestController): target_load) @cutils.synchronized(LOCK_NAME) - @wsme_pecan.wsexpose(Host, unicode, body=unicode) + @wsme_pecan.wsexpose(Host, six.text_type, body=six.text_type) def upgrade(self, uuid, body): """Upgrade the host to the specified load""" @@ -2571,7 +2571,7 @@ class HostController(rest.RestController): return Host.convert_with_links(rpc_ihost) @cutils.synchronized(LOCK_NAME) - @wsme_pecan.wsexpose(Host, unicode, body=unicode) + @wsme_pecan.wsexpose(Host, six.text_type, body=six.text_type) def downgrade(self, uuid, body): """Downgrade the host to the specified load""" diff --git a/sysinv/sysinv/sysinv/sysinv/api/controllers/v1/interface.py b/sysinv/sysinv/sysinv/sysinv/api/controllers/v1/interface.py index 0d278239f2..d1bec4391c 100644 --- a/sysinv/sysinv/sysinv/sysinv/api/controllers/v1/interface.py +++ b/sysinv/sysinv/sysinv/sysinv/api/controllers/v1/interface.py @@ -827,7 +827,7 @@ def _check_interface_vlan_id(op, interface, ihost, from_profile=False): if interface['vlan_id'] < 1 or interface['vlan_id'] > 4094: raise wsme.exc.ClientSideError(_("VLAN id must be between 1 and 4094.")) else: - interface['vlan_id'] = unicode(interface['vlan_id']) + interface['vlan_id'] = six.text_type(interface['vlan_id']) return interface diff --git a/sysinv/sysinv/sysinv/sysinv/api/controllers/v1/load.py b/sysinv/sysinv/sysinv/sysinv/api/controllers/v1/load.py index 93d6678fc4..c2c22b1aa4 100644 --- a/sysinv/sysinv/sysinv/sysinv/api/controllers/v1/load.py +++ b/sysinv/sysinv/sysinv/sysinv/api/controllers/v1/load.py @@ -23,6 +23,7 @@ import jsonpatch import socket import pecan +import six from pecan import rest import wsme @@ -192,7 +193,7 @@ class LoadController(rest.RestController): return self._get_loads_collection(marker, limit, sort_key, sort_dir, expand, resource_url) - @wsme_pecan.wsexpose(Load, unicode) + @wsme_pecan.wsexpose(Load, six.text_type) def get_one(self, load_uuid): """Retrieve information about the given Load.""" @@ -285,8 +286,8 @@ class LoadController(rest.RestController): return Load.convert_with_links(new_load) @cutils.synchronized(LOCK_NAME) - @wsme.validate(unicode, [LoadPatchType]) - @wsme_pecan.wsexpose(Load, unicode, + @wsme.validate(six.text_type, [LoadPatchType]) + @wsme_pecan.wsexpose(Load, six.text_type, body=[LoadPatchType]) def patch(self, load_id, patch): """Update an existing load.""" @@ -317,7 +318,7 @@ class LoadController(rest.RestController): return Load.convert_with_links(rpc_load) @cutils.synchronized(LOCK_NAME) - @wsme_pecan.wsexpose(None, unicode, status_code=204) + @wsme_pecan.wsexpose(None, six.text_type, status_code=204) def delete(self, load_id): """Delete a load.""" diff --git a/sysinv/sysinv/sysinv/sysinv/api/controllers/v1/network_infra.py b/sysinv/sysinv/sysinv/sysinv/api/controllers/v1/network_infra.py index 1d6b5fe71a..b3dfc3815f 100644 --- a/sysinv/sysinv/sysinv/sysinv/api/controllers/v1/network_infra.py +++ b/sysinv/sysinv/sysinv/sysinv/api/controllers/v1/network_infra.py @@ -25,6 +25,7 @@ import jsonpatch import pecan from pecan import rest +import six import wsme from wsme import types as wtypes import wsmeext.pecan as wsme_pecan @@ -259,7 +260,7 @@ class InfraNetworkController(rest.RestController): raise wsme.exc.ClientSideError(_( "VLAN id must be between 1 and 4094.")) else: - infra['infra_vlan_id'] = unicode(infra['infra_vlan_id']) + infra['infra_vlan_id'] = six.text_type(infra['infra_vlan_id']) return infra @staticmethod diff --git a/sysinv/sysinv/sysinv/sysinv/api/controllers/v1/profile.py b/sysinv/sysinv/sysinv/sysinv/api/controllers/v1/profile.py index 01582a0364..38da3fc9ad 100644 --- a/sysinv/sysinv/sysinv/sysinv/api/controllers/v1/profile.py +++ b/sysinv/sysinv/sysinv/sysinv/api/controllers/v1/profile.py @@ -809,8 +809,8 @@ class ProfileController(rest.RestController): return stor_profiles - @wsme_pecan.wsexpose(ProfileCollection, unicode, unicode, int, - unicode, unicode) + @wsme_pecan.wsexpose(ProfileCollection, six.text_type, six.text_type, int, + six.text_type, six.text_type) def get_all(self, chassis_id=None, marker=None, limit=None, sort_key='id', sort_dir='asc'): """Retrieve a list of ihosts.""" @@ -820,8 +820,8 @@ class ProfileController(rest.RestController): sort_key=sort_key, sort_dir=sort_dir) - @wsme_pecan.wsexpose(ProfileCollection, unicode, unicode, int, - unicode, unicode) + @wsme_pecan.wsexpose(ProfileCollection, six.text_type, six.text_type, int, + six.text_type, six.text_type) def detail(self, chassis_id=None, marker=None, limit=None, sort_key='id', sort_dir='asc'): """Retrieve a list of ihosts with detail.""" @@ -839,7 +839,7 @@ class ProfileController(rest.RestController): sort_key=sort_key, sort_dir=sort_dir) - @wsme_pecan.wsexpose(Profile, unicode) + @wsme_pecan.wsexpose(Profile, six.text_type) def get_one(self, uuid): """Retrieve information about the given ihost.""" if self._from_chassis: @@ -944,7 +944,7 @@ class ProfileController(rest.RestController): return iprofile.convert_with_links(new_ihost) @cutils.synchronized(LOCK_NAME) - @wsme_pecan.wsexpose(Profile, unicode, body=[unicode]) + @wsme_pecan.wsexpose(Profile, six.text_type, body=[six.text_type]) def patch(self, uuid, patch): """Update an existing iprofile. """ @@ -999,7 +999,7 @@ class ProfileController(rest.RestController): raise wsme.exc.ClientSideError(msg) @cutils.synchronized(LOCK_NAME) - @wsme_pecan.wsexpose(None, unicode, status_code=204) + @wsme_pecan.wsexpose(None, six.text_type, status_code=204) def delete(self, ihost_id): """Delete an ihost profile. """ @@ -2694,9 +2694,9 @@ def ifprofile_apply_to_host(host, profile): uses_list = [] usedby_list = [] for u in iinterfaces: - if unicode(u.ifname) in i.uses or u.uuid in i.uses: + if six.text_type(u.ifname) in i.uses or u.uuid in i.uses: uses_list.append(u.uuid) - if unicode(u.ifname) in i.used_by or u.uuid in i.used_by: + if six.text_type(u.ifname) in i.used_by or u.uuid in i.used_by: usedby_list.append(u.uuid) idict['uses'] = uses_list diff --git a/sysinv/sysinv/sysinv/sysinv/api/controllers/v1/sensorgroup.py b/sysinv/sysinv/sysinv/sysinv/api/controllers/v1/sensorgroup.py index 6e052ac385..f7bac94391 100644 --- a/sysinv/sysinv/sysinv/sysinv/api/controllers/v1/sensorgroup.py +++ b/sysinv/sysinv/sysinv/sysinv/api/controllers/v1/sensorgroup.py @@ -414,7 +414,7 @@ class SensorGroupController(rest.RestController): raise wsme.exc.ClientSideError("_get_host_uuid lookup failed") return host.uuid - @wsme_pecan.wsexpose('json', body=unicode) + @wsme_pecan.wsexpose('json', body=six.text_type) def relearn(self, body): """ Handle Sensor Model Relearn Request.""" host_uuid = self._get_host_uuid(body) diff --git a/sysinv/sysinv/sysinv/sysinv/api/controllers/v1/service.py b/sysinv/sysinv/sysinv/sysinv/api/controllers/v1/service.py index b0ca1de6af..781d8c0ee3 100644 --- a/sysinv/sysinv/sysinv/sysinv/api/controllers/v1/service.py +++ b/sysinv/sysinv/sysinv/sysinv/api/controllers/v1/service.py @@ -150,7 +150,7 @@ LOCK_NAME = 'SMServiceController' class SMServiceController(rest.RestController): - @wsme_pecan.wsexpose(SMService, unicode) + @wsme_pecan.wsexpose(SMService, six.text_type) def get_one(self, uuid): sm_service = sm_api.service_show(uuid) if sm_service is None: @@ -172,7 +172,7 @@ class SMServiceController(rest.RestController): "Bad response from SM API")) @cutils.synchronized(LOCK_NAME) - @wsme_pecan.wsexpose(Service, wtypes.text, body=[unicode]) + @wsme_pecan.wsexpose(Service, wtypes.text, body=[six.text_type]) def patch(self, service_name, patch): """Update the service configuration.""" diff --git a/sysinv/sysinv/sysinv/sysinv/api/controllers/v1/service_parameter.py b/sysinv/sysinv/sysinv/sysinv/api/controllers/v1/service_parameter.py index 2b4b199b38..2a3a12cf66 100644 --- a/sysinv/sysinv/sysinv/sysinv/api/controllers/v1/service_parameter.py +++ b/sysinv/sysinv/sysinv/sysinv/api/controllers/v1/service_parameter.py @@ -11,6 +11,7 @@ import copy import netaddr import pecan from pecan import rest +import six import wsme from wsme import types as wtypes import wsmeext.pecan as wsme_pecan @@ -996,7 +997,7 @@ class ServiceParameterController(rest.RestController): return service @cutils.synchronized(LOCK_NAME) - @wsme_pecan.wsexpose('json', body=unicode) + @wsme_pecan.wsexpose('json', body=six.text_type) def apply(self, body): """ Apply the service parameters.""" service = self._get_service(body) diff --git a/sysinv/sysinv/sysinv/sysinv/api/controllers/v1/servicegroup.py b/sysinv/sysinv/sysinv/sysinv/api/controllers/v1/servicegroup.py index 42ec714282..31084311c3 100644 --- a/sysinv/sysinv/sysinv/sysinv/api/controllers/v1/servicegroup.py +++ b/sysinv/sysinv/sysinv/sysinv/api/controllers/v1/servicegroup.py @@ -7,6 +7,7 @@ # this file is used for service group requests. Keeping naming consistent with sm client from pecan import rest +import six import wsme from wsme import types as wtypes import wsmeext.pecan as wsme_pecan @@ -55,7 +56,7 @@ class SMServiceGroupCollection(base.APIBase): class SMServiceGroupController(rest.RestController): - @wsme_pecan.wsexpose(SMServiceGroup, unicode) + @wsme_pecan.wsexpose(SMServiceGroup, six.text_type) def get_one(self, uuid): sm_servicegroup = sm_api.sm_servicegroup_show(uuid) if sm_servicegroup is None: diff --git a/sysinv/sysinv/sysinv/sysinv/api/controllers/v1/servicenode.py b/sysinv/sysinv/sysinv/sysinv/api/controllers/v1/servicenode.py index b5647509e2..3752657b68 100644 --- a/sysinv/sysinv/sysinv/sysinv/api/controllers/v1/servicenode.py +++ b/sysinv/sysinv/sysinv/sysinv/api/controllers/v1/servicenode.py @@ -5,6 +5,7 @@ # from pecan import rest +import six import wsme from wsme import types as wtypes import wsmeext.pecan as wsme_pecan @@ -51,7 +52,7 @@ class SMServiceNodeCollection(base.APIBase): class SMServiceNodeController(rest.RestController): - @wsme_pecan.wsexpose(SMServiceNode, unicode) + @wsme_pecan.wsexpose(SMServiceNode, six.text_type) def get_one(self, uuid): sm_servicenode = sm_api.servicenode_show(uuid) if sm_servicenode is None: diff --git a/sysinv/sysinv/sysinv/sysinv/api/controllers/v1/system.py b/sysinv/sysinv/sysinv/sysinv/api/controllers/v1/system.py index fc65b5a7cb..b2447d0f2d 100644 --- a/sysinv/sysinv/sysinv/sysinv/api/controllers/v1/system.py +++ b/sysinv/sysinv/sysinv/sysinv/api/controllers/v1/system.py @@ -358,7 +358,7 @@ class SystemController(rest.RestController): raise exception.OperationNotPermitted @cutils.synchronized(LOCK_NAME) - @wsme_pecan.wsexpose(System, types.uuid, body=[unicode]) + @wsme_pecan.wsexpose(System, types.uuid, body=[six.text_type]) def patch(self, isystem_uuid, patch): """Update an existing isystem. diff --git a/sysinv/sysinv/sysinv/sysinv/api/controllers/v1/upgrade.py b/sysinv/sysinv/sysinv/sysinv/api/controllers/v1/upgrade.py index 248546508c..834b51982c 100755 --- a/sysinv/sysinv/sysinv/sysinv/api/controllers/v1/upgrade.py +++ b/sysinv/sysinv/sysinv/sysinv/api/controllers/v1/upgrade.py @@ -10,6 +10,7 @@ import pecan from pecan import rest, expose import os +import six import socket import wsme from wsme import types as wtypes @@ -185,7 +186,7 @@ class UpgradeController(rest.RestController): return Upgrade.convert_with_links(rpc_upgrade) @cutils.synchronized(LOCK_NAME) - @wsme_pecan.wsexpose(Upgrade, body=unicode) + @wsme_pecan.wsexpose(Upgrade, body=six.text_type) def post(self, body): """Create a new Software Upgrade instance and start upgrade.""" @@ -420,7 +421,7 @@ class UpgradeController(rest.RestController): return Upgrade.convert_with_links(upgrade) - @wsme_pecan.wsexpose(wtypes.text, unicode) + @wsme_pecan.wsexpose(wtypes.text, six.text_type) def in_upgrade(self, uuid): # uuid is added here for potential future use try: diff --git a/sysinv/sysinv/sysinv/sysinv/common/exception.py b/sysinv/sysinv/sysinv/sysinv/common/exception.py index d472f5d8a0..0862179a4d 100644 --- a/sysinv/sysinv/sysinv/sysinv/common/exception.py +++ b/sysinv/sysinv/sysinv/sysinv/common/exception.py @@ -26,6 +26,7 @@ SHOULD include dedicated exception logging. """ import functools +import six from oslo_config import cfg @@ -158,7 +159,7 @@ class SysinvException(Exception): if self.__class__.__name__.endswith('_Remote'): return self.args[0] else: - return unicode(self) + return six.text_type(self) class NotAuthorized(SysinvException): diff --git a/sysinv/sysinv/sysinv/sysinv/common/utils.py b/sysinv/sysinv/sysinv/sysinv/common/utils.py index 8e0b0831c8..a4e71bbe6c 100644 --- a/sysinv/sysinv/sysinv/sysinv/common/utils.py +++ b/sysinv/sysinv/sysinv/sysinv/common/utils.py @@ -536,7 +536,7 @@ def convert_to_list_dict(lst, label): def sanitize_hostname(hostname): """Return a hostname which conforms to RFC-952 and RFC-1123 specs.""" - if isinstance(hostname, unicode): + if isinstance(hostname, six.string_types): hostname = hostname.encode('latin-1', 'ignore') hostname = re.sub('[ _]', '-', hostname)