Skip to content

Commit ee44471

Browse files
author
Milos Todic
committed
Adding VirtualCluster related cmdlets and corresponding tests and help files.
1 parent aea4fce commit ee44471

19 files changed

+25097
-24
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: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
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 = "eastus"
25+
$rg = Create-ResourceGroupForTest $location
26+
27+
$rgName = "RG_MIPlayground"
28+
$vnetName = "VNET_MIPlayground"
29+
$subnetName = "VCReservedSubnet"
30+
31+
# Setup VNET
32+
$virtualNetwork = CreateAndGetVirtualNetworkForManagedInstance $vnetName $subnetName $location $rgName
33+
$subnetId = $virtualNetwork.Subnets.where({ $_.Name -eq $subnetName }).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 $location $virtualCluster.Location
43+
Assert-AreEqual $rgName $virtualCluster.ResourceGroupName
44+
$virtualClusterName = $virtualCluster.VirtualClusterName
45+
46+
$virtualClusterList = Get-AzSqlVirtualCluster -ResourceGroupName $rgName
47+
$virtualCluster = $virtualClusterList.where({$_.SubnetId -eq $subnetId})
48+
Assert-AreEqual $location $virtualCluster.Location
49+
Assert-AreEqual $rgName $virtualCluster.ResourceGroupName
50+
Assert-AreEqual $virtualClusterName $virtualCluster.VirtualClusterName
51+
52+
$virtualCluster = Get-AzSqlVirtualCluster -ResourceGroupName $rgName -Name $virtualClusterName
53+
Assert-AreEqual $location $virtualCluster.Location
54+
Assert-AreEqual $rgName $virtualCluster.ResourceGroupName
55+
Assert-AreEqual $virtualClusterName $virtualCluster.VirtualClusterName
56+
Assert-AreEqual $subnetId $virtualCluster.SubnetId
57+
}
58+
finally
59+
{
60+
Remove-ResourceGroupForTest $rg
61+
}
62+
}
63+
64+
<#
65+
.SYNOPSIS
66+
Tests Removing a VirtualCluster
67+
.DESCRIPTION
68+
SmokeTest
69+
#>
70+
function Test-RemoveVirtualCluster
71+
{
72+
# Setup
73+
$location = "eastus"
74+
$rg = Create-ResourceGroupForTest $location
75+
76+
$rgName = "RG_MIPlayground"
77+
$vnetName = "VNET_MIPlayground"
78+
$subnetName = "VCReservedSubnet"
79+
80+
# Setup VNET
81+
$virtualNetwork = CreateAndGetVirtualNetworkForManagedInstance $vnetName $subnetName $location $rgName
82+
$subnetId = $virtualNetwork.Subnets.where({ $_.Name -eq $subnetName }).Id
83+
84+
$managedInstance = Create-ManagedInstanceForTest $rg $subnetId
85+
86+
try
87+
{
88+
$virtualClusterList = Get-AzSqlVirtualCluster -ResourceGroupName $rgName
89+
$virtualCluster = $virtualClusterList.where({$_.SubnetId -eq $subnetId})
90+
$virtualClusterName = $virtualCluster.VirtualClusterName
91+
92+
# Remove the managed instance first
93+
$managedInstance | Remove-AzSqlInstance -Force
94+
95+
# Remove virtual cluster
96+
$virtualCluster | Remove-AzSqlVirtualCluster
97+
98+
$all = Get-AzSqlVirtualCluster -ResourceGroupName $rgName
99+
$virtualCluster = $all.where({$_.VirtualClusterName -eq $virtualClusterName})
100+
Assert-AreEqual $virtualCluster.Count 0
101+
}
102+
finally
103+
{
104+
Remove-ResourceGroupForTest $rg
105+
}
106+
}

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

Lines changed: 1246 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: 22724 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/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: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
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+
Position = 1,
58+
HelpMessage = "The name of the resource group.")]
59+
[ResourceGroupCompleter]
60+
[ValidateNotNullOrEmpty]
61+
public override string ResourceGroupName { get; set; }
62+
63+
/// <summary>
64+
/// Gets or sets the resource id of the virtual cluster
65+
/// </summary>
66+
[Parameter(ParameterSetName = GetByResourceIdParameterSet,
67+
Mandatory = true,
68+
Position = 0,
69+
ValueFromPipelineByPropertyName = true,
70+
HelpMessage = "The resource id of instance object to get")]
71+
[ValidateNotNullOrEmpty]
72+
public string ResourceId { get; set; }
73+
74+
/// <summary>
75+
/// Gets virtual cluster from the service.
76+
/// </summary>
77+
/// <returns>A single virtual cluster</returns>
78+
protected override IEnumerable<AzureSqlVirtualClusterModel> GetEntity()
79+
{
80+
ICollection<AzureSqlVirtualClusterModel> results = null;
81+
82+
if (string.Equals(this.ParameterSetName, GetByResourceIdParameterSet, System.StringComparison.OrdinalIgnoreCase))
83+
{
84+
var resourceInfo = new ResourceIdentifier(ResourceId);
85+
86+
ResourceGroupName = resourceInfo.ResourceGroupName;
87+
Name = resourceInfo.ResourceName;
88+
89+
results = new List<AzureSqlVirtualClusterModel>();
90+
results.Add(ModelAdapter.GetVirtualCluster(this.ResourceGroupName, this.Name));
91+
}
92+
else if (string.Equals(this.ParameterSetName, GetByNameAndResourceGroupParameterSet, System.StringComparison.OrdinalIgnoreCase))
93+
{
94+
results = new List<AzureSqlVirtualClusterModel>();
95+
results.Add(ModelAdapter.GetVirtualCluster(this.ResourceGroupName, this.Name));
96+
}
97+
else if (string.Equals(this.ParameterSetName, GetByResourceGroupParameterSet, System.StringComparison.OrdinalIgnoreCase))
98+
{
99+
if (MyInvocation.BoundParameters.ContainsKey("ResourceGroupName"))
100+
{
101+
results = ModelAdapter.ListVirtualClustersByResourceGroup(this.ResourceGroupName);
102+
}
103+
else
104+
{
105+
results = ModelAdapter.ListVirtualClusters();
106+
}
107+
}
108+
109+
return results;
110+
}
111+
112+
/// <summary>
113+
/// No changes, thus nothing to persist.
114+
/// </summary>
115+
/// <param name="entity">The entity retrieved</param>
116+
/// <returns>The unchanged entity</returns>
117+
protected override IEnumerable<AzureSqlVirtualClusterModel> PersistChanges(IEnumerable<AzureSqlVirtualClusterModel> entity)
118+
{
119+
return entity;
120+
}
121+
122+
/// <summary>
123+
/// No user input to apply to model.
124+
/// </summary>
125+
/// <param name="model">The model to modify</param>
126+
/// <returns>The input model</returns>
127+
protected override IEnumerable<AzureSqlVirtualClusterModel> ApplyUserInputToModel(IEnumerable<AzureSqlVirtualClusterModel> model)
128+
{
129+
return model;
130+
}
131+
}
132+
}

0 commit comments

Comments
 (0)