add neutron client support

For good user experience, we should validate if Baymodel's network
is existed in Bay creation. Currently in Magnum there is no Neutron
client support. Neutron client is added for network related
resources validation.

Change-Id: I5668e3b67b8addbff14c7f859a5124c099f35a88
Patially-Implements: blueprint api-parameter-validation
This commit is contained in:
houming-wang 2015-11-26 11:34:04 -05:00
parent 2ad6faec88
commit 27e52ec206
3 changed files with 94 additions and 0 deletions

View File

@ -15,6 +15,7 @@
from barbicanclient import client as barbicanclient
from glanceclient.v2 import client as glanceclient
from heatclient.v1 import client as heatclient
from neutronclient.v2_0 import client as neutronclient
from novaclient.v2 import client as novaclient
from oslo_config import cfg
from oslo_log import log as logging
@ -88,11 +89,22 @@ nova_client_opts = [
'Type of endpoint in Identity service catalog to use '
'for communication with the OpenStack service.'))]
neutron_client_opts = [
cfg.StrOpt('region_name',
help=_('Region in Identity service catalog to use for '
'communication with the OpenStack service.')),
cfg.StrOpt('endpoint_type',
default='publicURL',
help=_(
'Type of endpoint in Identity service catalog to use '
'for communication with the OpenStack service.'))]
cfg.CONF.register_opts(magnum_client_opts, group='magnum_client')
cfg.CONF.register_opts(heat_client_opts, group='heat_client')
cfg.CONF.register_opts(glance_client_opts, group='glance_client')
cfg.CONF.register_opts(barbican_client_opts, group='barbican_client')
cfg.CONF.register_opts(nova_client_opts, group='nova_client')
cfg.CONF.register_opts(neutron_client_opts, group='neutron_client')
class OpenStackClients(object):
@ -105,6 +117,7 @@ class OpenStackClients(object):
self._glance = None
self._barbican = None
self._nova = None
self._neutron = None
def url_for(self, **kwargs):
return self.keystone().client.service_catalog.url_for(**kwargs)
@ -209,3 +222,22 @@ class OpenStackClients(object):
self._nova = novaclient.Client(auth_token=self.auth_token)
self._nova.client.management_url = endpoint
return self._nova
@exception.wrap_keystone_exception
def neutron(self):
if self._neutron:
return self._neutron
endpoint_type = self._get_client_option('neutron', 'endpoint_type')
region_name = self._get_client_option('neutron', 'region_name')
endpoint = self.url_for(service_type='network',
endpoint_type=endpoint_type,
region_name=region_name)
args = {
'auth_url': self.auth_url,
'token': self.auth_token,
'endpoint_url': endpoint,
'endpoint_type': endpoint_type,
}
self._neutron = neutronclient.Client(**args)
return self._neutron

View File

@ -14,6 +14,7 @@ from barbicanclient import client as barbicanclient
from glanceclient.v2 import client as glanceclient
from heatclient.v1 import client as heatclient
import mock
from neutronclient.v2_0 import client as neutronclient
from novaclient.v2 import client as novaclient
from oslo_config import cfg
@ -278,3 +279,63 @@ class ClientsTest(base.BaseTestCase):
nova = obj.nova()
nova_cached = obj.nova()
self.assertEqual(nova, nova_cached)
@mock.patch.object(neutronclient, 'Client')
@mock.patch.object(clients.OpenStackClients, 'url_for')
@mock.patch.object(clients.OpenStackClients, 'auth_url')
def _test_clients_neutron(self, expected_region_name, mock_auth, mock_url,
mock_call):
fake_endpoint_type = 'fake_endpoint_type'
cfg.CONF.set_override('endpoint_type', fake_endpoint_type,
group='neutron_client')
mock_auth.__get__ = mock.Mock(return_value="keystone_url")
con = mock.MagicMock()
con.auth_token = "3bcc3d3a03f44e3d8377f9247b0ad155"
con.auth_url = "keystone_url"
mock_url.return_value = "url_from_keystone"
obj = clients.OpenStackClients(con)
obj._neutron = None
obj.neutron()
mock_call.assert_called_once_with(
endpoint_url='url_from_keystone',
endpoint_type=fake_endpoint_type,
auth_url='keystone_url',
token='3bcc3d3a03f44e3d8377f9247b0ad155')
mock_url.assert_called_once_with(service_type='network',
endpoint_type=fake_endpoint_type,
region_name=expected_region_name)
def test_clients_neutron(self):
self._test_clients_neutron(None)
def test_clients_neutron_region(self):
cfg.CONF.set_override('region_name', 'myregion',
group='neutron_client')
self._test_clients_neutron('myregion')
def test_clients_neutron_noauth(self):
con = mock.MagicMock()
con.auth_token = None
con.auth_token_info = None
auth_url = mock.PropertyMock(name="auth_url",
return_value="keystone_url")
type(con).auth_url = auth_url
con.get_url_for = mock.Mock(name="get_url_for")
con.get_url_for.return_value = "url_from_keystone"
obj = clients.OpenStackClients(con)
obj._neutron = None
self.assertRaises(exception.AuthorizationFailure, obj.neutron)
@mock.patch.object(clients.OpenStackClients, 'url_for')
@mock.patch.object(clients.OpenStackClients, 'auth_url')
def test_clients_neutron_cached(self, mock_auth, mock_url):
mock_auth.__get__ = mock.Mock(return_value="keystone_url")
con = mock.MagicMock()
con.auth_token = "3bcc3d3a03f44e3d8377f9247b0ad155"
con.auth_url = "keystone_url"
mock_url.return_value = "url_from_keystone"
obj = clients.OpenStackClients(con)
obj._neutron = None
neutron = obj.neutron()
neutron_cached = obj.neutron()
self.assertEqual(neutron, neutron_cached)

View File

@ -41,6 +41,7 @@ pecan>=1.0.0
python-barbicanclient>=3.3.0
python-glanceclient>=0.18.0
python-heatclient>=0.6.0
python-neutronclient>=2.6.0
python-novaclient!=2.33.0,>=2.29.0
python-keystoneclient!=1.8.0,>=1.6.0
requests!=2.8.0,>=2.5.2