From abcf6c56b9fee1d03fd92b371943082ae2098194 Mon Sep 17 00:00:00 2001 From: David Fairbrother Date: Wed, 24 May 2023 11:01:27 +0100 Subject: [PATCH] Add the AQ machine name to VM metadata Add the AQ Machine name to the Aquilon metadata, based on a request from the ops team --- .../rabbit_consumer/message_consumer.py | 10 ++++--- .../test/test_message_consumer.py | 26 ++++++++++++------- OpenStack-Rabbit-Consumer/version.txt | 2 +- charts/rabbit-consumer/Chart.yaml | 4 +-- 4 files changed, 26 insertions(+), 16 deletions(-) diff --git a/OpenStack-Rabbit-Consumer/rabbit_consumer/message_consumer.py b/OpenStack-Rabbit-Consumer/rabbit_consumer/message_consumer.py index 0d51bbe8..4931c26b 100644 --- a/OpenStack-Rabbit-Consumer/rabbit_consumer/message_consumer.py +++ b/OpenStack-Rabbit-Consumer/rabbit_consumer/message_consumer.py @@ -151,7 +151,7 @@ def handle_create_machine(rabbit_message: RabbitMessage) -> None: aq_api.aq_manage(network_details, image_meta) aq_api.aq_make(network_details, image_meta) - add_hostname_to_metadata(vm_data, network_details) + add_aq_details_to_metadata(vm_data, network_details) logger.info( "=== Finished Aquilon creation hook for VM %s ===", vm_data.virtual_machine_id @@ -188,7 +188,7 @@ def handle_machine_delete(rabbit_message: RabbitMessage) -> None: ) -def add_hostname_to_metadata( +def add_aq_details_to_metadata( vm_data: VmData, network_details: List[OpenstackAddress] ) -> None: """ @@ -203,7 +203,11 @@ def add_hostname_to_metadata( return hostnames = [i.hostname for i in network_details] - metadata = {"HOSTNAMES": ",".join(hostnames), "AQ_STATUS": "SUCCESS"} + metadata = { + "HOSTNAMES": ",".join(hostnames), + "AQ_STATUS": "SUCCESS", + "AQ_MACHINE": aq_api.search_machine_by_serial(vm_data), + } openstack_api.update_metadata(vm_data, metadata) diff --git a/OpenStack-Rabbit-Consumer/test/test_message_consumer.py b/OpenStack-Rabbit-Consumer/test/test_message_consumer.py index a2b2bea6..d24c26f9 100644 --- a/OpenStack-Rabbit-Consumer/test/test_message_consumer.py +++ b/OpenStack-Rabbit-Consumer/test/test_message_consumer.py @@ -15,7 +15,7 @@ from rabbit_consumer.message_consumer import ( on_message, initiate_consumer, - add_hostname_to_metadata, + add_aq_details_to_metadata, handle_create_machine, handle_machine_delete, SUPPORTED_MESSAGE_TYPES, @@ -155,20 +155,26 @@ def test_initiate_consumer_actual_consumption(rabbitpy, message_mock, _): @patch("rabbit_consumer.message_consumer.openstack_api") -def test_add_hostname_to_metadata_machine_exists( - openstack_api, vm_data, openstack_address_list +@patch("rabbit_consumer.message_consumer.aq_api") +def test_add_aq_details_to_metadata( + aq_api, openstack_api, vm_data, openstack_address_list ): """ Test that the function adds the hostname to the metadata when the machine exists """ openstack_api.check_machine_exists.return_value = True - add_hostname_to_metadata(vm_data, openstack_address_list) + add_aq_details_to_metadata(vm_data, openstack_address_list) - openstack_api.check_machine_exists.assert_called_once_with(vm_data) hostnames = [i.hostname for i in openstack_address_list] - openstack_api.update_metadata.assert_called_with( - vm_data, {"HOSTNAMES": ",".join(hostnames), "AQ_STATUS": "SUCCESS"} - ) + expected = { + "HOSTNAMES": ",".join(hostnames), + "AQ_STATUS": "SUCCESS", + "AQ_MACHINE": aq_api.search_machine_by_serial.return_value, + } + + openstack_api.check_machine_exists.assert_called_once_with(vm_data) + aq_api.search_machine_by_serial.assert_called_once_with(vm_data) + openstack_api.update_metadata.assert_called_with(vm_data, expected) @patch("rabbit_consumer.message_consumer.openstack_api") @@ -177,7 +183,7 @@ def test_add_hostname_to_metadata_machine_does_not_exist(openstack_api, vm_data) Test that the function does not add the hostname to the metadata when the machine does not exist """ openstack_api.check_machine_exists.return_value = False - add_hostname_to_metadata(vm_data, []) + add_aq_details_to_metadata(vm_data, []) openstack_api.check_machine_exists.assert_called_once_with(vm_data) openstack_api.update_metadata.assert_not_called() @@ -200,7 +206,7 @@ def test_handle_create_machine_skips_invalid(openstack_api, machine_valid): @patch("rabbit_consumer.message_consumer.openstack_api") @patch("rabbit_consumer.message_consumer.aq_api") -@patch("rabbit_consumer.message_consumer.add_hostname_to_metadata") +@patch("rabbit_consumer.message_consumer.add_aq_details_to_metadata") # pylint: disable=too-many-arguments def test_consume_create_machine_hostnames_good_path( metadata, aq_api, openstack, rabbit_message, image_metadata diff --git a/OpenStack-Rabbit-Consumer/version.txt b/OpenStack-Rabbit-Consumer/version.txt index 7ec1d6db..ccbccc3d 100644 --- a/OpenStack-Rabbit-Consumer/version.txt +++ b/OpenStack-Rabbit-Consumer/version.txt @@ -1 +1 @@ -2.1.0 +2.2.0 diff --git a/charts/rabbit-consumer/Chart.yaml b/charts/rabbit-consumer/Chart.yaml index cd143118..c2868b1e 100644 --- a/charts/rabbit-consumer/Chart.yaml +++ b/charts/rabbit-consumer/Chart.yaml @@ -6,10 +6,10 @@ type: application # This is the chart version. This version number should be incremented each time you make changes # to the chart and its templates, including the app version. # Versions are expected to follow Semantic Versioning (https://semver.org/) -version: 1.2.0 +version: 1.3.0 # This is the version number of the application being deployed. This version number should be # incremented each time you make changes to the application. Versions are not expected to # follow Semantic Versioning. They should reflect the version the application is using. # It is recommended to use it with quotes. -appVersion: "v2.1.0" +appVersion: "v2.2.0"