From 12aa03929a30dc9d10b12a64551623d9fd998a2f Mon Sep 17 00:00:00 2001 From: Ivan Halomi Date: Thu, 18 Jul 2024 11:26:14 +0200 Subject: [PATCH] Add action for getting container names list This patch adds a new action to kolla_container_facts module, which retrieves list of names of all containers. It is intended to be an alternative to using "docker ps -a" type of commands in the Ansible code. Change-Id: I9c04d6fe77f20e5aa832684bd65de95d161cd8ea Signed-off-by: Ivan Halomi --- ansible/library/kolla_container_facts.py | 14 +++++++++++++- tests/test_kolla_container_facts.py | 24 ++++++++++++++++++++++++ 2 files changed, 37 insertions(+), 1 deletion(-) diff --git a/ansible/library/kolla_container_facts.py b/ansible/library/kolla_container_facts.py index 18c194c9fd..360b34ee10 100644 --- a/ansible/library/kolla_container_facts.py +++ b/ansible/library/kolla_container_facts.py @@ -46,7 +46,7 @@ options: - The action to perform required: True type: str -author: Jeffrey Zhang, Michal Nasiadka +author: Jeffrey Zhang, Michal Nasiadka, Ivan Halomi ''' EXAMPLES = ''' @@ -89,6 +89,11 @@ EXAMPLES = ''' kolla_container_facts: container_engine: docker action: get_volumes + + - name: Get container names + kolla_container_facts: + container_engine: docker + action: get_containers_names ''' @@ -122,6 +127,12 @@ class ContainerFactsWorker(): envs[key] = value return envs + def get_containers_names(self): + """Handles when module is called with action get_containers_names""" + containers = self.client.containers.list() + names = [cont.name for cont in containers] + self.result['container_names'] = names + def get_containers(self): """Handle when module is called with action get_containers""" names = self.params.get('name') @@ -216,6 +227,7 @@ def main(): choices=['get_containers', 'get_containers_env', 'get_containers_state', + 'get_containers_names', 'get_volumes']), ) diff --git a/tests/test_kolla_container_facts.py b/tests/test_kolla_container_facts.py index f1fc59f3e1..0db0c2eb46 100644 --- a/tests/test_kolla_container_facts.py +++ b/tests/test_kolla_container_facts.py @@ -310,3 +310,27 @@ class TestContainerFacts(base.BaseTestCase): self.assertIn('volumes', self.dfw.result) self.assertEqual(len(self.dfw.result['volumes']), 0) self.assertNotIn('nonexistent_volume', self.dfw.result['volumes']) + + def test_get_containers_names(self): + """Test fetching container names when containers exist""" + self.dfw = get_DockerFactsWorker({'action': 'get_containers_names'}) + full_cont_list = get_containers(self.fake_data['containers']) + self.dfw.client.containers.list.return_value = full_cont_list + expected_names = ["my_container"] + + self.dfw.get_containers_names() + + self.assertFalse(self.dfw.result['changed']) + self.assertIn('container_names', self.dfw.result) + self.assertEqual(self.dfw.result['container_names'], expected_names) + + def test_get_containers_names_no_containers(self): + """Test fetching container names when no containers exist""" + self.dfw = get_DockerFactsWorker({'action': 'get_containers_names'}) + self.dfw.client.containers.list.return_value = [] + + self.dfw.get_containers_names() + + self.assertFalse(self.dfw.result['changed']) + self.assertIn('container_names', self.dfw.result) + self.assertEqual(self.dfw.result['container_names'], [])