mirror of
https://github.com/ckaczor/sqltoolsservice.git
synced 2026-02-16 18:47:57 -05:00
Port Azure code from SSDT to the tools service (#477)
Porting of the vast majority of Azure-related code from SSDT. This is very large, so I want to put this out as one large "lift and shift" PR before I do the tools-service specific JSON-RPC service handlers, connect a new account handler (as the code to get necessary info from accounts and subscriptions isn't fully complete) and add tests over these **What's in this PR**: - Created 3 new projects: - Microsoft.SqlTools.ResourceProvider will host the executable that accepts requests for Azure-related actions over the JSON-RPC protocol. This must be separate from other DLLs since a direct dependency on the Azure SDK DLLs fails (they're NetStandard 1.4 and you can't reference them if you have RuntimeIdentifiers in your .csproj file) - Microsoft.SqlTools.ResourceProvider.Core is where all the main business logic is, including definitions and logic on how to navigate over resources and create firewall rules, etc. - Microsoft.SqlTools.ResourceProvider.DefaultImpl is the actual Azure implementation of the resource provider APIs. The reason for separating this is to support eventual integration back into other tools (since their Azure and Identity services will be different). - Implemented the AzureResourceManager that connects to Azure via ARM APIs and handles creating firewall rule and querying databases. The dependent DLLs have had major breaking changes, so will need additional verification to ensure this works as expected - Ported the unit tests for all code that was not a viewmodel. Viewmodel test code will be ported in a future update as we plumb through a service-equivalent to these. Also, the DependencyManager code which has overlap with our service provider code is commented out. Will work to uncomment in a future update as it has value to test some scenarios **What's not in this PR**: - Identity Services. We currently just have a stub for the interface, and even that will likely change a little - anything JSON-RPC or registered service related. These will be adapted from the viewmodels and added in a separate PR
This commit is contained in:
@@ -0,0 +1,44 @@
|
||||
//
|
||||
// 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.Composition;
|
||||
|
||||
namespace Microsoft.SqlTools.Extensibility
|
||||
{
|
||||
/// <summary>
|
||||
/// Base attribute class for all export definitions.
|
||||
/// </summary>
|
||||
[MetadataAttribute]
|
||||
[AttributeUsage(AttributeTargets.Class, AllowMultiple = false)]
|
||||
public abstract class ExportStandardMetadataAttribute : ExportAttribute, IStandardMetadata
|
||||
{
|
||||
/// <summary>
|
||||
/// Base class for DAC extensibility exports
|
||||
/// </summary>
|
||||
protected ExportStandardMetadataAttribute(Type contractType, string id, string displayName = null)
|
||||
: base(contractType)
|
||||
{
|
||||
Id = id;
|
||||
DisplayName = displayName;
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// The version of this extension
|
||||
/// </summary>
|
||||
public string Version { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// The id of the extension
|
||||
/// </summary>
|
||||
public string Id { get; private set; }
|
||||
|
||||
/// <summary>
|
||||
/// The display name for the extension
|
||||
/// </summary>
|
||||
public virtual string DisplayName { get; private set; }
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
//
|
||||
// Copyright (c) Microsoft. All rights reserved.
|
||||
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
|
||||
//
|
||||
|
||||
namespace Microsoft.SqlTools.Extensibility
|
||||
{
|
||||
/// <summary>
|
||||
/// Standard Metadata needed for extensions.
|
||||
/// </summary>
|
||||
public interface IStandardMetadata
|
||||
{
|
||||
/// <summary>
|
||||
/// Extension version. Should be in the format "1.0.0.0" or similar
|
||||
/// </summary>
|
||||
string Version { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Unique Id used to identify the export.
|
||||
/// </summary>
|
||||
string Id { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Optional Display name describing the export type
|
||||
/// </summary>
|
||||
string DisplayName { get; }
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,6 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
<PropertyGroup>
|
||||
<TargetFramework>netcoreapp2.0</TargetFramework>
|
||||
<TargetFramework>netstandard2.0</TargetFramework>
|
||||
<EnableDefaultItems>false</EnableDefaultItems>
|
||||
<EnableDefaultCompileItems>false</EnableDefaultCompileItems>
|
||||
<EnableDefaultEmbeddedResourceItems>false</EnableDefaultEmbeddedResourceItems>
|
||||
@@ -10,11 +10,9 @@
|
||||
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
|
||||
<PreserveCompilationContext>true</PreserveCompilationContext>
|
||||
<DebugType>portable</DebugType>
|
||||
<RuntimeIdentifiers>win7-x64;win7-x86;ubuntu.14.04-x64;ubuntu.16.04-x64;centos.7-x64;rhel.7.2-x64;debian.8-x64;fedora.23-x64;opensuse.13.2-x64;osx.10.11-x64</RuntimeIdentifiers>
|
||||
</PropertyGroup>
|
||||
<ItemGroup>
|
||||
<PackageReference Include="System.Data.SqlClient" Version="4.4.0" />
|
||||
<PackageReference Include="Microsoft.SqlServer.Smo" Version="140.2.6" />
|
||||
<PackageReference Include="Newtonsoft.Json" Version="10.0.2" />
|
||||
<PackageReference Include="Microsoft.Extensions.DependencyModel" Version="2.0.0" />
|
||||
<PackageReference Include="System.Runtime.Loader" Version="4.3.0" />
|
||||
@@ -25,6 +23,7 @@
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<EmbeddedResource Include="Localization\sr.resx" />
|
||||
<None Include="Localization\sr.strings" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<None Include="packages.config" />
|
||||
|
||||
55
src/Microsoft.SqlTools.Hosting/Utility/AutoLock.cs
Normal file
55
src/Microsoft.SqlTools.Hosting/Utility/AutoLock.cs
Normal file
@@ -0,0 +1,55 @@
|
||||
//
|
||||
// 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.Threading;
|
||||
|
||||
namespace Microsoft.SqlTools.Utility
|
||||
{
|
||||
/// <summary>
|
||||
/// A wrapper around the ReaderWriterLock to make sure the locks are released even if the action fails
|
||||
/// </summary>
|
||||
public class AutoLock
|
||||
{
|
||||
private readonly ReaderWriterLock _lock;
|
||||
private readonly bool _isWriteLocked;
|
||||
|
||||
/// <summary>
|
||||
/// Creates new lock given type of lock and timeout
|
||||
/// </summary>
|
||||
public AutoLock(ReaderWriterLock lockObj, bool isWriteLock, TimeSpan timeOut, Action action, out Exception exception)
|
||||
{
|
||||
exception = null;
|
||||
try
|
||||
{
|
||||
_lock = lockObj;
|
||||
_isWriteLocked = isWriteLock;
|
||||
if (_isWriteLocked)
|
||||
{
|
||||
_lock.AcquireWriterLock(timeOut);
|
||||
}
|
||||
else
|
||||
{
|
||||
_lock.AcquireReaderLock(timeOut);
|
||||
}
|
||||
action();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
exception = ex;
|
||||
}
|
||||
finally
|
||||
{
|
||||
if (_isWriteLocked && _lock.IsWriterLockHeld)
|
||||
{
|
||||
_lock.ReleaseWriterLock();
|
||||
}
|
||||
else if (!_isWriteLocked && _lock.IsReaderLockHeld)
|
||||
{
|
||||
_lock.ReleaseReaderLock();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -50,7 +50,7 @@ namespace Microsoft.SqlTools.Hosting.Utility
|
||||
ShouldExit = true;
|
||||
return;
|
||||
default:
|
||||
ErrorMessage += String.Format("Unknown argument \"{0}\"" + Environment.NewLine, argName);
|
||||
ErrorMessage += string.Format("Unknown argument \"{0}\"" + Environment.NewLine, argName);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
82
src/Microsoft.SqlTools.Hosting/Utility/ConcurrentCache.cs
Normal file
82
src/Microsoft.SqlTools.Hosting/Utility/ConcurrentCache.cs
Normal file
@@ -0,0 +1,82 @@
|
||||
//
|
||||
// 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.Collections.Generic;
|
||||
using System.Threading;
|
||||
|
||||
namespace Microsoft.SqlTools.Utility
|
||||
{
|
||||
public class ConcurrentCache<T>
|
||||
{
|
||||
private readonly Dictionary<string, T> _cache = new Dictionary<string, T>();
|
||||
private readonly ReaderWriterLock _readerWriterLock = new ReaderWriterLock();
|
||||
private readonly TimeSpan _timeout = TimeSpan.FromHours(1);
|
||||
|
||||
public void ClearCache(IEnumerable<string> keys)
|
||||
{
|
||||
Exception exception;
|
||||
new AutoLock(_readerWriterLock, true, _timeout, () =>
|
||||
{
|
||||
{
|
||||
foreach (var key in keys)
|
||||
{
|
||||
if (_cache.ContainsKey(key))
|
||||
{
|
||||
_cache.Remove(key);
|
||||
}
|
||||
}
|
||||
}
|
||||
}, out exception);
|
||||
if (exception != null)
|
||||
{
|
||||
throw exception;
|
||||
}
|
||||
}
|
||||
|
||||
public T Get(string key)
|
||||
{
|
||||
T result = default(T);
|
||||
Exception exception;
|
||||
new AutoLock(_readerWriterLock, false, _timeout, () =>
|
||||
{
|
||||
if (_cache.ContainsKey(key))
|
||||
{
|
||||
result = _cache[key];
|
||||
}
|
||||
}, out exception);
|
||||
if (exception != null)
|
||||
{
|
||||
throw exception;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
public T UpdateCache(string key, T newValue)
|
||||
{
|
||||
T result = newValue;
|
||||
|
||||
Exception exception;
|
||||
new AutoLock(_readerWriterLock, true, _timeout, () =>
|
||||
{
|
||||
bool isDefined = _cache.ContainsKey(key);
|
||||
if (!isDefined)
|
||||
{
|
||||
_cache.Add(key, newValue);
|
||||
}
|
||||
else
|
||||
{
|
||||
result = _cache[key];
|
||||
}
|
||||
}, out exception);
|
||||
if (exception != null)
|
||||
{
|
||||
throw exception;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,7 +1,6 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<packages>
|
||||
<package id="System.Data.SqlClient" version="4.4.0-preview1-25305-02" />
|
||||
<package id="Microsoft.SqlServer.Smo" version="140.17054.0" />
|
||||
<package id="Microsoft.SqlServer.Management.SqlScriptPublishModel" version="140.17050.0" />
|
||||
<package id="Newtonsoft.Json" version="10.0.2" />
|
||||
<package id="Microsoft.Extensions.DependencyModel" version="2.0.0-preview1-002111" />
|
||||
|
||||
Reference in New Issue
Block a user