From 03e005993262357eb955601fb8acef38654eadf8 Mon Sep 17 00:00:00 2001 From: Pierre Riteau Date: Fri, 11 Aug 2017 09:20:04 +0100 Subject: [PATCH] Allow update host API to add new extra capabilities The update host API can only update existing extra capabilities. However, we can support adding new extra capabilities to hosts without API change. With this patch, Blazar can both create and update extra capabilities via the update host API. Partial-Bug: #1674524 Change-Id: Ic9bd743a1c31858db9390a3379dc19e1e54327a8 --- blazar/plugins/oshosts/host_plugin.py | 26 +++++++++++++------ .../plugins/test_physical_host_plugin.py | 12 +++++++++ ...ities-in-update-host-c74ce75c88e88c67.yaml | 7 +++++ 3 files changed, 37 insertions(+), 8 deletions(-) create mode 100644 releasenotes/notes/create-extra-capabilities-in-update-host-c74ce75c88e88c67.yaml diff --git a/blazar/plugins/oshosts/host_plugin.py b/blazar/plugins/oshosts/host_plugin.py index a001fea4..7fb516ce 100644 --- a/blazar/plugins/oshosts/host_plugin.py +++ b/blazar/plugins/oshosts/host_plugin.py @@ -296,8 +296,6 @@ class PhysicalHostPlugin(base.BasePlugin, nova.NovaClientWrapper): return None def update_computehost(self, host_id, values): - # NOTE (sbauza): Only update existing extra capabilites, don't create - # other ones if values: cant_update_extra_capability = [] for value in values: @@ -305,17 +303,29 @@ class PhysicalHostPlugin(base.BasePlugin, nova.NovaClientWrapper): host_id, value, ) - for raw_capability in capabilities: - capability = { + if capabilities: + for raw_capability in capabilities: + capability = { + 'capability_name': value, + 'capability_value': values[value], + } + try: + db_api.host_extra_capability_update( + raw_capability['id'], capability) + except (db_ex.BlazarDBException, RuntimeError): + cant_update_extra_capability.append( + raw_capability['capability_name']) + else: + new_capability = { + 'computehost_id': host_id, 'capability_name': value, 'capability_value': values[value], } try: - db_api.host_extra_capability_update( - raw_capability['id'], capability) - except RuntimeError: + db_api.host_extra_capability_create(new_capability) + except (db_ex.BlazarDBException, RuntimeError): cant_update_extra_capability.append( - raw_capability['capability_name']) + new_capability['capability_name']) if cant_update_extra_capability: raise manager_ex.CantAddExtraCapability( host=host_id, diff --git a/blazar/tests/plugins/test_physical_host_plugin.py b/blazar/tests/plugins/test_physical_host_plugin.py index e1fda608..de71dbf4 100644 --- a/blazar/tests/plugins/test_physical_host_plugin.py +++ b/blazar/tests/plugins/test_physical_host_plugin.py @@ -267,6 +267,18 @@ class PhysicalHostPluginTestCase(tests.TestCase): self.fake_phys_plugin.update_computehost, self.fake_host_id, host_values) + def test_update_host_with_new_extra_capability(self): + host_values = {'buzz': 'word'} + + self.db_host_extra_capability_get_all_per_name.return_value = [] + self.fake_phys_plugin.update_computehost(self.fake_host_id, + host_values) + self.db_host_extra_capability_create.assert_called_once_with({ + 'computehost_id': '1', + 'capability_name': 'buzz', + 'capability_value': 'word' + }) + def test_delete_host(self): self.fake_phys_plugin.delete_computehost(self.fake_host_id) diff --git a/releasenotes/notes/create-extra-capabilities-in-update-host-c74ce75c88e88c67.yaml b/releasenotes/notes/create-extra-capabilities-in-update-host-c74ce75c88e88c67.yaml new file mode 100644 index 00000000..af39548a --- /dev/null +++ b/releasenotes/notes/create-extra-capabilities-in-update-host-c74ce75c88e88c67.yaml @@ -0,0 +1,7 @@ +--- +features: + - | + The update host API now allows new extra capabilities to be created on + existing hosts, in addition to allowing values of existing keys to be + modified. However, extra capabilities cannot yet be removed due to lack of + API support. As a workaround, operators can delete hosts and recreate them.