Skip to content

NEW PR Powershell cmdlets for list/get/cancel managed server operation #11452

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

Merged
merged 12 commits into from
Apr 1, 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
4 changes: 3 additions & 1 deletion src/Accounts/Accounts/AzureRmAlias/Mappings.json
Original file line number Diff line number Diff line change
Expand Up @@ -2319,7 +2319,9 @@
"Set-AzSqlDatabaseServerAuditingPolicy": "Set-AzureRmSqlDatabaseServerAuditingPolicy",
"Use-AzSqlDatabaseServerAuditingPolicy": "Use-AzureRmSqlDatabaseServerAuditingPolicy",
"Get-AzSqlDatabaseLongTermRetentionPolicy": "Get-AzureRmSqlDatabaseLongTermRetentionPolicy",
"Set-AzSqlDatabaseLongTermRetentionPolicy": "Set-AzureRmSqlDatabaseLongTermRetentionPolicy"
"Set-AzSqlDatabaseLongTermRetentionPolicy": "Set-AzureRmSqlDatabaseLongTermRetentionPolicy",
"Get-AzSqlInstanceOperation": "Get-AzureRmSqlInstanceOperation",
"Stop-AzSqlInstanceOperation": "Stop-AzureRmSqlInstanceOperation"
},
"Az.Storage": {
"Get-AzStorageAccount": "Get-AzureRmStorageAccount",
Expand Down
37 changes: 37 additions & 0 deletions src/Sql/Sql.Test/ScenarioTests/Common.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -566,6 +566,15 @@ function Get-VNetName
return getAssetName
}

<#
.SYNOPSIS
Gets valid managed instance operation name
#>
function Get-ManagedInstanceOperationName
{
return getAssetName
}

<#
.SYNOPSIS
Gets test mode - 'Record' or 'Playback'
Expand Down Expand Up @@ -929,6 +938,34 @@ function Create-ManagedInstanceForTest ($resourceGroup, $subnetId)
return $managedInstance
}

<#
.SYNOPSIS
Asyn update of hardware generation on Sql managed instance
#>
function Update-ManagedInstanceGenerationForTest ($resourceGroup, $managedInstance)
{
$computeGeneration = "Gen5"

$managedInstance = Set-AzSqlInstance -ResourceGroupName $resourceGroup.ResourceGroupName -Name $managedInstance.ManagedInstanceName `
-ComputeGeneration $computeGeneration -Force -AsJob

return $managedInstance
}

<#
.SYNOPSIS
Sync update of storage on Sql managed instance
#>
function Update-ManagedInstanceStorageForTest ($resourceGroup, $managedInstance)
{
$storageSize = 928

$managedInstance = Set-AzSqlInstance -ResourceGroupName $resourceGroup.ResourceGroupName -Name $managedInstance.ManagedInstanceName `
-StorageSizeInGb $storageSize -Force

return $managedInstance
}

<#
.SYNOPSIS
Creates a managed instance in an instance pool
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
// ----------------------------------------------------------------------------------
//
// Copyright Microsoft Corporation
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// ----------------------------------------------------------------------------------

using Microsoft.Azure.Commands.ScenarioTest.SqlTests;
using Microsoft.Azure.ServiceManagement.Common.Models;
using Microsoft.WindowsAzure.Commands.ScenarioTest;
using Xunit;
using Xunit.Abstractions;
using RestTestFramework = Microsoft.Rest.ClientRuntime.Azure.TestFramework;

namespace Microsoft.Azure.Commands.Sql.Test.ScenarioTests
{
public class ManagedInstanceOperationScenarioTests : SqlTestsBase
{
protected override void SetupManagementClients(RestTestFramework.MockContext context)
{
var sqlClient = GetSqlClient(context);
var newResourcesClient = GetResourcesClient(context);
var networkClient = GetNetworkClient(context);
Helper.SetupSomeOfManagementClients(sqlClient, newResourcesClient, networkClient);
}

public ManagedInstanceOperationScenarioTests(ITestOutputHelper output) : base(output)
{
////base.resourceTypesToIgnoreApiVersion = new string[] { "Microsoft.Sql/managedInstance/operations" };
}

[Fact]
[Trait(Category.AcceptanceType, Category.CheckIn)]
public void TestGetManagedInstanceOperation()
{
RunPowerShellTest("Test-GetManagedInstanceOperation");
}

[Fact]
[Trait(Category.AcceptanceType, Category.CheckIn)]
public void TestStopManagedInstanceOperation()
{
RunPowerShellTest("Test-StopManagedInstanceOperation");
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
# ----------------------------------------------------------------------------------
#
# Copyright Microsoft Corporation
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
# http://www.apache.org/licenses/LICENSE-2.0
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ----------------------------------------------------------------------------------

# Location to use for provisioning test managed instances
$instanceLocation = "eastus"

<#
.SYNOPSIS
Tests Getting a managedInstance operation
.DESCRIPTION
SmokeTest
#>
function Test-GetManagedInstanceOperation
{
# Setup
$rg = Create-ResourceGroupForTest
$vnetName = "cl_initial"
$subnetName = "CooL"

# Setup VNET
$virtualNetwork1 = CreateAndGetVirtualNetworkForManagedInstance $vnetName $subnetName $rg.Location
$subnetId = $virtualNetwork1.Subnets.where({ $_.Name -eq $subnetName })[0].Id

# Initiate sync create of managed instance.
$managedInstance = Create-ManagedInstanceForTest $rg $subnetId

try
{
# Get all operations on managed instance.
$all = Get-AzSqlInstanceOperation -ResourceGroupName $rg.ResourceGroupName -ManagedInstanceName $managedInstance.ManagedInstanceName
Assert-AreEqual 1 $all.Count

# Get single operation on managed instance.
$firstOperation = Get-AzSqlInstanceOperation -ResourceGroupName $rg.ResourceGroupName -ManagedInstanceName $managedInstance.ManagedInstanceName -Name ($all | Select-Object -index 0).Name
Assert-AreEqual $firstOperation.OperationFriendlyName "UPDATE MANAGED SERVER"
Assert-AreEqual $firstOperation.PercentComplete 100
Assert-StartsWith $firstOperation.State "Succeeded"
Assert-AreEqual $firstOperation.IsCancellable $false

# Initiate sync update of storage (this operation can not be canceled nor during its execution or after it has finsihed).
$updatedManagedInstance = Update-ManagedInstanceStorageForTest $rg $managedInstance

# Get all operations on managed instance.
$all = Get-AzSqlInstanceOperation -ResourceGroupName $rg.ResourceGroupName -ManagedInstanceName $managedInstance.ManagedInstanceName
while($all.Count -ne 2) { $all = Get-AzSqlInstanceOperation -ResourceGroupName $rg.ResourceGroupName -ManagedInstanceName $managedInstance.ManagedInstanceName }

# Get single operation on managed instance.
$secondOperation = Get-AzSqlInstanceOperation -ResourceGroupName $rg.ResourceGroupName -ManagedInstanceName $managedInstance.ManagedInstanceName -Name ($all | Select-Object -index 1).Name
Assert-AreEqual $secondOperation.OperationFriendlyName "UPDATE MANAGED SERVER"
Assert-AreEqual $secondOperation.PercentComplete 100
Assert-StartsWith $firstOperation.State "Succeeded"
Assert-AreEqual $secondOperation.IsCancellable $false
}
finally
{
Remove-ResourceGroupForTest $rg
}
}

<#
.SYNOPSIS
Tests Removing a managedInstance
.DESCRIPTION
SmokeTest
#>
function Test-StopManagedInstanceOperation
{
# Setup
$rg = Create-ResourceGroupForTest
$vnetName = "cl_initial"
$subnetName = "CooL"

# Setup VNET
$virtualNetwork1 = CreateAndGetVirtualNetworkForManagedInstance $vnetName $subnetName $rg.Location
$subnetId = $virtualNetwork1.Subnets.where({ $_.Name -eq $subnetName })[0].Id

# Initiate sync create of managed instance.
$managedInstance = Create-ManagedInstanceForTest $rg $subnetId

try
{
# Get all operations on managed instance.
$all = Get-AzSqlInstanceOperation -ResourceGroupName $rg.ResourceGroupName -ManagedInstanceName $managedInstance.ManagedInstanceName
Assert-AreEqual 1 $all.Count

# Verify that create operation has finished.
$firstOperation = Get-AzSqlInstanceOperation -ResourceGroupName $rg.ResourceGroupName -ManagedInstanceName $managedInstance.ManagedInstanceName -Name ($all | Select-Object -index 0).Name
Assert-AreEqual $firstOperation.OperationFriendlyName "UPDATE MANAGED SERVER"
Assert-AreEqual $firstOperation.PercentComplete 100
Assert-StartsWith $firstOperation.State "Succeeded"
Assert-AreEqual $firstOperation.IsCancellable $false

# Verify that cancel is not allowed on finished operations.
Assert-Throws { Stop-AzSqlInstanceOperation -ResourceGroupName $rg.ResourceGroupName -ManagedInstanceName $managedInstance.ManagedInstanceName -Name $firstOperation.Name -Force }

# Initiate async update of hardware generation (this operation is cancelable).
$updatedManagedInstance = Update-ManagedInstanceGenerationForTest $rg $managedInstance

# Get all operations on managed instance.
$all = Get-AzSqlInstanceOperation -ResourceGroupName $rg.ResourceGroupName -ManagedInstanceName $managedInstance.ManagedInstanceName

# Wait for second operation to be cancelable, then initate cancel.
while($all.Count -ne 2) { $all = Get-AzSqlInstanceOperation -ResourceGroupName $rg.ResourceGroupName -ManagedInstanceName $managedInstance.ManagedInstanceName }
$secondOperation = $all | Select-Object -index 1
while($secondOperation.IsCancellable -eq $false) { $secondOperation = Get-AzSqlInstanceOperation -ResourceGroupName $rg.ResourceGroupName -ManagedInstanceName $managedInstance.ManagedInstanceName -Name $secondOperation.Name }
Stop-AzSqlInstanceOperation -ResourceGroupName $rg.ResourceGroupName -ManagedInstanceName $managedInstance.ManagedInstanceName -Name $secondOperation.Name -Force

# Wait for second operaiton to be cancelled and verify fields.
while($secondOperation.State -ne "Cancelled") { $secondOperation = Get-AzSqlInstanceOperation -ResourceGroupName $rg.ResourceGroupName -ManagedInstanceName $managedInstance.ManagedInstanceName -Name $secondOperation.Name }
Assert-AreEqual $secondOperation.OperationFriendlyName "UPDATE MANAGED SERVER"
Assert-AreEqual $secondOperation.PercentComplete 100
Assert-AreEqual $secondOperation.IsCancellable $false
}
finally
{
Remove-ResourceGroupForTest $rg
}
}
Loading