Newton sync
Change-Id: Ia80646ae9cec7c4009614d98e20f8657fca4f28c
This commit is contained in:
parent
e28dfa475f
commit
3899fd7523
@ -1,3 +0,0 @@
|
||||
from neutronclient.neutron import v2_0 as neutronV2_0
|
||||
|
||||
_get_resource_plural = neutronV2_0._get_resource_plural
|
@ -17,13 +17,13 @@ import logging
|
||||
from neutronclient.common import utils as n_utils
|
||||
from neutronclient.i18n import _
|
||||
from neutronclient.neutron import v2_0 as neutronV20
|
||||
from neutronclient.v2_0 import client as neutron_client
|
||||
from oslo_serialization import jsonutils
|
||||
|
||||
from gbpclient.common import utils
|
||||
|
||||
|
||||
neutronV20.UUID_PATTERN = neutronV20.UUID_PATTERN + (
|
||||
'|auto' + neutronV20.HEX_ELEM + '{32}')
|
||||
neutron_client.UUID_PATTERN = neutron_client.UUID_PATTERN + (
|
||||
'|auto' + neutron_client.HEX_ELEM + '{32}')
|
||||
|
||||
|
||||
def _format_fixed_ips(pt):
|
||||
|
@ -16,7 +16,6 @@ from neutronclient.common import exceptions
|
||||
from neutronclient.tests.unit import test_cli20 as neutron_test_cli20
|
||||
import requests
|
||||
|
||||
from gbpclient.gbp import v2_0 as gbpV2_0
|
||||
from gbpclient import gbpshell
|
||||
from gbpclient.v2_0 import client as gbpclient
|
||||
|
||||
@ -90,8 +89,7 @@ class CLITestV20Base(neutron_test_cli20.CLITestV20Base):
|
||||
self.client.format = self.format
|
||||
resstr = self.client.serialize(ress)
|
||||
# url method body
|
||||
resource_plural = gbpV2_0._get_resource_plural(cmd_resource,
|
||||
self.client)
|
||||
resource_plural = self.client.get_resource_plural(cmd_resource)
|
||||
path = getattr(self.client, resource_plural + "_path")
|
||||
if parent_id:
|
||||
path = path % parent_id
|
||||
|
@ -12,16 +12,10 @@
|
||||
#
|
||||
|
||||
import logging
|
||||
import time
|
||||
|
||||
from gbpclient.gbp.v2_0 import purge as gbpclient_purge
|
||||
from neutronclient import client
|
||||
from neutronclient.common import exceptions
|
||||
from neutronclient.common import serializer
|
||||
from neutronclient.common import utils
|
||||
from neutronclient.i18n import _
|
||||
import requests
|
||||
import six.moves.urllib.parse as urlparse
|
||||
from neutronclient.v2_0 import client as clientV2_0
|
||||
|
||||
|
||||
_logger = logging.getLogger(__name__)
|
||||
@ -99,7 +93,7 @@ class APIParamsCall(object):
|
||||
return with_params
|
||||
|
||||
|
||||
class Client(object):
|
||||
class Client(clientV2_0.ClientBase):
|
||||
"""Client for the GBP API.
|
||||
|
||||
:param string username: Username for authentication. (optional)
|
||||
@ -729,152 +723,4 @@ class Client(object):
|
||||
|
||||
def __init__(self, **kwargs):
|
||||
"""Initialize a new client for the GBP v2.0 API."""
|
||||
super(Client, self).__init__()
|
||||
self.retries = kwargs.pop('retries', 0)
|
||||
self.raise_errors = kwargs.pop('raise_errors', True)
|
||||
self.httpclient = client.construct_http_client(**kwargs)
|
||||
self.version = '2.0'
|
||||
self.format = 'json'
|
||||
self.action_prefix = "/v%s" % (self.version)
|
||||
self.retry_interval = 1
|
||||
|
||||
def _handle_fault_response(self, status_code, response_body):
|
||||
# Create exception with HTTP status code and message
|
||||
_logger.debug("Error message: %s", response_body)
|
||||
# Add deserialized error message to exception arguments
|
||||
try:
|
||||
des_error_body = self.deserialize(response_body, status_code)
|
||||
except Exception:
|
||||
# If unable to deserialized body it is probably not a
|
||||
# Neutron error
|
||||
des_error_body = {'message': response_body}
|
||||
# Raise the appropriate exception
|
||||
exception_handler_v20(status_code, des_error_body)
|
||||
|
||||
def _check_uri_length(self, action):
|
||||
uri_len = len(self.httpclient.endpoint_url) + len(action)
|
||||
if uri_len > self.MAX_URI_LEN:
|
||||
raise exceptions.RequestURITooLong(
|
||||
excess=uri_len - self.MAX_URI_LEN)
|
||||
|
||||
def do_request(self, method, action, body=None, headers=None, params=None):
|
||||
# Add format and tenant_id
|
||||
action += ".%s" % self.format
|
||||
action = self.action_prefix + action
|
||||
if type(params) is dict and params:
|
||||
params = utils.safe_encode_dict(params)
|
||||
action += '?' + urlparse.urlencode(params, doseq=1)
|
||||
|
||||
if body:
|
||||
body = self.serialize(body)
|
||||
|
||||
resp, replybody = self.httpclient.do_request(action, method, body=body)
|
||||
|
||||
status_code = resp.status_code
|
||||
if status_code in (requests.codes.ok,
|
||||
requests.codes.created,
|
||||
requests.codes.accepted,
|
||||
requests.codes.no_content):
|
||||
return self.deserialize(replybody, status_code)
|
||||
else:
|
||||
if not replybody:
|
||||
replybody = resp.reason
|
||||
self._handle_fault_response(status_code, replybody)
|
||||
|
||||
def get_auth_info(self):
|
||||
return self.httpclient.get_auth_info()
|
||||
|
||||
def serialize(self, data):
|
||||
"""Serializes a dictionary into JSON.
|
||||
|
||||
A dictionary with a single key can be passed and it can contain any
|
||||
structure.
|
||||
"""
|
||||
if data is None:
|
||||
return None
|
||||
elif type(data) is dict:
|
||||
return serializer.Serializer().serialize(data)
|
||||
else:
|
||||
raise Exception(_("Unable to serialize object of type = '%s'") %
|
||||
type(data))
|
||||
|
||||
def deserialize(self, data, status_code):
|
||||
"""Deserializes a JSON string into a dictionary."""
|
||||
if status_code == 204:
|
||||
return data
|
||||
return serializer.Serializer().deserialize(
|
||||
data)['body']
|
||||
|
||||
def retry_request(self, method, action, body=None,
|
||||
headers=None, params=None):
|
||||
"""Call do_request with the default retry configuration.
|
||||
|
||||
Only idempotent requests should retry failed connection attempts.
|
||||
:raises: ConnectionFailed if the maximum # of retries is exceeded
|
||||
"""
|
||||
max_attempts = self.retries + 1
|
||||
for i in range(max_attempts):
|
||||
try:
|
||||
return self.do_request(method, action, body=body,
|
||||
headers=headers, params=params)
|
||||
except exceptions.ConnectionFailed:
|
||||
# Exception has already been logged by do_request()
|
||||
if i < self.retries:
|
||||
_logger.debug('Retrying connection to Neutron service')
|
||||
time.sleep(self.retry_interval)
|
||||
elif self.raise_errors:
|
||||
raise
|
||||
|
||||
if self.retries:
|
||||
msg = (_("Failed to connect to Neutron server after %d attempts")
|
||||
% max_attempts)
|
||||
else:
|
||||
msg = _("Failed to connect Neutron server")
|
||||
|
||||
raise exceptions.ConnectionFailed(reason=msg)
|
||||
|
||||
def delete(self, action, body=None, headers=None, params=None):
|
||||
return self.retry_request("DELETE", action, body=body,
|
||||
headers=headers, params=params)
|
||||
|
||||
def get(self, action, body=None, headers=None, params=None):
|
||||
return self.retry_request("GET", action, body=body,
|
||||
headers=headers, params=params)
|
||||
|
||||
def post(self, action, body=None, headers=None, params=None):
|
||||
# Do not retry POST requests to avoid the orphan objects problem.
|
||||
return self.do_request("POST", action, body=body,
|
||||
headers=headers, params=params)
|
||||
|
||||
def put(self, action, body=None, headers=None, params=None):
|
||||
return self.retry_request("PUT", action, body=body,
|
||||
headers=headers, params=params)
|
||||
|
||||
def list(self, collection, path, retrieve_all=True, **params):
|
||||
if retrieve_all:
|
||||
res = []
|
||||
for r in self._pagination(collection, path, **params):
|
||||
res.extend(r[collection])
|
||||
return {collection: res}
|
||||
else:
|
||||
return self._pagination(collection, path, **params)
|
||||
|
||||
def _pagination(self, collection, path, **params):
|
||||
if params.get('page_reverse', False):
|
||||
linkrel = 'previous'
|
||||
else:
|
||||
linkrel = 'next'
|
||||
next = True
|
||||
while next:
|
||||
res = self.get(path, params=params)
|
||||
yield res
|
||||
next = False
|
||||
try:
|
||||
for link in res['%s_links' % collection]:
|
||||
if link['rel'] == linkrel:
|
||||
query_str = urlparse.urlparse(link['href']).query
|
||||
params = urlparse.parse_qs(query_str)
|
||||
next = True
|
||||
break
|
||||
except KeyError:
|
||||
break
|
||||
super(Client, self).__init__(**kwargs)
|
||||
|
@ -3,7 +3,7 @@
|
||||
# process, which may cause wedges in the gate later.
|
||||
pbr>=1.6 # Apache-2.0
|
||||
python-heatclient>=0.8.0
|
||||
python-neutronclient<5.0.0,>=4.0.0,!=4.1.0
|
||||
python-neutronclient<6.0.0,>=5.1.0
|
||||
oslo.serialization>=1.10.0 # Apache-2.0
|
||||
oslo.utils>=3.5.0 # Apache-2.0
|
||||
# REVISIT: After mitaka release add dependency on released version of python-neutronclient
|
||||
|
Loading…
x
Reference in New Issue
Block a user