Skip to content

Commit

Permalink
Add resource group based LTR APIs + tests to azure sdk (Azure#6957)
Browse files Browse the repository at this point in the history
* Add resource group based LTR APIs + tests

* Update change history

* Bump Sql AssemblyInfo

* remove unnecessary checks

* remove unnecessary checks
  • Loading branch information
pixia authored and erich-wang committed Jul 19, 2019
1 parent a3d7eb2 commit 9f2816c
Show file tree
Hide file tree
Showing 11 changed files with 9,248 additions and 471 deletions.
4 changes: 2 additions & 2 deletions eng/mgmt/mgmtmetadata/sql_resource-manager.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ Commencing code generation
Generating CSharp code
Executing AutoRest command
cmd.exe /c autorest.cmd https://github.com/Azure/azure-rest-api-specs/blob/master/specification/sql/resource-manager/readme.md --csharp --version=latest --reflect-api-versions --csharp-sdks-folder=.
2019-07-16 23:22:54 UTC
2019-07-16 21:28:54 UTC
Azure-rest-api-specs repository information
GitHub fork: Azure
Branch: master
Commit: 6203666516223e8de80552d70c7157806a50f78b
Commit: 737110f8edd3689144c45d6fd34e7c6143171ab8
AutoRest information
Requested version: latest
Bootstrapper version: [email protected]

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -115,10 +115,9 @@ public static IEnumerable<Tuple<string, string, string>> ApiInfo_SqlManagementCl
public static readonly String AutoRestCmdExecuted = "cmd.exe /c autorest.cmd https://github.com/Azure/azure-rest-api-specs/blob/master/specification/sql/resource-manager/readme.md --csharp --version=latest --reflect-api-versions --csharp-sdks-folder=.";
public static readonly String GithubForkName = "Azure";
public static readonly String GithubBranchName = "master";
public static readonly String GithubCommidId = "6203666516223e8de80552d70c7157806a50f78b";
public static readonly String GithubCommidId = "737110f8edd3689144c45d6fd34e7c6143171ab8";
public static readonly String CodeGenerationErrors = "";
public static readonly String GithubRepoName = "azure-rest-api-specs";
// END: Code Generation Metadata Section
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -482,6 +482,19 @@ public partial class SqlManagementClient : ServiceClient<SqlManagementClient>, I
/// </summary>
public virtual IManagedInstancesOperations ManagedInstances { get; private set; }

/// <summary>
/// Initializes a new instance of the SqlManagementClient class.
/// </summary>
/// <param name='httpClient'>
/// HttpClient to be used
/// </param>
/// <param name='disposeHttpClient'>
/// True: will dispose the provided httpClient on calling SqlManagementClient.Dispose(). False: will not dispose provided httpClient</param>
protected SqlManagementClient(HttpClient httpClient, bool disposeHttpClient) : base(httpClient, disposeHttpClient)
{
Initialize();
}

/// <summary>
/// Initializes a new instance of the SqlManagementClient class.
/// </summary>
Expand Down Expand Up @@ -577,6 +590,33 @@ public SqlManagementClient(ServiceClientCredentials credentials, params Delegati
}
}

/// <summary>
/// Initializes a new instance of the SqlManagementClient class.
/// </summary>
/// <param name='credentials'>
/// Required. Credentials needed for the client to connect to Azure.
/// </param>
/// <param name='httpClient'>
/// HttpClient to be used
/// </param>
/// <param name='disposeHttpClient'>
/// True: will dispose the provided httpClient on calling SqlManagementClient.Dispose(). False: will not dispose provided httpClient</param>
/// <exception cref="System.ArgumentNullException">
/// Thrown when a required parameter is null
/// </exception>
public SqlManagementClient(ServiceClientCredentials credentials, HttpClient httpClient, bool disposeHttpClient) : this(httpClient, disposeHttpClient)
{
if (credentials == null)
{
throw new System.ArgumentNullException("credentials");
}
Credentials = credentials;
if (Credentials != null)
{
Credentials.InitializeServiceClient(this);
}
}

/// <summary>
/// Initializes a new instance of the SqlManagementClient class.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
<PackageReleaseNotes>
<![CDATA[
New features:
- Added support for resource group based LTR APIs.
- Added support for failover databases and elastic pools.
]]>
</PackageReleaseNotes>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -214,13 +214,29 @@ public void TestLongTermRetentionV2Backups()
//
IPage<LongTermRetentionBackup> backups = sqlClient.LongTermRetentionBackups.ListByLocation(server.Location);
backups = sqlClient.LongTermRetentionBackups.ListByServer(server.Location, server.Name);
Assert.True(backups.Count() == 0);
backups = sqlClient.LongTermRetentionBackups.ListByDatabase(server.Location, server.Name, database.Name);
Assert.True(backups.Count() == 0);
Assert.Throws(typeof(CloudException), () => sqlClient.LongTermRetentionBackups.Get(server.Location, server.Name, database.Name, "backup"));
}
}

[Fact]
public void TestLongTermRetentionV2ResourceGroupBasedBackups()
{
using (SqlManagementTestContext context = new SqlManagementTestContext(this))
{
ResourceGroup resourceGroup = context.CreateResourceGroup();
Server server = context.CreateServer(resourceGroup);
SqlManagementClient sqlClient = context.GetClient<SqlManagementClient>();
Database database = sqlClient.Databases.CreateOrUpdate(resourceGroup.Name, server.Name, SqlManagementTestUtilities.GenerateName(), new Database { Location = server.Location });

// Get the backups under the resource group, server and database. Assert there are no backups returned.
//
IPage<LongTermRetentionBackup> backups = sqlClient.LongTermRetentionBackups.ListByResourceGroupLocation(resourceGroup.Name, server.Location);
backups = sqlClient.LongTermRetentionBackups.ListByResourceGroupServer(resourceGroup.Name, server.Location, server.Name);
backups = sqlClient.LongTermRetentionBackups.ListByResourceGroupDatabase(resourceGroup.Name, server.Location, server.Name, database.Name);
Assert.Throws(typeof(CloudException), () => sqlClient.LongTermRetentionBackups.GetByResourceGroup(resourceGroup.Name, server.Location, server.Name, database.Name, "backup"));
}
}

[Fact]
public void TestShortTermRetentionPolicyOnPremium()
Expand Down Expand Up @@ -293,32 +309,24 @@ public void TestShortTermRetentionPolicyOnBasic()
[Fact(Skip = "Manual test due to long setup time required (over 18 hours).")]
public void TestLongTermRetentionV2Crud()
{
// MANUAL INSTRUCTIONS
// Create a server and database and fill in the appropriate information below
// Set the weekly retention on the database so that the first backup gets picked up
// Wait about 18 hours until it gets properly copied and you see the backup when run get backups
// MANUAL TEST INSTRUCTIONS
// PlayBack Mode:
// Remove skip flag
// Record Mode:
// Create a server and database and fill in the appropriate information below
// Set the weekly retention on the database so that the first backup gets picked up
// Wait about 18 hours until it gets properly copied and you see the backup when run get backups
//
string locationName = "";
string resourceGroupName = "";
string serverName = "";
string databaseName = "";
string locationName = "brazilsouth";
string resourceGroupName = "brrg";
string serverName = "ltrtest3";
string databaseName = "mydb";

using (SqlManagementTestContext context = new SqlManagementTestContext(this))
{
SqlManagementClient sqlClient = context.GetClient<SqlManagementClient>();
Database database = sqlClient.Databases.Get(resourceGroupName, serverName, databaseName);

// Set the retention policy to two weeks for the weekly retention policy
//
Microsoft.Azure.Management.Sql.Models.BackupLongTermRetentionPolicy parameters = new Microsoft.Azure.Management.Sql.Models.BackupLongTermRetentionPolicy(weeklyRetention: "P2W");
var policyResult = sqlClient.BackupLongTermRetentionPolicies.CreateOrUpdateWithHttpMessagesAsync(resourceGroupName, serverName, databaseName, parameters).Result;
sqlClient.GetPutOrPatchOperationResultAsync(policyResult, new Dictionary<string, List<string>>(), CancellationToken.None).Wait();

// Get the policy and verify the weekly policy is two weeks
//
Microsoft.Azure.Management.Sql.Models.BackupLongTermRetentionPolicy policy = sqlClient.BackupLongTermRetentionPolicies.Get(resourceGroupName, serverName, databaseName);
Assert.Equal(parameters.WeeklyRetention, policy.WeeklyRetention);

// Get the backups under the location, server, and database. Assert there is at least one backup for each call.
//
IPage<LongTermRetentionBackup> backups = sqlClient.LongTermRetentionBackups.ListByLocation(locationName);
Expand Down Expand Up @@ -346,30 +354,59 @@ public void TestLongTermRetentionV2Crud()

// Delete the backup.
//
var deleteResult = sqlClient.LongTermRetentionBackups.BeginDeleteWithHttpMessagesAsync(locationName, serverName, databaseName, backup.Name).Result;
sqlClient.GetPutOrPatchOperationResultAsync(deleteResult, new Dictionary<string, List<string>>(), CancellationToken.None).Wait();
sqlClient.LongTermRetentionBackups.DeleteWithHttpMessagesAsync(locationName, serverName, databaseName, backup.Name);
}
}

[Fact(Skip = "Manual test due to long setup time required (over 18 hours).")]
public void TestLongTermRetentionV2ResourceGroupBasedCrud()
{
// MANUAL TEST INSTRUCTIONS
// PlayBack Mode:
// Remove skip flag
// Record Mode:
// Create a server and database and fill in the appropriate information below
// Set the weekly retention on the database so that the first backup gets picked up
// Wait about 18 hours until it gets properly copied and you see the backup when run get backups
//
string locationName = "brazilsouth";
string resourceGroupName = "brrg";
string serverName = "ltrtest3";
string databaseName = "mydb";

using (SqlManagementTestContext context = new SqlManagementTestContext(this))
{
SqlManagementClient sqlClient = context.GetClient<SqlManagementClient>();
Database database = sqlClient.Databases.Get(resourceGroupName, serverName, databaseName);

// Get the backups under the location, server, and database. Assert there is at least one backup for each call.
//
IPage<LongTermRetentionBackup> backups = sqlClient.LongTermRetentionBackups.ListByResourceGroupLocation(resourceGroupName, locationName);
Assert.True(backups.Count() >= 1);
backups = sqlClient.LongTermRetentionBackups.ListByResourceGroupServer(resourceGroupName, locationName, serverName);
Assert.True(backups.Count() >= 1);
backups = sqlClient.LongTermRetentionBackups.ListByResourceGroupDatabase(resourceGroupName, locationName, serverName, databaseName);
Assert.True(backups.Count() >= 1);

// Verify the backup is gone.
// Get a specific backup using the previous call
//
try
{
sqlClient.LongTermRetentionBackups.Get(locationName, serverName, databaseName, backup.Name);
}
catch (CloudException e)
{
Assert.Contains(string.Format("'{0}' was not found.", backup.Name), e.Message);
}
LongTermRetentionBackup backup = sqlClient.LongTermRetentionBackups.GetByResourceGroup(resourceGroupName, locationName, serverName, databaseName, backups.First().Name);
Assert.NotNull(backup);

// Set the retention policy back to one week for the weekly retention policy
// Restore the backup
//
parameters = new Microsoft.Azure.Management.Sql.Models.BackupLongTermRetentionPolicy(weeklyRetention: "P1W");
policyResult = sqlClient.BackupLongTermRetentionPolicies.CreateOrUpdateWithHttpMessagesAsync(resourceGroupName, serverName, databaseName, parameters).Result;
sqlClient.GetPutOrPatchOperationResultAsync(policyResult, new Dictionary<string, List<string>>(), CancellationToken.None).Wait();
Database restoredDatabase = sqlClient.Databases.CreateOrUpdate(
resourceGroupName, serverName, databaseName: SqlManagementTestUtilities.GenerateName(),
parameters: new Database
{
Location = locationName,
CreateMode = CreateMode.RestoreLongTermRetentionBackup,
LongTermRetentionBackupResourceId = backup.Id
});

// Get the policy and verify the weekly policy is two weeks
// Delete the backup.
//
policy = sqlClient.BackupLongTermRetentionPolicies.Get(resourceGroupName, serverName, databaseName);
Assert.Equal(parameters.WeeklyRetention, policy.WeeklyRetention);
sqlClient.LongTermRetentionBackups.DeleteByResourceGroupWithHttpMessagesAsync(resourceGroupName, locationName, serverName, databaseName, backup.Name);
}
}

Expand Down
Loading

0 comments on commit 9f2816c

Please sign in to comment.