From 3b54eea32968e10fd7fbfd5891d6a0fe780793d6 Mon Sep 17 00:00:00 2001 From: Florian Fuchs Date: Wed, 16 Jan 2019 16:52:22 +0000 Subject: [PATCH] Fix introspection data lookup Accessing ironic-introspection data from swift doesn't work with utils.get_swift_client. This fix uses an auth session object and swiftclient's Connection class instead. Change-Id: Ie8c6cccb8fd9debf288cc449647dff5225f09e3e --- requirements.txt | 1 + .../tests/library/test_switch_vlans.py | 76 ++++++++++++------- validations/library/switch_vlans.py | 8 +- .../lookup_plugins/introspection_data.py | 29 ++++--- 4 files changed, 72 insertions(+), 42 deletions(-) diff --git a/requirements.txt b/requirements.txt index c36aff235..b5474eff0 100644 --- a/requirements.txt +++ b/requirements.txt @@ -9,6 +9,7 @@ python-novaclient>=9.1.0 # Apache-2.0 python-heatclient>=1.10.0 # Apache-2.0 python-glanceclient>=2.9.1 # Apache-2.0 python-ironicclient>=2.3.0 # Apache-2.0 +python-ironic-inspector-client>=3.1.1 # Apache-2.0 os-net-config>=7.1.0 # Apache-2.0 six>=1.10.0 # MIT tripleo-common>=7.1.0 # Apache-2.0 diff --git a/tripleo_validations/tests/library/test_switch_vlans.py b/tripleo_validations/tests/library/test_switch_vlans.py index eca320010..a83b3b8a8 100644 --- a/tripleo_validations/tests/library/test_switch_vlans.py +++ b/tripleo_validations/tests/library/test_switch_vlans.py @@ -24,28 +24,47 @@ class TestSwitchVlans(base.TestCase): super(TestSwitchVlans, self).__init__(display) self.introspect_data = { - u'inspector_data-8c3faec8-bc05-401c-8956-99c40cdea97d': - '{"all_interfaces":' - '{"em1": {"mac": "00:11:22:33:44:55",' - '"lldp_processed": { "switch_port_id": "555",' - '"switch_port_vlans":' - '[{"id": 101, "name": "vlan101"},' - '{"id": 104, "name": "vlan104"},' - '{"id": 203, "name": "vlan203"}]}},' - '"em2": {"mac": "00:11:22:33:44:66",' - '"lldp_processed": { "switch_port_id": "557",' - '"switch_port_vlans":' - '[{"id": 101, "name": "vlan101"},' - '{"id": 105, "name": "vlan105"},' - '{"id": 204, "name": "vlan204"}]}}}}', - u'inspector_data-c0d2568e-1825-4d34-96ec-f08bbf0ba7ae': - '{"all_interfaces":' - '{"em1":{"mac": "00:66:77:88:99:aa",' - '"lldp_processed": { "switch_port_id": "559",' - '"switch_port_vlans":' - '[{"id": 101, "name": "vlan101"},' - '{"id": 201, "name": "vlan201"},' - '{"id": 222, "name": "vlan222"}]}}}}' + "inspector_data-8c3faec8-bc05-401c-8956-99c40cdea97d": { + "all_interfaces": { + "em1": { + "mac": "00:11:22:33:44:55", + "lldp_processed": { + "switch_port_id": "555", + "switch_port_vlans": [ + {"id": 101, "name": "vlan101"}, + {"id": 104, "name": "vlan104"}, + {"id": 203, "name": "vlan203"} + ] + } + }, + "em2": { + "mac": "00:11:22:33:44:66", + "lldp_processed": { + "switch_port_id": "557", + "switch_port_vlans": [ + {"id": 101, "name": "vlan101"}, + {"id": 105, "name": "vlan105"}, + {"id": 204, "name": "vlan204"} + ] + } + } + } + }, + "inspector_data-c0d2568e-1825-4d34-96ec-f08bbf0ba7ae": { + "all_interfaces": { + "em1": { + "mac": "00:66:77:88:99:aa", + "lldp_processed": { + "switch_port_id": "559", + "switch_port_vlans": [ + {"id": 101, "name": "vlan101"}, + {"id": 201, "name": "vlan201"}, + {"id": 222, "name": "vlan222"} + ] + } + } + } + } } def test_valid_vlan_first_node(self): @@ -73,10 +92,15 @@ class TestSwitchVlans(base.TestCase): self.assertEqual(msg, []) def test_no_lldp_data(self): - local_data = {u'inspector_data-8c3faec8-bc05-401c-8956-99c40cdea97d': - '{"all_interfaces":' - '{"em1": {"mac": "00:11:22:33:44:55"}}}' - } + local_data = { + "inspector_data-8c3faec8-bc05-401c-8956-99c40cdea97d": { + "all_interfaces": { + "em1": { + "mac": "00:11:22:33:44:55" + } + } + } + } msg, result = validation.vlan_exists_on_switch( 104, local_data) diff --git a/validations/library/switch_vlans.py b/validations/library/switch_vlans.py index 6ff3a1f57..4f74ff712 100644 --- a/validations/library/switch_vlans.py +++ b/validations/library/switch_vlans.py @@ -171,12 +171,8 @@ def vlan_exists_on_switch(vlan_id, introspection_data): result: boolean indicating if VLAN was found """ - for node, content in introspection_data.items(): + for node, data in introspection_data.items(): node_valid_lldp = False - try: - data = yaml.safe_load(content) - except Exception as e: - return ["Can't open introspection data : {}" .format(e)], False all_interfaces = data.get('all_interfaces', []) @@ -211,7 +207,7 @@ def main(): netenv_path = module.params.get('path') template_files = {name: content[1] for (name, content) in module.params.get('template_files')} - introspection_data = {name: content[1] for (name, content) in + introspection_data = {name: content for (name, content) in module.params.get('introspection_data')} warnings, errors = validate_switch_vlans(netenv_path, template_files, diff --git a/validations/lookup_plugins/introspection_data.py b/validations/lookup_plugins/introspection_data.py index 0cd0a8cfa..d16d72c22 100644 --- a/validations/lookup_plugins/introspection_data.py +++ b/validations/lookup_plugins/introspection_data.py @@ -16,8 +16,11 @@ # under the License. from ansible.plugins.lookup import LookupBase +from ironic_inspector_client import ClientError +from ironic_inspector_client import ClientV1 +from ironicclient import client -from tripleo_validations import utils +from tripleo_validations.utils import get_auth_session class LookupModule(LookupBase): @@ -29,15 +32,21 @@ class LookupModule(LookupBase): :returns a list of tuples, one for each node. """ + + session = get_auth_session({ + 'auth_url': kwargs.get('auth_url'), + 'password': kwargs.get('password'), + 'username': 'ironic', + 'project_name': 'service', + }) + ironic = client.get_client(1, session=session) + ironic_inspector = ClientV1(session=session) + ret = [] - - swift = utils.get_swift_client(variables) - container = swift.get_container("ironic-inspector") - - for item in container[1]: - if item['name'].startswith('inspector_data') and \ - not item['name'].endswith("UNPROCESSED"): - obj = swift.get_object("ironic-inspector", item['name']) - ret.append((item['name'], obj)) + for node in ironic.node.list(): + try: + ret.append((node.name, ironic_inspector.get_data(node.uuid))) + except ClientError: + pass return ret