Skip to content

Commit d3bdf63

Browse files
committed
Added Set-AzureRmSqlDatabase -NewName param
1 parent 9fd5030 commit d3bdf63

File tree

8 files changed

+131
-39
lines changed

8 files changed

+131
-39
lines changed

src/ResourceManager/Sql/Commands.Sql.Test/Commands.Sql.Test.csproj

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -81,8 +81,7 @@
8181
<HintPath>..\..\..\packages\Microsoft.Azure.Management.Network.15.1.0-preview\lib\net452\Microsoft.Azure.Management.Network.dll</HintPath>
8282
</Reference>
8383
<Reference Include="Microsoft.Azure.Management.Sql, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
84-
<SpecificVersion>False</SpecificVersion>
85-
<HintPath>..\..\..\packages\Microsoft.Azure.Management.Sql.1.8.0-preview\lib\net452\Microsoft.Azure.Management.Sql.dll</HintPath>
84+
<HintPath>..\..\..\packages\Microsoft.Azure.Management.Sql.1.10.0-preview\lib\net452\Microsoft.Azure.Management.Sql.dll</HintPath>
8685
<Private>True</Private>
8786
</Reference>
8887
<Reference Include="Microsoft.Azure.Management.Storage">
@@ -263,7 +262,7 @@
263262
<None Include="ScenarioTests\DatabaseCrudStretchTests.ps1">
264263
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
265264
</None>
266-
<None Include="ScenarioTests\ServerDnsAliasTests.ps1" >
265+
<None Include="ScenarioTests\ServerDnsAliasTests.ps1">
267266
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
268267
</None>
269268
<None Include="ScenarioTests\ServerUpgradeTests.ps1">
@@ -989,16 +988,16 @@
989988
<None Include=".\SessionRecords\Microsoft.Azure.Commands.Sql.Test.ScenarioTests.VirtualNetworkRuleTest\TestVirtualNetworkRuleRemove.json">
990989
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
991990
</None>
992-
<None Include=".\SessionRecords\Microsoft.Azure.Commands.Sql.Test.ScenarioTests.ServerDnsAliasTests\TestCreateServerDnsAlias.json">
991+
<None Include=".\SessionRecords\Microsoft.Azure.Commands.Sql.Test.ScenarioTests.ServerDnsAliasTests\TestCreateServerDnsAlias.json">
993992
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
994993
</None>
995-
<None Include=".\SessionRecords\Microsoft.Azure.Commands.Sql.Test.ScenarioTests.ServerDnsAliasTests\TestServerDnsAliasGet.json">
994+
<None Include=".\SessionRecords\Microsoft.Azure.Commands.Sql.Test.ScenarioTests.ServerDnsAliasTests\TestServerDnsAliasGet.json">
996995
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
997996
</None>
998-
<None Include=".\SessionRecords\Microsoft.Azure.Commands.Sql.Test.ScenarioTests.ServerDnsAliasTests\TestServerDnsAliasUpdate.json">
997+
<None Include=".\SessionRecords\Microsoft.Azure.Commands.Sql.Test.ScenarioTests.ServerDnsAliasTests\TestServerDnsAliasUpdate.json">
999998
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
1000999
</None>
1001-
<None Include=".\SessionRecords\Microsoft.Azure.Commands.Sql.Test.ScenarioTests.ServerDnsAliasTests\TestServerDnsAliasRemove.json">
1000+
<None Include=".\SessionRecords\Microsoft.Azure.Commands.Sql.Test.ScenarioTests.ServerDnsAliasTests\TestServerDnsAliasRemove.json">
10021001
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
10031002
</None>
10041003
</ItemGroup>

src/ResourceManager/Sql/Commands.Sql.Test/packages.config

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
<package id="Microsoft.Azure.Management.Authorization" version="2.0.0" targetFramework="net45" />
1010
<package id="Microsoft.Azure.Management.Network" version="15.1.0-preview" targetFramework="net452" />
1111
<package id="Microsoft.Azure.Management.Resources" version="2.20.0-preview" targetFramework="net45" />
12-
<package id="Microsoft.Azure.Management.Sql" version="1.8.0-preview" targetFramework="net452" />
12+
<package id="Microsoft.Azure.Management.Sql" version="1.10.0-preview" targetFramework="net452" />
1313
<package id="Microsoft.Azure.Management.Storage" version="2.4.0-preview" targetFramework="net45" />
1414
<package id="Microsoft.Azure.Test.Framework" version="1.0.6179.26854-prerelease" targetFramework="net45" />
1515
<package id="Microsoft.Azure.Test.HttpRecorder" version="1.6.7-preview" targetFramework="net45" />

src/ResourceManager/Sql/Commands.Sql/Commands.Sql.csproj

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -453,8 +453,7 @@
453453
<!--<Reference Include="Microsoft.Azure.Management.Sql, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
454454
<HintPath>..\..\..\packages\Microsoft.Azure.Management.Sql.1.3.0-preview\lib\net452\Microsoft.Azure.Management.Sql.dll</HintPath>-->
455455
<Reference Include="Microsoft.Azure.Management.Sql, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
456-
<SpecificVersion>False</SpecificVersion>
457-
<HintPath>..\..\..\packages\Microsoft.Azure.Management.Sql.1.8.0-preview\lib\net452\Microsoft.Azure.Management.Sql.dll</HintPath>
456+
<HintPath>..\..\..\packages\Microsoft.Azure.Management.Sql.1.10.0-preview\lib\net452\Microsoft.Azure.Management.Sql.dll</HintPath>
458457
<Private>True</Private>
459458
</Reference>
460459
<Reference Include="Microsoft.Azure.Management.Storage">
@@ -472,13 +471,25 @@
472471
<HintPath>..\..\..\packages\Microsoft.Data.Services.Client.5.8.2\lib\net40\Microsoft.Data.Services.Client.dll</HintPath>
473472
<Private>True</Private>
474473
</Reference>
474+
<Reference Include="Microsoft.Rest.ClientRuntime, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
475+
<HintPath>..\..\..\packages\Microsoft.Rest.ClientRuntime.2.3.10\lib\net452\Microsoft.Rest.ClientRuntime.dll</HintPath>
476+
<Private>True</Private>
477+
</Reference>
478+
<Reference Include="Microsoft.Rest.ClientRuntime.Azure, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
479+
<HintPath>..\..\..\packages\Microsoft.Rest.ClientRuntime.Azure.3.3.10\lib\net452\Microsoft.Rest.ClientRuntime.Azure.dll</HintPath>
480+
<Private>True</Private>
481+
</Reference>
475482
<Reference Include="Microsoft.WindowsAzure.Management.Storage">
476483
<HintPath>..\..\..\packages\Microsoft.WindowsAzure.Management.Storage.5.1.1\lib\net40\Microsoft.WindowsAzure.Management.Storage.dll</HintPath>
477484
</Reference>
478485
<Reference Include="Microsoft.WindowsAzure.Storage, Version=8.1.1.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
479486
<HintPath>..\..\..\packages\WindowsAzure.Storage.8.1.1\lib\net45\Microsoft.WindowsAzure.Storage.dll</HintPath>
480487
<Private>True</Private>
481488
</Reference>
489+
<Reference Include="Newtonsoft.Json, Version=6.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
490+
<HintPath>..\..\..\packages\Newtonsoft.Json.6.0.8\lib\net45\Newtonsoft.Json.dll</HintPath>
491+
<Private>True</Private>
492+
</Reference>
482493
<Reference Include="System.Configuration.Install" />
483494
<Reference Include="System.Management.Automation, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
484495
<SpecificVersion>False</SpecificVersion>

src/ResourceManager/Sql/Commands.Sql/Database/Cmdlet/SetAzureSqlDatabase.cs

Lines changed: 75 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
// limitations under the License.
1313
// ----------------------------------------------------------------------------------
1414

15+
using System;
1516
using Microsoft.Azure.Commands.ResourceManager.Common.Tags;
1617
using Microsoft.Azure.Commands.Sql.Database.Model;
1718
using System.Collections;
@@ -28,6 +29,9 @@ namespace Microsoft.Azure.Commands.Sql.Database.Cmdlet
2829
ConfirmImpact = ConfirmImpact.Medium)]
2930
public class SetAzureSqlDatabase : AzureSqlDatabaseCmdletBase<IEnumerable<AzureSqlDatabaseModel>>
3031
{
32+
private const string UpdateParameterSetName = "Update";
33+
private const string RenameParameterSetName = "Rename";
34+
3135
/// <summary>
3236
/// Gets or sets the name of the Azure SQL Database
3337
/// </summary>
@@ -43,57 +47,72 @@ public class SetAzureSqlDatabase : AzureSqlDatabaseCmdletBase<IEnumerable<AzureS
4347
/// Gets or sets the maximum size of the Azure SQL Database in bytes
4448
/// </summary>
4549
[Parameter(Mandatory = false,
46-
HelpMessage = "The maximum size of the Azure SQL Database in bytes.")]
50+
HelpMessage = "The maximum size of the Azure SQL Database in bytes.",
51+
ParameterSetName = UpdateParameterSetName)]
4752
[ValidateNotNullOrEmpty]
4853
public long MaxSizeBytes { get; set; }
4954

5055
/// <summary>
5156
/// Gets or sets the edition to assign to the Azure SQL Database
5257
/// </summary>
5358
[Parameter(Mandatory = false,
54-
HelpMessage = "The edition to assign to the Azure SQL Database.")]
59+
HelpMessage = "The edition to assign to the Azure SQL Database.",
60+
ParameterSetName = UpdateParameterSetName)]
5561
[ValidateNotNullOrEmpty]
5662
public DatabaseEdition Edition { get; set; }
5763

5864
/// <summary>
5965
/// Gets or sets the name of the service objective to assign to the Azure SQL Database
6066
/// </summary>
6167
[Parameter(Mandatory = false,
62-
HelpMessage = "The name of the service objective to assign to the Azure SQL Database.")]
68+
HelpMessage = "The name of the service objective to assign to the Azure SQL Database.",
69+
ParameterSetName = UpdateParameterSetName)]
6370
[ValidateNotNullOrEmpty]
6471
public string RequestedServiceObjectiveName { get; set; }
6572

6673
/// <summary>
6774
/// Gets or sets the name of the Elastic Pool to put the database in
6875
/// </summary>
6976
[Parameter(Mandatory = false,
70-
HelpMessage = "The name of the Elastic Pool to put the database in.")]
77+
HelpMessage = "The name of the Elastic Pool to put the database in.",
78+
ParameterSetName = UpdateParameterSetName)]
7179
[ValidateNotNullOrEmpty]
7280
public string ElasticPoolName { get; set; }
7381

7482
/// <summary>
7583
/// Gets or sets the read scale option to assign to the Azure SQL Database
7684
/// </summary>
7785
[Parameter(Mandatory = false,
78-
HelpMessage = "The read scale option to assign to the Azure SQL Database.(Enabled/Disabled)")]
86+
HelpMessage = "The read scale option to assign to the Azure SQL Database.(Enabled/Disabled)",
87+
ParameterSetName = UpdateParameterSetName)]
7988
[ValidateNotNullOrEmpty]
8089
public DatabaseReadScale ReadScale { get; set; }
8190

8291
/// <summary>
8392
/// Gets or sets the tags associated with the Azure Sql Database
8493
/// </summary>
8594
[Parameter(Mandatory = false,
86-
HelpMessage = "The tags to associate with the Azure Sql Database")]
95+
HelpMessage = "The tags to associate with the Azure Sql Database",
96+
ParameterSetName = UpdateParameterSetName)]
8797
[Alias("Tag")]
8898
public Hashtable Tags { get; set; }
8999

90100
/// <summary>
91101
/// Gets or sets the zone redundant option to assign to the Azure SQL Database
92102
/// </summary>
93103
[Parameter(Mandatory = false,
94-
HelpMessage = "The zone redundancy to associate with the Azure Sql Database")]
104+
HelpMessage = "The zone redundancy to associate with the Azure Sql Database",
105+
ParameterSetName = UpdateParameterSetName)]
95106
public SwitchParameter ZoneRedundant { get; set; }
96107

108+
/// <summary>
109+
/// Gets or sets the new name.
110+
/// </summary>
111+
[Parameter(Mandatory = true,
112+
HelpMessage = "The new name to rename the database to.",
113+
ParameterSetName = RenameParameterSetName)]
114+
public string NewName { get; set; }
115+
97116
/// <summary>
98117
/// Overriding to add warning message
99118
/// </summary>
@@ -121,20 +140,26 @@ protected override IEnumerable<AzureSqlDatabaseModel> GetEntity()
121140
protected override IEnumerable<AzureSqlDatabaseModel> ApplyUserInputToModel(IEnumerable<AzureSqlDatabaseModel> model)
122141
{
123142
List<Model.AzureSqlDatabaseModel> newEntity = new List<AzureSqlDatabaseModel>();
124-
newEntity.Add(new AzureSqlDatabaseModel()
143+
if (this.ParameterSetName == UpdateParameterSetName)
125144
{
126-
ResourceGroupName = ResourceGroupName,
127-
ServerName = ServerName,
128-
DatabaseName = DatabaseName,
129-
Edition = Edition,
130-
MaxSizeBytes = MaxSizeBytes,
131-
RequestedServiceObjectiveName = RequestedServiceObjectiveName,
132-
Tags = TagsConversionHelper.ReadOrFetchTags(this, model.FirstOrDefault().Tags),
133-
ElasticPoolName = ElasticPoolName,
134-
Location = model.FirstOrDefault().Location,
135-
ReadScale = ReadScale,
136-
ZoneRedundant = MyInvocation.BoundParameters.ContainsKey("ZoneRedundant") ? (bool?)ZoneRedundant.ToBool() : null
137-
});
145+
newEntity.Add(new AzureSqlDatabaseModel()
146+
{
147+
ResourceGroupName = ResourceGroupName,
148+
ServerName = ServerName,
149+
DatabaseName = DatabaseName,
150+
Edition = Edition,
151+
MaxSizeBytes = MaxSizeBytes,
152+
RequestedServiceObjectiveName = RequestedServiceObjectiveName,
153+
Tags = TagsConversionHelper.ReadOrFetchTags(this, model.FirstOrDefault().Tags),
154+
ElasticPoolName = ElasticPoolName,
155+
Location = model.FirstOrDefault().Location,
156+
ReadScale = ReadScale,
157+
ZoneRedundant =
158+
MyInvocation.BoundParameters.ContainsKey("ZoneRedundant")
159+
? (bool?) ZoneRedundant.ToBool()
160+
: null
161+
});
162+
}
138163
return newEntity;
139164
}
140165

@@ -145,16 +170,38 @@ protected override IEnumerable<AzureSqlDatabaseModel> ApplyUserInputToModel(IEnu
145170
/// <returns>The input entity</returns>
146171
protected override IEnumerable<AzureSqlDatabaseModel> PersistChanges(IEnumerable<AzureSqlDatabaseModel> entity)
147172
{
148-
return new List<AzureSqlDatabaseModel>
173+
switch (this.ParameterSetName)
149174
{
150-
ModelAdapter.UpsertDatabaseWithNewSdk(
151-
this.ResourceGroupName,
152-
this.ServerName,
153-
new AzureSqlDatabaseCreateOrUpdateModel
175+
case UpdateParameterSetName:
176+
return new List<AzureSqlDatabaseModel>
154177
{
155-
Database = entity.First()
156-
})
157-
};
178+
ModelAdapter.UpsertDatabaseWithNewSdk(
179+
this.ResourceGroupName,
180+
this.ServerName,
181+
new AzureSqlDatabaseCreateOrUpdateModel
182+
{
183+
Database = entity.First()
184+
})
185+
};
186+
187+
case RenameParameterSetName:
188+
ModelAdapter.RenameDatabase(
189+
this.ResourceGroupName,
190+
this.ServerName,
191+
this.DatabaseName,
192+
this.NewName);
193+
194+
return new List<AzureSqlDatabaseModel>
195+
{
196+
ModelAdapter.GetDatabase(
197+
this.ResourceGroupName,
198+
this.ServerName,
199+
this.NewName)
200+
};
201+
202+
default:
203+
throw new ArgumentException(this.ParameterSetName);
204+
}
158205
}
159206
}
160207
}

src/ResourceManager/Sql/Commands.Sql/Database/Model/AzureSqlDatabaseModel.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,11 @@ namespace Microsoft.Azure.Commands.Sql.Database.Model
2323
/// </summary>
2424
public class AzureSqlDatabaseModel
2525
{
26+
/// <summary>
27+
/// Template to generate database id
28+
/// </summary>
29+
public const string IdTemplate = "/subscriptions/{0}/resourceGroups/{1}/providers/Microsoft.Sql/servers/{2}/databases/{3}";
30+
2631
/// <summary>
2732
/// Gets or sets the name of the resource group
2833
/// </summary>

src/ResourceManager/Sql/Commands.Sql/Database/Services/AzureSqlDatabaseAdapter.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -338,5 +338,10 @@ internal IEnumerable<AzureSqlDatabaseActivityModel> CancelDatabaseActivity(strin
338338

339339
return ListDatabaseActivity(resourceGroupName, serverName, elasticPoolName, databaseName, operationId);
340340
}
341+
342+
public void RenameDatabase(string resourceGroupName, string serverName, string databaseName, string newName)
343+
{
344+
Communicator.Rename(resourceGroupName, serverName, databaseName, newName);
345+
}
341346
}
342347
}

src/ResourceManager/Sql/Commands.Sql/Database/Services/AzureSqlDatabaseCommunicator.cs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323
using Microsoft.WindowsAzure.Management.Storage;
2424
using System;
2525
using System.Collections.Generic;
26+
using Microsoft.Azure.Commands.Sql.Database.Model;
27+
using Microsoft.Azure.Management.Sql.Models;
2628

2729
namespace Microsoft.Azure.Commands.Sql.Database.Services
2830
{
@@ -138,6 +140,26 @@ public void Remove(string resourceGroupName, string serverName, string databaseN
138140
GetLegacySqlClient().Databases.Delete(resourceGroupName, serverName, databaseName);
139141
}
140142

143+
/// <summary>
144+
/// Renames a database
145+
/// </summary>
146+
internal void Rename(string resourceGroupName, string serverName, string databaseName, string newName)
147+
{
148+
var client = GetCurrentSqlClient();
149+
client.Databases.Rename(
150+
resourceGroupName,
151+
serverName,
152+
databaseName,
153+
new ResourceMoveDefinition
154+
{
155+
Id = AzureSqlDatabaseModel.IdTemplate.FormatInvariant(
156+
client.SubscriptionId,
157+
resourceGroupName,
158+
serverName,
159+
newName)
160+
});
161+
}
162+
141163
/// <summary>
142164
/// Retrieve the SQL Management client for the currently selected subscription, adding the session and request
143165
/// id tracing headers for the current cmdlet invocation.
Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<packages>
33
<package id="Microsoft.Azure.KeyVault.Core" version="1.0.0" targetFramework="net45" />
4-
<package id="Microsoft.Azure.Management.Sql" version="1.8.0-preview" targetFramework="net452" />
4+
<package id="Microsoft.Azure.Management.Sql" version="1.10.0-preview" targetFramework="net452" />
55
<package id="Microsoft.Azure.Management.Storage" version="2.4.0-preview" targetFramework="net45" />
6+
<package id="Microsoft.Rest.ClientRuntime" version="2.3.10" targetFramework="net452" />
7+
<package id="Microsoft.Rest.ClientRuntime.Azure" version="3.3.10" targetFramework="net452" />
8+
<package id="Newtonsoft.Json" version="6.0.8" targetFramework="net452" />
69
<package id="WindowsAzure.Storage" version="8.1.1" targetFramework="net452" />
710
</packages>

0 commit comments

Comments
 (0)