Skip to content

Add support for managing Index Recommendatations #746

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 2 commits into from
Aug 14, 2015
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
Original file line number Diff line number Diff line change
Expand Up @@ -210,12 +210,14 @@
<Compile Include="ScenarioTests\ServerUpgradeTests.cs" />
<Compile Include="ScenarioTests\SqlTestsBase.cs" />
<Compile Include="ScenarioTests\TransparentDataEncryptionCrudTests.cs" />
<Compile Include="ScenarioTests\IndexRecommendationTests.cs" />
<Compile Include="UnitTests\AzureSqlCmdletBaseAttributeTests.cs" />
<Compile Include="UnitTests\AzureSqlDatabaseActivationAttributeTest.cs" />
<Compile Include="UnitTests\AzureSqlDatabaseAttributeTests.cs" />
<Compile Include="UnitTests\AzureSqlDatabaseServerServiceObjectiveAttributeTests.cs" />
<Compile Include="UnitTests\AzureSqlDatabaseServerFirewallRuleAttributeTests.cs" />
<Compile Include="UnitTests\AzureSqlDatabaseServerAttributeTests.cs" />
<Compile Include="UnitTests\AzureSqlDatabaseIndexRecommendationAttributeTests.cs" />
<Compile Include="UnitTests\AzureSqlServerUpgradeAttributeTests.cs" />
<Compile Include="UnitTests\AzureSqlServiceTierAdvisorAttributeTests.cs" />
<Compile Include="UnitTests\AzureSqlDatabaseBackupAttributeTests.cs" />
Expand Down Expand Up @@ -270,6 +272,9 @@
<None Include="ScenarioTests\Common.ps1">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Include="ScenarioTests\IndexRecommendationTests.ps1">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Include="ScenarioTests\ServerUpgradeTests.ps1">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
Expand Down Expand Up @@ -421,6 +426,8 @@
<None Include="SessionRecords\Microsoft.Azure.Commands.Sql.Test.ScenarioTests.ElasticPoolCrudTests\TestElasticPoolUpdate.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Include="SessionRecords\Microsoft.Azure.Commands.Sql.Test.ScenarioTests.IndexRecommendationTests\TestCreateIndex.json" />
<None Include="SessionRecords\Microsoft.Azure.Commands.Sql.Test.ScenarioTests.IndexRecommendationTests\TestGetIndexRecommendation.json" />
<None Include="SessionRecords\Microsoft.Azure.Commands.Sql.Test.ScenarioTests.RecommendedElasticPoolTests\ListRecommendedElasticPools.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// ----------------------------------------------------------------------------------
//
// 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.WindowsAzure.Commands.ScenarioTest;
using Xunit;

namespace Microsoft.Azure.Commands.Sql.Test.ScenarioTests
{
public class IndexRecommendationTests : SqlTestsBase
{
[Fact]
[Trait(Category.Sql, Category.CheckIn)]
public void TestGetIndexRecommendation()
{
RunPowerShellTest("Test-GetIndexRecommendations");
}

[Fact]
[Trait(Category.Sql, Category.CheckIn)]
public void TestCreateIndex()
{
RunPowerShellTest("Test-CreateIndex");
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
# ----------------------------------------------------------------------------------
#
# 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 index recommendations
#>
function Test-GetIndexRecommendations
{
# Get all recommended indexes for server
$response = Get-AzureSqlDatabaseIndexRecommendations -ResourceGroup Group-6 -ServerName witest-eus
ValidateResponse($response)
Assert-AreEqual "Active" $response[0].State

# Get all recommended indexes for database
$response = Get-AzureSqlDatabaseIndexRecommendations -ResourceGroup Group-6 -ServerName witest-eus -DatabaseName witestdb-eus
ValidateResponse($response)
Assert-AreEqual "Active" $response[0].State

# Get recommended indexes by name
$response = Get-AzureSqlDatabaseIndexRecommendations -ResourceGroup Group-6 -ServerName witest-eus -DatabaseName witestdb-eus -IndexRecommendationName nci_wi_Clusters_034590D0-0378-4AB9-96D5-C144B14F6A9B
ValidateResponse($response)
Assert-AreEqual "Active" $response[0].State
}

<#
.SYNOPSIS
Tests starting and canceling index operation
#>
function Test-CreateIndex
{
# Start index operation
$response = Start-AzureSqlDatabaseExecuteIndexRecommendation -ResourceGroup Group-6 -ServerName witest-eus -DatabaseName witestdb-eus -IndexRecommendationName nci_wi_Clusters_034590D0-0378-4AB9-96D5-C144B14F6A9B
Assert-AreEqual "Pending" $response[0].State

# Start index operation
$response = Stop-AzureSqlDatabaseExecuteIndexRecommendation -ResourceGroup Group-6 -ServerName witest-eus -DatabaseName witestdb-eus -IndexRecommendationName nci_wi_Clusters_034590D0-0378-4AB9-96D5-C144B14F6A9B
Assert-AreEqual "Active" $response[0].State
}

function ValidateResponse($response)
{
Assert-NotNull $response
Assert-AreEqual 1 $response.Count
Assert-AreEqual "nci_wi_Clusters_034590D0-0378-4AB9-96D5-C144B14F6A9B" $response[0].Name
Assert-AreEqual "Create" $response[0].Action
Assert-AreEqual '07/21/2015 17:12:32' $response[0].Created
Assert-AreEqual "NONCLUSTERED" $response[0].IndexType
Assert-AreEqual '07/21/2015 17:12:32' $response[0].LastModified
Assert-AreEqual "dbo" $response[0].Schema
Assert-AreEqual "Clusters" $response[0].Table
}

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
// ----------------------------------------------------------------------------------
//
// 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 System;
using Microsoft.Azure.Commands.Sql.Cmdlet;
using Microsoft.Azure.Commands.Sql.Server.Cmdlet;
using Microsoft.Azure.Commands.Sql.ServerUpgrade.Cmdlet;
using Microsoft.Azure.Commands.Sql.Test.Utilities;
using Microsoft.WindowsAzure.Commands.ScenarioTest;
using Xunit;

namespace Microsoft.Azure.Commands.Sql.Test.UnitTests
{
public class AzureSqlDatabaseIndexRecommendationAttributeTests
{
[Fact]
[Trait(Category.Sql, Category.CheckIn)]
public void GetAzureSqlIndexRecommendationAttributes()
{
Type type = typeof(GetAzureSqlDatabaseIndexRecommendations);
UnitTestHelper.CheckConfirmImpact(type, System.Management.Automation.ConfirmImpact.None);

UnitTestHelper.CheckCmdletParameterAttributes(type, "ServerName", isMandatory: true, valueFromPipelineByName: true);
UnitTestHelper.CheckCmdletParameterAttributes(type, "DatabaseName", isMandatory: false, valueFromPipelineByName: true);
UnitTestHelper.CheckCmdletParameterAttributes(type, "IndexRecommendationName", isMandatory: false, valueFromPipelineByName: true);
}

[Fact]
[Trait(Category.Sql, Category.CheckIn)]
public void StartAzureSqlDatabaseExecuteIndexRecommendationAttributes()
{
Type type = typeof(StartAzureSqlDatabaseExecuteIndexRecommendation);
UnitTestHelper.CheckConfirmImpact(type, System.Management.Automation.ConfirmImpact.Low);

UnitTestHelper.CheckCmdletParameterAttributes(type, "ServerName", isMandatory: true, valueFromPipelineByName: true);
UnitTestHelper.CheckCmdletParameterAttributes(type, "DatabaseName", isMandatory: true, valueFromPipelineByName: true);
UnitTestHelper.CheckCmdletParameterAttributes(type, "IndexRecommendationName", isMandatory: true, valueFromPipelineByName: true);
}

[Fact]
[Trait(Category.Sql, Category.CheckIn)]
public void StopAzureSqlDatabaseExecuteIndexRecommendationAttributes()
{
Type type = typeof(StopAzureSqlDatabaseExecuteIndexRecommendation);
UnitTestHelper.CheckConfirmImpact(type, System.Management.Automation.ConfirmImpact.Low);

UnitTestHelper.CheckCmdletParameterAttributes(type, "ServerName", isMandatory: true, valueFromPipelineByName: true);
UnitTestHelper.CheckCmdletParameterAttributes(type, "DatabaseName", isMandatory: true, valueFromPipelineByName: true);
UnitTestHelper.CheckCmdletParameterAttributes(type, "IndexRecommendationName", isMandatory: true, valueFromPipelineByName: true);
}
}
}
7 changes: 7 additions & 0 deletions src/ResourceManager/Sql/Commands.Sql/Commands.Sql.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,13 @@
<Compile Include="ServiceTierAdvisor\Model\UpgradeServerHint.cs" />
<Compile Include="ServiceTierAdvisor\Services\AzureSqlServiceTierAdvisorAdapter.cs" />
<Compile Include="ServiceTierAdvisor\Services\AzureSqlServiceTierAdvisorCommunicator.cs" />
<Compile Include="Index Recommendations\Cmdlet\AzureSqlDatabaseExecuteIndexRecommendationCmdletBase.cs" />
<Compile Include="Index Recommendations\Cmdlet\GetAzureSqlDatabaseIndexRecommendations.cs" />
<Compile Include="Index Recommendations\Cmdlet\StartAzureSqlDatabaseExecuteIndexRecommendation.cs" />
<Compile Include="Index Recommendations\Cmdlet\StopAzureSqlDatabaseExecuteIndexRecommendation.cs" />
<Compile Include="Index Recommendations\Model\IndexRecommendation.cs" />
<Compile Include="Index Recommendations\Service\AzureSqlDatabaseIndexRecommendationAdapter.cs" />
<Compile Include="Index Recommendations\Service\AzureSqlDatabaseIndexRecommendationCommunicator.cs" />
<Compile Include="TransparentDataEncryption\Cmdlet\AzureSqlDatabaseTransparentDataEncryptionActivityCmdletBase.cs" />
<Compile Include="TransparentDataEncryption\Cmdlet\AzureSqlDatabaseTransparentDataEncryptionCmdletBase.cs" />
<Compile Include="TransparentDataEncryption\Cmdlet\GetAzureSqlDatabaseTransparentDataEncryption.cs" />
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
// ----------------------------------------------------------------------------------
//
// 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 System.Linq;
using System.Management.Automation;
using Microsoft.Azure.Commands.Sql.Common;
using Microsoft.Azure.Commands.Sql.Model;
using Microsoft.Azure.Commands.Sql.Service;
using Microsoft.Azure.Common.Authentication.Models;

namespace Microsoft.Azure.Commands.Sql.Cmdlet
{
/// <summary>
/// The base class of cmdlets for Azure SQL Recommended Index
/// </summary>
public abstract class AzureSqlDatabaseExecuteIndexRecommendationCmdletBase : AzureSqlCmdletBase<IndexRecommendation, AzureSqlDatabaseIndexRecommendationAdapter>
{
/// <summary>
/// String constants for different error states.
/// </summary>
public class IndexState
{
public const string Active = "Active";
public const string Pending = "Pending";
public const string Error = "Error";
public const string PendingRevert = "Pending Revert";
public const string RevertCanceled = "Revert Canceled";
}

/// <summary>
/// Gets or sets the name of the server to use.
/// </summary>
[Parameter(Mandatory = true,
ValueFromPipelineByPropertyName = true,
HelpMessage = "Azure SQL Server name.")]
[ValidateNotNullOrEmpty]
public string ServerName { get; set; }

/// <summary>
/// Gets or sets the name of the database
/// </summary>
[Parameter(Mandatory = true,
ValueFromPipelineByPropertyName = true,
HelpMessage = "Azure SQL Database name.")]
[ValidateNotNullOrEmpty]
public string DatabaseName { get; set; }

/// <summary>
/// Gets or sets the name of the index recommendation.
/// </summary>
[Parameter(Mandatory = true,
ValueFromPipelineByPropertyName = true,
HelpMessage = "Azure SQL Index Recommendation name.")]
[ValidateNotNullOrEmpty]
public string IndexRecommendationName { get; set; }

/// <summary>
/// Return index recommendation with IndexRecommendationName name.
/// </summary>
/// <returns>Index recommendation with IndexRecommendationName name</returns>
protected override IndexRecommendation GetEntity()
{
var indexRecommendation = ModelAdapter.ListRecommendedIndexes(ResourceGroupName, ServerName, DatabaseName)
.Single(i => i.Name == IndexRecommendationName);
return indexRecommendation;
}

/// <summary>
/// Sends the changes to the service, this will trigger applying action.
/// </summary>
/// <param name="recommendation">Index recommendation</param>
/// <returns>Index recommendation</returns>
protected override IndexRecommendation PersistChanges(IndexRecommendation recommendation)
{
ModelAdapter.UpdateRecommendationState(ResourceGroupName, ServerName, recommendation);
return recommendation;
}

/// <summary>
/// Initializes the model adapter
/// </summary>
/// <param name="subscription">The subscription the cmdlets are operation under</param>
/// <returns>The recommended index adapter</returns>
protected override AzureSqlDatabaseIndexRecommendationAdapter InitModelAdapter(AzureSubscription subscription)
{
return new AzureSqlDatabaseIndexRecommendationAdapter(Profile, subscription);
}
}
}
Loading