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 ],
@@ -214,19 +241,21 @@ def prepare_vm_images(
214241 raise ValueError (msg )
215242
216243 raw_source = source .to_json ()
244+ arch_name = metadata .architecture
245+
217246 json_gen1 = {
218- "imageType" : get_image_type_mapping (metadata . architecture , "V1" ),
247+ "imageType" : get_image_type_mapping (arch_name , "V1" ),
219248 "source" : raw_source ,
220249 }
221250 json_gen2 = {
222- "imageType" : get_image_type_mapping (metadata . architecture , "V2" ),
251+ "imageType" : get_image_type_mapping (arch_name , "V2" ),
223252 "source" : raw_source ,
224253 }
225254
226255 if metadata .generation == "V2" :
227256 # In this case we need to set a V2 SAS URI
228257 gen2_new = VMImageDefinition .from_json (json_gen2 )
229- if metadata . support_legacy : # and in this case a V1 as well
258+ if is_legacy_gen_supported ( metadata ) : # and in this case a V1 as well
230259 gen1_new = VMImageDefinition .from_json (json_gen1 )
231260 return [gen2_new , gen1_new ]
232261 return [gen2_new ]
@@ -235,13 +264,25 @@ def prepare_vm_images(
235264 return [VMImageDefinition .from_json (json_gen1 )]
236265
237266
267+ def _len_vm_images (disk_versions : List [DiskVersion ]) -> int :
268+ count = 0
269+ for disk_version in disk_versions :
270+ count = count + len (disk_version .vm_images )
271+ return count
272+
273+
238274def _build_skus (
239275 disk_versions : List [DiskVersion ],
240276 default_gen : str ,
241277 alt_gen : str ,
242278 plan_name : str ,
243279 security_type : Optional [List [str ]] = None ,
244280) -> List [VMISku ]:
281+ def get_skuid (arch ):
282+ if arch == "x64" :
283+ return plan_name
284+ return f"{ plan_name } -{ arch .lower ()} "
285+
245286 sku_mapping : Dict [str , str ] = {}
246287 # Update the SKUs for each image in DiskVersions if needed
247288 for disk_version in disk_versions :
@@ -254,10 +295,11 @@ def _build_skus(
254295 new_img_alt_type = get_image_type_mapping (arch , alt_gen )
255296
256297 # we just want to add SKU whenever it's not set
298+ skuid = get_skuid (arch )
257299 if vmid .image_type == new_img_type :
258- sku_mapping .setdefault (new_img_type , plan_name )
300+ sku_mapping .setdefault (new_img_type , skuid )
259301 elif vmid .image_type == new_img_alt_type :
260- sku_mapping .setdefault (new_img_alt_type , f"{ plan_name } -gen{ alt_gen [1 :]} " )
302+ sku_mapping .setdefault (new_img_alt_type , f"{ skuid } -gen{ alt_gen [1 :]} " )
261303
262304 # Return the expected SKUs list
263305 res = [
@@ -290,16 +332,20 @@ def update_skus(
290332 The updated list with VMISkus.
291333 """
292334 if not old_skus :
335+ # First image's gen
336+ # generation = "V" + disk_versions[0].vm_images[0].image_type.split("Gen")[-1]
293337 alt_gen = "V2" if generation == "V1" else "V1"
294338 return _build_skus (
295339 disk_versions , default_gen = generation , alt_gen = alt_gen , plan_name = plan_name
296340 )
297341
298- # If we have SKUs for both genenerations we don't need to update them as they're already
342+ # Second case: All SKUs are provided
343+ # If we have SKUs for each image we don't need to update them as they're already
299344 # properly set.
300- if len (old_skus ) == 2 :
345+ if len (old_skus ) == _len_vm_images ( disk_versions ) :
301346 return old_skus
302347
348+ # Third case: Missing at least one SKU
303349 # Update SKUs to create the alternate gen.
304350 # The security type may exist only for Gen2, so it iterates over all gens to find it
305351 security_type = None
@@ -348,16 +394,17 @@ def create_disk_version_from_scratch(
348394 source (VMImageSource):
349395 The VMImageSource with the updated SAS URI.
350396 """
397+ arch_name = metadata .architecture
351398 vm_images = [
352399 {
353- "imageType" : get_image_type_mapping (metadata . architecture , metadata .generation ),
400+ "imageType" : get_image_type_mapping (arch_name , metadata .generation ),
354401 "source" : source .to_json (),
355402 }
356403 ]
357- if metadata . support_legacy :
404+ if is_legacy_gen_supported ( metadata ) :
358405 vm_images .append (
359406 {
360- "imageType" : get_image_type_mapping (metadata . architecture , "V1" ),
407+ "imageType" : get_image_type_mapping (arch_name , "V1" ),
361408 "source" : source .to_json (),
362409 }
363410 )
@@ -457,16 +504,17 @@ def create_vm_image_definitions(
457504 log .debug ("Creating VMImageDefinitions for \" %s\" " , metadata .destination )
458505 vm_images = []
459506
507+ arch_name = metadata .architecture
460508 vm_images .append (
461509 VMImageDefinition (
462- image_type = get_image_type_mapping (metadata . architecture , metadata .generation ),
510+ image_type = get_image_type_mapping (arch_name , metadata .generation ),
463511 source = source .to_json (),
464512 )
465513 )
466- if metadata . support_legacy : # Only True when metadata.generation == V2
514+ if is_legacy_gen_supported ( metadata ):
467515 vm_images .append (
468516 VMImageDefinition (
469- image_type = get_image_type_mapping (metadata . architecture , "V1" ),
517+ image_type = get_image_type_mapping (arch_name , "V1" ),
470518 source = source .to_json (),
471519 )
472520 )
0 commit comments