From c9acace928f6f63a6016d0481eb8f73be0085575 Mon Sep 17 00:00:00 2001 From: Jiri Hnidek Date: Wed, 10 Apr 2024 12:36:04 +0200 Subject: [PATCH 1/3] feat: Added Azure location to facts * Card ID: CCT-384 * Add azure_location to facts, when available * TODO: update required version of metadata and get extended locatition fact --- src/rhsmlib/facts/cloud_facts.py | 5 ++++- test/rhsmlib/facts/test_cloud_facts.py | 5 ++++- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/src/rhsmlib/facts/cloud_facts.py b/src/rhsmlib/facts/cloud_facts.py index 30445f230b..ea415bcb82 100644 --- a/src/rhsmlib/facts/cloud_facts.py +++ b/src/rhsmlib/facts/cloud_facts.py @@ -118,12 +118,13 @@ def get_aws_facts(self) -> Dict[str, Union[str, None]]: def get_azure_facts(self) -> Dict[str, str]: """ - Try to get facts of VM running on Azure public cloud. Returned dictionary has following format: + Try to get facts of VM running on Azure public cloud. Returned dictionary has the following format: { "azure_instance_id": some_instance_ID, "azure_offer": some_offer, "azure_sku": some_sku, "azure_subscription_id": some_subscription_ID + "azure_location: azure region the VM is running in } :return: dictionary containing Azure facts, when the machine is able to gather metadata from Azure cloud provider; otherwise returns empty dictionary {} @@ -143,6 +144,8 @@ def get_azure_facts(self) -> Dict[str, str]: facts["azure_offer"] = values["compute"]["offer"] if "subscriptionId" in values["compute"]: facts["azure_subscription_id"] = values["compute"]["subscriptionId"] + if "location" in values["compute"]: + facts["azure_location"] = values["compute"]["location"] return facts def get_gcp_facts(self) -> Dict[str, str]: diff --git a/test/rhsmlib/facts/test_cloud_facts.py b/test/rhsmlib/facts/test_cloud_facts.py index ed78738840..fbf2ad881a 100644 --- a/test/rhsmlib/facts/test_cloud_facts.py +++ b/test/rhsmlib/facts/test_cloud_facts.py @@ -169,6 +169,7 @@ AZURE_SKU = "8.1-ci" AZURE_OFFER = "RHEL" AZURE_SUBSCRIPTION_ID = "01234567-0123-0123-0123-012345679abc" +AZURE_LOCATION = "westeurope" def mock_prepare_request(request): @@ -309,13 +310,15 @@ def test_get_azure_facts(self): # azure_instance_id should be included in the facts self.assertIn("azure_instance_id", facts) self.assertEqual(facts["azure_instance_id"], AZURE_INSTANCE_ID) - # some other azure facts should be included in facts too + # some other azure facts could be included in facts too self.assertIn("azure_sku", facts) self.assertEqual(facts["azure_sku"], AZURE_SKU) self.assertIn("azure_offer", facts) self.assertEqual(facts["azure_offer"], AZURE_OFFER) self.assertIn("azure_subscription_id", facts) self.assertEqual(facts["azure_subscription_id"], AZURE_SUBSCRIPTION_ID) + self.assertIn("azure_location", facts) + self.assertEqual(facts["azure_location"], AZURE_LOCATION) @patch("cloud_what.providers.gcp.GCPCloudProvider._write_token_to_cache_file") @patch("cloud_what.providers.gcp.GCPCloudProvider._get_metadata_from_cache") From 76253dc8ae01b9491b1f61466520196987f7decc Mon Sep 17 00:00:00 2001 From: Jiri Hnidek Date: Wed, 10 Apr 2024 13:34:21 +0200 Subject: [PATCH 2/3] fix: Update version of Azure metadata * Update the version of metadata we try to get from Azure IMDS server, because we need facts from new version of metadata and old version of metadata could be deprecated in the future --- src/cloud_what/providers/azure.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/cloud_what/providers/azure.py b/src/cloud_what/providers/azure.py index 914c7cb199..a02bb9b18e 100644 --- a/src/cloud_what/providers/azure.py +++ b/src/cloud_what/providers/azure.py @@ -39,7 +39,7 @@ class AzureCloudProvider(BaseCloudProvider): # for very long time. It would be good to update the version from time to time, # because old versions (three years) are deprecated. It would be good to update # the API version with every minor version of RHEL - API_VERSION = "2021-02-01" + API_VERSION = "2023-07-01" BASE_CLOUD_PROVIDER_METADATA_URL = "http://169.254.169.254/metadata/instance?api-version=" From 0bba05ead9f12234e49dcc05c1681a3e34d7171b Mon Sep 17 00:00:00 2001 From: Jiri Hnidek Date: Mon, 15 Apr 2024 12:32:47 +0200 Subject: [PATCH 3/3] feat: Azure: added extended location and type of location fact * Card ID: CCT-384 * Added new facts from Azure IMDS metadata available since 2021-03-01 * Metadata with given version provides extendedLocation.name and extendedLocation.type * Extendd unit tests to include new facts --- src/rhsmlib/facts/cloud_facts.py | 5 +++++ test/rhsmlib/facts/test_cloud_facts.py | 13 +++++++++++++ 2 files changed, 18 insertions(+) diff --git a/src/rhsmlib/facts/cloud_facts.py b/src/rhsmlib/facts/cloud_facts.py index ea415bcb82..c429412b88 100644 --- a/src/rhsmlib/facts/cloud_facts.py +++ b/src/rhsmlib/facts/cloud_facts.py @@ -146,6 +146,11 @@ def get_azure_facts(self) -> Dict[str, str]: facts["azure_subscription_id"] = values["compute"]["subscriptionId"] if "location" in values["compute"]: facts["azure_location"] = values["compute"]["location"] + if "extendedLocation" in values["compute"]: + if "name" in values["compute"]["extendedLocation"]: + facts["azure_extended_location_name"] = values["compute"]["extendedLocation"]["name"] + if "type" in values["compute"]["extendedLocation"]: + facts["azure_extended_location_type"] = values["compute"]["extendedLocation"]["type"] return facts def get_gcp_facts(self) -> Dict[str, str]: diff --git a/test/rhsmlib/facts/test_cloud_facts.py b/test/rhsmlib/facts/test_cloud_facts.py index fbf2ad881a..2a8230622f 100644 --- a/test/rhsmlib/facts/test_cloud_facts.py +++ b/test/rhsmlib/facts/test_cloud_facts.py @@ -48,6 +48,10 @@ { "compute": { "azEnvironment": "AzurePublicCloud", + "extendedLocation": { + "type": "edgeZone", + "name": "microsoftlondon" + }, "customData": "", "location": "westeurope", "name": "foo-bar", @@ -169,7 +173,12 @@ AZURE_SKU = "8.1-ci" AZURE_OFFER = "RHEL" AZURE_SUBSCRIPTION_ID = "01234567-0123-0123-0123-012345679abc" +# There is no list of valid values of locations, extended locations and +# types of extended locations. Value "microsoftlondon" probably does not +# exist at all, and it was added only for testing purpose. AZURE_LOCATION = "westeurope" +AZURE_EXTENDED_LOCATION_NAME = "microsoftlondon" +AZURE_EXTENDED_LOCATION_TYPE = "edgeZone" def mock_prepare_request(request): @@ -319,6 +328,10 @@ def test_get_azure_facts(self): self.assertEqual(facts["azure_subscription_id"], AZURE_SUBSCRIPTION_ID) self.assertIn("azure_location", facts) self.assertEqual(facts["azure_location"], AZURE_LOCATION) + self.assertIn("azure_extended_location_name", facts) + self.assertEqual(facts["azure_extended_location_name"], AZURE_EXTENDED_LOCATION_NAME) + self.assertIn("azure_extended_location_type", facts) + self.assertEqual(facts["azure_extended_location_type"], AZURE_EXTENDED_LOCATION_TYPE) @patch("cloud_what.providers.gcp.GCPCloudProvider._write_token_to_cache_file") @patch("cloud_what.providers.gcp.GCPCloudProvider._get_metadata_from_cache")