Skip to content

Support for Serverless specific parameters for CRUD #9105

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 20 commits into from
May 3, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
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
14 changes: 14 additions & 0 deletions src/Sql/Sql.Test/ScenarioTests/DatabaseCrudTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,13 @@ public void TestVcoreDatabaseCreateWithLicenseType()
RunPowerShellTest("Test-CreateVcoreDatabaseWithLicenseType");
}

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

[Fact]
[Trait(Category.AcceptanceType, Category.CheckIn)]
public void TestDatabaseCreateWithSampleName()
Expand Down Expand Up @@ -103,6 +110,13 @@ public void TestDatabaseUpdateWithZoneRedundancyNotSpecified()
RunPowerShellTest("Test-UpdateDatabaseWithZoneRedundantNotSpecified");
}

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

[Fact]
[Trait(Category.AcceptanceType, Category.CheckIn)]
public void TestDatabaseRename()
Expand Down
101 changes: 101 additions & 0 deletions src/Sql/Sql.Test/ScenarioTests/DatabaseCrudTests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,39 @@ function Test-CreateVcoreDatabaseWithLicenseType
}
}

<#
.SYNOPSIS
Tests creating a Serverless based database
#>
function Test-CreateServerlessDatabase
{
# Setup
$location = Get-Location "Microsoft.Sql" "operations" "Japan East"
$rg = Create-ResourceGroupForTest $location
$server = Create-ServerForTest $rg $location

try
{
# Create with Edition and RequestedServiceObjectiveName
$databaseName = Get-DatabaseName
$job1 = New-AzSqlDatabase -ResourceGroupName $rg.ResourceGroupName -ServerName $server.ServerName -DatabaseName $databaseName -RequestedServiceObjectiveName GP_S_Gen5_2 -AutoPauseDelayInMinutes 360 -MinVCore 0.5 -AsJob
$job1 | Wait-Job
$db = $job1.Output

Assert-AreEqual $databaseName $db.DatabaseName
Assert-NotNull $db.MaxSizeBytes
Assert-AreEqual GP_S_Gen5_2 $db.CurrentServiceObjectiveName
Assert-AreEqual 2 $db.Capacity
Assert-AreEqual 360 $db.AutoPauseDelayInMinutes
Assert-AreEqual 0.5 $db.MinimumCapacity
Assert-AreEqual GeneralPurpose $db.Edition
}
finally
{
Remove-ResourceGroupForTest $rg
}
}

<#
.SYNOPSIS
Tests creating a database with sample name.
Expand Down Expand Up @@ -512,6 +545,74 @@ function Test-UpdateDatabaseWithZoneRedundant ()
}
}

<#
.SYNOPSIS
Tests updating a vcore database
#>
function Test-UpdateServerlessDatabase()
Copy link
Contributor

Choose a reason for hiding this comment

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

Good test, thx

{
# Setup
$location = Get-Location "Microsoft.Sql" "operations" "Japan East"
$rg = Create-ResourceGroupForTest $location
$server = Create-ServerForTest $rg $location

$databaseName = Get-DatabaseName
$db = New-AzSqlDatabase -ResourceGroupName $rg.ResourceGroupName -ServerName $server.ServerName -DatabaseName $databaseName `
-VCore 2 -Edition GeneralPurpose -ComputeGeneration Gen5 -MaxSizeBytes 250GB -ComputeModel Serverless
Assert-AreEqual $db.DatabaseName $databaseName

try
{
# Alter with defaults
$job = Set-AzSqlDatabase -ResourceGroupName $db.ResourceGroupName -ServerName $db.ServerName -DatabaseName $db.DatabaseName `
-AsJob
$job | Wait-Job
$db1 = $job.Output

Assert-AreEqual $db1.DatabaseName $db.DatabaseName
Assert-NotNull $db1.MaxSizeBytes
Assert-NotNull $db1.Edition
Assert-NotNull $db1.CurrentServiceObjectiveName
Assert-NotNull $db1.MinimumCapacity
Assert-NotNull $db1.AutoPauseDelayInMinutes

# Alter to dtu database
$job = Set-AzSqlDatabase -ResourceGroupName $db.ResourceGroupName -ServerName $db.ServerName -DatabaseName $db.DatabaseName `
-Edition Premium -AsJob
$job | Wait-Job
$db1 = $job.Output
Assert-AreEqual $db1.DatabaseName $db.DatabaseName
Assert-AreEqual $db1.Edition Premium
Assert-Null $db1.MinimumCapacity
Assert-Null $db1.AutoPauseDelayInMinutes

# Alter back to Serverless
$job = Set-AzSqlDatabase -ResourceGroupName $db.ResourceGroupName -ServerName $db.ServerName -DatabaseName $db.DatabaseName `
-VCore 2 -Edition GeneralPurpose -ComputeModel Serverless -ComputeGeneration Gen5 -AsJob
$job | Wait-Job
$db1 = $job.Output
Assert-AreEqual $db1.DatabaseName $db.DatabaseName
Assert-AreEqual $db1.Edition GeneralPurpose
Assert-AreEqual $db1.CurrentServiceObjectiveName GP_S_Gen5_2
Assert-NotNull $db1.MinimumCapacity
Assert-NotNull $db1.AutoPauseDelayInMinutes

# Alter mincapacity and AutoPauseDelayInMinutes
$job = Set-AzSqlDatabase -ResourceGroupName $db.ResourceGroupName -ServerName $db.ServerName -DatabaseName $db.DatabaseName `
-Vcore 2 -Edition GeneralPurpose -ComputeModel Serverless -ComputeGeneration Gen5 -MinimumCapacity 2 -AutoPauseDelayInMinutes 1440 -AsJob
$job | Wait-Job
$db1 = $job.Output
Assert-AreEqual $db1.DatabaseName $db.DatabaseName
Assert-AreEqual $db1.Edition GeneralPurpose
Assert-AreEqual $db1.CurrentServiceObjectiveName GP_S_Gen5_2
Assert-AreEqual $db1.MinimumCapacity 2
Assert-AreEqual $db1.AutoPauseDelayInMinutes 1440
}
finally
{
Remove-ResourceGroupForTest $rg
}
}
<#
.SYNOPSIS
Tests updating a database with zone redundancy not specified
Expand Down

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 @@ -12,7 +12,7 @@

<ItemGroup>
<PackageReference Include="Microsoft.Azure.Management.Monitor" Version="0.22.0-preview" />
<PackageReference Include="Microsoft.Azure.Management.Sql" Version="1.29.0-preview" />
<PackageReference Include="Microsoft.Azure.Management.Sql" Version="1.31.0-preview" />
<PackageReference Include="Microsoft.Azure.Management.EventHub" Version="2.5.0" />
<PackageReference Include="Microsoft.Azure.Management.Network" Version="19.11.0-preview" />
<PackageReference Include="Microsoft.Azure.Graph.RBAC" Version="3.2.0-preview" />
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 @@ -27,6 +27,7 @@
* Ability to Get-AzSqlServerServiceObjective by location without needing a preexisting server in the region.
* Support for time zone parameter in Managed Instance create.
* Fix documentation for wildcards
* Support of Serverless specific parameters in New-AzSqlDatabase and Set-AzSqlDatabase

## Version 1.8.0
* Support Database Data Classification.
Expand Down
33 changes: 30 additions & 3 deletions src/Sql/Sql/Database/Cmdlet/NewAzureSqlDatabase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ public class NewAzureSqlDatabase : AzureSqlDatabaseCmdletBase<AzureSqlDatabaseCr
/// </summary>
[Parameter(ParameterSetName = VcoreDatabaseParameterSet, Mandatory = true,
HelpMessage = "The Vcore number for the Azure Sql database")]
[Alias("Capacity")]
[Alias("Capacity", "MaxVCore", "MaxCapacity")]
public int VCore { get; set; }

/// <summary>
Expand All @@ -159,6 +159,31 @@ public class NewAzureSqlDatabase : AzureSqlDatabaseCmdletBase<AzureSqlDatabaseCr
Management.Sql.Models.DatabaseLicenseType.BasePrice)]
public string LicenseType { get; set; }

/// <summary>
/// Gets or sets the compute model for Azure Sql database
/// </summary>
[Parameter(ParameterSetName = VcoreDatabaseParameterSet, Mandatory = false,
HelpMessage="The compute model for database. Serverless or Provisioned")]
[PSArgumentCompleter(
DatabaseComputeModel.Provisioned,
DatabaseComputeModel.Serverless)]
public string ComputeModel { get; set; }

/// <summary>
/// Gets or sets the Auto Pause delay for Azure Sql Database
/// </summary>
[Parameter(Mandatory = false,
HelpMessage = "The auto pause delay in minutes for database(serverless only), -1 to opt out from pausing")]
public int AutoPauseDelayInMinutes { get; set; }

/// <summary>
/// Gets or sets the Minimal capacity that database will always have allocated, if not paused
/// </summary>
[Parameter(Mandatory = false,
HelpMessage = "The Minimal capacity that database will always have allocated, if not paused. For serverless database only.")]
[Alias("MinVCore", "MinCapacity")]
public double MinimumCapacity { get; set; }

/// <summary>
/// Overriding to add warning message
/// </summary>
Expand Down Expand Up @@ -218,7 +243,9 @@ protected override AzureSqlDatabaseCreateOrUpdateModel ApplyUserInputToModel(Azu
ElasticPoolName = ElasticPoolName,
ReadScale = ReadScale,
ZoneRedundant = MyInvocation.BoundParameters.ContainsKey("ZoneRedundant") ? (bool?)ZoneRedundant.ToBool() : null,
LicenseType = LicenseType // note: default license type will be LicenseIncluded in SQL RP if not specified
LicenseType = LicenseType, // note: default license type will be LicenseIncluded in SQL RP if not specified
AutoPauseDelayInMinutes = MyInvocation.BoundParameters.ContainsKey("AutoPauseDelayInMinutes") ? AutoPauseDelayInMinutes : (int?)null,
MinimumCapacity = MyInvocation.BoundParameters.ContainsKey("AutoPauseDelayInMinutes") ? MinimumCapacity : (double?)null,
};

if(ParameterSetName == DtuDatabaseParameterSet)
Expand All @@ -228,7 +255,7 @@ protected override AzureSqlDatabaseCreateOrUpdateModel ApplyUserInputToModel(Azu
}
else
{
newDbModel.SkuName = AzureSqlDatabaseAdapter.GetDatabaseSkuName(Edition);
newDbModel.SkuName = AzureSqlDatabaseAdapter.GetDatabaseSkuName(Edition, ComputeModel == DatabaseComputeModel.Serverless);
newDbModel.Edition = Edition;
newDbModel.Capacity = VCore;
newDbModel.Family = ComputeGeneration;
Expand Down
49 changes: 46 additions & 3 deletions src/Sql/Sql/Database/Cmdlet/SetAzureSqlDatabase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ public class SetAzureSqlDatabase : AzureSqlDatabaseCmdletBase<IEnumerable<AzureS
/// </summary>
[Parameter(ParameterSetName = VcoreDatabaseParameterSet, Mandatory = false,
HelpMessage = "The Vcore number for the Azure Sql database")]
[Alias("Capacity")]
[Alias("Capacity", "MaxVCore", "MaxCapacity")]
public int VCore { get; set; }

/// <summary>
Expand All @@ -176,6 +176,43 @@ public class SetAzureSqlDatabase : AzureSqlDatabaseCmdletBase<IEnumerable<AzureS
Management.Sql.Models.DatabaseLicenseType.BasePrice)]
public string LicenseType { get; set; }

/// <summary>
/// Gets or sets the compute model for the Azure Sql Database
/// </summary>
[Parameter(Mandatory = false,
HelpMessage = "The computed model of database. Serverless or Provisioned",
ParameterSetName = UpdateParameterSetName)]
[Parameter(Mandatory = false,
HelpMessage = "The computed model of database. Serverless or Provisioned",
ParameterSetName = VcoreDatabaseParameterSet)]
[PSArgumentCompleter(
DatabaseComputeModel.Provisioned,
DatabaseComputeModel.Serverless)]
public string ComputeModel { get; set; }

/// <summary>
/// Gets or sets the auto pause delay for the Azure Sql Database
/// </summary>
[Parameter(Mandatory = false,
HelpMessage = "The auto pause delay in minutes for database (serverless only), -1 to opt out from pausing",
ParameterSetName = UpdateParameterSetName)]
[Parameter(Mandatory = false,
HelpMessage = "The auto pause delay in minutes for database (serverless only), -1 to opt out from pausing",
ParameterSetName = VcoreDatabaseParameterSet)]
public int AutoPauseDelayInMinutes { get; set; }

/// <summary>
/// Gets or sets the Minimal capacity that database will always have allocated, if not paused
/// </summary>
[Parameter(Mandatory = false,
HelpMessage = "The Minimal capacity that database will always have allocated, if not paused. For serverless database only.",
ParameterSetName = UpdateParameterSetName)]
[Parameter(Mandatory = false,
HelpMessage = "The Minimal capacity that database will always have allocated, if not paused. For serverless database only.",
ParameterSetName = VcoreDatabaseParameterSet)]
[Alias("MinVCore", "MinCapacity")]
public double MinimumCapacity { get; set; }

/// <summary>
/// Overriding to add warning message
/// </summary>
Expand Down Expand Up @@ -217,7 +254,9 @@ protected override IEnumerable<AzureSqlDatabaseModel> ApplyUserInputToModel(IEnu
ZoneRedundant != null
? (bool?)ZoneRedundant.ToBool()
: null,
LicenseType = LicenseType ?? model.FirstOrDefault().LicenseType // set to original license type
LicenseType = LicenseType ?? model.FirstOrDefault().LicenseType, // set to original license type
AutoPauseDelayInMinutes = MyInvocation.BoundParameters.ContainsKey("AutoPauseDelayInMinutes") ? AutoPauseDelayInMinutes : (int?)null,
MinimumCapacity = MyInvocation.BoundParameters.ContainsKey("MinimumCapacity") ? MinimumCapacity : (double?)null,
};

var database = ModelAdapter.GetDatabase(ResourceGroupName, ServerName, DatabaseName);
Expand All @@ -229,6 +268,9 @@ protected override IEnumerable<AzureSqlDatabaseModel> ApplyUserInputToModel(IEnu
Capacity = database.Capacity
};

// check if current db is serverless
string databaseCurrentComputeModel = database.CurrentServiceObjectiveName.Contains("_S_") ? DatabaseComputeModel.Serverless : DatabaseComputeModel.Provisioned;

if (this.ParameterSetName == UpdateParameterSetName)
{
newDbModel.SkuName = string.IsNullOrWhiteSpace(RequestedServiceObjectiveName) ? AzureSqlDatabaseAdapter.GetDatabaseSkuName(Edition) : RequestedServiceObjectiveName;
Expand All @@ -243,7 +285,8 @@ protected override IEnumerable<AzureSqlDatabaseModel> ApplyUserInputToModel(IEnu
MyInvocation.BoundParameters.ContainsKey("VCore"))
{
string skuTier = string.IsNullOrWhiteSpace(Edition) ? databaseCurrentSku.Tier : Edition;
newDbModel.SkuName = AzureSqlDatabaseAdapter.GetDatabaseSkuName(skuTier);
string requestedComputeModel = string.IsNullOrWhiteSpace(ComputeModel) ? databaseCurrentComputeModel : ComputeModel;
newDbModel.SkuName = AzureSqlDatabaseAdapter.GetDatabaseSkuName(skuTier, requestedComputeModel == DatabaseComputeModel.Serverless);
newDbModel.Edition = skuTier;
newDbModel.Family = string.IsNullOrWhiteSpace(ComputeGeneration) ? databaseCurrentSku.Family : ComputeGeneration;
newDbModel.Capacity = MyInvocation.BoundParameters.ContainsKey("VCore") ? VCore : databaseCurrentSku.Capacity;
Expand Down
15 changes: 15 additions & 0 deletions src/Sql/Sql/Database/Model/AzureSqlDatabaseModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,16 @@ public class AzureSqlDatabaseModel
/// </summary>
public string LicenseType { get; set; }

/// <summary>
/// Gets or sets the Auto pause delay of the database
/// </summary>
public int? AutoPauseDelayInMinutes { get; set; }

/// <summary>
/// Minimal capacity that database will always have allocated, if not paused
/// </summary>
public double? MinimumCapacity { get; set; }

/// <summary>
/// Construct AzureSqlDatabaseModel
/// </summary>
Expand Down Expand Up @@ -213,6 +223,8 @@ public AzureSqlDatabaseModel(string resourceGroup, string serverName, Management
ReadScale = readScale;

ZoneRedundant = false;
AutoPauseDelayInMinutes = null;
MinimumCapacity = null;
Copy link
Contributor

Choose a reason for hiding this comment

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

You should be setting these. Otherwise they will always be null in response!

Copy link
Contributor

Choose a reason for hiding this comment

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

Sorry, I'm wrong. This is converting from legacy model

}

/// <summary>
Expand Down Expand Up @@ -259,6 +271,9 @@ public AzureSqlDatabaseModel(string resourceGroup, string serverName, Management
SkuName = database.Sku == null ? null : database.Sku.Name;

LicenseType = database.LicenseType;

AutoPauseDelayInMinutes = database.AutoPauseDelay;
MinimumCapacity = database.MinCapacity;
}
}
}
13 changes: 13 additions & 0 deletions src/Sql/Sql/Database/Model/DatabaseComputeModel.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using System;
using System.Collections.Generic;
using System.Text;

namespace Microsoft.Azure.Commands.Sql.Database.Model
{
public class DatabaseComputeModel
{
public const string Provisioned = "Provisioned";

public const string Serverless = "Serverless";
}
}
11 changes: 8 additions & 3 deletions src/Sql/Sql/Database/Services/AzureSqlDatabaseAdapter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,9 @@ internal AzureSqlDatabaseModel UpsertDatabaseWithNewSdk(string resourceGroup, st
SampleName = model.SampleName,
ZoneRedundant = model.Database.ZoneRedundant,
ElasticPoolId = elasticPoolId,
LicenseType = model.Database.LicenseType
LicenseType = model.Database.LicenseType,
AutoPauseDelay = model.Database.AutoPauseDelayInMinutes,
MinCapacity = model.Database.MinimumCapacity,
});

return CreateDatabaseModelFromResponse(resourceGroup, serverName, resp);
Expand Down Expand Up @@ -338,17 +340,20 @@ public void RenameDatabase(string resourceGroupName, string serverName, string d
/// Standard | Standard
/// Basic | Basic
/// Premium | Premium
///
/// Also adds _S in the end of SkuName in case if it is Serverless
/// </summary>
/// <param name="tier">Azure Sql database edition</param>
/// <param name="isServerless">If sku should be serverless type</param>
/// <returns>The sku name</returns>
public static string GetDatabaseSkuName(string tier)
public static string GetDatabaseSkuName(string tier, bool isServerless = false)
{
if (string.IsNullOrWhiteSpace(tier))
{
return null;
}

return SqlSkuUtils.GetVcoreSkuPrefix(tier) ?? tier;
return (SqlSkuUtils.GetVcoreSkuPrefix(tier) ?? tier) + (isServerless ? "_S" : "");
Copy link
Contributor

Choose a reason for hiding this comment

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

There is already a unit test for this method, would be nice if you added isServerless coverage to that test.

}

/// <summary>
Expand Down
Loading