commit 38827df96967677edf136620ca499e1645920bc6 Author: Harald Jensås Date: Wed Oct 14 13:12:49 2020 +0200 Automatically set tag's based on role names To keep backward compatibility automatically set tags for role based on the role name. Log's a deprecation warning when automatically adding a tag to give user's heads up to add the tags to their role file. Change-Id: I6f44856a5149a623d963f3f26e189bcbb418703a diff --git a/tripleo_common/tests/utils/test_template.py b/tripleo_common/tests/utils/test_template.py index 92bd2c9..8e0a35f 100644 --- a/tripleo_common/tests/utils/test_template.py +++ b/tripleo_common/tests/utils/test_template.py @@ -526,6 +526,75 @@ class ProcessTemplatesTest(base.TestCase): resource_registry, 'overcloud') mock_put.assert_not_called() + @mock.patch.object(template_utils, 'LOG', autospec=True) + def test__set_tags_based_on_role_name(self, mock_log): + role_data = [ + {'name': 'CephStorageFoo'}, + {'name': 'CephStorageBar', 'tags': ['ceph', 'storage']}, + {'name': 'ObjectStorageFoo'}, + {'name': 'ObjectStorageBar', 'tags': ['storage']}, + {'name': 'BlockStorageFoo'}, + {'name': 'BlockStorageFoo', 'tags': ['storage']}, + {'name': 'ComputeOvsDpdkFoo'}, + {'name': 'ComputeOvsDpdkBar', 'tags': ['ovsdpdk']}, + ] + template_utils._set_tags_based_on_role_name(role_data) + expected = [ + {'name': 'CephStorageFoo', 'tags': ['ceph', 'storage']}, + {'name': 'CephStorageBar', 'tags': ['ceph', 'storage']}, + {'name': 'ObjectStorageFoo', 'tags': ['storage']}, + {'name': 'ObjectStorageBar', 'tags': ['storage']}, + {'name': 'BlockStorageFoo', 'tags': ['storage']}, + {'name': 'BlockStorageFoo', 'tags': ['storage']}, + {'name': 'ComputeOvsDpdkFoo', 'tags': ['compute', 'ovsdpdk']}, + {'name': 'ComputeOvsDpdkBar', 'tags': ['ovsdpdk', 'compute']}, + ] + self.assertEqual(expected, role_data) + mock_log.assert_has_calls([ + mock.call.warning( + "DEPRECATED: Role 'CephStorageFoo' without the 'ceph' tag " + "detected, the tag was added automatically. Please add the " + "'ceph' tag in roles data. The function to automatically " + "add tags based on role name will be removed in the next " + "release."), + mock.call.warning( + "DEPRECATED: Role 'CephStorageFoo' without the 'storage' " + "tag detected, the tag was added automatically. Please add " + "the 'storage' tag in roles data. The function to " + "automatically add tags based on role name will be removed in " + "the next release."), + mock.call.warning( + "DEPRECATED: Role 'ObjectStorageFoo' without the 'storage' " + "tag detected, the tag was added automatically. Please add " + "the 'storage' tag in roles data. The function to " + "automatically add tags based on role name will be " + "removed in the next release."), + mock.call.warning( + "DEPRECATED: Role 'BlockStorageFoo' without the 'storage' tag " + "detected, the tag was added automatically. Please add " + "the 'storage' tag in roles data. The function to " + "automatically add tags based on role name will be removed " + "in the next release."), + mock.call.warning( + "DEPRECATED: Role 'ComputeOvsDpdkFoo' without the 'compute' " + "tag detected, the tag was added automatically. Please add " + "the 'compute' tag in roles data. The function to " + "automatically add tags based on role name will be removed in " + "the next release."), + mock.call.warning( + "DEPRECATED: Role 'ComputeOvsDpdkFoo' without the 'ovsdpdk' " + "tag detected, the tag was added automatically. Please add " + "the 'ovsdpdk' tag in roles data. The function to " + "automatically add tags based on role name will be removed in " + "the next release."), + mock.call.warning( + "DEPRECATED: Role 'ComputeOvsDpdkBar' without the 'compute' " + "tag detected, the tag was added automatically. Please add " + "the 'compute' tag in roles data. The function to " + "automatically add tags based on role name will be removed in " + "the next release."), + ]) + class UploadTemplatesTest(base.TestCase): diff --git a/tripleo_common/utils/template.py b/tripleo_common/utils/template.py index e1e74f3..1d8f8d7 100644 --- a/tripleo_common/utils/template.py +++ b/tripleo_common/utils/template.py @@ -136,6 +136,47 @@ def heat_resource_exists(heat, stack, nested_stack_name, resource_name): return True +def _set_tags_based_on_role_name(role_data): + for role in role_data: + role['tags'] = role.get('tags', []) + role_name = role.get('name', str()) + + if ((role_name.startswith('Compute') or role_name.startswith('HciCeph') + or role_name.startswith('DistributedCompute')) + and 'compute' not in role['tags']): + role['tags'].append('compute') + LOG.warning("DEPRECATED: Role '%s' without the 'compute' tag " + "detected, the tag was added automatically. Please " + "add the 'compute' tag in roles data. The function to " + "automatically add tags based on role name will be " + "removed in the next release." % role_name) + if role_name.startswith('Ceph') and 'ceph' not in role['tags']: + role['tags'].append('ceph') + LOG.warning("DEPRECATED: Role '%s' without the 'ceph' tag " + "detected, the tag was added automatically. Please " + "add the 'ceph' tag in roles data. The function to " + "automatically add tags based on role name will be " + "removed in the next release." % role_name) + if (role_name.startswith('ComputeOvsDpdk') + and 'ovsdpdk' not in role['tags']): + role['tags'].append('ovsdpdk') + LOG.warning("DEPRECATED: Role '%s' without the 'ovsdpdk' tag " + "detected, the tag was added automatically. Please " + "add the 'ovsdpdk' tag in roles data. The function to " + "automatically add tags based on role name will be " + "removed in the next release." % role_name) + if ((role_name.startswith('ObjectStorage') + or role_name.startswith('BlockStorage') + or role_name.startswith('Ceph')) + and 'storage' not in role['tags']): + role['tags'].append('storage') + LOG.warning("DEPRECATED: Role '%s' without the 'storage' tag " + "detected, the tag was added automatically. Please " + "add the 'storage' tag in roles data. The function to " + "automatically add tags based on role name will be " + "removed in the next release." % role_name) + + def process_custom_roles(swift, heat, container=constants.DEFAULT_CONTAINER_NAME): try: @@ -172,6 +213,10 @@ def process_custom_roles(swift, heat, LOG.error(error_msg) raise RuntimeError(error_msg) + # TODO(hjensas): In next release remove the function to automatically add + # tags based on role name. + _set_tags_based_on_role_name(role_data) + role_names = [r.get('name') for r in role_data] r_map = {} for r in role_data: