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:
Kevin Cunnane
2017-10-04 12:37:20 -07:00
committed by GitHub
parent 98f7e59f1a
commit ac64ac063b
108 changed files with 8181 additions and 21 deletions

View File

@@ -0,0 +1,113 @@
//
// 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.Linq;
using System.Threading;
using System.Threading.Tasks;
namespace Microsoft.SqlTools.ResourceProvider.Core
{
internal static class AzureUtil
{
/// <summary>
/// Execute an async action for each input in the a list of input in parallel.
/// If any task fails, adds the exeption message to the response errors
/// If cancellation token is set to cancel, returns empty response
/// </summary>
/// <param name="session">Resource management session to use to call the resource manager</param>
/// <param name="inputs">List of inputs</param>
/// <param name="serverName">server name to filter the result</param>
/// <param name="cancellationToken">Cancellation token</param>
/// <param name="asyncAction">Async action</param>
/// <returns>ServiceResponse including the list of data and errors</returns>
public static async Task<ServiceResponse<TResult>> ExecuteGetAzureResourceAsParallel<TInput, TResult>(
IAzureResourceManagementSession session,
IEnumerable<TInput> inputs,
string serverName,
CancellationToken cancellationToken,
Func<IAzureResourceManagementSession,
TInput,
string,
CancellationToken,
CancellationToken,
Task<ServiceResponse<TResult>>> asyncAction
)
{
List<TResult> mergedResult = new List<TResult>();
List<Exception> mergedErrors = new List<Exception>();
try
{
if (inputs == null)
{
return new ServiceResponse<TResult>(mergedResult);
}
List<TInput> inputList = inputs.ToList();
ServiceResponse<TResult>[] resultList = new ServiceResponse<TResult>[inputList.Count];
CancellationTokenSource cancellationTokenSource = new CancellationTokenSource();
var tasks = Enumerable.Range(0, inputList.Count())
.Select(async i =>
{
ServiceResponse<TResult> result = await GetResult(session, inputList[i], serverName, cancellationToken,
cancellationTokenSource.Token, asyncAction);
//server name is used to filter the result and if the data is already found not, we need to cancel the other tasks
if (!string.IsNullOrEmpty(serverName) && result.Found)
{
cancellationTokenSource.Cancel();
}
resultList[i] = result;
return result;
}
);
await Task.WhenAll(tasks);
if (!cancellationToken.IsCancellationRequested)
{
foreach (ServiceResponse<TResult> resultForEachInput in resultList)
{
mergedResult.AddRange(resultForEachInput.Data);
mergedErrors.AddRange(resultForEachInput.Errors);
}
}
}
catch (Exception ex)
{
mergedErrors.Add(ex);
return new ServiceResponse<TResult>(mergedResult, mergedErrors);
}
return new ServiceResponse<TResult>(mergedResult, mergedErrors);
}
private static async Task<ServiceResponse<TResult>> GetResult<TInput, TResult>(
IAzureResourceManagementSession session,
TInput input,
string serverName,
CancellationToken cancellationToken,
CancellationToken internalCancellationToken,
Func<IAzureResourceManagementSession,
TInput,
string,
CancellationToken,
CancellationToken,
Task<ServiceResponse<TResult>>> asyncAction
)
{
if (cancellationToken.IsCancellationRequested || internalCancellationToken.IsCancellationRequested)
{
return new ServiceResponse<TResult>();
}
try
{
return await asyncAction(session, input, serverName, cancellationToken, internalCancellationToken);
}
catch (Exception ex)
{
return new ServiceResponse<TResult>(ex);
}
}
}
}