@@ -2265,16 +2265,18 @@ def _next_lun(start=0):
22652265 current_lun = lun
22662266 for disk_id in disk_ids :
22672267 if current_lun is not None :
2268- disk_lun = current_lun
2269- current_lun += 1
2268+ disk_lun = _next_lun ( start = current_lun )
2269+ current_lun = disk_lun + 1
22702270 else :
22712271 disk_lun = _next_lun ()
22722272 payload = {
22732273 'diskId' : disk_id ,
22742274 'lun' : disk_lun ,
22752275 'caching' : caching ,
2276- 'writeAcceleratorEnabled' : enable_write_accelerator
22772276 }
2277+ if enable_write_accelerator :
2278+ payload ['writeAcceleratorEnabled' ] = enable_write_accelerator
2279+
22782280 attach_payload .append (payload )
22792281 return AttachDetachDataDisk (cli_ctx = cmd .cli_ctx )(command_args = {
22802282 'vm_name' : vm_name ,
@@ -2307,8 +2309,17 @@ def _next_lun(start=0):
23072309
23082310 # attach existing / new disks
23092311 if disks_to_process :
2312+ # if a user-specified LUN is provided, allocate it (or the next available
2313+ # starting from that value) for the first disk, then auto-increment for
2314+ # subsequent disks to avoid LUN conflicts.
2315+ next_lun = _next_lun (start = lun ) if lun is not None else None
23102316 for disk_item in disks_to_process :
2311- disk_lun = lun if lun is not None else _next_lun ()
2317+ if lun is not None :
2318+ disk_lun = next_lun
2319+ next_lun = _next_lun ()
2320+ else :
2321+ disk_lun = _next_lun ()
2322+
23122323 if new :
23132324 disk_name = parse_resource_id (disk_item )['name' ]
23142325 disk_obj = {
@@ -2410,14 +2421,16 @@ def detach_managed_data_disk(cmd, resource_group_name, vm_name, disk_name=None,
24102421
24112422 vm = VMShow (cli_ctx = cmd .cli_ctx )(command_args = {
24122423 'resource_group' : resource_group_name ,
2413- " vm_name" : vm_name
2424+ ' vm_name' : vm_name
24142425 })
24152426
2416- # To avoid unnecessary permission check of image
2417- storage_profile = vm . get ( 'storageProfile ' , {} )
2418- storage_profile [ "imageReference" ] = None
2427+ # work on a local copy of the VM dict to avoid mutating the original object.
2428+ vm_result = vm if isinstance ( vm , dict ) else getattr ( vm , 'result ' , vm )
2429+ vm_dict = json . loads ( json . dumps ( vm_result ))
24192430
2420- vm_dict = vm if isinstance (vm , dict ) else getattr (vm , 'result' , vm )
2431+ # to avoid unnecessary permission check of image
2432+ storage_profile = vm_dict .get ('storageProfile' , {})
2433+ storage_profile ["imageReference" ] = None
24212434
24222435 target_disk = None
24232436 data_disks = safe_get (vm_dict , 'storageProfile.dataDisks' , default = []) or []
@@ -2429,15 +2442,15 @@ def detach_managed_data_disk(cmd, resource_group_name, vm_name, disk_name=None,
24292442 break
24302443
24312444 if not target_disk :
2432- attached_names = [d .get ('name' ) for d in (_ (vm_dict , 'storageProfile.dataDisks' , []) or [])]
2445+ attached_names = [d .get ('name' ) for d in (safe_get (vm_dict , 'storageProfile.dataDisks' , []) or [])]
24332446 raise ResourceNotFoundError (
24342447 "No disk with the name '{}' was found. Attached: {}" .format (disk_name , attached_names )
24352448 )
24362449
24372450 disk_id = safe_get (target_disk , 'managedDisk.id' )
24382451 if not disk_id :
24392452 raise CLIError (
2440- "Disk '{}' is not a managed disk (no managedDisk.id). Only managed disks are supported by AAZ detach ."
2453+ "Disk '{}' is not a managed disk (no managedDisk.id). Only managed disks are supported for this operation ."
24412454 .format (disk_name )
24422455 )
24432456
0 commit comments