From 5c5d758681e60151487c38bf34b4aa6a0b68ea8a Mon Sep 17 00:00:00 2001 From: wangliuan Date: Tue, 22 Mar 2016 11:56:13 +0800 Subject: [PATCH] Implement Neutron service client Change-Id: Ie33bf59b5d71d0123bf161ad61bbfc9f569d459d --- requirements.txt | 1 + smaug/context.py | 2 +- smaug/services/protection/clients/neutron.py | 50 +++++++++++++++++++ .../tests/unit/clients/test_neutron_client.py | 48 ++++++++++++++++++ 4 files changed, 100 insertions(+), 1 deletion(-) create mode 100644 smaug/services/protection/clients/neutron.py create mode 100644 smaug/tests/unit/clients/test_neutron_client.py diff --git a/requirements.txt b/requirements.txt index c8f7118e..64a25d85 100644 --- a/requirements.txt +++ b/requirements.txt @@ -26,6 +26,7 @@ python-cinderclient>=1.3.1 # Apache-2.0 requests>=2.8.1 Routes!=2.0,!=2.1,>=1.12.3;python_version=='2.7' Routes!=2.0,>=1.12.3;python_version!='2.7' +python-neutronclient>=2.6.0,!=4.1.0 # Apache-2.0 six>=1.9.0 SQLAlchemy<1.1.0,>=0.9.9 sqlalchemy-migrate>=0.9.6 diff --git a/smaug/context.py b/smaug/context.py index e089e8f6..0b89485b 100644 --- a/smaug/context.py +++ b/smaug/context.py @@ -79,7 +79,7 @@ class RequestContext(context.RequestContext): self.service_catalog = [s for s in service_catalog if s.get('type') in ('identity', 'compute', 'object-store', - 'image', 'volume', 'volumev2')] + 'image', 'volume', 'volumev2', 'network')] else: # if list is empty or none self.service_catalog = [] diff --git a/smaug/services/protection/clients/neutron.py b/smaug/services/protection/clients/neutron.py new file mode 100644 index 00000000..67354669 --- /dev/null +++ b/smaug/services/protection/clients/neutron.py @@ -0,0 +1,50 @@ +# 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. + +from neutronclient.v2_0 import client as neutron_client +from oslo_config import cfg +from oslo_log import log as logging +from smaug.i18n import _LE, _LI +from smaug.services.protection import utils + +LOG = logging.getLogger(__name__) + +SERVICE = 'neutron' +neutron_client_opts = [ + cfg.StrOpt(SERVICE + '_endpoint', + help='URL of the neutron endpoint.'), + cfg.StrOpt(SERVICE + '_catalog_info', + default='network:neutron:publicURL', + help='Info to match when looking for neutron in the service ' + 'catalog. Format is: separated values of the form: ' + ':: - ' + 'Only used if neutron_endpoint is unset'), +] + +cfg.CONF.register_opts(neutron_client_opts, group=SERVICE + '_client') + + +def create(context): + try: + url = utils.get_url(SERVICE, context) + except Exception: + LOG.error(_LE("Get neutron service endpoint url failed")) + raise + + LOG.info(_LI("Creating neutron client with url %s."), url) + + args = { + 'endpoint_url': url, + 'token': context.auth_token, + } + + return neutron_client.Client(**args) diff --git a/smaug/tests/unit/clients/test_neutron_client.py b/smaug/tests/unit/clients/test_neutron_client.py new file mode 100644 index 00000000..4d468e80 --- /dev/null +++ b/smaug/tests/unit/clients/test_neutron_client.py @@ -0,0 +1,48 @@ + +# 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. + +from oslo_config import cfg + +from smaug.context import RequestContext +from smaug.services.protection.clients import neutron +from smaug.tests import base + + +class NeutronClientTest(base.TestCase): + def setUp(self): + super(NeutronClientTest, self).setUp() + service_catalog = [ + { + 'endpoints': [ + {'publicURL': 'http://127.0.0.1:9696', } + ], + 'type': 'network', + 'name': 'neutron', + }, + ] + + self._context = RequestContext(user_id='admin', + project_id='abcd', + auth_token='efgh', + service_catalog=service_catalog) + + def test_create_client_by_endpoint(self): + cfg.CONF.set_default('neutron_endpoint', + 'http://127.0.0.1:9696', + 'neutron_client') + nc = neutron.create(self._context) + self.assertEqual('http://127.0.0.1:9696', nc.httpclient.endpoint_url) + + def test_create_client_by_catalog(self): + nc = neutron.create(self._context) + self.assertEqual('http://127.0.0.1:9696', nc.httpclient.endpoint_url)