azure-powershell icon indicating copy to clipboard operation
azure-powershell copied to clipboard

Update NewAzureVMCommand.cs

Open mfortin opened this issue 1 year ago • 15 comments

Description

Adding validation for SharedGalleryImageId and CommunityGalleryImageId in New-AzVM cmdlet

Mandatory Checklist

  • Please choose the target release of Azure PowerShell

    • [X] General release
    • [ ] Public preview
    • [ ] Private preview
    • [ ] Engineering build
    • [ ] N/A
  • [X] Check this box to confirm: I have read the Submitting Changes section of CONTRIBUTING.md and reviewed the following information:

  • SHOULD update ChangeLog.md file(s) appropriately
    • For SDK-based development mode, update src/{{SERVICE}}/{{SERVICE}}/ChangeLog.md.
      • A snippet outlining the change(s) made in the PR should be written under the ## Upcoming Release header in the past tense.
    • For autorest-based development mode, include the changelog in the PR description.
    • Should not change ChangeLog.md if no new release is required, such as fixing test case only.
  • SHOULD regenerate markdown help files if there is cmdlet API change. Instruction
  • SHOULD have proper test coverage for changes in pull request.
  • SHOULD NOT adjust version of module manually in pull request

mfortin avatar Mar 20 '24 00:03 mfortin

️✔️Az.Accounts
️✔️Build
️✔️PowerShell Core - Windows
️✔️Windows PowerShell - Windows
️✔️Az.Compute
️✔️Build
️✔️PowerShell Core - Windows
️✔️Windows PowerShell - Windows
️✔️Breaking Change Check
️✔️PowerShell Core - Windows
️✔️Windows PowerShell - Windows
️✔️Signature Check
️✔️PowerShell Core - Windows
️✔️Windows PowerShell - Windows
️✔️Help File Existence Check
️✔️PowerShell Core - Windows
️✔️Windows PowerShell - Windows
️✔️File Change Check
️✔️PowerShell Core - Windows
️✔️Windows PowerShell - Windows
️✔️UX Metadata Check
️✔️PowerShell Core - Windows
️✔️Windows PowerShell - Windows
️✔️Test
️✔️ - Linux
️✔️ - MacOS
️✔️PowerShell Core - Windows
️✔️Windows PowerShell - Windows
️✔️Az.KeyVault
️✔️Build
️✔️PowerShell Core - Windows
️✔️Windows PowerShell - Windows
️✔️Az.ManagedServiceIdentity
️✔️Build
️✔️PowerShell Core - Windows
️✔️Windows PowerShell - Windows
️✔️Az.Monitor
️✔️Build
️✔️PowerShell Core - Windows
️✔️Windows PowerShell - Windows
️✔️Az.Network
️✔️Build
️✔️PowerShell Core - Windows
️✔️Windows PowerShell - Windows
️✔️Test
️✔️ - Linux
️✔️ - MacOS
️✔️PowerShell Core - Windows
️✔️Windows PowerShell - Windows
️✔️Az.OperationalInsights
️✔️Build
️✔️PowerShell Core - Windows
️✔️Windows PowerShell - Windows
️✔️Az.PrivateDns
️✔️Build
️✔️PowerShell Core - Windows
️✔️Windows PowerShell - Windows
⚠️Az.RecoveryServices
️✔️Build
️✔️PowerShell Core - Windows
️✔️Windows PowerShell - Windows
⚠️Test
⚠️ - Linux
Type Title Current Coverage Last Coverage Description
⚠️ Test Coverage Less Than 80% 62.50 % 66.67% Test coverage cannot be lower than the number of the last release.
⚠️ - MacOS
Type Title Current Coverage Last Coverage Description
⚠️ Test Coverage Less Than 80% 62.50% 66.67% Test coverage cannot be lower than the number of the last release.
⚠️PowerShell Core - Windows
Type Title Current Coverage Last Coverage Description
⚠️ Test Coverage Less Than 80% 62.50% 66.67% Test coverage cannot be lower than the number of the last release.
⚠️Windows PowerShell - Windows
Type Title Current Coverage Last Coverage Description
⚠️ Test Coverage Less Than 80% 62.50% 66.67% Test coverage cannot be lower than the number of the last release.
️✔️Az.Security
️✔️Build
️✔️PowerShell Core - Windows
️✔️Windows PowerShell - Windows
️✔️Test
️✔️ - Linux
️✔️ - MacOS
️✔️PowerShell Core - Windows
️✔️Windows PowerShell - Windows
️✔️Az.Sql
️✔️Build
️✔️PowerShell Core - Windows
️✔️Windows PowerShell - Windows
️✔️Az.Ssh
️✔️Build
️✔️PowerShell Core - Windows
️✔️Windows PowerShell - Windows
️✔️Test
️✔️ - Linux
️✔️ - MacOS
️✔️PowerShell Core - Windows
️✔️Windows PowerShell - Windows
️✔️Az.Storage
️✔️Build
️✔️PowerShell Core - Windows
️✔️Windows PowerShell - Windows

@microsoft-github-policy-service agree

mfortin avatar Mar 20 '24 12:03 mfortin

More context to this PR.

Before this PR:

> $VmConfig = New-AzVMConfig -VMName testvm -VMSize Standard_B2s -SharedGalleryImageId /SharedGalleries/<subscription>-<gallery>/Images/<definition/Versions/<version> -Location WestUS2
Write-Output $VmConfig.StorageProfile.ImageReference

Publisher               :
Offer                   :
Sku                     :
Version                 :
ExactVersion            :
SharedGalleryImageId    : /SharedGalleries/<sub>-<gallery>/Images/<definition/Versions/<version>
CommunityGalleryImageId :
Id                      : 

# And then launching a Virtual Machine
> New-AzVM -VM $VmConfig -ResourceGroupName $ResourceGroupName -Location $VmRegion

System.NullReferenceException: Object reference not set to an instance of an object.
   at Microsoft.Azure.Commands.Compute.NewAzureVMCommand.retrieveImageVersion(String publisher, String offer, String sku, String version)
   at Microsoft.Azure.Commands.Compute.NewAzureVMCommand.retrieveSpecificImageFromNotId()
   at Microsoft.Azure.Commands.Compute.NewAzureVMCommand.DefaultExecuteCmdlet()
   at Microsoft.Azure.Commands.Compute.NewAzureVMCommand.ExecuteCmdlet()
   at Microsoft.WindowsAzure.Commands.Utilities.Common.AzurePSCmdlet.ProcessRecord()
System.Management.Automation.InvocationInfo

Now, after this PR:

$VmConfig = New-AzVMConfig -VMName testvm -VMSize Standard_B2s -SharedGalleryImageId /SharedGalleries/<subscription>-<gallery>/Images/<definition/Versions/<version> -Location WestUS2
Write-Output $VmConfig.StorageProfile.ImageReference

Publisher               :
Offer                   :
Sku                     :
Version                 :
ExactVersion            :
SharedGalleryImageId    : /SharedGalleries/<sub>-<gallery>/Images/<definition/Versions/<version>
CommunityGalleryImageId :
Id                      : 

# And then launching a Virtual Machine
> New-AzVM -VM $VmConfig -ResourceGroupName $ResourceGroupName -Location $VmRegion
# Success!

mfortin avatar Mar 20 '24 14:03 mfortin

@bilaakpan-ms @Sandido @haagha @grizzlytheodore Would you mind reviewing this simple PR ?

mfortin avatar Mar 30 '24 17:03 mfortin

Up @bilaakpan-ms @Sandido @haagha @grizzlytheodore

mfortin avatar Apr 12 '24 01:04 mfortin

@bilaakpan-ms @Sandido @haagha @grizzlytheodore up

mfortin avatar Apr 24 '24 23:04 mfortin

/azp run azure-powershell - security-tools

YanaXu avatar Apr 25 '24 07:04 YanaXu

Azure Pipelines successfully started running 1 pipeline(s).

azure-pipelines[bot] avatar Apr 25 '24 07:04 azure-pipelines[bot]

Will look into this next week.

Sandido avatar Apr 26 '24 21:04 Sandido

@Sandido have you had the chance to have a look by any chance ?

mfortin avatar May 01 '24 23:05 mfortin

@mfortin , looking at this now, running some tests. In the future, and if you can now, share how you made the SharedGallery.

Sandido avatar May 03 '24 18:05 Sandido

Hi @Sandido, yes absolutely. This specific gallery was created with the Direct Share feature https://learn.microsoft.com/en-ca/azure/virtual-machines/share-gallery-direct?tabs=portaldirect#prerequisites

mfortin avatar May 03 '24 19:05 mfortin

az sig create \
   --gallery-name myGallery \
   --permissions groups \
   --resource-group myResourceGroup

mfortin avatar May 03 '24 19:05 mfortin

@mfortin do you expect the SharedGalleryImageId to be in this exact format as seen under the resource Properties? /SharedGalleries/-/Images/<definition/Versions/

Sandido avatar May 03 '24 19:05 Sandido

This should work from any shared gallery, even if using the RBAC.

> Get-AzGalleryImageVersion -GalleryUniqueName $GalleryUniqueName -GalleryImageDefinitionName $GalleryImageDefinitionName -Location $Location -GalleryImageVersionName latest

Name          : 2024.04.121845
Location      : $Location
UniqueId      : /SharedGalleries/<subscription uuid>-$GalleryUniqueName/Images/$GalleryImageDefinitionName/Versions/2024.04.121845
PublishedDate : 2024-04-12 6:45:07 PM
EndOfLifeDate : 2024-07-11 6:45:06 PM

It will always return the UniqueID as

/SharedGalleries/<subscription uuid>-<gallery name uppercase>/Images/<image definition name>/Versions/<version name>

mfortin avatar May 03 '24 20:05 mfortin

PR is nearly ready for merge, just a couple of tweaks.

Sandido avatar May 09 '24 17:05 Sandido

@sandido Updated as per your review.

mfortin avatar May 10 '24 00:05 mfortin

/azp run azure-powershell - security-tools

YanaXu avatar May 10 '24 01:05 YanaXu

Azure Pipelines successfully started running 1 pipeline(s).

azure-pipelines[bot] avatar May 10 '24 01:05 azure-pipelines[bot]

/azp run azure-powershell - security-tools

YanaXu avatar May 10 '24 04:05 YanaXu

Azure Pipelines successfully started running 1 pipeline(s).

azure-pipelines[bot] avatar May 10 '24 04:05 azure-pipelines[bot]