Skip to content

Commit 25679d6

Browse files
author
Maddie Clayton
authored
Merge pull request #8932 from milostod/master
Adding VirtualCluster related cmdlets and corresponding tests...
2 parents 084c498 + d592aa8 commit 25679d6

18 files changed

+56527
-10
lines changed

src/Sql/Sql.Test/ScenarioTests/Common.ps1

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -716,18 +716,17 @@ function Create-ManagedInstanceForTest ($resourceGroup, $subnetId)
716716
.SYNOPSIS
717717
Create a virtual network
718718
#>
719-
function CreateAndGetVirtualNetworkForManagedInstance ($vnetName, $subnetName, $location = "westcentralus")
719+
function CreateAndGetVirtualNetworkForManagedInstance ($vnetName, $subnetName, $location = "westcentralus", $resourceGroupName = "cl_one")
720720
{
721721
$vNetAddressPrefix = "10.0.0.0/16"
722-
$defaultResourceGroupName = "cl_one"
723722
$defaultSubnetAddressPrefix = "10.0.0.0/24"
724723

725724
try {
726-
$getVnet = Get-AzVirtualNetwork -Name $vnetName -ResourceGroupName $defaultResourceGroupName
725+
$getVnet = Get-AzVirtualNetwork -Name $vnetName -ResourceGroupName $resourceGroupName
727726
return $getVnet
728727
} catch {
729728
$virtualNetwork = New-AzVirtualNetwork `
730-
-ResourceGroupName $defaultResourceGroupName `
729+
-ResourceGroupName $resourceGroupName `
731730
-Location $location `
732731
-Name $vNetName `
733732
-AddressPrefix $vNetAddressPrefix
@@ -738,7 +737,7 @@ function CreateAndGetVirtualNetworkForManagedInstance ($vnetName, $subnetName, $
738737
$virtualNetwork | Set-AzVirtualNetwork
739738
$routeTableMiManagementService = New-AzRouteTable `
740739
-Name 'myRouteTableMiManagementService' `
741-
-ResourceGroupName $defaultResourceGroupName `
740+
-ResourceGroupName $resourceGroupName `
742741
-location $location
743742
Set-AzVirtualNetworkSubnetConfig `
744743
-VirtualNetwork $virtualNetwork `
@@ -747,15 +746,15 @@ function CreateAndGetVirtualNetworkForManagedInstance ($vnetName, $subnetName, $
747746
-RouteTable $routeTableMiManagementService | `
748747
Set-AzVirtualNetwork
749748
Get-AzRouteTable `
750-
-ResourceGroupName $defaultResourceGroupName `
749+
-ResourceGroupName $resourceGroupName `
751750
-Name "myRouteTableMiManagementService" `
752751
| Add-AzRouteConfig `
753752
-Name "ToManagedInstanceManagementService" `
754753
-AddressPrefix 0.0.0.0/0 `
755754
-NextHopType "Internet" `
756755
| Set-AzRouteTable
757756

758-
$getVnet = Get-AzVirtualNetwork -Name $vnetName -ResourceGroupName $defaultResourceGroupName
757+
$getVnet = Get-AzVirtualNetwork -Name $vnetName -ResourceGroupName $resourceGroupName
759758
return $getVnet
760759
}
761760
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
// ----------------------------------------------------------------------------------
2+
//
3+
// Copyright Microsoft Corporation
4+
// Licensed under the Apache License, Version 2.0 (the "License");
5+
// you may not use this file except in compliance with the License.
6+
// You may obtain a copy of the License at
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
// Unless required by applicable law or agreed to in writing, software
9+
// distributed under the License is distributed on an "AS IS" BASIS,
10+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
// See the License for the specific language governing permissions and
12+
// limitations under the License.
13+
// ----------------------------------------------------------------------------------
14+
15+
using Microsoft.Azure.Commands.ScenarioTest.SqlTests;
16+
using Microsoft.Azure.ServiceManagement.Common.Models;
17+
using Microsoft.WindowsAzure.Commands.ScenarioTest;
18+
using Xunit;
19+
using Xunit.Abstractions;
20+
using RestTestFramework = Microsoft.Rest.ClientRuntime.Azure.TestFramework;
21+
22+
namespace Microsoft.Azure.Commands.Sql.Test.ScenarioTests
23+
{
24+
public class VirtualClusterCrudScenarioTests : SqlTestsBase
25+
{
26+
protected override void SetupManagementClients(RestTestFramework.MockContext context)
27+
{
28+
var sqlClient = GetSqlClient(context);
29+
var newResourcesClient = GetResourcesClient(context);
30+
var networkClient = GetNetworkClient(context);
31+
Helper.SetupSomeOfManagementClients(sqlClient, newResourcesClient, networkClient);
32+
}
33+
34+
public VirtualClusterCrudScenarioTests(ITestOutputHelper output) : base(output)
35+
{
36+
}
37+
38+
[Fact]
39+
[Trait(Category.AcceptanceType, Category.CheckIn)]
40+
public void TestGetVirtualCluster()
41+
{
42+
RunPowerShellTest("Test-GetVirtualCluster");
43+
}
44+
45+
[Fact]
46+
[Trait(Category.AcceptanceType, Category.CheckIn)]
47+
public void TestRemoveVirtualCluster()
48+
{
49+
RunPowerShellTest("Test-RemoveVirtualCluster");
50+
}
51+
}
52+
}
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
# ----------------------------------------------------------------------------------
2+
#
3+
# Copyright Microsoft Corporation
4+
# Licensed under the Apache License, Version 2.0 (the "License");
5+
# you may not use this file except in compliance with the License.
6+
# You may obtain a copy of the License at
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
# Unless required by applicable law or agreed to in writing, software
9+
# distributed under the License is distributed on an "AS IS" BASIS,
10+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
# See the License for the specific language governing permissions and
12+
# limitations under the License.
13+
# ----------------------------------------------------------------------------------
14+
15+
<#
16+
.SYNOPSIS
17+
Tests Getting a VirtualCluster
18+
.DESCRIPTION
19+
SmokeTest
20+
#>
21+
function Test-GetVirtualCluster
22+
{
23+
# Setup
24+
$location = Get-ProviderLocation "Microsoft.Sql/virtualclusters"
25+
$rg = Create-ResourceGroupForTest $location
26+
27+
$rgName = $rg.ResourceGroupName
28+
$vnetName = "cl_initial"
29+
$subnetName = "Cool"
30+
31+
# Setup VNET
32+
$virtualNetwork = CreateAndGetVirtualNetworkForManagedInstance $vnetName $subnetName $location $rgName
33+
$subnetId = $virtualNetwork.Subnets.where({ $_.Name -eq $subnetName })[0].Id
34+
35+
$managedInstance = Create-ManagedInstanceForTest $rg $subnetId
36+
37+
try
38+
{
39+
# Test using all parameters
40+
$virtualClusterList = Get-AzSqlVirtualCluster
41+
$virtualCluster = $virtualClusterList.where({$_.SubnetId -eq $subnetId})
42+
Assert-AreEqual $rgName $virtualCluster.ResourceGroupName
43+
$virtualClusterName = $virtualCluster.VirtualClusterName
44+
45+
$virtualClusterList = Get-AzSqlVirtualCluster -ResourceGroupName $rgName
46+
$virtualCluster = $virtualClusterList.where({$_.SubnetId -eq $subnetId})
47+
Assert-AreEqual $rgName $virtualCluster.ResourceGroupName
48+
Assert-AreEqual $virtualClusterName $virtualCluster.VirtualClusterName
49+
50+
$virtualCluster = Get-AzSqlVirtualCluster -ResourceGroupName $rgName -Name $virtualClusterName
51+
Assert-AreEqual $rgName $virtualCluster.ResourceGroupName
52+
Assert-AreEqual $virtualClusterName $virtualCluster.VirtualClusterName
53+
Assert-AreEqual $subnetId $virtualCluster.SubnetId
54+
}
55+
finally
56+
{
57+
Remove-ResourceGroupForTest $rg
58+
}
59+
}
60+
61+
<#
62+
.SYNOPSIS
63+
Tests Removing a VirtualCluster
64+
.DESCRIPTION
65+
SmokeTest
66+
#>
67+
function Test-RemoveVirtualCluster
68+
{
69+
# Setup
70+
$location = Get-ProviderLocation "Microsoft.Sql/virtualclusters"
71+
$rg = Create-ResourceGroupForTest $location
72+
73+
$rgName = $rg.ResourceGroupName
74+
$vnetName = "cl_initial"
75+
$subnetName = "Cool"
76+
77+
# Setup VNET
78+
$virtualNetwork = CreateAndGetVirtualNetworkForManagedInstance $vnetName $subnetName $location $rgName
79+
$subnetId = $virtualNetwork.Subnets.where({ $_.Name -eq $subnetName })[0].Id
80+
81+
$managedInstance = Create-ManagedInstanceForTest $rg $subnetId
82+
83+
try
84+
{
85+
$virtualClusterList = Get-AzSqlVirtualCluster -ResourceGroupName $rgName
86+
$virtualCluster = $virtualClusterList.where({$_.SubnetId -eq $subnetId})
87+
$virtualClusterName = $virtualCluster.VirtualClusterName
88+
89+
# Remove the managed instance first
90+
$managedInstance | Remove-AzSqlInstance -Force
91+
92+
# Remove virtual cluster
93+
$virtualCluster | Remove-AzSqlVirtualCluster
94+
95+
$all = Get-AzSqlVirtualCluster -ResourceGroupName $rgName
96+
$virtualCluster = $all.where({$_.VirtualClusterName -eq $virtualClusterName})
97+
Assert-AreEqual $virtualCluster.Count 0
98+
}
99+
finally
100+
{
101+
Remove-ResourceGroupForTest $rg
102+
}
103+
}

src/Sql/Sql.Test/SessionRecords/Microsoft.Azure.Commands.Sql.Test.ScenarioTests.VirtualClusterCrudScenarioTests/TestGetVirtualCluster.json

Lines changed: 25864 additions & 0 deletions
Large diffs are not rendered by default.

src/Sql/Sql.Test/SessionRecords/Microsoft.Azure.Commands.Sql.Test.ScenarioTests.VirtualClusterCrudScenarioTests/TestRemoveVirtualCluster.json

Lines changed: 29556 additions & 0 deletions
Large diffs are not rendered by default.

src/Sql/Sql.Test/Sql.Test.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
</PropertyGroup>
1212

1313
<ItemGroup>
14-
<PackageReference Include="Microsoft.Azure.Management.Sql" Version="1.28.0-preview" />
14+
<PackageReference Include="Microsoft.Azure.Management.Sql" Version="1.29.0-preview" />
1515
<PackageReference Include="Microsoft.Azure.Management.EventHub" Version="2.3.0" />
1616
<PackageReference Include="Microsoft.Azure.Management.Network" Version="19.10.0-preview" />
1717
<PackageReference Include="Microsoft.Azure.Graph.RBAC" Version="3.2.0-preview" />

src/Sql/Sql/Az.Sql.psd1

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,8 @@ CmdletsToExport = 'Get-AzSqlDatabaseTransparentDataEncryption',
217217
'Remove-AzSqlDatabaseSensitivityClassification',
218218
'Remove-AzSqlInstanceDatabaseSensitivityClassification',
219219
'Get-AzSqlDatabaseSensitivityRecommendation',
220-
'Get-AzSqlInstanceDatabaseSensitivityRecommendation'
220+
'Get-AzSqlInstanceDatabaseSensitivityRecommendation',
221+
'Get-AzSqlVirtualCluster', 'Remove-AzSqlVirtualCluster'
221222

222223
# Variables to export from this module
223224
# VariablesToExport = @()

src/Sql/Sql/ChangeLog.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323

2424
## Version 1.8.0
2525
* Support Database Data Classification.
26+
* Add Get/Remove AzSqlVirtualCluster cmdlets.
2627

2728
## Version 1.7.0
2829
* Add Vulnerability Assessment cmdlets on Server and Managed Instance

src/Sql/Sql/Sql.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
</PropertyGroup>
1212

1313
<ItemGroup>
14-
<PackageReference Include="Microsoft.Azure.Management.Sql" Version="1.28.0-preview" />
14+
<PackageReference Include="Microsoft.Azure.Management.Sql" Version="1.29.0-preview" />
1515
<PackageReference Include="System.Security.Permissions" Version="4.5.0" />
1616
<PackageReference Include="Microsoft.Azure.Management.Monitor" Version="0.21.0-preview" />
1717
</ItemGroup>
Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
// ----------------------------------------------------------------------------------
2+
//
3+
// Copyright Microsoft Corporation
4+
// Licensed under the Apache License, Version 2.0 (the "License");
5+
// you may not use this file except in compliance with the License.
6+
// You may obtain a copy of the License at
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
// Unless required by applicable law or agreed to in writing, software
9+
// distributed under the License is distributed on an "AS IS" BASIS,
10+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
// See the License for the specific language governing permissions and
12+
// limitations under the License.
13+
// ----------------------------------------------------------------------------------
14+
15+
using Microsoft.Azure.Commands.ResourceManager.Common.ArgumentCompleters;
16+
using Microsoft.Azure.Commands.Sql.VirtualCluster.Model;
17+
using Microsoft.Azure.Management.Internal.Resources.Utilities.Models;
18+
using System.Collections.Generic;
19+
using System.Management.Automation;
20+
21+
namespace Microsoft.Azure.Commands.Sql.VirtualCluster.Cmdlet
22+
{
23+
[Cmdlet(VerbsCommon.Get, ResourceManager.Common.AzureRMConstants.AzureRMPrefix + "SqlVirtualCluster", DefaultParameterSetName = GetByResourceGroupParameterSet)]
24+
[OutputType(typeof(AzureSqlVirtualClusterModel))]
25+
public class GetAzureSqlVirtualCluster : VirtualClusterCmdletBase
26+
{
27+
protected const string GetByNameAndResourceGroupParameterSet =
28+
"GetVirtualClusterByNameAndResourceGroup";
29+
30+
protected const string GetByResourceGroupParameterSet =
31+
"GetVirtualClusterByResourceGroup";
32+
33+
protected const string GetByResourceIdParameterSet =
34+
"GetVirtualClusterByResourceId";
35+
36+
/// <summary>
37+
/// Gets or sets the name of the virtual cluster.
38+
/// </summary>
39+
[Parameter(ParameterSetName = GetByNameAndResourceGroupParameterSet,
40+
Mandatory = true,
41+
Position = 0,
42+
HelpMessage = "The name of the virtual cluster.")]
43+
[Alias("VirtualClusterName")]
44+
[ResourceNameCompleter("Microsoft.Sql/virtualClusters", "ResourceGroupName")]
45+
[ValidateNotNullOrEmpty]
46+
public string Name { get; set; }
47+
48+
/// <summary>
49+
/// Gets or sets the name of the resource group.
50+
/// </summary>
51+
[Parameter(ParameterSetName = GetByNameAndResourceGroupParameterSet,
52+
Mandatory = true,
53+
Position = 1,
54+
HelpMessage = "The name of the resource group.")]
55+
[Parameter(ParameterSetName = GetByResourceGroupParameterSet,
56+
Mandatory = false,
57+
HelpMessage = "The name of the resource group.")]
58+
[ResourceGroupCompleter]
59+
[ValidateNotNullOrEmpty]
60+
public override string ResourceGroupName { get; set; }
61+
62+
/// <summary>
63+
/// Gets or sets the resource id of the virtual cluster
64+
/// </summary>
65+
[Parameter(ParameterSetName = GetByResourceIdParameterSet,
66+
Mandatory = true,
67+
Position = 0,
68+
ValueFromPipelineByPropertyName = true,
69+
HelpMessage = "The resource id of instance object to get")]
70+
[ValidateNotNullOrEmpty]
71+
public string ResourceId { get; set; }
72+
73+
/// <summary>
74+
/// Gets virtual cluster from the service.
75+
/// </summary>
76+
/// <returns>A single virtual cluster</returns>
77+
protected override IEnumerable<AzureSqlVirtualClusterModel> GetEntity()
78+
{
79+
ICollection<AzureSqlVirtualClusterModel> results = null;
80+
81+
if (string.Equals(this.ParameterSetName, GetByResourceIdParameterSet, System.StringComparison.OrdinalIgnoreCase))
82+
{
83+
var resourceInfo = new ResourceIdentifier(ResourceId);
84+
85+
ResourceGroupName = resourceInfo.ResourceGroupName;
86+
Name = resourceInfo.ResourceName;
87+
88+
results = new List<AzureSqlVirtualClusterModel>();
89+
results.Add(ModelAdapter.GetVirtualCluster(this.ResourceGroupName, this.Name));
90+
}
91+
else if (string.Equals(this.ParameterSetName, GetByNameAndResourceGroupParameterSet, System.StringComparison.OrdinalIgnoreCase))
92+
{
93+
results = new List<AzureSqlVirtualClusterModel>();
94+
results.Add(ModelAdapter.GetVirtualCluster(this.ResourceGroupName, this.Name));
95+
}
96+
else if (string.Equals(this.ParameterSetName, GetByResourceGroupParameterSet, System.StringComparison.OrdinalIgnoreCase))
97+
{
98+
if (MyInvocation.BoundParameters.ContainsKey("ResourceGroupName"))
99+
{
100+
results = ModelAdapter.ListVirtualClustersByResourceGroup(this.ResourceGroupName);
101+
}
102+
else
103+
{
104+
results = ModelAdapter.ListVirtualClusters();
105+
}
106+
}
107+
108+
return results;
109+
}
110+
111+
/// <summary>
112+
/// No changes, thus nothing to persist.
113+
/// </summary>
114+
/// <param name="entity">The entity retrieved</param>
115+
/// <returns>The unchanged entity</returns>
116+
protected override IEnumerable<AzureSqlVirtualClusterModel> PersistChanges(IEnumerable<AzureSqlVirtualClusterModel> entity)
117+
{
118+
return entity;
119+
}
120+
121+
/// <summary>
122+
/// No user input to apply to model.
123+
/// </summary>
124+
/// <param name="model">The model to modify</param>
125+
/// <returns>The input model</returns>
126+
protected override IEnumerable<AzureSqlVirtualClusterModel> ApplyUserInputToModel(IEnumerable<AzureSqlVirtualClusterModel> model)
127+
{
128+
return model;
129+
}
130+
}
131+
}

0 commit comments

Comments
 (0)