Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Edit New-AzSnapshot so it checks for already existing duplicate resource before creation #13580

Merged
merged 7 commits into from
Dec 4, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions src/Compute/Compute.Test/ScenarioTests/DiskRPTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -107,5 +107,12 @@ public void TestGetDiskEncryptionSetAssociatedResource()
{
TestRunner.RunTestScript("Test-GetDiskEncryptionSetAssociatedResource");
}

[Fact]
[Trait(Category.AcceptanceType, Category.CheckIn)]
public void TestSnapshotDuplicateCreationFails()
{
TestRunner.RunTestScript("Test-SnapshotDuplicateCreationFails");
}
}
}
42 changes: 40 additions & 2 deletions src/Compute/Compute.Test/ScenarioTests/DiskRPTests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ function Test-Snapshot
$snapshotconfig.EncryptionSettingsCollection.Enabled = $false;
$snapshotconfig.EncryptionSettingsCollection.EncryptionSettings = $null;
$snapshotconfig.CreationData.ImageReference = $null;
$job = New-AzSnapshot -ResourceGroupName $rgname -SnapshotName $snapshotname -Snapshot $snapshotconfig -AsJob;
$job = Update-AzSnapshot -ResourceGroupName $rgname -SnapshotName $snapshotname -Snapshot $snapshotconfig -AsJob;
$result = $job | Wait-Job;
Assert-AreEqual "Completed" $result.State;

Expand Down Expand Up @@ -619,7 +619,7 @@ function Test-SnapshotEncrypt
Assert-AreEqual 0 $snapshotconfig.CreationData.ImageReference.Lun;

$snapshotconfig.CreationData.ImageReference = $null;
$job = New-AzSnapshot -ResourceGroupName $rgname -SnapshotName $snapshotname -Snapshot $snapshotconfig -AsJob;
$job = Update-AzSnapshot -ResourceGroupName $rgname -SnapshotName $snapshotname -Snapshot $snapshotconfig -AsJob;
$result = $job | Wait-Job;
Assert-AreEqual "Completed" $result.State;

Expand Down Expand Up @@ -1336,4 +1336,42 @@ function Test-DiskConfigTierSectorSizeReadOnly
# Cleanup
Clean-ResourceGroup $rgname
}
}

<#
.SYNOPSIS
Test the New-AzSnapshot cmdlet throws an error when attempting to create a snapshot with
the same name in the same resource group.
#>
function Test-SnapshotDuplicateCreationFails
{
# Setup
$rgname = Get-ComputeTestResourceName;
$loc = Get-ComputeVMLocation;

try
{
# Common
New-AzResourceGroup -Name $rgname -Location $loc -Force;
$snapshotName = "test1";

$snapshotconfig = New-AzSnapshotConfig -Location $loc -DiskSizeGB 5 -AccountType Standard_LRS -OsType Windows -CreateOption Empty;

$snapshot = New-AzSnapshot -ResourceGroupName $rgname -SnapshotName $snapshotName -Snapshot $snapshotconfig;
Assert-NotNull $snapshot;

# Assert duplicate snapshot fails to create.
Assert-ThrowsContains { $snapshot2 = New-AzSnapshot -ResourceGroupName $rgname -SnapshotName $snapshotName -Snapshot $snapshotconfig; } "Please use Update-AzSnapshot to update an existing Snapshot.";

# Assert update snapshot succeeds.
$snapshotconfig2 = New-AzSnapshotUpdateConfig -DiskSizeGB 10 -AccountType Standard_LRS -OsType Windows;
$job = Update-AzSnapshot -ResourceGroupName $rgname -SnapshotName $snapshotName -SnapshotUpdate $snapshotconfig2 -AsJob;
$result = $job | Wait-Job;
Assert-AreEqual "Completed" $result.State;
}
finally
{
# Cleanup
Clean-ResourceGroup $rgname
}
}
7 changes: 3 additions & 4 deletions src/Compute/Compute.Test/ScenarioTests/GalleryTests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -673,16 +673,15 @@ function Test-GalleryImageVersionDiskImage

$snapshotname1 = 'ossnapshot' + $rgname;
$snapshotconfig = New-AzSnapshotConfig -Location $loc -DiskSizeGB 5 -AccountType Standard_LRS -OsType Windows -CreateOption Empty -HyperVGeneration "V1";
$snapshot1 = New-AzSnapshot -ResourceGroupName $rgname -SnapshotName $snapshotname1 -Snapshot $snapshotconfig
$snapshot1 = Update-AzSnapshot -ResourceGroupName $rgname -SnapshotName $snapshotname1 -Snapshot $snapshotconfig

$snapshotname2 = 'data1snapshot' + $rgname;
$snapshotconfig = New-AzSnapshotConfig -Location $loc -DiskSizeGB 5 -AccountType Standard_LRS -OsType Windows -CreateOption Empty -HyperVGeneration "V1";
$snapshot2 = New-AzSnapshot -ResourceGroupName $rgname -SnapshotName $snapshotname2 -Snapshot $snapshotconfig
$snapshot2 = Update-AzSnapshot -ResourceGroupName $rgname -SnapshotName $snapshotname2 -Snapshot $snapshotconfig

$snapshotname3 = 'data2snapshot' + $rgname;
$snapshotconfig = New-AzSnapshotConfig -Location $loc -DiskSizeGB 5 -AccountType Standard_LRS -OsType Windows -CreateOption Empty -HyperVGeneration "V1";
$snapshot3 = New-AzSnapshot -ResourceGroupName $rgname -SnapshotName $snapshotname3 -Snapshot $snapshotconfig

$snapshot3 = Update-AzSnapshot -ResourceGroupName $rgname -SnapshotName $snapshotname3 -Snapshot $snapshotconfig

$targetRegions = @(@{Name='South Central US';ReplicaCount=1;StorageAccountType='Standard_LRS'},@{Name='East US';ReplicaCount=2},@{Name='Central US'});
$tag = @{test1 = "testval1"; test2 = "testval2" };
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3612,7 +3612,7 @@ function Test-VirtualMachineManagedDisk
# Create OS snapshot from the VM
$snapshotConfig = New-AzSnapshotConfig -SourceUri $vm.Storageprofile.OsDisk.ManagedDisk.Id -Location $loc -CreateOption Copy;
$snapshotname = "ossnapshot";
New-AzSnapshot -Snapshot $snapshotConfig -SnapshotName $snapshotname -ResourceGroupName $rgname;
Update-AzSnapshot -Snapshot $snapshotConfig -SnapshotName $snapshotname -ResourceGroupName $rgname;
$snapshot = Get-AzSnapshot -SnapshotName $snapshotname -ResourceGroupName $rgname;

Assert-NotNull $snapshot.Id;
Expand Down

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions src/Compute/Compute/ChangeLog.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
## Upcoming Release
* Edited Get-AzVm to filter by `-Name` prior to checking for throttling due to too many resources.
* New cmdlet `Start-AzVmssRollingExtensionUpgrade`.
* Edited `New-AzSnapshot` cmdlet to check for existing snapshot with the same name in the same resource group.
- Throws an error if a duplicate snapshot exists.

## Version 4.6.0
* Added `-VmssId` parameter to `New-AzVm`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,21 @@ public override void ExecuteCmdlet()
base.ExecuteCmdlet();
ExecuteClientAction(() =>
{
Snapshot existingResource;
try
{
existingResource = SnapshotsClient.Get(this.ResourceGroupName, this.SnapshotName);
}
catch
{
existingResource = null;
Sandido marked this conversation as resolved.
Show resolved Hide resolved
}

if (existingResource != null)
{
throw new Exception(string.Format("A Snapshot with name '{0}' in resource group '{1}' already exists. Please use Update-AzSnapshot to update an existing Snapshot.", this.SnapshotName, this.ResourceGroupName));
}

if (ShouldProcess(this.SnapshotName, VerbsCommon.New))
Sandido marked this conversation as resolved.
Show resolved Hide resolved
{
string resourceGroupName = this.ResourceGroupName;
Expand Down