Skip to content

Commit fc5d0e3

Browse files
committed
Addressing code review comments
1 parent c60c467 commit fc5d0e3

File tree

6 files changed

+145
-183
lines changed

6 files changed

+145
-183
lines changed

src/ServiceManagement/Sql/Commands.SqlDatabase.Test/UnitTests/Database/Cmdlet/SqlAuthv12MockTests.cs

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,18 @@
1-
using Microsoft.VisualStudio.TestTools.UnitTesting;
1+
// ----------------------------------------------------------------------------------
2+
//
3+
// Copyright Microsoft Corporation
4+
// Licensed under the Apache License, Version 2.0 (the "License");
5+
// you may not use this file except in compliance with the License.
6+
// You may obtain a copy of the License at
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
// Unless required by applicable law or agreed to in writing, software
9+
// distributed under the License is distributed on an "AS IS" BASIS,
10+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
// See the License for the specific language governing permissions and
12+
// limitations under the License.
13+
// ---
14+
15+
using Microsoft.VisualStudio.TestTools.UnitTesting;
216
using Microsoft.WindowsAzure.Commands.SqlDatabase.Services.Server;
317
using Microsoft.WindowsAzure.Commands.SqlDatabase.Test.UnitTests.TSql;
418
using System;
@@ -35,11 +49,9 @@ public void Cleanup()
3549
[TestMethod]
3650
public void NewAzureSqlDatabaseWithSqlAuthv12()
3751
{
38-
3952
using (System.Management.Automation.PowerShell powershell =
4053
System.Management.Automation.PowerShell.Create())
4154
{
42-
4355
// Create a context
4456
NewAzureSqlDatabaseServerContextTests.CreateServerContextSqlAuthV2(
4557
powershell,

src/ServiceManagement/Sql/Commands.SqlDatabase.Test/UnitTests/TSql/CustomAttributeProviderExtensions.cs

Lines changed: 0 additions & 87 deletions
This file was deleted.

src/ServiceManagement/Sql/Commands.SqlDatabase/Commands.SqlDatabase.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,7 @@
190190
<Compile Include="Services\Server\ServerDataServiceEnums.cs" />
191191
<Compile Include="Services\Server\ServerDataServiceSqlAuth.cs" />
192192
<Compile Include="Services\Server\ServerContextInternal.cs" />
193+
<Compile Include="Services\Server\SqlAuthContextFactory.cs" />
193194
<Compile Include="Services\Server\TSqlConnectionContext.cs" />
194195
<Compile Include="Services\ServiceResourceError.cs" />
195196
<Compile Include="Services\SqlDatabaseManagementError.cs" />

src/ServiceManagement/Sql/Commands.SqlDatabase/Database/Cmdlet/NewAzureSqlDatabaseServerContext.cs

Lines changed: 20 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -89,10 +89,10 @@ public class NewAzureSqlDatabaseServerContext : AzurePSCmdlet
8989
/// <summary>
9090
/// Gets or sets the management site data connection fully qualified server name.
9191
/// </summary>
92-
[Parameter(Mandatory = true, Position = 0,
92+
[Parameter(Mandatory = true, Position = 0,
9393
ParameterSetName = FullyQualifiedServerNameWithSqlAuthParamSet,
9494
HelpMessage = "The fully qualified server name")]
95-
[Parameter(Mandatory = true, Position = 0,
95+
[Parameter(Mandatory = true, Position = 0,
9696
ParameterSetName = FullyQualifiedServerNameWithCertAuthParamSet,
9797
HelpMessage = "The fully qualified server name")]
9898
[ValidateNotNull]
@@ -109,13 +109,13 @@ public class NewAzureSqlDatabaseServerContext : AzurePSCmdlet
109109
/// <summary>
110110
/// Gets or sets the server credentials
111111
/// </summary>
112-
[Parameter(Mandatory = true, Position = 1,
112+
[Parameter(Mandatory = true, Position = 1,
113113
ParameterSetName = ServerNameWithSqlAuthParamSet,
114114
HelpMessage = "The credentials for the server")]
115-
[Parameter(Mandatory = true, Position = 1,
115+
[Parameter(Mandatory = true, Position = 1,
116116
ParameterSetName = FullyQualifiedServerNameWithSqlAuthParamSet,
117117
HelpMessage = "The credentials for the server")]
118-
[Parameter(Mandatory = true, Position = 1,
118+
[Parameter(Mandatory = true, Position = 1,
119119
ParameterSetName = ManageUrlWithSqlAuthParamSet,
120120
HelpMessage = "The credentials for the server")]
121121
[ValidateNotNull]
@@ -124,10 +124,10 @@ public class NewAzureSqlDatabaseServerContext : AzurePSCmdlet
124124
/// <summary>
125125
/// Gets or sets whether or not the current subscription should be used for authentication
126126
/// </summary>
127-
[Parameter(Mandatory = true, Position = 1,
127+
[Parameter(Mandatory = true, Position = 1,
128128
ParameterSetName = ServerNameWithCertAuthParamSet,
129129
HelpMessage = "Use certificate authentication")]
130-
[Parameter(Mandatory = true, Position = 1,
130+
[Parameter(Mandatory = true, Position = 1,
131131
ParameterSetName = FullyQualifiedServerNameWithCertAuthParamSet,
132132
HelpMessage = "Use certificate authentication")]
133133
public SwitchParameter UseSubscription { get; set; }
@@ -140,13 +140,6 @@ public class NewAzureSqlDatabaseServerContext : AzurePSCmdlet
140140
HelpMessage = "The subscription to use, or uses the current subscription if not specified")]
141141
public string SubscriptionName { get; set; }
142142

143-
/// <summary>
144-
/// Switch to indiciate the the server is an ESA server
145-
/// </summary>
146-
[Parameter(Mandatory = false,
147-
HelpMessage = "Indicates the server version being targeted. Valid values [2.0, 12.0]. Default = 2.0")]
148-
public float Version { get; set; }
149-
150143
#endregion
151144

152145
#region Current Subscription Management
@@ -188,63 +181,20 @@ internal IServerDataServiceContext GetServerDataServiceBySqlAuth(
188181
IServerDataServiceContext context = null;
189182
Guid sessionActivityId = Guid.NewGuid();
190183

191-
if (this.MyInvocation.BoundParameters.ContainsKey("Version"))
184+
try
192185
{
193-
if (this.Version == 12.0f)
194-
{
195-
try
196-
{
197-
context = new TSqlConnectionContext(
198-
sessionActivityId,
199-
manageUrl.Host,
200-
credentials.UserName,
201-
credentials.Password);
202-
}
203-
catch (Exception ex)
204-
{
205-
SqlDatabaseExceptionHandler.WriteErrorDetails(
206-
this,
207-
sessionActivityId.ToString(),
208-
ex);
209-
210-
// The context is not in an valid state because of the error, set the context
211-
// back to null.
212-
context = null;
213-
}
214-
}
215-
else
216-
{
217-
try
218-
{
219-
context = ServerDataServiceSqlAuth.Create(
220-
managementServiceUri,
221-
sessionActivityId,
222-
credentials,
223-
serverName);
224-
225-
// Retrieve $metadata to verify model version compatibility
226-
XDocument metadata = ((ServerDataServiceSqlAuth)context).RetrieveMetadata();
227-
XDocument filteredMetadata = DataConnectionUtility.FilterMetadataDocument(metadata);
228-
string metadataHash = DataConnectionUtility.GetDocumentHash(filteredMetadata);
229-
if (!((ServerDataServiceSqlAuth)context).metadataHashes.Any(knownHash => metadataHash == knownHash))
230-
{
231-
this.WriteWarning(Resources.WarningModelOutOfDate);
232-
}
233-
234-
((ServerDataServiceSqlAuth)context).MergeOption = MergeOption.PreserveChanges;
235-
}
236-
catch (Exception ex)
237-
{
238-
SqlDatabaseExceptionHandler.WriteErrorDetails(
239-
this,
240-
sessionActivityId.ToString(),
241-
ex);
242-
243-
// The context is not in an valid state because of the error, set the context
244-
// back to null.
245-
context = null;
246-
}
247-
}
186+
context = SqlAuthContextFactory.GetContext(this, serverName, manageUrl, credentials, sessionActivityId, managementServiceUri);
187+
}
188+
catch (Exception ex)
189+
{
190+
SqlDatabaseExceptionHandler.WriteErrorDetails(
191+
this,
192+
sessionActivityId.ToString(),
193+
ex);
194+
195+
// The context is not in an valid state because of the error, set the context
196+
// back to null.
197+
context = null;
248198
}
249199

250200
return context;
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
using Microsoft.WindowsAzure.Commands.SqlDatabase.Properties;
2+
using Microsoft.WindowsAzure.Commands.SqlDatabase.Services.Common;
3+
using System;
4+
using System.Collections.Generic;
5+
using System.Data.Services.Client;
6+
using System.Data.SqlClient;
7+
using System.Linq;
8+
using System.Management.Automation;
9+
using System.Text;
10+
using System.Threading.Tasks;
11+
using System.Xml.Linq;
12+
13+
namespace Microsoft.WindowsAzure.Commands.SqlDatabase.Services.Server
14+
{
15+
public class SqlAuthContextFactory
16+
{
17+
public static IServerDataServiceContext GetContext(
18+
PSCmdlet cmdlet,
19+
string serverName,
20+
Uri manageUrl,
21+
SqlAuthenticationCredentials credentials,
22+
Guid sessionActivityId,
23+
Uri managementServiceUri)
24+
{
25+
Version version = GetVersion(manageUrl, credentials);
26+
27+
IServerDataServiceContext context = null;
28+
29+
if (version.Major >= 12)
30+
{
31+
context = new TSqlConnectionContext(
32+
sessionActivityId,
33+
manageUrl.Host,
34+
credentials.UserName,
35+
credentials.Password);
36+
}
37+
else
38+
{
39+
context = ServerDataServiceSqlAuth.Create(
40+
managementServiceUri,
41+
sessionActivityId,
42+
credentials,
43+
serverName);
44+
45+
// Retrieve $metadata to verify model version compatibility
46+
XDocument metadata = ((ServerDataServiceSqlAuth)context).RetrieveMetadata();
47+
XDocument filteredMetadata = DataConnectionUtility.FilterMetadataDocument(metadata);
48+
string metadataHash = DataConnectionUtility.GetDocumentHash(filteredMetadata);
49+
if (!((ServerDataServiceSqlAuth)context).metadataHashes.Any(knownHash => metadataHash == knownHash))
50+
{
51+
cmdlet.WriteWarning(Resources.WarningModelOutOfDate);
52+
}
53+
54+
((ServerDataServiceSqlAuth)context).MergeOption = MergeOption.PreserveChanges;
55+
}
56+
57+
return context;
58+
}
59+
60+
/// <summary>
61+
/// Queries the server to get the server version
62+
/// </summary>
63+
/// <param name="manageUrl">The manage url of the server. Eg: https://{serverName}.database.windows.net</param>
64+
/// <param name="credentials">The login credentials</param>
65+
/// <returns>The server version</returns>
66+
private static Version GetVersion(Uri manageUrl, SqlAuthenticationCredentials credentials)
67+
{
68+
string serverName = manageUrl.Host.Split('.').First();
69+
SqlConnectionStringBuilder builder = new SqlConnectionStringBuilder();
70+
builder["Server"] = manageUrl.Host;
71+
builder.UserID = credentials.UserName + "@" + serverName;
72+
builder.Password = credentials.Password;
73+
builder["Database"] = null;
74+
builder["Encrypt"] = false;
75+
builder.ConnectTimeout = 60;
76+
77+
string commandText = "select serverproperty('ProductVersion')";
78+
79+
using(SqlConnection conn = new SqlConnection(builder.ConnectionString))
80+
{
81+
using (SqlCommand command = new SqlCommand(commandText, conn))
82+
{
83+
conn.Open();
84+
85+
string val = (string)command.ExecuteScalar();
86+
return new Version(val);
87+
}
88+
}
89+
}
90+
}
91+
}

0 commit comments

Comments
 (0)