expand security groups in get_hostvars_from_server

Add a call to `list_server_security_groups` to
`meta.get_hostvars_from_server`.  This replaces the minimal list of
security group names with detailed information that includes security
group ids.

Change-Id: I5a01b37efd5520205c6ce94f0fdc4c09f75053c2
This commit is contained in:
Lars Kellogg-Stedman 2015-10-23 22:09:02 -04:00
parent 7dff4f07e8
commit 631a8e53e9
2 changed files with 31 additions and 1 deletions

View File

@ -264,6 +264,12 @@ def expand_server_vars(cloud, server):
return server_vars
def expand_server_security_groups(cloud, server):
groups = cloud.list_server_security_groups(server)
server['security_groups'] = groups
def get_hostvars_from_server(cloud, server, mounts=None):
"""Expand additional server information useful for ansible inventory."""
server_vars = expand_server_vars(cloud, server)
@ -274,6 +280,8 @@ def get_hostvars_from_server(cloud, server, mounts=None):
server_vars['flavor']['name'] = flavor_name
server_vars['flavor'].pop('links', None)
expand_server_security_groups(cloud, server)
# OpenStack can return image as a string when you've booted from volume
if str(server['image']) == server['image']:
image_id = server['image']

View File

@ -60,6 +60,9 @@ class FakeCloud(object):
def get_external_networks(self):
return []
def list_server_security_groups(self, server):
return []
class FakeServer(object):
id = 'test-id-0'
@ -149,13 +152,15 @@ class TestMeta(testtools.TestCase):
filters={'router:external': False}
)
@mock.patch.object(shade.OpenStackCloud, 'list_server_security_groups')
@mock.patch.object(shade.OpenStackCloud, 'get_image_name')
@mock.patch.object(shade.OpenStackCloud, 'get_flavor_name')
@mock.patch.object(shade.OpenStackCloud, 'has_service')
@mock.patch.object(shade.OpenStackCloud, 'search_networks')
def test_get_server_private_ip_devstack(
self, mock_search_networks, mock_has_service,
mock_get_flavor_name, mock_get_image_name):
mock_get_flavor_name, mock_get_image_name,
mock_list_server_security_groups):
mock_get_image_name.return_value = 'cirros-0.3.4-x86_64-uec'
mock_get_flavor_name.return_value = 'm1.tiny'
mock_has_service.return_value = True
@ -328,6 +333,23 @@ class TestMeta(testtools.TestCase):
self.assertEqual(new_list[0]['value'], 0)
self.assertEqual(new_list[1]['value'], 1)
@mock.patch.object(FakeCloud, 'list_server_security_groups')
def test_get_security_groups(self,
mock_list_server_security_groups):
'''This test verifies that calling get_hostvars_froms_server
ultimately calls list_server_security_groups, and that the return
value from list_server_security_groups ends up in
server['security_groups'].'''
mock_list_server_security_groups.return_value = [
{'name': 'testgroup', 'id': '1'}]
server = meta.obj_to_dict(FakeServer())
hostvars = meta.get_hostvars_from_server(FakeCloud(), server)
mock_list_server_security_groups.assert_called_once_with(server)
self.assertEqual('testgroup',
hostvars['security_groups'][0]['name'])
@mock.patch.object(shade.meta, 'get_server_external_ipv6')
@mock.patch.object(shade.meta, 'get_server_external_ipv4')
def test_basic_hostvars(