mirror of
https://github.com/ckaczor/sqltoolsservice.git
synced 2026-01-14 01:25:40 -05:00
132 lines
5.0 KiB
C#
132 lines
5.0 KiB
C#
//
|
|
// Copyright (c) Microsoft. All rights reserved.
|
|
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
|
|
//
|
|
|
|
using System;
|
|
using System.IO;
|
|
using System.Linq;
|
|
using System.Runtime.InteropServices;
|
|
using Microsoft.SqlTools.ServiceLayer.Connection.Contracts;
|
|
using Newtonsoft.Json.Linq;
|
|
|
|
namespace Microsoft.SqlTools.ServiceLayer.TestDriver.Utility
|
|
{
|
|
/// <summary>
|
|
/// Contains useful utility methods for testing connections
|
|
/// </summary>
|
|
public class ConnectionTestUtils
|
|
{
|
|
private static readonly Lazy<ConnectParams> azureTestServerConnection =
|
|
new Lazy<ConnectParams>(() => GetConnectionFromVsCodeSettings("***REMOVED***"));
|
|
|
|
public static ConnectParams AzureTestServerConnection
|
|
{
|
|
get { return azureTestServerConnection.Value; }
|
|
}
|
|
|
|
public static ConnectParams LocalhostConnection
|
|
{
|
|
get
|
|
{
|
|
return new ConnectParams()
|
|
{
|
|
Connection = new ConnectionDetails()
|
|
{
|
|
DatabaseName = "master",
|
|
ServerName = "localhost",
|
|
AuthenticationType = "Integrated"
|
|
}
|
|
};
|
|
}
|
|
}
|
|
|
|
public static ConnectParams InvalidConnection
|
|
{
|
|
get
|
|
{
|
|
return new ConnectParams()
|
|
{
|
|
Connection = new ConnectionDetails()
|
|
{
|
|
DatabaseName = "master",
|
|
ServerName = "localhost",
|
|
AuthenticationType = "SqlLogin",
|
|
UserName = "invalid",
|
|
Password = ".."
|
|
}
|
|
};
|
|
}
|
|
}
|
|
|
|
private static readonly Lazy<ConnectParams> sqlDataToolsAzureConnection =
|
|
new Lazy<ConnectParams>(() => GetConnectionFromVsCodeSettings("***REMOVED***"));
|
|
|
|
public static ConnectParams SqlDataToolsAzureConnection
|
|
{
|
|
get { return sqlDataToolsAzureConnection.Value; }
|
|
}
|
|
|
|
private static readonly Lazy<ConnectParams> dataToolsTelemetryAzureConnection =
|
|
new Lazy<ConnectParams>(() => GetConnectionFromVsCodeSettings("***REMOVED***"));
|
|
|
|
public static ConnectParams DataToolsTelemetryAzureConnection
|
|
{
|
|
get { return dataToolsTelemetryAzureConnection.Value; }
|
|
}
|
|
|
|
/// <summary>
|
|
/// Create a connection parameters object
|
|
/// </summary>
|
|
public static ConnectParams CreateConnectParams(string server, string database, string username, string password)
|
|
{
|
|
ConnectParams connectParams = new ConnectParams();
|
|
connectParams.Connection = new ConnectionDetails();
|
|
connectParams.Connection.ServerName = server;
|
|
connectParams.Connection.DatabaseName = database;
|
|
connectParams.Connection.UserName = username;
|
|
connectParams.Connection.Password = password;
|
|
connectParams.Connection.AuthenticationType = "SqlLogin";
|
|
return connectParams;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Retrieve connection parameters from the vscode settings file
|
|
/// </summary>
|
|
public static ConnectParams GetConnectionFromVsCodeSettings(string serverName)
|
|
{
|
|
try
|
|
{
|
|
string settingsFilename;
|
|
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
|
|
{
|
|
settingsFilename = Environment.GetEnvironmentVariable("APPDATA") + @"\Code\User\settings.json";
|
|
}
|
|
else if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
|
|
{
|
|
settingsFilename = Environment.GetEnvironmentVariable("HOME") + @"/Library/Application Support/Code/User/settings.json";
|
|
}
|
|
else
|
|
{
|
|
settingsFilename = Environment.GetEnvironmentVariable("HOME") + @"/.config/Code/User/settings.json";
|
|
}
|
|
string settingsFileContents = File.ReadAllText(settingsFilename);
|
|
|
|
JObject root = JObject.Parse(settingsFileContents);
|
|
JArray connections = (JArray)root["mssql.connections"];
|
|
|
|
var connectionObject = connections.Where(x => x["server"].ToString() == serverName).First();
|
|
|
|
return CreateConnectParams( connectionObject["server"].ToString(),
|
|
connectionObject["database"].ToString(),
|
|
connectionObject["user"].ToString(),
|
|
connectionObject["password"].ToString());
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
throw new Exception("Unable to load connection " + serverName + " from the vscode settings.json. Ensure the file is formatted correctly.", ex);
|
|
}
|
|
}
|
|
}
|
|
}
|