Skip to content

Commit 45ae34d

Browse files
grizzlytheodoregithub-actions
andauthored
Add param to Add-AzVMDataDisk (#25314)
* Autogen code * changes added * fix changelog * show new property when shown object * add test --------- Co-authored-by: github-actions <[email protected]>
1 parent 5a245ca commit 45ae34d

File tree

7 files changed

+158
-8
lines changed

7 files changed

+158
-8
lines changed

src/Compute/Compute.Test/ScenarioTests/VirtualMachineTests.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -647,6 +647,12 @@ public void TestVMDefaultsToTrustedLaunchImgWhenStnd()
647647
{
648648
TestRunner.RunTestScript("Test-VMDefaultsToTrustedLaunchImgWhenStnd");
649649
}
650-
650+
651+
[Fact]
652+
[Trait(Category.AcceptanceType, Category.CheckIn)]
653+
public void TestAddVMDataDisk()
654+
{
655+
TestRunner.RunTestScript("Test-AddVMDataDisk");
656+
}
651657
}
652658
}

src/Compute/Compute.Test/ScenarioTests/VirtualMachineTests.ps1

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7532,4 +7532,23 @@ function Test-VMDefaultsToTrustedLaunchImgWhenStnd
75327532
# Cleanup
75337533
Clean-ResourceGroup $rgname;
75347534
}
7535+
}
7536+
7537+
<#
7538+
.SYNOPSIS
7539+
Test Add-AzVMDataDisk
7540+
#>
7541+
function Test-AddVMDataDisk
7542+
{
7543+
# To have a test recording
7544+
Get-AzVm
7545+
7546+
$name = Get-ComputeTestResourceName;
7547+
$vmname = 'vm' + $name;
7548+
$vmConfig = New-AzVmConfig -VMName $vmname -VMSize 'testVMSize'
7549+
7550+
$vmConfig = Add-AzVMDataDisk -VM $vmConfig -Name datadisk0 -VhdUri "testVhdUri" -SourceResourceId "testSourceResourceId" -CreateOption Copy -Lun 1
7551+
7552+
# Validate
7553+
Assert-AreEqual $vmConfig.StorageProfile.DataDisks[0].SourceResource.id "testSourceResourceId"
75357554
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
{
2+
"Entries": [
3+
{
4+
"RequestUri": "/subscriptions/e37510d7-33b6-4676-886f-ee75bcc01871/providers/Microsoft.Compute/virtualMachines?api-version=2024-03-01",
5+
"EncodedRequestUri": "L3N1YnNjcmlwdGlvbnMvZTM3NTEwZDctMzNiNi00Njc2LTg4NmYtZWU3NWJjYzAxODcxL3Byb3ZpZGVycy9NaWNyb3NvZnQuQ29tcHV0ZS92aXJ0dWFsTWFjaGluZXM/YXBpLXZlcnNpb249MjAyNC0wMy0wMQ==",
6+
"RequestMethod": "GET",
7+
"RequestHeaders": {
8+
"x-ms-client-request-id": [
9+
"c6d08848-d048-4554-8338-94c5a51ef7e9"
10+
],
11+
"Accept-Language": [
12+
"en-US"
13+
],
14+
"User-Agent": [
15+
"FxVersion/6.0.3124.26714",
16+
"OSName/Windows",
17+
"OSVersion/Microsoft.Windows.10.0.22631",
18+
"Microsoft.Azure.Management.Compute.ComputeManagementClient/8.0.0"
19+
]
20+
},
21+
"RequestBody": "",
22+
"ResponseHeaders": {
23+
"Cache-Control": [
24+
"no-cache"
25+
],
26+
"Pragma": [
27+
"no-cache"
28+
],
29+
"x-ms-ratelimit-remaining-resource": [
30+
"Microsoft.Compute/HighCostGetSubscriptionMaximum;899"
31+
],
32+
"Strict-Transport-Security": [
33+
"max-age=31536000; includeSubDomains"
34+
],
35+
"x-ms-request-id": [
36+
"22ce7d2d-8c95-475a-9743-fe7f09be5dd8"
37+
],
38+
"x-ms-throttling-version": [
39+
"v1"
40+
],
41+
"x-ms-ratelimit-remaining-subscription-reads": [
42+
"11998"
43+
],
44+
"x-ms-correlation-request-id": [
45+
"a36c1c16-f7ba-4820-9fba-48595d30af95"
46+
],
47+
"x-ms-routing-request-id": [
48+
"EASTUS2:20240620T192308Z:a36c1c16-f7ba-4820-9fba-48595d30af95"
49+
],
50+
"X-Content-Type-Options": [
51+
"nosniff"
52+
],
53+
"X-Cache": [
54+
"CONFIG_NOCACHE"
55+
],
56+
"X-MSEdge-Ref": [
57+
"Ref A: 45D0144D49294FC29023018DF3C79DC7 Ref B: BL2AA2030103049 Ref C: 2024-06-20T19:23:07Z"
58+
],
59+
"Date": [
60+
"Thu, 20 Jun 2024 19:23:08 GMT"
61+
],
62+
"Content-Length": [
63+
"2366"
64+
],
65+
"Content-Type": [
66+
"application/json; charset=utf-8"
67+
],
68+
"Expires": [
69+
"-1"
70+
]
71+
},
72+
"ResponseBody": "{\r\n \"value\": [\r\n {\r\n \"name\": \"mytestvm\",\r\n \"id\": \"/subscriptions/e37510d7-33b6-4676-886f-ee75bcc01871/resourceGroups/MYRESOURCEGROUP/providers/Microsoft.Compute/virtualMachines/mytestvm\",\r\n \"type\": \"Microsoft.Compute/virtualMachines\",\r\n \"location\": \"eastus\",\r\n \"tags\": {\r\n \"azsecpack\": \"nonprod\",\r\n \"platformsettings.host_environment.service.platform_optedin_for_rootcerts\": \"true\"\r\n },\r\n \"properties\": {\r\n \"hardwareProfile\": {\r\n \"vmSize\": \"Standard_D2s_v3\"\r\n },\r\n \"provisioningState\": \"Failed\",\r\n \"vmId\": \"ae6bfd4e-41aa-4f96-b779-6734400206d8\",\r\n \"storageProfile\": {\r\n \"imageReference\": {\r\n \"publisher\": \"kinvolk\",\r\n \"offer\": \"flatcar-container-linux-free\",\r\n \"sku\": \"stable-gen2\",\r\n \"version\": \"latest\",\r\n \"exactVersion\": \"3815.2.2\"\r\n },\r\n \"osDisk\": {\r\n \"osType\": \"Linux\",\r\n \"name\": \"mytestvm_OsDisk_1_07c20fb9eb8e43dd9c76f9c5b7ad7f1c\",\r\n \"createOption\": \"FromImage\",\r\n \"caching\": \"ReadWrite\",\r\n \"managedDisk\": {\r\n \"storageAccountType\": \"Premium_LRS\",\r\n \"id\": \"/subscriptions/e37510d7-33b6-4676-886f-ee75bcc01871/resourceGroups/MyResourceGroup/providers/Microsoft.Compute/disks/mytestvm_OsDisk_1_07c20fb9eb8e43dd9c76f9c5b7ad7f1c\"\r\n },\r\n \"deleteOption\": \"Detach\",\r\n \"diskSizeGB\": 30\r\n },\r\n \"dataDisks\": [],\r\n \"diskControllerType\": \"SCSI\"\r\n },\r\n \"osProfile\": {\r\n \"computerName\": \"mytestvm\",\r\n \"adminUsername\": \"usertest\",\r\n \"linuxConfiguration\": {\r\n \"disablePasswordAuthentication\": false,\r\n \"provisionVMAgent\": true,\r\n \"patchSettings\": {\r\n \"patchMode\": \"ImageDefault\",\r\n \"assessmentMode\": \"ImageDefault\"\r\n }\r\n },\r\n \"secrets\": [],\r\n \"allowExtensionOperations\": true,\r\n \"requireGuestProvisionSignal\": true\r\n },\r\n \"networkProfile\": {\r\n \"networkInterfaces\": [\r\n {\r\n \"id\": \"/subscriptions/e37510d7-33b6-4676-886f-ee75bcc01871/resourceGroups/MyResourceGroup/providers/Microsoft.Network/networkInterfaces/mytestvm\"\r\n }\r\n ]\r\n },\r\n \"timeCreated\": \"2024-05-21T10:23:00.9788091-04:00\"\r\n }\r\n }\r\n ]\r\n}",
73+
"StatusCode": 200
74+
}
75+
],
76+
"Names": {
77+
"Test-AddVMDataDisk": [
78+
"crptestps9113"
79+
]
80+
},
81+
"Variables": {
82+
"SubscriptionId": "e37510d7-33b6-4676-886f-ee75bcc01871"
83+
}
84+
}

src/Compute/Compute/ChangeLog.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
2121
-->
2222
## Upcoming Release
23+
* Added parameter `-SourceResourceId` to cmdlet `Add-AzVMDataDisk`.
2324
* Added parameter `-IdentityType` to cmdlet `Update-AzDiskEncryptionSet`.
2425

2526
## Version 8.0.0

src/Compute/Compute/Compute.format.ps1xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -409,6 +409,10 @@
409409
<Label>SourceImage</Label>
410410
<PropertyName>SourceImage</PropertyName>
411411
</ListItem>
412+
<ListItem>
413+
<Label>SourceResource</Label>
414+
<PropertyName>SourceResource</PropertyName>
415+
</ListItem>
412416
<ListItem>
413417
<Label>VirtualHardDisk</Label>
414418
<ScriptBlock>

src/Compute/Compute/VirtualMachine/Config/AddAzureVMDataDiskCommand.cs

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// ----------------------------------------------------------------------------------
1+
// ----------------------------------------------------------------------------------
22
//
33
// Copyright Microsoft Corporation
44
// Licensed under the Apache License, Version 2.0 (the "License");
@@ -99,6 +99,7 @@ public class AddAzureVMDataDiskCommand : ComputeClientBaseCmdlet
9999
Position = 6,
100100
ValueFromPipelineByPropertyName = true,
101101
HelpMessage = HelpMessages.VMDataDiskCreateOption)]
102+
[PSArgumentCompleter("Attach", "Empty", "FromImage", "Copy", "Restore")]
102103
public string CreateOption { get; set; }
103104

104105
[Alias("SourceImage")]
@@ -144,6 +145,13 @@ public class AddAzureVMDataDiskCommand : ComputeClientBaseCmdlet
144145
[PSArgumentCompleter("Detach", "Delete")]
145146
public string DeleteOption { get; set; }
146147

148+
[Parameter(
149+
Mandatory = false,
150+
ValueFromPipelineByPropertyName = true,
151+
HelpMessage = "ARM ID of snapshot or disk restore point from which to create a disk.")]
152+
[ValidateNotNullOrEmpty]
153+
public string SourceResourceId { get; set; }
154+
147155
public override void ExecuteCmdlet()
148156
{
149157
if (this.ParameterSetName.Equals(VmNormalDiskParameterSet))
@@ -175,7 +183,11 @@ public override void ExecuteCmdlet()
175183
{
176184
Uri = this.SourceImageUri
177185
},
178-
DeleteOption = this.DeleteOption
186+
DeleteOption = this.DeleteOption,
187+
SourceResource = string.IsNullOrEmpty(this.SourceResourceId) ? null : new ApiEntityReference
188+
{
189+
Id = this.SourceResourceId
190+
}
179191
});
180192

181193
this.VM.StorageProfile = storageProfile;
@@ -213,7 +225,11 @@ public override void ExecuteCmdlet()
213225
CreateOption = this.CreateOption,
214226
ManagedDisk = SetManagedDisk(this.ManagedDiskId, this.DiskEncryptionSetId, this.StorageAccountType),
215227
WriteAcceleratorEnabled = this.WriteAccelerator.IsPresent,
216-
DeleteOption = this.DeleteOption
228+
DeleteOption = this.DeleteOption,
229+
SourceResource = string.IsNullOrEmpty(this.SourceResourceId) ? null : new ApiEntityReference
230+
{
231+
Id = this.SourceResourceId
232+
}
217233
});
218234

219235
this.VM.StorageProfile = storageProfile;
@@ -222,4 +238,4 @@ public override void ExecuteCmdlet()
222238
}
223239
}
224240
}
225-
}
241+
}

src/Compute/Compute/help/Add-AzVMDataDisk.md

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,17 @@ Adds a data disk to a virtual machine.
1717
```
1818
Add-AzVMDataDisk [-VM] <PSVirtualMachine> [[-Name] <String>] [[-VhdUri] <String>] [[-Caching] <CachingTypes>]
1919
[[-DiskSizeInGB] <Int32>] [-Lun] <Int32> [-CreateOption] <String> [[-SourceImageUri] <String>]
20-
[-DiskEncryptionSetId <String>] [-DeleteOption <String>] [-DefaultProfile <IAzureContextContainer>]
21-
[<CommonParameters>]
20+
[-DiskEncryptionSetId <String>] [-DeleteOption <String>] [-SourceResourceId <String>]
21+
[-DefaultProfile <IAzureContextContainer>] [-ProgressAction <ActionPreference>] [<CommonParameters>]
2222
```
2323

2424
### VmManagedDiskParameterSetName
2525
```
2626
Add-AzVMDataDisk [-VM] <PSVirtualMachine> [[-Name] <String>] [[-Caching] <CachingTypes>]
2727
[[-DiskSizeInGB] <Int32>] [-Lun] <Int32> [-CreateOption] <String> [[-ManagedDiskId] <String>]
2828
[[-StorageAccountType] <String>] [-DiskEncryptionSetId <String>] [-WriteAccelerator] [-DeleteOption <String>]
29-
[-DefaultProfile <IAzureContextContainer>] [<CommonParameters>]
29+
[-SourceResourceId <String>] [-DefaultProfile <IAzureContextContainer>] [-ProgressAction <ActionPreference>]
30+
[<CommonParameters>]
3031
```
3132

3233
## DESCRIPTION
@@ -133,6 +134,10 @@ Specify this to create an empty data disk.
133134
Specify this option to create a virtual machine from a generalized image or disk.
134135
When you specify this option, you must specify the *SourceImageUri* parameter also in order to tell the Azure platform the location of the VHD to attach as a data disk.
135136
The *VhdUri* parameter is used as the location identifying where the data disk VHD will be stored when it is used by the virtual machine.
137+
- Empty.
138+
This value is used when creating an empty data disk.
139+
- Copy.
140+
This value is used to create a data disk from a snapshot or another disk. **Restore:** This value is used to create a data disk from a disk restore point.
136141
137142
```yaml
138143
Type: System.String
@@ -266,6 +271,21 @@ Accept pipeline input: True (ByPropertyName)
266271
Accept wildcard characters: False
267272
```
268273
274+
### -SourceResourceId
275+
ARM ID of snapshot or disk restore point from which to create a disk.
276+
277+
```yaml
278+
Type: System.String
279+
Parameter Sets: (All)
280+
Aliases:
281+
282+
Required: False
283+
Position: Named
284+
Default value: None
285+
Accept pipeline input: True (ByPropertyName)
286+
Accept wildcard characters: False
287+
```
288+
269289
### -StorageAccountType
270290
Specifies the storage account type of managed disk.
271291

0 commit comments

Comments
 (0)