Skip to content

Commit 24ac91f

Browse files
authored
New-AzGalleryImageVersion new parameter SourceImageVMId (#24449)
* dev and test * test success * comment * error msg * test error fix * changelog * help doc * tmp * help doc * remove errors already in backend
1 parent ee7a92d commit 24ac91f

File tree

7 files changed

+4450
-4
lines changed

7 files changed

+4450
-4
lines changed

src/Compute/Compute.Test/ScenarioTests/GalleryTests.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,5 +57,12 @@ public void TestGalleryDirectSharing()
5757
{
5858
TestRunner.RunTestScript("Test-GalleryDirectSharing");
5959
}
60+
61+
[Fact]
62+
[Trait(Category.AcceptanceType, Category.CheckIn)]
63+
public void TestGalleryVersionWithSourceImageVMId()
64+
{
65+
TestRunner.RunTestScript("Test-GalleryVersionWithSourceImageVMId");
66+
}
6067
}
6168
}

src/Compute/Compute.Test/ScenarioTests/GalleryTests.ps1

Lines changed: 111 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -767,4 +767,114 @@ function Test-GalleryDirectSharing
767767
# Cleanup
768768
Clean-ResourceGroup $rgname
769769
}
770-
}
770+
}
771+
772+
<#
773+
.SYNOPSIS
774+
Tests the New-AzGalleryImageVersion new parameter SourceImageVMId.
775+
#>
776+
function Test-GalleryVersionWithSourceImageVMId
777+
{
778+
# Setup
779+
$rgname = Get-ComputeTestResourceName;
780+
$loc = Get-ComputeVMLocation;
781+
782+
try
783+
{
784+
785+
$location = $loc;
786+
New-AzResourceGroup -Name $rgname -Location $loc -Force;
787+
# create credential
788+
$password = Get-PasswordForVM;
789+
$securePassword = $password | ConvertTo-SecureString -AsPlainText -Force;
790+
$user = Get-ComputeTestResourceName;
791+
$cred = New-Object System.Management.Automation.PSCredential ($user, $securePassword);
792+
793+
# Add one VM from creation
794+
$vmname = 'vm' + $rgname;
795+
$domainNameLabel = "d1" + $rgname;
796+
$securityType_TL = "TrustedLaunch";
797+
$PublisherName = "MicrosoftWindowsServer";
798+
$Offer = "WindowsServer";
799+
$SKU = "2022-datacenter-azure-edition";
800+
$version = "latest";
801+
$disable = $false;
802+
$enable = $true;
803+
$galleryName = "g" + $rgname;
804+
$VMSize = "Standard_DS2_v2";
805+
$vnetname = "vn" + $rgname;
806+
$vnetAddress = "10.0.0.0/16";
807+
$subnetname = "slb" + $rgname;
808+
$subnetAddress = "10.0.2.0/24";
809+
$pubipname = "p" + $rgname;
810+
$OSDiskName = $vmname + "-osdisk";
811+
$NICName = $vmname+ "-nic";
812+
$NSGName = $vmname + "-NSG";
813+
$nsgrulename = "nsr" + $rgname;
814+
$OSDiskSizeinGB = 128;
815+
$VMSize = "Standard_DS2_v2";
816+
$vmname2 = "2" + $vmname;
817+
818+
819+
# Gallery variables
820+
$resourceGroup = $rgname
821+
$galleryName = 'gl' + $rgname
822+
$definitionName = 'def' + $rgname
823+
$skuDetails = @{
824+
Publisher = 'test'
825+
Offer = 'test'
826+
Sku = 'test'
827+
}
828+
$osType = 'Windows'
829+
$osState = 'Specialized'
830+
[bool]$trustedLaunch = $false
831+
$storageAccountSku = 'Standard_LRS'
832+
$hyperVGeneration = 'v1'
833+
834+
# create new VM
835+
$vm = New-AzVM -ResourceGroupName $rgname -Location $loc -Name $vmname -Credential $cred -SecurityType "Standard" -DomainNameLabel $domainNameLabel;
836+
start-sleep -seconds 300
837+
838+
# Setup Image Gallery
839+
New-AzGallery -ResourceGroupName $rgname -Name $galleryName -location $location -ErrorAction 'Stop' | Out-Null;
840+
841+
# Setup Image Definition
842+
$paramNewAzImageDef = @{
843+
ResourceGroupName = $rgname
844+
GalleryName = $galleryName
845+
Name = $definitionName
846+
Publisher = $skuDetails.Publisher
847+
Offer = $skuDetails.Offer
848+
Sku = $skuDetails.Sku
849+
Location = $location
850+
OSState = $osState
851+
OsType = $osType
852+
HyperVGeneration = $hyperVGeneration
853+
ErrorAction = 'Stop'
854+
}
855+
856+
New-AzGalleryImageDefinition @paramNewAzImageDef;
857+
858+
# Setup Image Version
859+
$imageVersionName = "1.0.0";
860+
$paramNewAzImageVer = @{
861+
ResourceGroupName = $rgname
862+
GalleryName = $galleryName
863+
GalleryImageDefinitionName = $definitionName
864+
Name = $imageVersionName
865+
Location = $location
866+
SourceImageVMId = $vm.Id
867+
ErrorAction = 'Stop'
868+
StorageAccountType = $storageAccountSku
869+
}
870+
$galversion = New-AzGalleryImageVersion @paramNewAzImageVer;
871+
872+
# Assert VMId in version was set to the vm.Id value and was created.
873+
Assert-AreEqual $galversion.StorageProfile.Source.VirtualMachineId $vm.Id;
874+
}
875+
finally
876+
{
877+
# Cleanup
878+
Clean-ResourceGroup $rgname;
879+
}
880+
}

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7442,7 +7442,6 @@ function Test-VMTLWithGallerySourceImage
74427442
}
74437443
}
74447444

7445-
74467445
<#
74477446
.SYNOPSIS
74487447
Testing Capacity Reservation Sharing profile parameter
@@ -7488,4 +7487,4 @@ function Test-CapacityReservationSharingProfile
74887487
# Cleanup
74897488
Clean-ResourceGroup $rgname;
74907489
}
7491-
}
7490+
}

src/Compute/Compute.Test/SessionRecords/Microsoft.Azure.Commands.Compute.Test.ScenarioTests.GalleryTests/TestGalleryVersionWithSourceImageVMId.json

Lines changed: 4280 additions & 0 deletions
Large diffs are not rendered by default.

src/Compute/Compute/ChangeLog.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
`New-AzVM` and `New-AzVmss` will default to the image `Windows Server 2022 Azure Edition` instead of `Windows 2016 Datacenter` by default.
3232
`Get-AzVmss` will no longer allow empty values to `ResourceGroupName` and `VMScaleSetName` to avoid a bug where it will just return nothing.
3333
* Added a new parameter `-SharingProfile` to `New-AzCapacityReservationGroup` and `Update-AzCapacityReservationGroup`.
34+
* Added the new parameter `SourceImageVMId` to the `New-AzGalleryImageVersion` cmdlet. Also added some error messages for this new parameter and the existing parameter `SourceImageId`.
3435

3536
## Version 7.1.2
3637
* Fixed `New-AzVM` when a source image is specified to avoid an error on the `Version` value.

src/Compute/Compute/Generated/GalleryImageVersion/GalleryImageVersionCreateOrUpdateMethod.cs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,33 @@ public override void ExecuteCmdlet()
260260
}
261261
}
262262

263+
if (this.IsParameterBound(c => c.SourceImageVMId))
264+
{
265+
if (galleryImageVersion.StorageProfile == null)
266+
{
267+
galleryImageVersion.StorageProfile = new GalleryImageVersionStorageProfile();
268+
}
269+
if (galleryImageVersion.StorageProfile.Source == null)
270+
{
271+
galleryImageVersion.StorageProfile.Source = new GalleryArtifactVersionFullSource();
272+
}
273+
galleryImageVersion.StorageProfile.Source.VirtualMachineId = this.SourceImageVMId;
274+
275+
var resourceId = ResourceId.TryParse(this.SourceImageVMId);
276+
277+
if (string.Equals("galleries", resourceId?.ResourceType?.Provider, StringComparison.OrdinalIgnoreCase)
278+
&& !string.Equals(this.ComputeClient?.ComputeManagementClient?.SubscriptionId, resourceId?.SubscriptionId, StringComparison.OrdinalIgnoreCase))
279+
{
280+
List<string> resourceIds = new List<string>();
281+
resourceIds.Add(this.SourceImageVMId);
282+
var auxHeaderDictionary = GetAuxilaryAuthHeaderFromResourceIds(resourceIds);
283+
if (auxHeaderDictionary != null && auxHeaderDictionary.Count > 0)
284+
{
285+
auxAuthHeader = new Dictionary<string, List<string>>(auxHeaderDictionary);
286+
}
287+
}
288+
}
289+
263290
GalleryImageVersion result;
264291
if (auxAuthHeader != null)
265292
{
@@ -355,6 +382,12 @@ public override void ExecuteCmdlet()
355382
ValueFromPipelineByPropertyName = true)]
356383
public string SourceImageId { get; set; }
357384

385+
[Parameter(
386+
Mandatory = false,
387+
ValueFromPipelineByPropertyName = true,
388+
HelpMessage = "The resource Id of the source virtual machine. Only required when capturing a virtual machine to source this Gallery Image Version.")]
389+
public string SourceImageVMId { get; set; }
390+
358391
[Parameter(
359392
Mandatory = false,
360393
ValueFromPipelineByPropertyName = true)]

src/Compute/Compute/help/New-AzGalleryImageVersion.md

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ New-AzGalleryImageVersion [-ResourceGroupName] <String> [-GalleryName] <String>
1818
[-GalleryImageDefinitionName] <String> [-Name] <String> [-AsJob] -Location <String>
1919
[-DataDiskImage <GalleryDataDiskImage[]>] [-OSDiskImage <GalleryOSDiskImage>]
2020
[-PublishingProfileEndOfLifeDate <DateTime>] [-PublishingProfileExcludeFromLatest] [-ReplicaCount <Int32>]
21-
[-SourceImageId <String>] [-StorageAccountType <String>] [-Tag <Hashtable>] [-TargetRegion <Hashtable[]>]
21+
[-SourceImageId <String>] [-StorageAccountType <String>] [-SourceImageVMId <String>] [-Tag <Hashtable>] [-TargetRegion <Hashtable[]>]
2222
[-TargetExtendedLocation <Hashtable[]>] [-DefaultProfile <IAzureContextContainer>]
2323
[-WhatIf] [-Confirm] [<CommonParameters>]
2424
```
@@ -509,6 +509,22 @@ Accept pipeline input: True (ByPropertyName)
509509
Accept wildcard characters: False
510510
```
511511
512+
### -SourceImageVMId
513+
514+
The resource Id of the source virtual machine. Only required when capturing a virtual machine to source this Gallery Image Version.
515+
516+
```yaml
517+
Type: System.String
518+
Parameter Sets: (All)
519+
Aliases:
520+
521+
Required: False
522+
Position: Named
523+
Default value: None
524+
Accept pipeline input: True (ByPropertyName)
525+
Accept wildcard characters: False
526+
```
527+
512528
### -Tag
513529
514530
Resource tags

0 commit comments

Comments
 (0)