From f637a368d78e1b2f5f199e59bceb38f77339fa7c Mon Sep 17 00:00:00 2001 From: licanwei Date: Sat, 15 Apr 2017 17:03:18 +0800 Subject: [PATCH] Add ironicclient This patch set adds ironicclient. Change-Id: I122a26465d339ee6e36c0f234d3fd6c57cee2afa Partially-Implements: blueprint build-baremetal-data-model-in-watcher --- requirements.txt | 1 + watcher/common/clients.py | 14 ++++++++++ watcher/conf/__init__.py | 2 ++ watcher/conf/ironic_client.py | 41 ++++++++++++++++++++++++++++ watcher/tests/common/test_clients.py | 40 +++++++++++++++++++++++++++ watcher/tests/conf/test_list_opts.py | 2 +- 6 files changed, 99 insertions(+), 1 deletion(-) mode change 100644 => 100755 requirements.txt mode change 100644 => 100755 watcher/common/clients.py mode change 100644 => 100755 watcher/conf/__init__.py create mode 100755 watcher/conf/ironic_client.py mode change 100644 => 100755 watcher/tests/common/test_clients.py mode change 100644 => 100755 watcher/tests/conf/test_list_opts.py diff --git a/requirements.txt b/requirements.txt old mode 100644 new mode 100755 index c2221da70..a575ed797 --- a/requirements.txt +++ b/requirements.txt @@ -36,6 +36,7 @@ python-monascaclient>=1.1.0 # Apache-2.0 python-neutronclient>=5.1.0 # Apache-2.0 python-novaclient>=7.1.0 # Apache-2.0 python-openstackclient>=3.3.0 # Apache-2.0 +python-ironicclient>=1.11.0 # Apache-2.0 six>=1.9.0 # MIT SQLAlchemy!=1.1.5,!=1.1.6,!=1.1.7,!=1.1.8,>=1.0.10 # MIT stevedore>=1.20.0 # Apache-2.0 diff --git a/watcher/common/clients.py b/watcher/common/clients.py old mode 100644 new mode 100755 index 6050db4fc..a9f0bc75f --- a/watcher/common/clients.py +++ b/watcher/common/clients.py @@ -14,6 +14,7 @@ from ceilometerclient import client as ceclient from cinderclient import client as ciclient from glanceclient import client as glclient from gnocchiclient import client as gnclient +from ironicclient import client as irclient from keystoneauth1 import loading as ka_loading from keystoneclient import client as keyclient from monascaclient import client as monclient @@ -45,6 +46,7 @@ class OpenStackClients(object): self._ceilometer = None self._monasca = None self._neutron = None + self._ironic = None def _get_keystone_session(self): auth = ka_loading.load_auth_from_conf_options(CONF, @@ -188,3 +190,15 @@ class OpenStackClients(object): session=self.session) self._neutron.format = 'json' return self._neutron + + @exception.wrap_keystone_exception + def ironic(self): + if self._ironic: + return self._ironic + + ironicclient_version = self._get_client_option('ironic', 'api_version') + endpoint_type = self._get_client_option('ironic', 'endpoint_type') + self._ironic = irclient.get_client(ironicclient_version, + ironic_url=endpoint_type, + session=self.session) + return self._ironic diff --git a/watcher/conf/__init__.py b/watcher/conf/__init__.py old mode 100644 new mode 100755 index a561d7609..625401b54 --- a/watcher/conf/__init__.py +++ b/watcher/conf/__init__.py @@ -29,6 +29,7 @@ from watcher.conf import decision_engine from watcher.conf import exception from watcher.conf import glance_client from watcher.conf import gnocchi_client +from watcher.conf import ironic_client from watcher.conf import monasca_client from watcher.conf import neutron_client from watcher.conf import nova_client @@ -56,3 +57,4 @@ cinder_client.register_opts(CONF) ceilometer_client.register_opts(CONF) neutron_client.register_opts(CONF) clients_auth.register_opts(CONF) +ironic_client.register_opts(CONF) diff --git a/watcher/conf/ironic_client.py b/watcher/conf/ironic_client.py new file mode 100755 index 000000000..a241cf271 --- /dev/null +++ b/watcher/conf/ironic_client.py @@ -0,0 +1,41 @@ +# -*- encoding: utf-8 -*- +# Copyright (c) 2017 ZTE Corp +# +# Authors: Prudhvi Rao Shedimbi +# +# 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 + +ironic_client = cfg.OptGroup(name='ironic_client', + title='Configuration Options for Ironic') + +IRONIC_CLIENT_OPTS = [ + cfg.StrOpt('api_version', + default=1, + help='Version of Ironic API to use in ironicclient.'), + cfg.StrOpt('endpoint_type', + default='internalURL', + help='Type of endpoint to use in ironicclient.' + 'Supported values: internalURL, publicURL, adminURL' + 'The default is internalURL.')] + + +def register_opts(conf): + conf.register_group(ironic_client) + conf.register_opts(IRONIC_CLIENT_OPTS, group=ironic_client) + + +def list_opts(): + return [('ironic_client', IRONIC_CLIENT_OPTS)] diff --git a/watcher/tests/common/test_clients.py b/watcher/tests/common/test_clients.py old mode 100644 new mode 100755 index 56fd5f242..813eec712 --- a/watcher/tests/common/test_clients.py +++ b/watcher/tests/common/test_clients.py @@ -17,6 +17,8 @@ from cinderclient.v1 import client as ciclient_v1 from glanceclient import client as glclient from gnocchiclient import client as gnclient from gnocchiclient.v1 import client as gnclient_v1 +from ironicclient import client as irclient +from ironicclient.v1 import client as irclient_v1 from keystoneauth1 import loading as ka_loading import mock from monascaclient import client as monclient @@ -387,3 +389,41 @@ class TestClients(base.TestCase): monasca = osc.monasca() monasca_cached = osc.monasca() self.assertEqual(monasca, monasca_cached) + + @mock.patch.object(irclient, 'Client') + @mock.patch.object(clients.OpenStackClients, 'session') + def test_clients_ironic(self, mock_session, mock_call): + osc = clients.OpenStackClients() + osc._ironic = None + osc.ironic() + mock_call.assert_called_once_with( + CONF.ironic_client.api_version, + CONF.ironic_client.endpoint_type, + max_retries=None, + os_ironic_api_version=None, + retry_interval=None, + session=mock_session) + + @mock.patch.object(clients.OpenStackClients, 'session') + def test_clients_ironic_diff_vers(self, mock_session): + CONF.set_override('api_version', '1', group='ironic_client') + osc = clients.OpenStackClients() + osc._ironic = None + osc.ironic() + self.assertEqual(irclient_v1.Client, type(osc.ironic())) + + @mock.patch.object(clients.OpenStackClients, 'session') + def test_clients_ironic_diff_endpoint(self, mock_session): + CONF.set_override('endpoint_type', 'publicURL', group='ironic_client') + osc = clients.OpenStackClients() + osc._ironic = None + osc.ironic() + self.assertEqual('publicURL', osc.ironic().http_client.endpoint) + + @mock.patch.object(clients.OpenStackClients, 'session') + def test_clients_ironic_cached(self, mock_session): + osc = clients.OpenStackClients() + osc._ironic = None + ironic = osc.ironic() + ironic_cached = osc.ironic() + self.assertEqual(ironic, ironic_cached) diff --git a/watcher/tests/conf/test_list_opts.py b/watcher/tests/conf/test_list_opts.py old mode 100644 new mode 100755 index d724f03ab..ef7f4f1a6 --- a/watcher/tests/conf/test_list_opts.py +++ b/watcher/tests/conf/test_list_opts.py @@ -31,7 +31,7 @@ class TestListOpts(base.TestCase): 'DEFAULT', 'api', 'database', 'watcher_decision_engine', 'watcher_applier', 'watcher_planner', 'nova_client', 'glance_client', 'gnocchi_client', 'cinder_client', - 'ceilometer_client', 'monasca_client', + 'ceilometer_client', 'monasca_client', 'ironic_client', 'neutron_client', 'watcher_clients_auth'] self.opt_sections = list(dict(opts.list_opts()).keys())