Add support for all_tenants in OpenStackInventory

This would allow for the Ansible inventory plugin to list servers
from all tenants.

Change-Id: I77c4d6e83cd0761c91609e3ae01a0db08c493b3b
This commit is contained in:
Maxime Guyot 2019-04-30 21:47:15 -06:00
parent 90449d53a3
commit bc0cff52c0
2 changed files with 28 additions and 4 deletions

View File

@ -57,13 +57,15 @@ class OpenStackInventory(object):
for cloud in self.clouds:
cloud._cache.invalidate()
def list_hosts(self, expand=True, fail_on_cloud_config=True):
def list_hosts(self, expand=True, fail_on_cloud_config=True,
all_projects=False):
hostvars = []
for cloud in self.clouds:
try:
# Cycle on servers
for server in cloud.list_servers(detailed=expand):
for server in cloud.list_servers(detailed=expand,
all_projects=all_projects):
hostvars.append(server)
except exceptions.OpenStackCloudException:
# Don't fail on one particular cloud as others may work

View File

@ -68,7 +68,8 @@ class TestInventory(base.TestCase):
ret = inv.list_hosts()
inv.clouds[0].list_servers.assert_called_once_with(detailed=True)
inv.clouds[0].list_servers.assert_called_once_with(detailed=True,
all_projects=False)
self.assertFalse(inv.clouds[0].get_openstack_vars.called)
self.assertEqual([server], ret)
@ -88,9 +89,30 @@ class TestInventory(base.TestCase):
inv.list_hosts(expand=False)
inv.clouds[0].list_servers.assert_called_once_with(detailed=False)
inv.clouds[0].list_servers.assert_called_once_with(detailed=False,
all_projects=False)
self.assertFalse(inv.clouds[0].get_openstack_vars.called)
@mock.patch("openstack.config.loader.OpenStackConfig")
@mock.patch("openstack.connection.Connection")
def test_list_hosts_all_projects(self, mock_cloud, mock_config):
mock_config.return_value.get_all.return_value = [{}]
inv = inventory.OpenStackInventory()
server = dict(id='server_id', name='server_name')
self.assertIsInstance(inv.clouds, list)
self.assertEqual(1, len(inv.clouds))
inv.clouds[0].list_servers.return_value = [server]
inv.clouds[0].get_openstack_vars.return_value = server
ret = inv.list_hosts(all_projects=True)
inv.clouds[0].list_servers.assert_called_once_with(detailed=True,
all_projects=True)
self.assertFalse(inv.clouds[0].get_openstack_vars.called)
self.assertEqual([server], ret)
@mock.patch("openstack.config.loader.OpenStackConfig")
@mock.patch("openstack.connection.Connection")
def test_search_hosts(self, mock_cloud, mock_config):