Skip to content

Commit

Permalink
New-AzVmssConfig, Update-AzVmss, add OS Image Scheduled Events (#21268)
Browse files Browse the repository at this point in the history
* dev and start test

* test

* test done

* enabled edit

* help doc

* empty commit

* cleanup

* fix statics

* changelog

* Update ChangeLog.md

* Update New-AzVmssConfig.md

---------

Co-authored-by: Jin Lei <[email protected]>
  • Loading branch information
Sandido and msJinLei authored Apr 26, 2023
1 parent bf25276 commit 82d9d13
Show file tree
Hide file tree
Showing 8 changed files with 3,659 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -311,5 +311,12 @@ public void TestVirtualMachineScaleSetConfidentialVMSecurityTypeDiskWithVMGuestS
{
TestRunner.RunTestScript("Test-VirtualMachineScaleSetConfidentialVMDiskWithVMGuestStateCMK");
}

[Fact]
[Trait(Category.AcceptanceType, Category.CheckIn)]
public void TestVirtualMachineScaleSetOSImageScheduledEvents()
{
TestRunner.RunTestScript("Test-VirtualMachineScaleSetOSImageScheduledEvents");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4279,4 +4279,87 @@ function Test-VirtualMachineScaleSetConfidentialVMDiskWithVMGuestStateCMK
# Cleanup
Clean-ResourceGroup $rgname;
}
}
}

<#
.SYNOPSIS
Vmss Os Image Scheduled Events tests
#>
function Test-VirtualMachineScaleSetOSImageScheduledEvents
{

# Setup
$rgname = Get-ComputeTestResourceName;
$loc = Get-ComputeVMLocation;

try
{
New-AzResourceGroup -Name $rgname -Location $loc -Force;

# Setup variables
$publisher = "MicrosoftWindowsServer";
$offer = "WindowsServer";
$imgSku = "2019-Datacenter";
$version = "latest";
$vmssName = 'vmss' + $rgname;
$vmssSku = "Standard_D2s_v3";
$vmssname = "vmss" + $rgname;
$domainNameLabel = "d" + $rgname;
$username = "admin01";
$password = Get-PasswordForVM;
$securePassword = $password | ConvertTo-SecureString -AsPlainText -Force

$credential = New-Object System.Management.Automation.PSCredential ($username, $securePassword);

# SRP
$stoname = 'sto' + $rgname;
$stotype = 'Standard_GRS';
New-AzStorageAccount -ResourceGroupName $rgname -Name $stoname -Location $loc -Type $stotype;
$stoaccount = Get-AzStorageAccount -ResourceGroupName $rgname -Name $stoname;

# NRP
$subnet = New-AzVirtualNetworkSubnetConfig -Name ('subnet' + $rgname) -AddressPrefix "10.0.0.0/24";
$vnet = New-AzVirtualNetwork -Force -Name ('vnet' + $rgname) -ResourceGroupName $rgname -Location $loc -AddressPrefix "10.0.0.0/16" -Subnet $subnet;
$vnet = Get-AzVirtualNetwork -Name ('vnet' + $rgname) -ResourceGroupName $rgname;
$subnetId = $vnet.Subnets[0].Id;

# Create VMSS with managed disk
$ipCfg = New-AzVmssIPConfig -Name 'test' -SubnetId $subnetId;
$vmss = New-AzVmssConfig -Location $loc -SkuCapacity 2 -SkuName $vmssSku -OSImageScheduledEventEnabled -OSImageScheduledEventNotBeforeTimeoutInMinutes "PT15M" -UpgradePolicy "Automatic" `
| Add-AzVmssNetworkInterfaceConfiguration -Name 'test' -Primary $true -IPConfiguration $ipCfg `
| Set-AzVmssOSProfile -ComputerNamePrefix 'test' -AdminUsername $username -AdminPassword $password `
| Set-AzVmssStorageProfile -OsDiskCreateOption 'FromImage' -OsDiskCaching 'None' `
-ImageReferenceOffer $offer -ImageReferenceSku $imgSku -ImageReferenceVersion $version `
-ImageReferencePublisher $publisher;

$result = New-AzVmss -ResourceGroupName $rgname -Name $vmssName -VirtualMachineScaleSet $vmss;

$vmss = Get-AzVmss -ResourceGroupName $rgname -VMScaleSetName $vmssName;
Assert-True {$vmss.VirtualMachineProfile.ScheduledEventsProfile.OsImageNotificationProfile.Enable};
Assert-AreEqual 'PT15M' $vmss.VirtualMachineProfile.ScheduledEventsProfile.OsImageNotificationProfile.NotBeforeTimeout;


# Update-AzVmss test
$vmssName2 = 'vs2' + $rgname;
$vmss2 = New-AzVmssConfig -Location $loc -SkuCapacity 2 -SkuName $vmssSku -UpgradePolicyMode "Automatic" `
| Add-AzVmssNetworkInterfaceConfiguration -Name 'test2' -Primary $true -IPConfiguration $ipCfg `
| Set-AzVmssOSProfile -ComputerNamePrefix 'test2' -AdminUsername $username -AdminPassword $password `
| Set-AzVmssStorageProfile -OsDiskCreateOption 'FromImage' -OsDiskCaching 'None' `
-ImageReferenceOffer $offer -ImageReferenceSku $imgSku -ImageReferenceVersion $version `
-ImageReferencePublisher $publisher;
$result = New-AzVmss -ResourceGroupName $rgname -Name $vmssName2 -VirtualMachineScaleSet $vmss2;
$vmss = Get-AzVmss -ResourceGroupName $rgname -VMScaleSetName $vmssName2;
Assert-False {$vmss.VirtualMachineProfile.ScheduledEventsProfile.OsImageNotificationProfile.Enable};
Assert-Null $vmss.VirtualMachineProfile.ScheduledEventsProfile.OsImageNotificationProfile.NotBeforeTimeout;

Update-AzVmss -VMScaleSetName $vmssName2 -ResourceGroupName $rgname -OSImageScheduledEventEnabled -OSImageScheduledEventNotBeforeTimeoutInMinutes "PT15M";
$vmss = Get-AzVmss -ResourceGroupName $rgname -VMScaleSetName $vmssName2;
Assert-True {$vmss.VirtualMachineProfile.ScheduledEventsProfile.OsImageNotificationProfile.Enable};
Assert-AreEqual 'PT15M' $vmss.VirtualMachineProfile.ScheduledEventsProfile.OsImageNotificationProfile.NotBeforeTimeout;
}
finally
{
# Cleanup
Clean-ResourceGroup $rgname;
}
}

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions src/Compute/Compute/ChangeLog.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
-->
## Upcoming Release
* Added new switch parameter `OSImageScheduledEventEnabled` and string parameter `OSImageScheduledEventNotBeforeTimeoutInMinutes` to the cmdlets `New-AzVmssConfig` and `Update-AzVmss`.

## Version 5.7.1
* Added a breaking change warning to the `Get-AzVM` cmdlet to show that the `NextLink` parameter and parameter set will be removed in June 2023. The parameter has been non-functional for a long time.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,16 @@ public partial class NewAzureRmVmssConfigCommand : Microsoft.Azure.Commands.Reso
HelpMessage = "Specified the shared gallery image unique id for vm deployment. This can be fetched from shared gallery image GET call.")]
public string SharedGalleryImageId { get; set; }

[Parameter(
Mandatory = false,
HelpMessage = "Specifies whether the OS Image Scheduled event is enabled or disabled.")]
public SwitchParameter OSImageScheduledEventEnabled { get; set; }

[Parameter(
Mandatory = false,
HelpMessage = "The length of time a virtual machine being reimaged or having its OS upgraded will have to potentially approve the OS Image Scheduled Event before the event is auto approved (timed out). The configuration is specified in ISO 8601 format, with the value set to 15 minutes (PT15M).")]
public string OSImageScheduledEventNotBeforeTimeoutInMinutes { get; set; }

protected override void ProcessRecord()
{
if (ShouldProcess("VirtualMachineScaleSet", "New"))
Expand Down Expand Up @@ -793,6 +803,40 @@ private void Run()
vVirtualMachineProfile.StorageProfile.ImageReference.SharedGalleryImageId = this.SharedGalleryImageId;
}

if (this.IsParameterBound(c => c.OSImageScheduledEventEnabled))
{
if (vVirtualMachineProfile == null)
{
vVirtualMachineProfile = new PSVirtualMachineScaleSetVMProfile();
}
if (vVirtualMachineProfile.ScheduledEventsProfile == null)
{
vVirtualMachineProfile.ScheduledEventsProfile = new ScheduledEventsProfile();
}
if (vVirtualMachineProfile.ScheduledEventsProfile.OsImageNotificationProfile == null)
{
vVirtualMachineProfile.ScheduledEventsProfile.OsImageNotificationProfile = new OSImageNotificationProfile();
}
vVirtualMachineProfile.ScheduledEventsProfile.OsImageNotificationProfile.Enable = this.OSImageScheduledEventEnabled;
}

if (this.IsParameterBound(c => c.OSImageScheduledEventNotBeforeTimeoutInMinutes))
{
if (vVirtualMachineProfile == null)
{
vVirtualMachineProfile = new PSVirtualMachineScaleSetVMProfile();
}
if (vVirtualMachineProfile.ScheduledEventsProfile == null)
{
vVirtualMachineProfile.ScheduledEventsProfile = new ScheduledEventsProfile();
}
if (vVirtualMachineProfile.ScheduledEventsProfile.OsImageNotificationProfile == null)
{
vVirtualMachineProfile.ScheduledEventsProfile.OsImageNotificationProfile = new OSImageNotificationProfile();
}
vVirtualMachineProfile.ScheduledEventsProfile.OsImageNotificationProfile.NotBeforeTimeout = this.OSImageScheduledEventNotBeforeTimeoutInMinutes;
}

var vVirtualMachineScaleSet = new PSVirtualMachineScaleSet
{
Overprovision = this.IsParameterBound(c => c.Overprovision) ? this.Overprovision : (bool?)null,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -376,6 +376,16 @@ public override void ExecuteCmdlet()
HelpMessage = "Specified the shared gallery image unique id for vm deployment. This can be fetched from shared gallery image GET call.")]
public string SharedGalleryImageId { get; set; }

[Parameter(
Mandatory = false,
HelpMessage = "Specifies whether the OS Image Scheduled event is enabled or disabled.")]
public SwitchParameter OSImageScheduledEventEnabled { get; set; }

[Parameter(
Mandatory = false,
HelpMessage = "The length of time a virtual machine being reimaged or having its OS upgraded will have to potentially approve the OS Image Scheduled Event before the event is auto approved (timed out). The configuration is specified in ISO 8601 format, with the value set to 15 minutes (PT15M).")]
public string OSImageScheduledEventNotBeforeTimeoutInMinutes { get; set; }

private void BuildPatchObject()
{
if (this.IsParameterBound(c => c.AutomaticOSUpgrade))
Expand Down Expand Up @@ -1227,6 +1237,48 @@ private void BuildPatchObject()
this.VirtualMachineScaleSet.VirtualMachineProfile.UserData = this.UserData;
}

if (this.IsParameterBound(c => c.OSImageScheduledEventEnabled))
{
if (this.VirtualMachineScaleSetUpdate == null)
{
this.VirtualMachineScaleSetUpdate = new VirtualMachineScaleSetUpdate();
}
if (this.VirtualMachineScaleSetUpdate.VirtualMachineProfile == null)
{
this.VirtualMachineScaleSetUpdate.VirtualMachineProfile = new VirtualMachineScaleSetUpdateVMProfile();
}
if (this.VirtualMachineScaleSetUpdate.VirtualMachineProfile.ScheduledEventsProfile == null)
{
this.VirtualMachineScaleSetUpdate.VirtualMachineProfile.ScheduledEventsProfile = new ScheduledEventsProfile();
}
if (this.VirtualMachineScaleSetUpdate.VirtualMachineProfile.ScheduledEventsProfile.OsImageNotificationProfile == null)
{
this.VirtualMachineScaleSetUpdate.VirtualMachineProfile.ScheduledEventsProfile.OsImageNotificationProfile = new OSImageNotificationProfile();
}
this.VirtualMachineScaleSetUpdate.VirtualMachineProfile.ScheduledEventsProfile.OsImageNotificationProfile.Enable = this.OSImageScheduledEventEnabled;
}

if (this.IsParameterBound(c => c.OSImageScheduledEventNotBeforeTimeoutInMinutes))
{
if (this.VirtualMachineScaleSetUpdate == null)
{
this.VirtualMachineScaleSetUpdate = new VirtualMachineScaleSetUpdate();
}
if (this.VirtualMachineScaleSetUpdate.VirtualMachineProfile == null)
{
this.VirtualMachineScaleSetUpdate.VirtualMachineProfile = new VirtualMachineScaleSetUpdateVMProfile();
}
if (this.VirtualMachineScaleSetUpdate.VirtualMachineProfile.ScheduledEventsProfile == null)
{
this.VirtualMachineScaleSetUpdate.VirtualMachineProfile.ScheduledEventsProfile = new ScheduledEventsProfile();
}
if (this.VirtualMachineScaleSetUpdate.VirtualMachineProfile.ScheduledEventsProfile.OsImageNotificationProfile == null)
{
this.VirtualMachineScaleSetUpdate.VirtualMachineProfile.ScheduledEventsProfile.OsImageNotificationProfile = new OSImageNotificationProfile();
}
this.VirtualMachineScaleSetUpdate.VirtualMachineProfile.ScheduledEventsProfile.OsImageNotificationProfile.NotBeforeTimeout = this.OSImageScheduledEventNotBeforeTimeoutInMinutes;
}

if (this.VirtualMachineScaleSetUpdate != null
&& this.VirtualMachineScaleSetUpdate.VirtualMachineProfile != null
&& this.VirtualMachineScaleSetUpdate.VirtualMachineProfile.OsProfile != null
Expand Down
84 changes: 81 additions & 3 deletions src/Compute/Compute/help/New-AzVmssConfig.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,9 @@ New-AzVmssConfig [[-Overprovision] <Boolean>] [[-Location] <String>] [-EdgeZone
[-ScaleInPolicy <String[]>] [-EncryptionAtHost] [-OrchestrationMode <String>]
[-CapacityReservationGroupId <String>] [-UserData <String>] [-AutomaticRepairAction <String>]
[-BaseRegularPriorityCount <Int32>] [-RegularPriorityPercentage <Int32>] [-ImageReferenceId <String>]
[-SharedGalleryImageId <String>] [-DefaultProfile <IAzureContextContainer>] [-WhatIf] [-Confirm]
[<CommonParameters>]
[-SharedGalleryImageId <String>] [-OSImageScheduledEventEnabled]
[-OSImageScheduledEventNotBeforeTimeoutInMinutes <String>] [-DefaultProfile <IAzureContextContainer>]
[-WhatIf] [-Confirm] [<CommonParameters>]
```

### ExplicitIdentityParameterSet
Expand All @@ -51,7 +52,8 @@ New-AzVmssConfig [[-Overprovision] <Boolean>] [[-Location] <String>] [-EdgeZone
[-ScaleInPolicy <String[]>] -IdentityType <ResourceIdentityType> [-IdentityId <String[]>] [-EncryptionAtHost]
[-OrchestrationMode <String>] [-CapacityReservationGroupId <String>] [-UserData <String>]
[-AutomaticRepairAction <String>] [-BaseRegularPriorityCount <Int32>] [-RegularPriorityPercentage <Int32>]
[-ImageReferenceId <String>] [-SharedGalleryImageId <String>] [-DefaultProfile <IAzureContextContainer>]
[-ImageReferenceId <String>] [-SharedGalleryImageId <String>] [-OSImageScheduledEventEnabled]
[-OSImageScheduledEventNotBeforeTimeoutInMinutes <String>] [-DefaultProfile <IAzureContextContainer>]
[-WhatIf] [-Confirm] [<CommonParameters>]
```

Expand Down Expand Up @@ -111,6 +113,52 @@ Creates a VMSS configuration object. (autogenerated)
New-AzVmssConfig -Location <String> -SkuCapacity 2 -SkuName 'Standard_A0' -UpgradePolicyMode Automatic -IdentityType SystemAssigned;
```

### Example 4: Create a VMSS with the OS Image Scheduled Events enabled
```powershell
$publisher = "MicrosoftWindowsServer";
$offer = "WindowsServer";
$imgSku = "2019-Datacenter";
$version = "latest";
$vmssName = 'vmss' + $rgname;
$vmssSku = "Standard_D2s_v3";
$vmssname = "vmss" + $rgname;
$domainNameLabel = "d" + $rgname;
$username = <Username>;
$password = <Password>;
$securePassword = $password | ConvertTo-SecureString -AsPlainText -Force
$credential = New-Object System.Management.Automation.PSCredential ($username, $securePassword);
# SRP
$stoname = 'sto' + $rgname;
$stotype = 'Standard_GRS';
New-AzStorageAccount -ResourceGroupName $rgname -Name $stoname -Location $loc -Type $stotype;
$stoaccount = Get-AzStorageAccount -ResourceGroupName $rgname -Name $stoname;
# NRP
$subnet = New-AzVirtualNetworkSubnetConfig -Name ('subnet' + $rgname) -AddressPrefix "10.0.0.0/24";
$vnet = New-AzVirtualNetwork -Force -Name ('vnet' + $rgname) -ResourceGroupName $rgname -Location $loc -AddressPrefix "10.0.0.0/16" -Subnet $subnet;
$vnet = Get-AzVirtualNetwork -Name ('vnet' + $rgname) -ResourceGroupName $rgname;
$subnetId = $vnet.Subnets[0].Id;
# Create VMSS with managed disk
$timeoutValue = 'PT15M';
$ipCfg = New-AzVmssIpConfig -Name 'test' -SubnetId $subnetId;
$vmss = New-AzVmssConfig -Location $loc -SkuCapacity 2 -SkuName $vmssSku -OSImageScheduledEventEnabled -OSImageScheduledEventNotBeforeTimeoutInMinutes $timeoutValue -UpgradePolicyMode "Automatic" `
| Add-AzVmssNetworkInterfaceConfiguration -Name 'test' -Primary $true -IPConfiguration $ipCfg `
| Set-AzVmssOsProfile -ComputerNamePrefix 'test' -AdminUsername $username -AdminPassword $password `
| Set-AzVmssStorageProfile -OsDiskCreateOption 'FromImage' -OsDiskCaching 'None' `
-ImageReferenceOffer $offer -ImageReferenceSku $imgSku -ImageReferenceVersion $version `
-ImageReferencePublisher $publisher;
$result = New-AzVmss -ResourceGroupName $rgname -Name $vmssName -VirtualMachineScaleSet $vmss;
$vmss = Get-AzVmss -ResourceGroupName $rgname -VMScaleSetName $vmssName;
# $vmss.VirtualMachineProfile.ScheduledEventsProfile.OsImageNotificationProfile.Enable is the OSImageScheduledEventEnabled flag.
# $vmss.VirtualMachineProfile.ScheduledEventsProfile.OsImageNotificationProfile.NotBeforeTimeout is the timeout value 'PT15M'.
```

## PARAMETERS

### -AutomaticRepairAction
Expand Down Expand Up @@ -486,6 +534,36 @@ Accept pipeline input: True (ByPropertyName)
Accept wildcard characters: False
```
### -OSImageScheduledEventEnabled
Specifies whether the OS Image Scheduled event is enabled or disabled.
```yaml
Type: System.Management.Automation.SwitchParameter
Parameter Sets: (All)
Aliases:

Required: False
Position: Named
Default value: None
Accept pipeline input: False
Accept wildcard characters: False
```
### -OSImageScheduledEventNotBeforeTimeoutInMinutes
The length of time a virtual machine being reimaged or having its OS upgraded will have to potentially approve the OS Image Scheduled Event before the event is auto approved (timed out). The configuration is specified in ISO 8601 format, with the value set to 15 minutes (PT15M).
```yaml
Type: System.String
Parameter Sets: (All)
Aliases:

Required: False
Position: Named
Default value: None
Accept pipeline input: False
Accept wildcard characters: False
```
### -OsProfile
Specifies the operating system profile object that contains the operating system properties for the VMSS configuration.
You can use the **Set-AzVmssOsProfile** cmdlet to set this object.
Expand Down
Loading

0 comments on commit 82d9d13

Please sign in to comment.