Parcourir la source

Normalize RG-name case in Azure machine-image ids

compute.images.list_by_resource_group returns the resource-group
segment of an image id in a case that can differ from what create/get
echo back (observed: lowercase from create/get vs uppercase from list
for the same RG). This is specific to Microsoft.Compute/images and
isn't reproduced by other resource types in the same subscription.

Normalize just the RG-name segment to lowercase in
AzureMachineImage.id so equality checks against ids built via
different code paths (e.g. compare a freshly-created image's id
against the same image as returned by list) stop failing. Other path
segments (Microsoft.Compute, images, resource name) are preserved.
Case differences elsewhere in the ID, if they ever surface, can be
handled with the same helper.
Nuwan Goonasekera il y a 1 jour
Parent
commit
ab90972f42

+ 18 - 0
cloudbridge/providers/azure/helpers.py

@@ -1,6 +1,24 @@
+import re
+
 from cloudbridge.interfaces.exceptions import InvalidValueException
 
 
+_RG_NAME_RE = re.compile(r'(/resourceGroups/)([^/]+)', re.IGNORECASE)
+
+
+def normalize_rg_case(azure_id):
+    # Microsoft.Compute/images list_by_resource_group returns the RG segment
+    # in a case that can differ from what create/get echo back (we've seen
+    # uppercase from list, lowercase from create/get for the same RG).
+    # Lowercase just the RG-name segment so that ids from any code path
+    # compare equal. Provider/type segments and the resource name itself are
+    # preserved.
+    if not azure_id:
+        return azure_id
+    return _RG_NAME_RE.sub(
+        lambda m: m.group(1) + m.group(2).lower(), azure_id)
+
+
 # def filter_by_tag(list_items, filters):
 #     """
 #     This function filter items on the tags

+ 1 - 1
cloudbridge/providers/azure/resources.py

@@ -609,7 +609,7 @@ class AzureMachineImage(BaseMachineImage):
         if self.is_gallery_image:
             return azure_helpers.generate_urn(self._image)
         else:
-            return self._image.id
+            return azure_helpers.normalize_rg_case(self._image.id)
 
     @property
     def name(self):