some flake8 cleanups, more to do
This commit is contained in:
parent
3f39ea0a33
commit
76d161fdf9
@ -174,7 +174,8 @@ class RedfishConnection(object):
|
|||||||
request_headers['X-Auth-Token'] = self.auth_token
|
request_headers['X-Auth-Token'] = self.auth_token
|
||||||
# else use user_name/password and Basic Auth
|
# else use user_name/password and Basic Auth
|
||||||
elif self.user_name is not None and self.password is not None:
|
elif self.user_name is not None and self.password is not None:
|
||||||
request_headers['Authorization'] = "BASIC " + base64.b64encode(self.user_name + ":" + self.password)
|
request_headers['Authorization'] = ("BASIC " + base64.b64encode(
|
||||||
|
self.user_name + ":" + self.password))
|
||||||
# TODO: add support for other types of auth
|
# TODO: add support for other types of auth
|
||||||
|
|
||||||
# TODO: think about redirects....
|
# TODO: think about redirects....
|
||||||
@ -182,34 +183,38 @@ class RedfishConnection(object):
|
|||||||
while redir_count:
|
while redir_count:
|
||||||
conn = None
|
conn = None
|
||||||
if url.scheme == 'https':
|
if url.scheme == 'https':
|
||||||
# New in Python 2.7.9, SSL enforcement is defaulted on, but can be opted-out of.
|
# New in Python 2.7.9, SSL enforcement is defaulted on.
|
||||||
# The below case is the Opt-Out condition and should be used with GREAT caution.
|
# It can be opted-out of, which might be useful for debugging
|
||||||
# But could be useful for debugging some things, so we're leaving it in.
|
# some things. The below case is the Opt-Out condition and
|
||||||
if( sys.version_info.major == 2 and
|
# should be used with GREAT caution.
|
||||||
sys.version_info.minor == 7 and
|
if (sys.version_info.major == 2
|
||||||
sys.version_info.micro >= 9 and
|
and sys.version_info.minor == 7
|
||||||
self.enforce_SSL == False):
|
and sys.version_info.micro >= 9
|
||||||
cont=ssl.SSLContext(ssl.PROTOCOL_TLSv1)
|
and self.enforce_SSL == False):
|
||||||
|
cont = ssl.SSLContext(ssl.PROTOCOL_TLSv1)
|
||||||
cont.verify_mode = ssl.CERT_NONE
|
cont.verify_mode = ssl.CERT_NONE
|
||||||
conn = httplib.HTTPSConnection(host=url.netloc, strict=True, context=cont)
|
conn = httplib.HTTPSConnection(
|
||||||
|
host=url.netloc, strict=True, context=cont)
|
||||||
else:
|
else:
|
||||||
conn = httplib.HTTPSConnection(host=url.netloc, strict=True)
|
conn = httplib.HTTPSConnection(host=url.netloc,
|
||||||
|
strict=True)
|
||||||
elif url.scheme == 'http':
|
elif url.scheme == 'http':
|
||||||
conn = httplib.HTTPConnection(host=url.netloc, strict=True)
|
conn = httplib.HTTPConnection(host=url.netloc, strict=True)
|
||||||
else:
|
else:
|
||||||
raise exception.RedfishException(message='Unknown connection schema')
|
raise exception.RedfishException(
|
||||||
|
message='Unknown connection schema')
|
||||||
|
|
||||||
# NOTE: Do not assume every HTTP operation will return a JSON body.
|
# NOTE: Do not assume every HTTP operation will return a JSON body.
|
||||||
# For example, ExtendedError structures are only required for HTTP 400
|
# For example, ExtendedError structures are only required for
|
||||||
# errors and are optional elsewhere as they are mostly redundant for many
|
# HTTP 400 errors and are optional elsewhere as they are mostly
|
||||||
# of the other HTTP status code. In particular, 200 OK responses
|
# redundant for many of the other HTTP status code. In particular,
|
||||||
# should not have to return any body.
|
# 200 OK responses should not have to return any body.
|
||||||
|
conn.request(operation, url.path, headers=request_headers,
|
||||||
conn.request(operation, url.path, headers=request_headers, body=json.dumps(request_body))
|
body=json.dumps(request_body))
|
||||||
resp = conn.getresponse()
|
resp = conn.getresponse()
|
||||||
body = resp.read()
|
body = resp.read()
|
||||||
# NOTE: this makes sure the headers names are all lower cases because
|
# NOTE: this makes sure the headers names are all lower case
|
||||||
# HTTP says they are case insensitive
|
# because HTTP says they are case insensitive
|
||||||
headers = dict((x.lower(), y) for x, y in resp.getheaders())
|
headers = dict((x.lower(), y) for x, y in resp.getheaders())
|
||||||
|
|
||||||
# Follow HTTP redirect
|
# Follow HTTP redirect
|
||||||
@ -225,14 +230,16 @@ class RedfishConnection(object):
|
|||||||
response = json.loads(body.decode('utf-8'))
|
response = json.loads(body.decode('utf-8'))
|
||||||
LOG.debug("Loaded json: %s" % response)
|
LOG.debug("Loaded json: %s" % response)
|
||||||
except ValueError: # if it doesn't decode as json
|
except ValueError: # if it doesn't decode as json
|
||||||
# NOTE: resources may return gzipped content
|
# NOTE: resources may return gzipped content, so try to decode
|
||||||
# try to decode as gzip (we should check the headers for Content-Encoding=gzip)
|
# as gzip (we should check the headers for Content-Encoding=gzip)
|
||||||
try:
|
try:
|
||||||
gzipper = gzip.GzipFile(fileobj=StringIO.StringIO(body))
|
gzipper = gzip.GzipFile(fileobj=StringIO.StringIO(body))
|
||||||
uncompressed_string = gzipper.read().decode('UTF-8')
|
uncompressed_string = gzipper.read().decode('UTF-8')
|
||||||
response = json.loads(uncompressed_string)
|
response = json.loads(uncompressed_string)
|
||||||
except:
|
except:
|
||||||
raise exception.RedfishException(message='Failed to parse response as a JSON document, received "%s".' % body)
|
raise exception.RedfishException(message=
|
||||||
|
'Failed to parse response as a JSON document, '
|
||||||
|
'received "%s".' % body)
|
||||||
|
|
||||||
return resp.status, headers, response
|
return resp.status, headers, response
|
||||||
|
|
||||||
@ -242,8 +249,9 @@ class RedfishConnection(object):
|
|||||||
:param: suburi
|
:param: suburi
|
||||||
:param: request_headers
|
:param: request_headers
|
||||||
"""
|
"""
|
||||||
if not isinstance(request_headers, dict): request_headers = dict()
|
if not isinstance(request_headers, dict):
|
||||||
# NOTE: be prepared for various HTTP responses including 500, 404, etc.
|
request_headers = dict()
|
||||||
|
# NOTE: be prepared for various HTTP responses including 500, 404, etc
|
||||||
return self._op('GET', suburi, request_headers, None)
|
return self._op('GET', suburi, request_headers, None)
|
||||||
|
|
||||||
def rest_patch(self, suburi, request_headers, request_body):
|
def rest_patch(self, suburi, request_headers, request_body):
|
||||||
@ -256,10 +264,11 @@ class RedfishConnection(object):
|
|||||||
redfish does not follow IETF JSONPATCH standard
|
redfish does not follow IETF JSONPATCH standard
|
||||||
https://tools.ietf.org/html/rfc6902
|
https://tools.ietf.org/html/rfc6902
|
||||||
"""
|
"""
|
||||||
if not isinstance(request_headers, dict): request_headers = dict()
|
if not isinstance(request_headers, dict):
|
||||||
|
request_headers = dict()
|
||||||
request_headers['Content-Type'] = 'application/json'
|
request_headers['Content-Type'] = 'application/json'
|
||||||
|
# NOTE: be prepared for various HTTP responses including 500, 404, 202
|
||||||
return self._op('PATCH', suburi, request_headers, request_body)
|
return self._op('PATCH', suburi, request_headers, request_body)
|
||||||
# NOTE: be prepared for various HTTP responses including 500, 404, 202 etc.
|
|
||||||
|
|
||||||
def rest_put(self, suburi, request_headers, request_body):
|
def rest_put(self, suburi, request_headers, request_body):
|
||||||
"""REST PUT
|
"""REST PUT
|
||||||
@ -268,10 +277,11 @@ class RedfishConnection(object):
|
|||||||
:param: request_headers
|
:param: request_headers
|
||||||
:param: request_body
|
:param: request_body
|
||||||
"""
|
"""
|
||||||
if not isinstance(request_headers, dict): request_headers = dict()
|
if not isinstance(request_headers, dict):
|
||||||
|
request_headers = dict()
|
||||||
request_headers['Content-Type'] = 'application/json'
|
request_headers['Content-Type'] = 'application/json'
|
||||||
|
# NOTE: be prepared for various HTTP responses including 500, 404, 202
|
||||||
return self._op('PUT', suburi, request_headers, request_body)
|
return self._op('PUT', suburi, request_headers, request_body)
|
||||||
# NOTE: be prepared for various HTTP responses including 500, 404, 202 etc.
|
|
||||||
|
|
||||||
def rest_post(self, suburi, request_headers, request_body):
|
def rest_post(self, suburi, request_headers, request_body):
|
||||||
"""REST POST
|
"""REST POST
|
||||||
@ -280,11 +290,14 @@ class RedfishConnection(object):
|
|||||||
:param: request_headers
|
:param: request_headers
|
||||||
:param: request_body
|
:param: request_body
|
||||||
"""
|
"""
|
||||||
if not isinstance(request_headers, dict): request_headers = dict()
|
if not isinstance(request_headers, dict):
|
||||||
|
request_headers = dict()
|
||||||
request_headers['Content-Type'] = 'application/json'
|
request_headers['Content-Type'] = 'application/json'
|
||||||
|
# NOTE: don't assume any newly created resource is included in the
|
||||||
|
# # response. Only the Location header matters.
|
||||||
|
# the response body may be the new resource, it may be an
|
||||||
|
# ExtendedError, or it may be empty.
|
||||||
return self._op('POST', suburi, request_headers, request_body)
|
return self._op('POST', suburi, request_headers, request_body)
|
||||||
# NOTE: don't assume any newly created resource is included in the response. Only the Location header matters.
|
|
||||||
# the response body may be the new resource, it may be an ExtendedError, or it may be empty.
|
|
||||||
|
|
||||||
def rest_delete(self, suburi, request_headers):
|
def rest_delete(self, suburi, request_headers):
|
||||||
"""REST DELETE
|
"""REST DELETE
|
||||||
@ -292,10 +305,11 @@ class RedfishConnection(object):
|
|||||||
:param: suburi
|
:param: suburi
|
||||||
:param: request_headers
|
:param: request_headers
|
||||||
"""
|
"""
|
||||||
if not isinstance(request_headers, dict): request_headers = dict()
|
if not isinstance(request_headers, dict):
|
||||||
return self._op('DELETE', suburi, request_headers, None)
|
request_headers = dict()
|
||||||
# NOTE: be prepared for various HTTP responses including 500, 404, etc.
|
# NOTE: be prepared for various HTTP responses including 500, 404
|
||||||
# NOTE: response may be an ExtendedError or may be empty
|
# NOTE: response may be an ExtendedError or may be empty
|
||||||
|
return self._op('DELETE', suburi, request_headers, None)
|
||||||
|
|
||||||
# this is a generator that returns collection members
|
# this is a generator that returns collection members
|
||||||
def collection(self, collection_uri, request_headers):
|
def collection(self, collection_uri, request_headers):
|
||||||
@ -331,7 +345,6 @@ class RedfishConnection(object):
|
|||||||
# contains both.
|
# contains both.
|
||||||
|
|
||||||
if 'Items' in thecollection:
|
if 'Items' in thecollection:
|
||||||
|
|
||||||
# iterate items
|
# iterate items
|
||||||
for item in thecollection['Items']:
|
for item in thecollection['Items']:
|
||||||
# if the item has a self uri pointer, supply that for convenience
|
# if the item has a self uri pointer, supply that for convenience
|
||||||
@ -344,11 +357,11 @@ class RedfishConnection(object):
|
|||||||
|
|
||||||
# else walk the member links
|
# else walk the member links
|
||||||
elif 'links' in thecollection and 'Member' in thecollection['links']:
|
elif 'links' in thecollection and 'Member' in thecollection['links']:
|
||||||
|
|
||||||
# iterate members
|
# iterate members
|
||||||
for memberuri in thecollection['links']['Member']:
|
for memberuri in thecollection['links']['Member']:
|
||||||
# for each member return the resource indicated by the member link
|
# for each member return the resource indicated by the member link
|
||||||
status, headers, member = rest_get(host, memberuri['href'], request_headers, user_name, password)
|
status, headers, member = rest_get(
|
||||||
|
host, memberuri['href'], request_headers, user_name, password)
|
||||||
|
|
||||||
# Read up on Python generator functions to understand what this does.
|
# Read up on Python generator functions to understand what this does.
|
||||||
yield status, headers, member, memberuri['href']
|
yield status, headers, member, memberuri['href']
|
||||||
@ -369,6 +382,7 @@ def get_type(obj):
|
|||||||
typesplit = typever.split('.')
|
typesplit = typever.split('.')
|
||||||
return typesplit[0] + '.' + typesplit[1]
|
return typesplit[0] + '.' + typesplit[1]
|
||||||
|
|
||||||
|
|
||||||
# checks HTTP response headers for specified operation (e.g. 'GET' or 'PATCH')
|
# checks HTTP response headers for specified operation (e.g. 'GET' or 'PATCH')
|
||||||
def operation_allowed(headers_dict, operation):
|
def operation_allowed(headers_dict, operation):
|
||||||
if 'allow' in headers_dict:
|
if 'allow' in headers_dict:
|
||||||
|
Loading…
Reference in New Issue
Block a user