Skip to content

Commit f506f6e

Browse files
committed
Increase performance of Get-AzureSqlDatabase.
Regenerated MockSessions file for tests. Updated some tests. Updated help documentation to contain correct server versions. Fixed New-AzureSqlDatabaseServer not reporting server version and state.
1 parent fe9bb87 commit f506f6e

File tree

7 files changed

+424
-168
lines changed

7 files changed

+424
-168
lines changed

src/ServiceManagement/Sql/Commands.SqlDatabase.Test/Resources/MockSessions.xml

Lines changed: 322 additions & 127 deletions
Large diffs are not rendered by default.

src/ServiceManagement/Sql/Commands.SqlDatabase.Test/UnitTests/Server/Cmdlet/ServerCmdletTests.cs

Lines changed: 32 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -33,23 +33,35 @@ namespace Microsoft.WindowsAzure.Commands.SqlDatabase.Test.UnitTests.Server.Cmdl
3333
[TestClass]
3434
public class ServerCmdletTests : TestBase
3535
{
36+
// String ID for server version 2.
37+
public const string ServerVersion2 = "2.0";
38+
39+
// String ID for server version 12.
40+
public const string ServerVersion12 = "12.0";
41+
42+
// The default server version
43+
public const string DefaultServerVersion = ServerVersion12;
44+
3645
[TestCleanup]
3746
public void CleanupTest()
3847
{
3948
// Save the mock session results
4049
MockServerHelper.SaveDefaultSessionCollection();
4150
}
4251

43-
private static void VerifyServer(SqlDatabaseServerContext server, string adminLogin, string location)
52+
/// <summary>
53+
/// Verifys the server object to make sure the fields match what is provided
54+
/// </summary>
55+
/// <param name="server">The server object to validate</param>
56+
/// <param name="adminLogin">The expected administration login</param>
57+
/// <param name="location">The expected server location</param>
58+
/// <param name="version">The expected server verions</param>
59+
/// <param name="state">The expected state of the server</param>
60+
private static void VerifyServer(SqlDatabaseServerContext server, string adminLogin, string location, string version, string state)
4461
{
4562
Assert.AreEqual(adminLogin, server.AdministratorLogin, "Expecting server login to match.");
4663
Assert.AreEqual(location, server.Location, "Expecting matching location.");
4764
Assert.AreEqual(10, server.ServerName.Length, "Expecting a valid server name.");
48-
}
49-
50-
private static void VerifyServer(SqlDatabaseServerContext server, string adminLogin, string location, string version, string state)
51-
{
52-
VerifyServer(server, adminLogin, location);
5365
Assert.AreEqual(version, server.Version, "Server version doesn't match");
5466
Assert.AreEqual(state, server.State, "Server state does not match");
5567
}
@@ -190,14 +202,18 @@ public void AzureSqlDatabaseServerTests()
190202
VerifyServer(
191203
server,
192204
(string)powershell.Runspace.SessionStateProxy.GetVariable("login"),
193-
(string)powershell.Runspace.SessionStateProxy.GetVariable("location"));
205+
(string)powershell.Runspace.SessionStateProxy.GetVariable("location"),
206+
ServerVersion12,
207+
"Ready");
194208

195209
SqlDatabaseServerContext server2 = newServerResult2.Single().BaseObject as SqlDatabaseServerContext;
196210
Assert.IsNotNull(server2, "Expecting a SqlDatabaseServerContext object");
197211
VerifyServer(
198212
server,
199213
(string)powershell.Runspace.SessionStateProxy.GetVariable("login"),
200-
(string)powershell.Runspace.SessionStateProxy.GetVariable("location"));
214+
(string)powershell.Runspace.SessionStateProxy.GetVariable("location"),
215+
ServerVersion12,
216+
"Ready");
201217

202218
// Validate Get-AzureSqlDatabaseServer results
203219
server = getServerResult.Single().BaseObject as SqlDatabaseServerContext;
@@ -206,7 +222,7 @@ public void AzureSqlDatabaseServerTests()
206222
server,
207223
(string)powershell.Runspace.SessionStateProxy.GetVariable("login"),
208224
(string)powershell.Runspace.SessionStateProxy.GetVariable("location"),
209-
"2.0",
225+
ServerVersion12,
210226
"Ready");
211227

212228
server = setServerResult.Single().BaseObject as SqlDatabaseServerContext;
@@ -215,7 +231,7 @@ public void AzureSqlDatabaseServerTests()
215231
server,
216232
(string)powershell.Runspace.SessionStateProxy.GetVariable("login"),
217233
(string)powershell.Runspace.SessionStateProxy.GetVariable("location"),
218-
"2.0",
234+
ServerVersion12,
219235
"Ready");
220236

221237
server2 = setServerResult2.Single().BaseObject as SqlDatabaseServerContext;
@@ -224,7 +240,7 @@ public void AzureSqlDatabaseServerTests()
224240
server2,
225241
(string)powershell.Runspace.SessionStateProxy.GetVariable("login"),
226242
(string)powershell.Runspace.SessionStateProxy.GetVariable("location"),
227-
"2.0",
243+
ServerVersion12,
228244
"Ready");
229245

230246
// Validate Remove-AzureSqlDatabaseServer results
@@ -319,7 +335,9 @@ public void AzureSqlDatabaseServerV2Tests()
319335
VerifyServer(
320336
server,
321337
(string)powershell.Runspace.SessionStateProxy.GetVariable("login"),
322-
(string)powershell.Runspace.SessionStateProxy.GetVariable("location"));
338+
(string)powershell.Runspace.SessionStateProxy.GetVariable("location"),
339+
ServerVersion2,
340+
"Ready");
323341

324342

325343
// Validate Get-AzureSqlDatabaseServer results
@@ -329,8 +347,8 @@ public void AzureSqlDatabaseServerV2Tests()
329347
server,
330348
(string)powershell.Runspace.SessionStateProxy.GetVariable("login"),
331349
(string)powershell.Runspace.SessionStateProxy.GetVariable("location"),
332-
"2.0",
333-
null);
350+
ServerVersion2,
351+
"Ready");
334352

335353
powershell.Streams.ClearStreams();
336354
}

src/ServiceManagement/Sql/Commands.SqlDatabase/Microsoft.WindowsAzure.Commands.SqlDatabase.dll-Help.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2192,7 +2192,7 @@ Business Edition values: 10, 20, 30, 40, 50, 100, or 150</maml:para>
21922192
<command:parameter required="false" variableLength="false" globbing="false" pipelineInput="false" position="named">
21932193
<maml:name>Version</maml:name>
21942194
<maml:description>
2195-
<maml:para>Specifies the type of SQL Database Server to create. Accepts values 1.0 or 2.0.</maml:para>
2195+
<maml:para>Specifies the type of SQL Database Server to create. Accepts values 2.0 or 12.0.</maml:para>
21962196
</maml:description>
21972197
<command:parameterValue required="true" variableLength="false">Single</command:parameterValue>
21982198
</command:parameter>
@@ -2259,7 +2259,7 @@ Business Edition values: 10, 20, 30, 40, 50, 100, or 150</maml:para>
22592259
<command:parameter required="false" variableLength="false" globbing="false" pipelineInput="false" position="named">
22602260
<maml:name>Version</maml:name>
22612261
<maml:description>
2262-
<maml:para>Specifies the type of SQL Database Server to create. Accepts values 1.0 or 2.0.</maml:para>
2262+
<maml:para>Specifies the type of SQL Database Server to create. Accepts values 2.0 or 12.0.</maml:para>
22632263
</maml:description>
22642264
<command:parameterValue required="true" variableLength="false">Single</command:parameterValue>
22652265
<dev:type>

src/ServiceManagement/Sql/Commands.SqlDatabase/Properties/Resources.Designer.cs

Lines changed: 10 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/ServiceManagement/Sql/Commands.SqlDatabase/Properties/Resources.resx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,9 @@
121121
<value>Invalid Parameter combination: Both "DatabaseName" and "DatabaseDeletionDate" need to be specified to get a restorable dropped database.</value>
122122
<comment>Parameter validation</comment>
123123
</data>
124+
<data name="CreateServerServerNotFound" xml:space="preserve">
125+
<value>Unable to retrieve newly created server '{0}' from the service.</value>
126+
</data>
124127
<data name="CredentialNotSpecified" xml:space="preserve">
125128
<value>The Sql Authenticate Credential was not specified.</value>
126129
<comment>Parameter validation</comment>

src/ServiceManagement/Sql/Commands.SqlDatabase/Server/Cmdlet/NewAzureSqlDatabaseServer.cs

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@
2121
using System;
2222
using System.Management.Automation;
2323
using Hyak.Common.TransientFaultHandling;
24+
using System.Collections.Generic;
25+
using System.Linq;
26+
using System.Globalization;
2427

2528
namespace Microsoft.WindowsAzure.Commands.SqlDatabase.Server.Cmdlet
2629
{
@@ -119,14 +122,27 @@ internal SqlDatabaseServerContext NewAzureSqlDatabaseServerProcess(
119122
Version = version.HasValue ? version.Value.ToString("F1") : null
120123
});
121124

125+
IEnumerable<Management.Sql.Models.Server> servers = sqlManagementClient.Servers.List().Servers;
126+
var newServer = servers.Where(s => s.Name == response.ServerName).FirstOrDefault();
127+
128+
if (newServer == null)
129+
{
130+
throw new ItemNotFoundException(string.Format(
131+
CultureInfo.InvariantCulture,
132+
Resources.CreateServerServerNotFound,
133+
response.ServerName));
134+
}
135+
122136
SqlDatabaseServerContext operationContext = new SqlDatabaseServerContext()
123137
{
124138
OperationStatus = Services.Constants.OperationSuccess,
125139
OperationDescription = CommandRuntime.ToString(),
126140
OperationId = response.RequestId,
127-
ServerName = response.ServerName,
141+
ServerName = newServer.Name,
128142
Location = location,
129143
AdministratorLogin = adminLogin,
144+
State = newServer.State,
145+
Version = newServer.Version
130146
};
131147

132148
return operationContext;

src/ServiceManagement/Sql/Commands.SqlDatabase/Services/Server/ServerDataServiceCertAuth.cs

Lines changed: 38 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -329,11 +329,20 @@ public void RemoveDatabase(string databaseName)
329329

330330
#region Service Objective Operations
331331

332-
/// <summary>
333-
/// Retrieves the list of all service objectives on the server.
334-
/// </summary>
335-
/// <returns>An array of all service objectives on the server.</returns>
336-
public ServiceObjective[] GetServiceObjectives()
332+
private ServiceObjective[] objectivesCache;
333+
private ServiceObjective[] Objectives
334+
{
335+
get
336+
{
337+
if (objectivesCache == null)
338+
{
339+
PopulateSloCache();
340+
}
341+
return objectivesCache;
342+
}
343+
}
344+
345+
private void PopulateSloCache()
337346
{
338347
this.clientRequestId = SqlDatabaseCmdletBase.GenerateClientTracingId();
339348

@@ -345,9 +354,17 @@ public ServiceObjective[] GetServiceObjectives()
345354
ServiceObjectiveListResponse response = sqlManagementClient.ServiceObjectives.List(
346355
this.serverName);
347356

348-
// Construct the resulting Database object
349-
ServiceObjective[] serviceObjectives = response.Select(serviceObjective => CreateServiceObjectiveFromResponse(serviceObjective)).ToArray();
350-
return serviceObjectives;
357+
// Populate the cache;
358+
objectivesCache = response.Select(serviceObjective => CreateServiceObjectiveFromResponse(serviceObjective)).ToArray();
359+
}
360+
361+
/// <summary>
362+
/// Retrieves the list of all service objectives on the server.
363+
/// </summary>
364+
/// <returns>An array of all service objectives on the server.</returns>
365+
public ServiceObjective[] GetServiceObjectives()
366+
{
367+
return Objectives;
351368
}
352369

353370
/// <summary>
@@ -359,9 +376,8 @@ public ServiceObjective[] GetServiceObjectives()
359376
/// </returns>
360377
public ServiceObjective GetServiceObjective(string serviceObjectiveName)
361378
{
362-
ServiceObjective serviceObjective = GetServiceObjectives()
363-
.Where(s => s.Name == serviceObjectiveName)
364-
.FirstOrDefault();
379+
var serviceObjective = Objectives.Where(s => s.Name == serviceObjectiveName).FirstOrDefault();
380+
365381
if (serviceObjective == null)
366382
{
367383
throw new InvalidOperationException(
@@ -383,19 +399,18 @@ public ServiceObjective GetServiceObjective(string serviceObjectiveName)
383399
/// </returns>
384400
public ServiceObjective GetServiceObjective(ServiceObjective serviceObjectiveToRefresh)
385401
{
386-
this.clientRequestId = SqlDatabaseCmdletBase.GenerateClientTracingId();
387-
388-
// Get the SQL management client
389-
SqlManagementClient sqlManagementClient = AzureSession.ClientFactory.CreateClient<SqlManagementClient>(subscription, AzureEnvironment.Endpoint.ServiceManagement);
390-
this.AddTracingHeaders(sqlManagementClient);
391-
392-
// Retrieve the specified database
393-
ServiceObjectiveGetResponse response = sqlManagementClient.ServiceObjectives.Get(
394-
this.serverName,
395-
serviceObjectiveToRefresh.Id.ToString());
402+
var serviceObjective = Objectives.Where(s => s.Id == serviceObjectiveToRefresh.Id).FirstOrDefault();
403+
404+
if (serviceObjective == null)
405+
{
406+
throw new InvalidOperationException(
407+
string.Format(
408+
CultureInfo.InvariantCulture,
409+
Resources.ServiceObjectiveNotFound,
410+
this.ServerName,
411+
serviceObjectiveToRefresh.Id));
412+
}
396413

397-
// Construct the resulting Database object
398-
ServiceObjective serviceObjective = CreateServiceObjectiveFromResponse(response.ServiceObjective);
399414
return serviceObjective;
400415
}
401416

0 commit comments

Comments
 (0)