Skip to content

Adding VirtualCluster related cmdlets and corresponding tests... #8932

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 6 commits into from
Apr 12, 2019
Merged
Show file tree
Hide file tree
Changes from 5 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
13 changes: 6 additions & 7 deletions src/Sql/Sql.Test/ScenarioTests/Common.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -716,18 +716,17 @@ function Create-ManagedInstanceForTest ($resourceGroup, $subnetId)
.SYNOPSIS
Create a virtual network
#>
function CreateAndGetVirtualNetworkForManagedInstance ($vnetName, $subnetName, $location = "westcentralus")
function CreateAndGetVirtualNetworkForManagedInstance ($vnetName, $subnetName, $location = "westcentralus", $resourceGroupName = "cl_one")
{
$vNetAddressPrefix = "10.0.0.0/16"
$defaultResourceGroupName = "cl_one"
$defaultSubnetAddressPrefix = "10.0.0.0/24"

try {
$getVnet = Get-AzVirtualNetwork -Name $vnetName -ResourceGroupName $defaultResourceGroupName
$getVnet = Get-AzVirtualNetwork -Name $vnetName -ResourceGroupName $resourceGroupName
return $getVnet
} catch {
$virtualNetwork = New-AzVirtualNetwork `
-ResourceGroupName $defaultResourceGroupName `
-ResourceGroupName $resourceGroupName `
-Location $location `
-Name $vNetName `
-AddressPrefix $vNetAddressPrefix
Expand All @@ -738,7 +737,7 @@ function CreateAndGetVirtualNetworkForManagedInstance ($vnetName, $subnetName, $
$virtualNetwork | Set-AzVirtualNetwork
$routeTableMiManagementService = New-AzRouteTable `
-Name 'myRouteTableMiManagementService' `
-ResourceGroupName $defaultResourceGroupName `
-ResourceGroupName $resourceGroupName `
-location $location
Set-AzVirtualNetworkSubnetConfig `
-VirtualNetwork $virtualNetwork `
Expand All @@ -747,15 +746,15 @@ function CreateAndGetVirtualNetworkForManagedInstance ($vnetName, $subnetName, $
-RouteTable $routeTableMiManagementService | `
Set-AzVirtualNetwork
Get-AzRouteTable `
-ResourceGroupName $defaultResourceGroupName `
-ResourceGroupName $resourceGroupName `
-Name "myRouteTableMiManagementService" `
| Add-AzRouteConfig `
-Name "ToManagedInstanceManagementService" `
-AddressPrefix 0.0.0.0/0 `
-NextHopType "Internet" `
| Set-AzRouteTable

$getVnet = Get-AzVirtualNetwork -Name $vnetName -ResourceGroupName $defaultResourceGroupName
$getVnet = Get-AzVirtualNetwork -Name $vnetName -ResourceGroupName $resourceGroupName
return $getVnet
}
}
52 changes: 52 additions & 0 deletions src/Sql/Sql.Test/ScenarioTests/VirtualClusterCrudScenarioTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
// ----------------------------------------------------------------------------------
//
// 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 VirtualClusterCrudScenarioTests : 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 VirtualClusterCrudScenarioTests(ITestOutputHelper output) : base(output)
{
}

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

[Fact]
[Trait(Category.AcceptanceType, Category.CheckIn)]
public void TestRemoveVirtualCluster()
{
RunPowerShellTest("Test-RemoveVirtualCluster");
}
}
}
106 changes: 106 additions & 0 deletions src/Sql/Sql.Test/ScenarioTests/VirtualClusterCrudScenarioTests.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
# ----------------------------------------------------------------------------------
#
# 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.
# ----------------------------------------------------------------------------------

<#
.SYNOPSIS
Tests Getting a VirtualCluster
.DESCRIPTION
SmokeTest
#>
function Test-GetVirtualCluster
{
# Setup
$location = "eastus"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please get location using Get-Location to ensure this will work on all clouds: https://github.com/Azure/azure-powershell/blob/master/tools/ScenarioTest.ResourceManager/Common.ps1#L583

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @maddieclayton , I used some pre-made resources to speed up the test because provisioning of a new virtual cluster takes several hours. I've made the changes to create everything in the test, however, each test takes almost 4 hours to execute. If this is not a problem, I'll commit the change. Please let me know.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@milostod It is fine to take 4 hours to record this test - because the SDK calls will be instantaneous in playback mode, it will not affect our CI, and having a test take 4 hours to record is much better than having to do manual set up that takes the same amount of time.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@maddieclayton I agree. I've made the changes, tests are now creating everything from scratch, including ResourceGroup, VNet and Subnet. Total running time was 7hrs:30min for two tests. I used Get-ProviderLocation since it uses Get-AzResourceProvider instead of Get-AzureRmResourceProvider command for which I had some trouble running locally.

$rg = Create-ResourceGroupForTest $location

$rgName = "RG_MIPlayground"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please have all resource name be randomly generated using getAssetName: https://github.com/Azure/azure-powershell/blob/master/tools/ScenarioTest.ResourceManager/Common.ps1#L371

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done. rgName is created in Create-ResourceGroupForTest using getAssetName. I kept hardcoded names for vnet and subnet as it seems that calling getAssetName multiple times in short period produces "queue is empty" error. I see multiple tests are keeping hardcoded values for vnet and subnet, probably for the same reason.

$vnetName = "VNET_MIPlayground"
$subnetName = "VCReservedSubnet"

# Setup VNET
$virtualNetwork = CreateAndGetVirtualNetworkForManagedInstance $vnetName $subnetName $location $rgName
$subnetId = $virtualNetwork.Subnets.where({ $_.Name -eq $subnetName }).Id

$managedInstance = Create-ManagedInstanceForTest $rg $subnetId

try
{
# Test using all parameters
$virtualClusterList = Get-AzSqlVirtualCluster
$virtualCluster = $virtualClusterList.where({$_.SubnetId -eq $subnetId})
Assert-AreEqual $location $virtualCluster.Location
Assert-AreEqual $rgName $virtualCluster.ResourceGroupName
$virtualClusterName = $virtualCluster.VirtualClusterName

$virtualClusterList = Get-AzSqlVirtualCluster -ResourceGroupName $rgName
$virtualCluster = $virtualClusterList.where({$_.SubnetId -eq $subnetId})
Assert-AreEqual $location $virtualCluster.Location
Assert-AreEqual $rgName $virtualCluster.ResourceGroupName
Assert-AreEqual $virtualClusterName $virtualCluster.VirtualClusterName

$virtualCluster = Get-AzSqlVirtualCluster -ResourceGroupName $rgName -Name $virtualClusterName
Assert-AreEqual $location $virtualCluster.Location
Assert-AreEqual $rgName $virtualCluster.ResourceGroupName
Assert-AreEqual $virtualClusterName $virtualCluster.VirtualClusterName
Assert-AreEqual $subnetId $virtualCluster.SubnetId
}
finally
{
Remove-ResourceGroupForTest $rg
}
}

<#
.SYNOPSIS
Tests Removing a VirtualCluster
.DESCRIPTION
SmokeTest
#>
function Test-RemoveVirtualCluster
{
# Setup
$location = "eastus"
$rg = Create-ResourceGroupForTest $location

$rgName = "RG_MIPlayground"
$vnetName = "VNET_MIPlayground"
$subnetName = "VCReservedSubnet"

# Setup VNET
$virtualNetwork = CreateAndGetVirtualNetworkForManagedInstance $vnetName $subnetName $location $rgName
$subnetId = $virtualNetwork.Subnets.where({ $_.Name -eq $subnetName }).Id

$managedInstance = Create-ManagedInstanceForTest $rg $subnetId

try
{
$virtualClusterList = Get-AzSqlVirtualCluster -ResourceGroupName $rgName
$virtualCluster = $virtualClusterList.where({$_.SubnetId -eq $subnetId})
$virtualClusterName = $virtualCluster.VirtualClusterName

# Remove the managed instance first
$managedInstance | Remove-AzSqlInstance -Force

# Remove virtual cluster
$virtualCluster | Remove-AzSqlVirtualCluster

$all = Get-AzSqlVirtualCluster -ResourceGroupName $rgName
$virtualCluster = $all.where({$_.VirtualClusterName -eq $virtualClusterName})
Assert-AreEqual $virtualCluster.Count 0
}
finally
{
Remove-ResourceGroupForTest $rg
}
}

Large diffs are not rendered by default.

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion src/Sql/Sql.Test/Sql.Test.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Azure.Management.Sql" Version="1.28.0-preview" />
<PackageReference Include="Microsoft.Azure.Management.Sql" Version="1.29.0-preview" />
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note: New NuGet package has been published from the Azure SDK for .NET repository

<PackageReference Include="Microsoft.Azure.Management.EventHub" Version="2.3.0" />
<PackageReference Include="Microsoft.Azure.Management.Network" Version="19.10.0-preview" />
<PackageReference Include="Microsoft.Azure.Graph.RBAC" Version="3.2.0-preview" />
Expand Down
5 changes: 3 additions & 2 deletions src/Sql/Sql/Az.Sql.psd1
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,8 @@ CmdletsToExport = 'Get-AzSqlDatabaseTransparentDataEncryption',
'Remove-AzSqlDatabaseSensitivityClassification',
'Remove-AzSqlInstanceDatabaseSensitivityClassification',
'Get-AzSqlDatabaseSensitivityRecommendation',
'Get-AzSqlInstanceDatabaseSensitivityRecommendation'
'Get-AzSqlInstanceDatabaseSensitivityRecommendation',
'Get-AzSqlVirtualCluster', 'Remove-AzSqlVirtualCluster'

# Variables to export from this module
# VariablesToExport = @()
Expand Down Expand Up @@ -271,7 +272,7 @@ PrivateData = @{
# IconUri = ''

# ReleaseNotes of this module
ReleaseNotes = '* Support Database Data Classification.'
ReleaseNotes = '* Add Virtual Cluster cmdlets'
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please revert this change, we will update during versioning.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done, thanks.


# Prerelease string of this module
# Prerelease = ''
Expand Down
1 change: 1 addition & 0 deletions src/Sql/Sql/ChangeLog.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

## Version 1.8.0
* Support Database Data Classification.
* Add Get/Remove AzSqlVirtualCluster cmdlets.

## Version 1.7.0
* Add Vulnerability Assessment cmdlets on Server and Managed Instance
Expand Down
2 changes: 1 addition & 1 deletion src/Sql/Sql/Sql.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Azure.Management.Sql" Version="1.28.0-preview" />
<PackageReference Include="Microsoft.Azure.Management.Sql" Version="1.29.0-preview" />
<PackageReference Include="System.Security.Permissions" Version="4.5.0" />
<PackageReference Include="Microsoft.Azure.Management.Monitor" Version="0.21.0-preview" />
</ItemGroup>
Expand Down
131 changes: 131 additions & 0 deletions src/Sql/Sql/VirtualCluster/Cmdlet/GetAzureSqlVirtualCluster.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
// ----------------------------------------------------------------------------------
//
// 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.ResourceManager.Common.ArgumentCompleters;
using Microsoft.Azure.Commands.Sql.VirtualCluster.Model;
using Microsoft.Azure.Management.Internal.Resources.Utilities.Models;
using System.Collections.Generic;
using System.Management.Automation;

namespace Microsoft.Azure.Commands.Sql.VirtualCluster.Cmdlet
{
[Cmdlet(VerbsCommon.Get, ResourceManager.Common.AzureRMConstants.AzureRMPrefix + "SqlVirtualCluster", DefaultParameterSetName = GetByResourceGroupParameterSet)]
[OutputType(typeof(AzureSqlVirtualClusterModel))]
public class GetAzureSqlVirtualCluster : VirtualClusterCmdletBase
{
protected const string GetByNameAndResourceGroupParameterSet =
"GetVirtualClusterByNameAndResourceGroup";

protected const string GetByResourceGroupParameterSet =
"GetVirtualClusterByResourceGroup";

protected const string GetByResourceIdParameterSet =
"GetVirtualClusterByResourceId";

/// <summary>
/// Gets or sets the name of the virtual cluster.
/// </summary>
[Parameter(ParameterSetName = GetByNameAndResourceGroupParameterSet,
Mandatory = true,
Position = 0,
HelpMessage = "The name of the virtual cluster.")]
[Alias("VirtualClusterName")]
[ResourceNameCompleter("Microsoft.Sql/virtualClusters", "ResourceGroupName")]
[ValidateNotNullOrEmpty]
public string Name { get; set; }

/// <summary>
/// Gets or sets the name of the resource group.
/// </summary>
[Parameter(ParameterSetName = GetByNameAndResourceGroupParameterSet,
Mandatory = true,
Position = 1,
HelpMessage = "The name of the resource group.")]
[Parameter(ParameterSetName = GetByResourceGroupParameterSet,
Mandatory = false,
HelpMessage = "The name of the resource group.")]
[ResourceGroupCompleter]
[ValidateNotNullOrEmpty]
public override string ResourceGroupName { get; set; }

/// <summary>
/// Gets or sets the resource id of the virtual cluster
/// </summary>
[Parameter(ParameterSetName = GetByResourceIdParameterSet,
Mandatory = true,
Position = 0,
ValueFromPipelineByPropertyName = true,
HelpMessage = "The resource id of instance object to get")]
[ValidateNotNullOrEmpty]
public string ResourceId { get; set; }

/// <summary>
/// Gets virtual cluster from the service.
/// </summary>
/// <returns>A single virtual cluster</returns>
protected override IEnumerable<AzureSqlVirtualClusterModel> GetEntity()
{
ICollection<AzureSqlVirtualClusterModel> results = null;

if (string.Equals(this.ParameterSetName, GetByResourceIdParameterSet, System.StringComparison.OrdinalIgnoreCase))
{
var resourceInfo = new ResourceIdentifier(ResourceId);

ResourceGroupName = resourceInfo.ResourceGroupName;
Name = resourceInfo.ResourceName;

results = new List<AzureSqlVirtualClusterModel>();
results.Add(ModelAdapter.GetVirtualCluster(this.ResourceGroupName, this.Name));
}
else if (string.Equals(this.ParameterSetName, GetByNameAndResourceGroupParameterSet, System.StringComparison.OrdinalIgnoreCase))
{
results = new List<AzureSqlVirtualClusterModel>();
results.Add(ModelAdapter.GetVirtualCluster(this.ResourceGroupName, this.Name));
}
else if (string.Equals(this.ParameterSetName, GetByResourceGroupParameterSet, System.StringComparison.OrdinalIgnoreCase))
{
if (MyInvocation.BoundParameters.ContainsKey("ResourceGroupName"))
{
results = ModelAdapter.ListVirtualClustersByResourceGroup(this.ResourceGroupName);
}
else
{
results = ModelAdapter.ListVirtualClusters();
}
}

return results;
}

/// <summary>
/// No changes, thus nothing to persist.
/// </summary>
/// <param name="entity">The entity retrieved</param>
/// <returns>The unchanged entity</returns>
protected override IEnumerable<AzureSqlVirtualClusterModel> PersistChanges(IEnumerable<AzureSqlVirtualClusterModel> entity)
{
return entity;
}

/// <summary>
/// No user input to apply to model.
/// </summary>
/// <param name="model">The model to modify</param>
/// <returns>The input model</returns>
protected override IEnumerable<AzureSqlVirtualClusterModel> ApplyUserInputToModel(IEnumerable<AzureSqlVirtualClusterModel> model)
{
return model;
}
}
}
Loading