26241533dc
Implements HPEConnector class that overrides the '_op' instance method from sushy's Connector class to include retrying logic. The retrying logic tries for MAX_RETRY_ATTEMPTS attempts waiting for MAX_TIME_BEFORE_RETRY time after each retry. Also adds constructor to HPESushy class that bypasses the initialization of the Sushy class and initialize the ResourceBase class with customized HPE specific connector subtype. Change-Id: Ic11552ed12195206717e9fcfaffa4c6d20bc6543 Closes-Bug: #1710071
48 lines
1.8 KiB
Python
48 lines
1.8 KiB
Python
# Copyright 2017 Hewlett Packard Enterprise Development LP
|
|
#
|
|
# Licensed under the Apache License, Version 2.0 (the "License"); you may
|
|
# not use this file except in compliance with the License. You may obtain
|
|
# a copy of the License at
|
|
#
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
#
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
|
# License for the specific language governing permissions and limitations
|
|
# under the License.
|
|
|
|
__author__ = 'HPE'
|
|
|
|
import retrying
|
|
from sushy import connector
|
|
from sushy import exceptions
|
|
|
|
|
|
class HPEConnector(connector.Connector):
|
|
"""Class that extends base Sushy Connector class
|
|
|
|
This class extends the Sushy's Connector class to override certain methods
|
|
required to customize the functionality of the http operations.
|
|
"""
|
|
|
|
MAX_RETRY_ATTEMPTS = 3 # Maximum number of attempts to be retried
|
|
MAX_TIME_BEFORE_RETRY = 2 * 1000 # wait time in milliseconds before retry
|
|
|
|
@retrying.retry(
|
|
retry_on_exception=(
|
|
lambda e: isinstance(e, exceptions.ConnectionError)),
|
|
stop_max_attempt_number=MAX_RETRY_ATTEMPTS,
|
|
wait_fixed=MAX_TIME_BEFORE_RETRY)
|
|
def _op(self, method, path='', data=None, headers=None):
|
|
"""Overrides the base method to support retrying the operation.
|
|
|
|
:param method: The HTTP method to be used, e.g: GET, POST,
|
|
PUT, PATCH, etc...
|
|
:param path: The sub-URI path to the resource.
|
|
:param data: Optional JSON data.
|
|
:param headers: Optional dictionary of headers.
|
|
:returns: The response from the connector.Connector's _op method.
|
|
"""
|
|
return super(HPEConnector, self)._op(method, path, data, headers)
|