From 0c926c1fef9ed8dd511a86f6a85e02378ff2f316 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 (cherry picked from commit 3b54eea32968e10fd7fbfd5891d6a0fe780793d6) --- requirements.txt | 1 + test-requirements.txt | 3 +- .../tests/library/test_switch_vlans.py | 76 ++++++++++++------- validations/library/switch_vlans.py | 8 +- .../lookup_plugins/introspection_data.py | 33 ++++---- 5 files changed, 73 insertions(+), 48 deletions(-) diff --git a/requirements.txt b/requirements.txt index 51340da6e..5c0c88794 100644 --- a/requirements.txt +++ b/requirements.txt @@ -8,6 +8,7 @@ keystoneauth1>=3.4.0 # Apache-2.0 python-novaclient>=9.1.0 # Apache-2.0 python-heatclient>=1.10.0 # 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/test-requirements.txt b/test-requirements.txt index a0ef4bdb1..fdb0845ae 100644 --- a/test-requirements.txt +++ b/test-requirements.txt @@ -7,7 +7,8 @@ hacking<0.12,>=0.11.0 # Apache-2.0 coverage!=4.4,>=4.0 # Apache-2.0 python-subunit>=1.0.0 # Apache-2.0/BSD -sphinx!=1.6.6,!=1.6.7,>=1.6.2 # BSD +sphinx!=1.6.6,!=1.6.7,>=1.6.2,<2.0.0;python_version=='2.7' # BSD +sphinx!=1.6.6,!=1.6.7,>=1.6.2;python_version>='3.4' # BSD oslotest>=3.2.0 # Apache-2.0 testrepository>=0.0.18 # Apache-2.0/BSD testscenarios>=0.4 # Apache-2.0/BSD 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 e0f78a2dc..d16d72c22 100644 --- a/validations/lookup_plugins/introspection_data.py +++ b/validations/lookup_plugins/introspection_data.py @@ -16,8 +16,10 @@ # 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 swiftclient.client import Connection from tripleo_validations.utils import get_auth_session @@ -30,20 +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 = [] - - session = get_auth_session(kwargs.get('auth_url'), - "ironic", - "service", - kwargs.get('password')) - - swift_client = Connection(session=session) - container = swift_client.get_container("ironic-inspector") - - for item in container[1]: - if item['name'].startswith('inspector_data') and \ - not item['name'].endswith("UNPROCESSED"): - obj = swift_client.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