From 8c225448aad945d8b17a85b971f7a6744ddf533c Mon Sep 17 00:00:00 2001 From: Jacob Huff Date: Fri, 14 Feb 2025 10:13:20 -0500 Subject: [PATCH] adds wilds to data get action --- CHANGES.md | 3 +++ actions/data_get_all.py | 25 ++++++++++++++++++++++++- pack.yaml | 2 +- tests/test_action_data_get_all.py | 22 ++++++++++++++++------ 4 files changed, 44 insertions(+), 8 deletions(-) diff --git a/CHANGES.md b/CHANGES.md index 782a1c2..919d8fa 100755 --- a/CHANGES.md +++ b/CHANGES.md @@ -1,3 +1,6 @@ +## 1.1.4 +- Adds wilds to data get action + ## 1.1.3 - Added actions for looking up attributes on datastores diff --git a/actions/data_get_all.py b/actions/data_get_all.py index 8e692d7..f178287 100644 --- a/actions/data_get_all.py +++ b/actions/data_get_all.py @@ -86,6 +86,9 @@ def update_objects(self, objects, object_type): 'VNET': self.update_network } + # Get correct update function + update_function = update_functions.get(object_type) + # Get the update function based on object type for i, obj in enumerate(objects): # Convert zone names to CSV format @@ -94,7 +97,6 @@ def update_objects(self, objects, object_type): if object_type not in update_functions.keys(): continue - update_function = update_functions.get(object_type) objects[i] = update_function(objects[i]) return objects @@ -165,6 +167,24 @@ def lowercase_keys(self, obj): else: return obj + def add_wilds(self, hosts): + result = [] + for host in hosts: + # continue to next host if no wilds are found + if 'vm' not in host['template']: + continue + + # Convert instances of single wilds to lists + wilds = host['template']['vm'] + if isinstance(wilds, dict): + wilds = [wilds] + + # Add all wilds to our list of vm objects + for wild in wilds: + result.append(wild['vm_name']) + + return result + def run(self, api_config, template_label_filters, open_nebula=None): self.session = self.xmlrpc_session_create(open_nebula) self.template_label_filters = template_label_filters @@ -174,6 +194,9 @@ def run(self, api_config, template_label_filters, open_nebula=None): objects = self.get_objects(config['endpoint'], config['options'], config['type']) all_objs[config['name']] = objects + # Add wilds from host data to separate object + all_objs['wilds'] = self.add_wilds(all_objs['hosts']) + # Add hostname to data conn = self._get_connection_info(open_nebula) all_objs['hostname'] = conn['host'] diff --git a/pack.yaml b/pack.yaml index c10a07e..4a27548 100755 --- a/pack.yaml +++ b/pack.yaml @@ -3,7 +3,7 @@ ref: open_nebula name: open_nebula description: Open Nebula stackstorm_version: ">=2.9.0" -version: 1.1.3 +version: 1.1.4 author: John Schoewe email: john.schoewe@encore.tech contributors: diff --git a/tests/test_action_data_get_all.py b/tests/test_action_data_get_all.py index 716f4b9..f46052c 100644 --- a/tests/test_action_data_get_all.py +++ b/tests/test_action_data_get_all.py @@ -195,33 +195,43 @@ def test_lowercase_keys(self): result = self.action.lowercase_keys(obj) assert result == [{'key': 'value', 'nested': {'nested_key': 'nested_value'}}] + @mock.patch("data_get_all.DataGetAll.add_wilds") @mock.patch("data_get_all.DataGetAll.get_objects") @mock.patch("lib.action_base.BaseAction.xmlrpc_session_create") - def test_run(self, mock_session, mock_get_objects): + def test_run(self, mock_session, mock_get_objects, mock_add_wilds): action = self.get_action_instance(self._config_good) api_config = [ { 'name': 'vms', - 'endpoint': 'one.vmpool.infoextended', + 'endpoint': 'one.test.endpoint', 'options': [-2, -1, -1, -1], - 'type': 'VM' + 'type': 'TEST' }, { 'name': 'networks', - 'endpoint': 'one.vnpool.info', + 'endpoint': 'one.test.endpoint', 'options': [-2, -1, -1], - 'type': 'VNET' + 'type': 'TEST' + }, + { + 'name': 'hosts', + 'endpoint': 'one.test.endpoint', + 'options': [-2, -1, -1], + 'type': 'TEST' } ] template_label_filters = ['filter', 'list'] open_nebula = 'default' test_session = 'session' mock_session.return_value = test_session - mock_get_objects.side_effect = ['obj1', 'obj2'] + mock_get_objects.side_effect = ['obj1', 'obj2', 'obj3'] + mock_add_wilds.return_value = ['wild1', 'wild2'] expected_result = { 'vms': 'obj1', 'networks': 'obj2', + 'hosts': 'obj3', + 'wilds': ['wild1', 'wild2'], 'hostname': 'test.com' }