Implement Neutron service client

Change-Id: Ie33bf59b5d71d0123bf161ad61bbfc9f569d459d
This commit is contained in:
wangliuan 2016-03-22 11:56:13 +08:00
parent 676b8205bf
commit 5c5d758681
4 changed files with 100 additions and 1 deletions

View File

@ -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

View File

@ -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 = []

View File

@ -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: '
'<service_type>:<service_name>:<endpoint_type> - '
'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)

View File

@ -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)