From 6edac7ea24f245d0b01e858423f1b16563394b87 Mon Sep 17 00:00:00 2001 From: Jared Moore Date: Wed, 7 Feb 2018 13:30:04 -0800 Subject: [PATCH] Add convenient Database.Rename methods Fixes #4055 --- .../Management.Sql/DatabasesOperations.cs | 65 +++++++++++++++ .../DatabasesOperationsExtensions.cs | 80 +++++++++++++++++++ .../Management.Sql/IDatabasesOperations.cs | 51 ++++++++++++ .../Microsoft.Azure.Management.Sql.csproj | 6 +- .../Management.Sql/Properties/AssemblyInfo.cs | 2 +- .../Sql.Tests/DatabaseCrudScenarioTests.cs | 16 +++- 6 files changed, 213 insertions(+), 7 deletions(-) create mode 100644 src/SDKs/SqlManagement/Management.Sql/DatabasesOperations.cs create mode 100644 src/SDKs/SqlManagement/Management.Sql/DatabasesOperationsExtensions.cs create mode 100644 src/SDKs/SqlManagement/Management.Sql/IDatabasesOperations.cs diff --git a/src/SDKs/SqlManagement/Management.Sql/DatabasesOperations.cs b/src/SDKs/SqlManagement/Management.Sql/DatabasesOperations.cs new file mode 100644 index 000000000000..ff9f77bbc240 --- /dev/null +++ b/src/SDKs/SqlManagement/Management.Sql/DatabasesOperations.cs @@ -0,0 +1,65 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. See License.txt in the project root for +// license information. + +namespace Microsoft.Azure.Management.Sql +{ + using Microsoft.Rest.Azure; + using System.Collections.Generic; + using System.Threading; + using System.Threading.Tasks; + + internal partial class DatabasesOperations + { + /// + /// Renames a database. + /// + /// + /// The name of the resource group that contains the resource. You can + /// obtain this value from the Azure Resource Manager API or the + /// portal. + /// + /// + /// The name of the server. + /// + /// + /// The name of the database to rename. + /// + /// + /// The new name that the database should be renamed to. + /// + /// + /// The headers that will be added to request. + /// + /// + /// The cancellation token. + /// + /// + /// Thrown when the operation returned an invalid status code + /// + /// + /// Thrown when a required parameter is null + /// + public async Task RenameWithHttpMessagesAsync( + string resourceGroupName, + string serverName, + string databaseName, + string newName, + Dictionary> customHeaders = null, + CancellationToken cancellationToken = default(CancellationToken)) + { + return await this.RenameWithHttpMessagesAsync( + resourceGroupName, + serverName, + databaseName, + new Models.ResourceMoveDefinition( + string.Format("/subscriptions/{0}/resourceGroups/{1}/providers/Microsoft.Sql/servers/{2}/databases/{3}", + this.Client.SubscriptionId, + resourceGroupName, + serverName, + newName)), + customHeaders, + cancellationToken); + } + } +} diff --git a/src/SDKs/SqlManagement/Management.Sql/DatabasesOperationsExtensions.cs b/src/SDKs/SqlManagement/Management.Sql/DatabasesOperationsExtensions.cs new file mode 100644 index 000000000000..8de3c147b9b1 --- /dev/null +++ b/src/SDKs/SqlManagement/Management.Sql/DatabasesOperationsExtensions.cs @@ -0,0 +1,80 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. See License.txt in the project root for +// license information. + +namespace Microsoft.Azure.Management.Sql +{ + using System.Threading; + using System.Threading.Tasks; + + public partial class DatabasesOperationsExtensions + { + /// + /// Renames a database. + /// + /// + /// The operations group for this extension method. + /// + /// + /// The name of the resource group that contains the resource. You can obtain + /// this value from the Azure Resource Manager API or the portal. + /// + /// + /// The name of the server. + /// + /// + /// The name of the database to rename. + /// + /// + /// The new name that the database should be renamed to. + /// + public static void Rename( + this IDatabasesOperations operations, + string resourceGroupName, + string serverName, + string databaseName, + string newName) + { + RenameAsync(operations, resourceGroupName, serverName, databaseName, newName).GetAwaiter().GetResult(); + } + + /// + /// Renames a database. + /// + /// + /// The operations group for this extension method. + /// + /// + /// The name of the resource group that contains the resource. You can obtain + /// this value from the Azure Resource Manager API or the portal. + /// + /// + /// The name of the server. + /// + /// + /// The name of the database to rename. + /// + /// + /// The new name that the database should be renamed to. + /// + /// + /// The cancellation token. + /// + public static async Task RenameAsync( + this IDatabasesOperations operations, + string resourceGroupName, + string serverName, + string databaseName, + string newName, + CancellationToken cancellationToken = default(CancellationToken)) + { + (await operations.RenameWithHttpMessagesAsync( + resourceGroupName, + serverName, + databaseName, + newName, + null, + cancellationToken).ConfigureAwait(false)).Dispose(); + } + } +} diff --git a/src/SDKs/SqlManagement/Management.Sql/IDatabasesOperations.cs b/src/SDKs/SqlManagement/Management.Sql/IDatabasesOperations.cs new file mode 100644 index 000000000000..fcd44c1c7f3e --- /dev/null +++ b/src/SDKs/SqlManagement/Management.Sql/IDatabasesOperations.cs @@ -0,0 +1,51 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. See License.txt in the project root for +// license information. + +namespace Microsoft.Azure.Management.Sql +{ + using Microsoft.Rest.Azure; + using System.Collections.Generic; + using System.Threading; + using System.Threading.Tasks; + + public partial interface IDatabasesOperations + { + /// + /// Renames a database. + /// + /// + /// The name of the resource group that contains the resource. You can + /// obtain this value from the Azure Resource Manager API or the + /// portal. + /// + /// + /// The name of the server. + /// + /// + /// The name of the database to rename. + /// + /// + /// The new name that the database should be renamed to. + /// + /// + /// The headers that will be added to request. + /// + /// + /// The cancellation token. + /// + /// + /// Thrown when the operation returned an invalid status code + /// + /// + /// Thrown when a required parameter is null + /// + Task RenameWithHttpMessagesAsync( + string resourceGroupName, + string serverName, + string databaseName, + string newName, + Dictionary> customHeaders = null, + CancellationToken cancellationToken = default(CancellationToken)); + } +} diff --git a/src/SDKs/SqlManagement/Management.Sql/Microsoft.Azure.Management.Sql.csproj b/src/SDKs/SqlManagement/Management.Sql/Microsoft.Azure.Management.Sql.csproj index e355bfbfab6a..6ecfd71d4c8a 100644 --- a/src/SDKs/SqlManagement/Management.Sql/Microsoft.Azure.Management.Sql.csproj +++ b/src/SDKs/SqlManagement/Management.Sql/Microsoft.Azure.Management.Sql.csproj @@ -7,13 +7,13 @@ Microsoft.Azure.Management.Sql Azure SQL Management SDK library Microsoft.Azure.Management.Sql - 1.11.0-preview + 1.12.0-preview Microsoft Azure SQL Management;SQL;SQL Management; diff --git a/src/SDKs/SqlManagement/Management.Sql/Properties/AssemblyInfo.cs b/src/SDKs/SqlManagement/Management.Sql/Properties/AssemblyInfo.cs index 41e59123eccd..efb7bbafea1a 100644 --- a/src/SDKs/SqlManagement/Management.Sql/Properties/AssemblyInfo.cs +++ b/src/SDKs/SqlManagement/Management.Sql/Properties/AssemblyInfo.cs @@ -22,7 +22,7 @@ [assembly: AssemblyTitle("Microsoft Azure SQL Management Library")] [assembly: AssemblyDescription("Provides management functionality for Microsoft Azure SQL.")] [assembly: AssemblyVersion("1.0.0.0")] -[assembly: AssemblyFileVersion("1.11.0.0")] +[assembly: AssemblyFileVersion("1.12.0.0")] diff --git a/src/SDKs/SqlManagement/Sql.Tests/DatabaseCrudScenarioTests.cs b/src/SDKs/SqlManagement/Sql.Tests/DatabaseCrudScenarioTests.cs index fd8624f3b101..9259f43b8c8b 100644 --- a/src/SDKs/SqlManagement/Sql.Tests/DatabaseCrudScenarioTests.cs +++ b/src/SDKs/SqlManagement/Sql.Tests/DatabaseCrudScenarioTests.cs @@ -126,7 +126,7 @@ public void TestRenameDatabase() }); Assert.NotNull(db1); - // Rename + // Rename using id string newSuffix = "_renamed"; string newName = db1.Name + newSuffix; string newId = db1.Id + newSuffix; @@ -136,8 +136,18 @@ public void TestRenameDatabase() }); // Get database at its new id - Database db2 = sqlClient.Databases.Get(resourceGroup.Name, server.Name, newName); - Assert.Equal(newId, db2.Id); + Database newDb = sqlClient.Databases.Get(resourceGroup.Name, server.Name, newName); + Assert.Equal(newId, newDb.Id); + + // Rename using new name + string newSuffix2 = "2"; + string newName2 = newName + newSuffix2; + string newId2 = newId + newSuffix2; + sqlClient.Databases.Rename(resourceGroup.Name, server.Name, newName, newName2); + + // Get database at its new id + Database newDb2 = sqlClient.Databases.Get(resourceGroup.Name, server.Name, newName2); + Assert.Equal(newId2, newDb2.Id); } }