From dd3f3e30a902d869b5a85cc992f14afa7c6f212d Mon Sep 17 00:00:00 2001 From: Masahito Muroi Date: Fri, 21 Dec 2018 17:44:18 +0900 Subject: [PATCH] Add BlazarNeutronClient base class Partially Implements: blueprint floatingip-reservation Change-Id: Ie1e41123331bdbcc7c5a8f96ad6ed512d85d4963 --- blazar/tests/utils/openstack/test_neutron.py | 36 +++++++++++++ blazar/utils/openstack/neutron.py | 57 ++++++++++++++++++++ lower-constraints.txt | 1 + requirements.txt | 1 + 4 files changed, 95 insertions(+) create mode 100644 blazar/tests/utils/openstack/test_neutron.py create mode 100644 blazar/utils/openstack/neutron.py diff --git a/blazar/tests/utils/openstack/test_neutron.py b/blazar/tests/utils/openstack/test_neutron.py new file mode 100644 index 00000000..8d1c2514 --- /dev/null +++ b/blazar/tests/utils/openstack/test_neutron.py @@ -0,0 +1,36 @@ +# Copyright (c) 2019 NTT. +# +# 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 oslo_config import fixture + +from blazar import tests +from blazar.utils.openstack import neutron + +CONF = cfg.CONF + + +class TestBlazarNeutronClient(tests.TestCase): + def setUp(self): + super(TestBlazarNeutronClient, self).setUp() + self.cfg = self.useFixture(fixture.Config(CONF)) + + def test_client_from_kwargs(self): + kwargs = { + 'auth_url': 'http://foo:8080/identity/v3' + } + client = neutron.BlazarNeutronClient(**kwargs) + self.assertEqual("http://foo:8080/identity/v3", + client.neutron.httpclient.session.auth.auth_url) diff --git a/blazar/utils/openstack/neutron.py b/blazar/utils/openstack/neutron.py new file mode 100644 index 00000000..9728334e --- /dev/null +++ b/blazar/utils/openstack/neutron.py @@ -0,0 +1,57 @@ +# Copyright (c) 2019 NTT. +# +# 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 keystoneauth1.identity import v3 +from keystoneauth1 import session +from neutronclient.v2_0 import client as neutron_client + +from oslo_config import cfg +from oslo_log import log as logging + +CONF = cfg.CONF +LOG = logging.getLogger(__name__) + + +class BlazarNeutronClient(object): + """Client class for Neutron service.""" + + def __init__(self, **kwargs): + username = kwargs.pop('username', + CONF.os_admin_username) + password = kwargs.pop('password', + CONF.os_admin_password) + project_name = kwargs.pop('project_name', + CONF.os_admin_project_name) + user_domain_name = kwargs.pop('user_domain_name', + CONF.os_admin_user_domain_name) + project_domain_name = kwargs.pop('project_domain_name', + CONF.os_admin_project_domain_name) + auth_url = kwargs.pop('auth_url', None) + + if auth_url is None: + auth_url = "%s://%s:%s/%s/%s" % (CONF.os_auth_protocol, + CONF.os_auth_host, + CONF.os_auth_port, + CONF.os_auth_prefix, + CONF.os_auth_version) + + auth = v3.Password(auth_url=auth_url, + username=username, + password=password, + project_name=project_name, + user_domain_name=user_domain_name, + project_domain_name=project_domain_name) + sess = session.Session(auth=auth) + self.neutron = neutron_client.Client(session=sess) diff --git a/lower-constraints.txt b/lower-constraints.txt index ae2a5039..01d8f648 100644 --- a/lower-constraints.txt +++ b/lower-constraints.txt @@ -83,6 +83,7 @@ python-dateutil==2.7.0 python-editor==1.0.3 python-keystoneclient==3.8.0 python-mimeparse==1.6.0 +python-neutronclient==6.0.0 python-novaclient==9.1.0 python-subunit==1.2.0 pytz==2018.3 diff --git a/requirements.txt b/requirements.txt index 46c5261d..386459b9 100644 --- a/requirements.txt +++ b/requirements.txt @@ -23,6 +23,7 @@ oslo.serialization!=2.19.1,>=2.18.0 # Apache-2.0 oslo.service!=1.28.1,>=1.24.0 # Apache-2.0 oslo.upgradecheck>=0.1.0 # Apache-2.0 oslo.utils>=3.33.0 # Apache-2.0 +python-neutronclient>=6.0.0 # Apache-2.0 python-novaclient>=9.1.0 # Apache-2.0 netaddr>=0.7.18 # BSD python-keystoneclient>=3.8.0 # Apache-2.0