FIX: module rename issue for Python 2/3 compatible code

import six.moves to fix rename of urllib, urllib and httplib

Story: 2003433
Task: 28481

Change-Id: Ib37e69f09e77a6ed5167d54bb1602448023d622c
Signed-off-by: Sun Austin <austin.sun@intel.com>
This commit is contained in:
Sun Austin 2018-12-13 08:56:07 +08:00
parent c521b8c28c
commit 4941e8559f
6 changed files with 48 additions and 38 deletions

View File

@ -4,14 +4,17 @@ Copyright (c) 2015-2017 Wind River Systems, Inc.
SPDX-License-Identifier: Apache-2.0
"""
import httplib
import json
import urllib2
from controllerconfig.common.exceptions import KeystoneFail
from controllerconfig.common import dcmanager
from controllerconfig.common import keystone
from controllerconfig.common import log
from six.moves import http_client as httplib
from six.moves.urllib import request as urlrequest
from six.moves.urllib.error import HTTPError
from six.moves.urllib.error import URLError
LOG = log.get_logger(__name__)
@ -22,7 +25,7 @@ def rest_api_request(token, method, api_cmd, api_cmd_headers=None,
Make a rest-api request
"""
try:
request_info = urllib2.Request(api_cmd)
request_info = urlrequest.Request(api_cmd)
request_info.get_method = lambda: method
request_info.add_header("X-Auth-Token", token.get_id())
request_info.add_header("Accept", "application/json")
@ -35,7 +38,7 @@ def rest_api_request(token, method, api_cmd, api_cmd_headers=None,
request_info.add_header("Content-type", "application/json")
request_info.add_data(api_cmd_payload)
request = urllib2.urlopen(request_info)
request = urlrequest.urlopen(request_info)
response = request.read()
if response == "":
@ -46,7 +49,7 @@ def rest_api_request(token, method, api_cmd, api_cmd_headers=None,
return response
except urllib2.HTTPError as e:
except HTTPError as e:
if httplib.UNAUTHORIZED == e.code:
token.set_expired()
LOG.exception(e)
@ -54,7 +57,7 @@ def rest_api_request(token, method, api_cmd, api_cmd_headers=None,
"REST API HTTP Error for url: %s. Error: %s" %
(api_cmd, e))
except (urllib2.URLError, httplib.BadStatusLine) as e:
except (URLError, httplib.BadStatusLine) as e:
LOG.exception(e)
raise KeystoneFail(
"REST API URL Error for url: %s. Error: %s" %
@ -68,7 +71,7 @@ def get_token(auth_url, auth_project, auth_user, auth_password,
"""
try:
url = auth_url + "/auth/tokens"
request_info = urllib2.Request(url)
request_info = urlrequest.Request(url)
request_info.add_header("Content-Type", "application/json")
request_info.add_header("Accept", "application/json")
@ -94,7 +97,7 @@ def get_token(auth_url, auth_project, auth_user, auth_password,
request_info.add_data(payload)
request = urllib2.urlopen(request_info)
request = urlrequest.urlopen(request_info)
# Identity API v3 returns token id in X-Subject-Token
# response header.
token_id = request.info().getheader('X-Subject-Token')
@ -103,11 +106,11 @@ def get_token(auth_url, auth_project, auth_user, auth_password,
return keystone.Token(response, token_id)
except urllib2.HTTPError as e:
except HTTPError as e:
LOG.error("%s, %s" % (e.code, e.read()))
return None
except (urllib2.URLError, httplib.BadStatusLine) as e:
except (URLError, httplib.BadStatusLine) as e:
LOG.error(e)
return None

View File

@ -10,11 +10,15 @@ System Inventory Interactions
import json
import openstack
import urllib2
from six.moves.urllib import request as urlrequest
from six.moves.urllib.error import URLError
from six.moves.urllib.error import HTTPError
from controllerconfig.common import log
from controllerconfig.common.exceptions import KeystoneFail
LOG = log.get_logger(__name__)
API_VERSION = 1
@ -153,11 +157,11 @@ class Host(object):
region_name)
url += "/ihosts/" + self.name
request_info = urllib2.Request(url)
request_info = urlrequest.Request(url)
request_info.add_header("X-Auth-Token", admin_token.get_id())
request_info.add_header("Accept", "application/json")
request = urllib2.urlopen(request_info)
request = urlrequest.urlopen(request_info)
response = json.loads(request.read())
request.close()
return response
@ -166,13 +170,13 @@ class Host(object):
LOG.error("Keystone authentication failed:{} ".format(e))
return None
except urllib2.HTTPError as e:
except HTTPError as e:
LOG.error("%s, %s" % (e.code, e.read()))
if e.code == 401:
admin_token.set_expired()
return None
except urllib2.URLError as e:
except URLError as e:
LOG.error(e)
return None
@ -182,14 +186,14 @@ class Host(object):
region_name)
url += "/ihosts/" + self.name
request_info = urllib2.Request(url)
request_info = urlrequest.Request(url)
request_info.get_method = lambda: 'PATCH'
request_info.add_header("X-Auth-Token", admin_token.get_id())
request_info.add_header("Content-type", "application/json")
request_info.add_header("Accept", "application/json")
request_info.add_data(action)
request = urllib2.urlopen(request_info)
request = urlrequest.urlopen(request_info)
request.close()
return True
@ -197,13 +201,13 @@ class Host(object):
LOG.error("Keystone authentication failed:{} ".format(e))
return False
except urllib2.HTTPError as e:
except HTTPError as e:
LOG.error("%s, %s" % (e.code, e.read()))
if e.code == 401:
admin_token.set_expired()
return False
except urllib2.URLError as e:
except URLError as e:
LOG.error(e)
return False
@ -314,11 +318,11 @@ def get_hosts(admin_token, region_name, personality=None,
region_name)
url += "/ihosts/"
request_info = urllib2.Request(url)
request_info = urlrequest.Request(url)
request_info.add_header("X-Auth-Token", admin_token.get_id())
request_info.add_header("Accept", "application/json")
request = urllib2.urlopen(request_info)
request = urlrequest.urlopen(request_info)
response = json.loads(request.read())
request.close()
@ -348,13 +352,13 @@ def get_hosts(admin_token, region_name, personality=None,
LOG.error("Keystone authentication failed:{} ".format(e))
return []
except urllib2.HTTPError as e:
except HTTPError as e:
LOG.error("%s, %s" % (e.code, e.read()))
if e.code == 401:
admin_token.set_expired()
return []
except urllib2.URLError as e:
except URLError as e:
LOG.error(e)
return []

View File

@ -5,8 +5,11 @@
#
import json
import signal
import urllib2
from six.moves.urllib.request import urlopen
from six.moves.urllib.request import Request
from six.moves.urllib.error import HTTPError
from six.moves.urllib.error import URLError
from sysinv.common import configp
from sysinv.common import exception as si_exception
from sysinv.openstack.common.keystone_objects import Token
@ -26,7 +29,7 @@ def _get_token(auth_url, auth_project, username, password, user_domain,
"""
try:
url = auth_url + "/v3/auth/tokens"
request_info = urllib2.Request(url)
request_info = Request(url)
request_info.add_header("Content-type", "application/json")
request_info.add_header("Accept", "application/json")
payload = json.dumps(
@ -51,7 +54,7 @@ def _get_token(auth_url, auth_project, username, password, user_domain,
request_info.add_data(payload)
request = urllib2.urlopen(request_info)
request = urlopen(request_info)
# Identity API v3 returns token id in X-Subject-Token
# response header.
token_id = request.info().getheader('X-Subject-Token')
@ -60,11 +63,11 @@ def _get_token(auth_url, auth_project, username, password, user_domain,
# save the region name for service url lookup
return Token(response, token_id, region_name)
except urllib2.HTTPError as e:
except HTTPError as e:
LOG.error("%s, %s" % (e.code, e.read()))
return None
except urllib2.URLError as e:
except URLError as e:
LOG.error(e)
return None
@ -112,7 +115,7 @@ def rest_api_request(token, method, api_cmd, api_cmd_headers=None,
response = None
try:
request_info = urllib2.Request(api_cmd)
request_info = Request(api_cmd)
request_info.get_method = lambda: method
if token:
request_info.add_header("X-Auth-Token", token.get_id())
@ -125,7 +128,7 @@ def rest_api_request(token, method, api_cmd, api_cmd_headers=None,
if api_cmd_payload is not None:
request_info.add_data(api_cmd_payload)
request = urllib2.urlopen(request_info, timeout=timeout)
request = urlopen(request_info, timeout=timeout)
response = request.read()
if response == "":
@ -136,7 +139,7 @@ def rest_api_request(token, method, api_cmd, api_cmd_headers=None,
LOG.info("Response=%s" % response)
except urllib2.HTTPError as e:
except HTTPError as e:
if 401 == e.code:
if token:
token.set_expired()
@ -149,7 +152,7 @@ def rest_api_request(token, method, api_cmd, api_cmd_headers=None,
LOG.info("HTTPError response=%s" % (response))
raise OpenStackRestAPIException(e.message, e.code, "%s" % e)
except urllib2.URLError as e:
except URLError as e:
LOG.warn("URLError Error e=%s" % (e))
raise OpenStackException(e.message, "%s" % e)

View File

@ -12,13 +12,13 @@
""" System Inventory Kubernetes Utilities and helper functions."""
from __future__ import absolute_import
import httplib
import json
from kubernetes import config
from kubernetes import client
from kubernetes.client import Configuration
from kubernetes.client.rest import ApiException
from six.moves import http_client as httplib
from sysinv.common import exception
from sysinv.openstack.common import log as logging

View File

@ -33,7 +33,6 @@ import errno
import filecmp
import glob
import hashlib
import httplib
import os
import re
import shutil
@ -60,6 +59,7 @@ from netaddr import IPNetwork
from oslo_config import cfg
from platform_util.license import license
from sqlalchemy.orm import exc
from six.moves import http_client as httplib
from sysinv.agent import rpcapi as agent_rpcapi
from sysinv.api.controllers.v1 import address_pool
from sysinv.api.controllers.v1 import cpu_utils

View File

@ -58,11 +58,11 @@ as it allows particular rules to be explicitly disabled.
import abc
import re
import urllib
import six
import urllib2
from six.moves.urllib.parse import urlencode
from six.moves.urllib.request import urlopen
from sysinv.openstack.common.gettextutils import _
from sysinv.openstack.common import jsonutils
from sysinv.openstack.common import log as logging
@ -754,8 +754,8 @@ class HttpCheck(Check):
url = ('http:' + self.match) % target
data = {'target': jsonutils.dumps(target),
'credentials': jsonutils.dumps(creds)}
post_data = urllib.urlencode(data)
f = urllib2.urlopen(url, post_data)
post_data = urlencode(data)
f = urlopen(url, post_data)
return f.read() == "True"