11# SPDX-License-Identifier: GPL-3.0-or-later
22import logging
33from operator import attrgetter
4- from typing import Dict , List , Optional , Tuple
4+ from typing import Any , Dict , List , Optional , Tuple
55
66from deepdiff import DeepDiff
77
@@ -69,7 +69,22 @@ def __init__(
6969 super (AzurePublishingMetadata , self ).__init__ (** kwargs )
7070 self .__validate ()
7171 # Adjust the x86_64 architecture string for Azure
72- self .architecture = "x64" if self .architecture == "x86_64" else self .architecture
72+ arch = self .__convert_arch (self .architecture )
73+ self .architecture = arch
74+
75+ def __setattr__ (self , name : str , value : Any ) -> None :
76+ if name == "architecture" :
77+ arch = self .__convert_arch (value )
78+ value = arch
79+ return super ().__setattr__ (name , value )
80+
81+ @staticmethod
82+ def __convert_arch (arch : str ) -> str :
83+ converter = {
84+ "x86_64" : "x64" ,
85+ "aarch64" : "arm64" ,
86+ }
87+ return converter .get (arch , None ) or arch
7388
7489 def __validate (self ):
7590 mandatory = [
@@ -91,9 +106,10 @@ def __validate(self):
91106def get_image_type_mapping (architecture : str , generation : str ) -> str :
92107 """Return the image type required by VMImageDefinition."""
93108 gen_map = {
94- "V1" : f"{ architecture } Gen1" ,
95109 "V2" : f"{ architecture } Gen2" ,
96110 }
111+ if architecture == "x64" :
112+ gen_map .update ({"V1" : f"{ architecture } Gen1" })
97113 return gen_map .get (generation , "" )
98114
99115
@@ -185,6 +201,17 @@ def is_azure_job_not_complete(job_details: ConfigureStatus) -> bool:
185201 return False
186202
187203
204+ def is_legacy_gen_supported (metadata : AzurePublishingMetadata ) -> bool :
205+ """Return True when the legagy V1 SKU is supported, False otherwise.
206+
207+ Args:
208+ metadata: The incoming publishing metadata.
209+ Returns:
210+ bool: True when V1 is supported, False otherwise.
211+ """
212+ return metadata .architecture == "x64" and metadata .support_legacy
213+
214+
188215def prepare_vm_images (
189216 metadata : AzurePublishingMetadata ,
190217 gen1 : Optional [VMImageDefinition ],
@@ -226,7 +253,7 @@ def prepare_vm_images(
226253 if metadata .generation == "V2" :
227254 # In this case we need to set a V2 SAS URI
228255 gen2_new = VMImageDefinition .from_json (json_gen2 )
229- if metadata . support_legacy : # and in this case a V1 as well
256+ if is_legacy_gen_supported ( metadata ) : # and in this case a V1 as well
230257 gen1_new = VMImageDefinition .from_json (json_gen1 )
231258 return [gen2_new , gen1_new ]
232259 return [gen2_new ]
@@ -235,13 +262,25 @@ def prepare_vm_images(
235262 return [VMImageDefinition .from_json (json_gen1 )]
236263
237264
265+ def _len_vm_images (disk_versions : List [DiskVersion ]) -> int :
266+ count = 0
267+ for disk_version in disk_versions :
268+ count = count + len (disk_version .vm_images )
269+ return count
270+
271+
238272def _build_skus (
239273 disk_versions : List [DiskVersion ],
240274 default_gen : str ,
241275 alt_gen : str ,
242276 plan_name : str ,
243277 security_type : Optional [List [str ]] = None ,
244278) -> List [VMISku ]:
279+ def get_skuid (arch ):
280+ if arch == "x64" :
281+ return plan_name
282+ return f"{ plan_name } -{ arch .lower ()} "
283+
245284 sku_mapping : Dict [str , str ] = {}
246285 # Update the SKUs for each image in DiskVersions if needed
247286 for disk_version in disk_versions :
@@ -254,10 +293,11 @@ def _build_skus(
254293 new_img_alt_type = get_image_type_mapping (arch , alt_gen )
255294
256295 # we just want to add SKU whenever it's not set
296+ skuid = get_skuid (arch )
257297 if vmid .image_type == new_img_type :
258- sku_mapping .setdefault (new_img_type , plan_name )
298+ sku_mapping .setdefault (new_img_type , skuid )
259299 elif vmid .image_type == new_img_alt_type :
260- sku_mapping .setdefault (new_img_alt_type , f"{ plan_name } -gen{ alt_gen [1 :]} " )
300+ sku_mapping .setdefault (new_img_alt_type , f"{ skuid } -gen{ alt_gen [1 :]} " )
261301
262302 # Return the expected SKUs list
263303 res = [
@@ -295,9 +335,9 @@ def update_skus(
295335 disk_versions , default_gen = generation , alt_gen = alt_gen , plan_name = plan_name
296336 )
297337
298- # If we have SKUs for both genenerations we don't need to update them as they're already
338+ # If we have SKUs for each image we don't need to update them as they're already
299339 # properly set.
300- if len (old_skus ) == 2 :
340+ if len (old_skus ) == _len_vm_images ( disk_versions ) :
301341 return old_skus
302342
303343 # Update SKUs to create the alternate gen.
@@ -354,7 +394,7 @@ def create_disk_version_from_scratch(
354394 "source" : source .to_json (),
355395 }
356396 ]
357- if metadata . support_legacy :
397+ if is_legacy_gen_supported ( metadata ) :
358398 vm_images .append (
359399 {
360400 "imageType" : get_image_type_mapping (metadata .architecture , "V1" ),
@@ -457,16 +497,17 @@ def create_vm_image_definitions(
457497 log .debug ("Creating VMImageDefinitions for \" %s\" " , metadata .destination )
458498 vm_images = []
459499
500+ arch_name = metadata .architecture
460501 vm_images .append (
461502 VMImageDefinition (
462- image_type = get_image_type_mapping (metadata . architecture , metadata .generation ),
503+ image_type = get_image_type_mapping (arch_name , metadata .generation ),
463504 source = source .to_json (),
464505 )
465506 )
466- if metadata . support_legacy : # Only True when metadata.generation == V2
507+ if is_legacy_gen_supported ( metadata ):
467508 vm_images .append (
468509 VMImageDefinition (
469- image_type = get_image_type_mapping (metadata . architecture , "V1" ),
510+ image_type = get_image_type_mapping (arch_name , "V1" ),
470511 source = source .to_json (),
471512 )
472513 )
0 commit comments