Skip to content

Commit

Permalink
Microsoft.Azure.Commands.Common.Stratagies
Browse files Browse the repository at this point in the history
  • Loading branch information
sergey-shandar committed Nov 15, 2017
1 parent 61d92df commit 29af582
Show file tree
Hide file tree
Showing 22 changed files with 871 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
using System.Collections.Concurrent;
using System.Threading;
using System.Threading.Tasks;

namespace Microsoft.Azure.Commands.Common.Strategies
{
abstract class AsyncOperationVisitor : IResourceConfigVisitor<Task<object>>
{
public IClient Client { get; }

public CancellationToken CancellationToken { get; }

public State Result { get; } = new State();


public AsyncOperationVisitor(IClient client, CancellationToken cancellationToken)
{
Client = client;
CancellationToken = cancellationToken;
}

public async Task<object> GetOrAddUntyped(IResourceConfig config)
=> await TaskMap.GetOrAdd(
config,
async _ =>
{
var model = await config.Apply(this);
Result.GetOrAddUntyped(config, () => model);
return model;
});

public async Task<Model> GetOrAdd<Model>(IResourceConfig<Model> config)
where Model : class
{
var model = await GetOrAddUntyped(config);
return model as Model;
}

public abstract Task<object> Visit<Model>(ResourceConfig<Model> config) where Model : class;

public abstract Task<object> Visit<Model, ParentModel>(
NestedResourceConfig<Model, ParentModel> config)
where Model : class
where ParentModel : class;

ConcurrentDictionary<IResourceConfig, Task<object>> TaskMap { get; }
= new ConcurrentDictionary<IResourceConfig, Task<object>>();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="14.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProjectGuid>{EEA69772-D41B-482A-9252-2B4595C59E53}</ProjectGuid>
<OutputType>Library</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>Microsoft.Azure.Commands.Common.Strategies</RootNamespace>
<AssemblyName>Microsoft.Azure.Commands.Common.Strategies</AssemblyName>
<TargetFrameworkVersion>v4.5.2</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="Microsoft.Azure.Management.Compute, Version=16.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<HintPath>..\..\..\packages\Microsoft.Azure.Management.Compute.16.2.0\lib\net452\Microsoft.Azure.Management.Compute.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="Microsoft.Azure.Management.Network, Version=14.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<HintPath>..\..\..\packages\Microsoft.Azure.Management.Network.14.0.0-preview\lib\net452\Microsoft.Azure.Management.Network.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="Microsoft.Azure.Management.ResourceManager, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<HintPath>..\..\..\packages\Microsoft.Azure.Management.ResourceManager.1.6.0-preview\lib\net452\Microsoft.Azure.Management.ResourceManager.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="Microsoft.Rest.ClientRuntime, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<HintPath>..\..\..\packages\Microsoft.Rest.ClientRuntime.2.3.9\lib\net452\Microsoft.Rest.ClientRuntime.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="Microsoft.Rest.ClientRuntime.Azure, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<HintPath>..\..\..\packages\Microsoft.Rest.ClientRuntime.Azure.3.3.9\lib\net452\Microsoft.Rest.ClientRuntime.Azure.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="Newtonsoft.Json, Version=6.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
<HintPath>..\..\..\packages\Newtonsoft.Json.6.0.8\lib\net45\Newtonsoft.Json.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="Microsoft.CSharp" />
<Reference Include="System.Data" />
<Reference Include="System.Net.Http" />
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="AsyncOperationVisitor.cs" />
<Compile Include="CreateOrUpdateAsyncOperation.cs" />
<Compile Include="CreateOrUpdateAsyncParams.cs" />
<Compile Include="Extensions.cs" />
<Compile Include="GetAsyncOperation.cs" />
<Compile Include="GetAsyncParams.cs" />
<Compile Include="IClient.cs" />
<Compile Include="IResourceConfig.cs" />
<Compile Include="IResourceConfigVisitor.cs" />
<Compile Include="IResourcePolicy.cs" />
<Compile Include="IState.cs" />
<Compile Include="NestedResourceConfig.cs" />
<Compile Include="NestedResourcePolicy.cs" />
<Compile Include="TargetState.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="ResourceConfig.cs" />
<Compile Include="ResourcePolicy.cs" />
<Compile Include="State.cs" />
<Compile Include="StateLocation.cs" />
</ItemGroup>
<ItemGroup>
<None Include="packages.config" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
<Target Name="BeforeBuild">
</Target>
<Target Name="AfterBuild">
</Target>
-->
</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
using System.Linq;
using System.Threading;
using System.Threading.Tasks;

namespace Microsoft.Azure.Commands.Common.Strategies
{
public static class CreateOrUpdateAsyncOperation
{
public static async Task<IState> CreateOrUpdateAsync<Model>(
this IResourceConfig<Model> config,
IClient client,
IState current,
IState target,
CancellationToken cancellationToken)
where Model : class
{
var visitor = new CreateAsyncVisitor(client, current, target, cancellationToken);
await visitor.GetOrAdd(config);
return visitor.Result;
}

sealed class CreateAsyncVisitor : AsyncOperationVisitor
{
public override async Task<object> Visit<Model>(ResourceConfig<Model> config)
{
var current = Current.GetOrNull(config);
if (current != null)
{
return current;
}
var tasks = config.Dependencies.Select(GetOrAddUntyped);
await Task.WhenAll(tasks);
return await config.Policy.CreateOrUpdateAsync(CreateOrUpdateAsyncParams.Create(
Client,
config.ResourceGroupName,
config.Name,
Target.GetOrNull(config),
CancellationToken));
}

public override async Task<object> Visit<Model, ParentModel>(
NestedResourceConfig<Model, ParentModel> config)
{
var parent = await GetOrAdd(config.Parent);
return config.Policy.Get(parent, config.Name);
}

public CreateAsyncVisitor(
IClient client,
IState current,
IState target,
CancellationToken cancellationToken)
: base(client, cancellationToken)
{
Current = current;
Target = target;
}

IState Current { get; }

IState Target { get; }
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
using System.Threading;

namespace Microsoft.Azure.Commands.Common.Strategies
{
public static class CreateOrUpdateAsyncParams
{
public static CreateOrUpdateAsyncParams<Operations, Model> Create<Operations, Model>(
Operations operations,
string resourceGroupName,
string name,
Model model,
CancellationToken cancellationToken)
=> new CreateOrUpdateAsyncParams<Operations, Model>(
operations, resourceGroupName, name, model, cancellationToken);
}

public sealed class CreateOrUpdateAsyncParams<TOperations, TModel>
{
public TOperations Operations { get; }

public string ResourceGroupName { get; }

public string Name { get; }

public CancellationToken CancellationToken { get; }

public TModel Model { get; }

public CreateOrUpdateAsyncParams(
TOperations operations,
string resourceGroupName,
string name,
TModel model,
CancellationToken cancellationToken)
{
Operations = operations;
ResourceGroupName = resourceGroupName;
Name = name;
Model = model;
CancellationToken = cancellationToken;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using System.Collections.Generic;
using System.Linq;

namespace Microsoft.Azure.Commands.Common.Strategies
{
public static class Extensions
{
public static IEnumerable<T> EmptyIfNull<T>(this IEnumerable<T> value)
=> value ?? Enumerable.Empty<T>();

public static V GetOrNull<K, V>(this IDictionary<K, V> dictionary, K key)
where V : class
{
V result;
return dictionary.TryGetValue(key, out result) ? result : null;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
using Microsoft.Rest.Azure;
using System.Linq;
using System.Net;
using System.Threading;
using System.Threading.Tasks;

namespace Microsoft.Azure.Commands.Common.Strategies
{
public static class GetAsyncOperation
{
public static async Task<IState> GetAsync<Model>(
this IResourceConfig<Model> resourceConfig,
IClient client,
CancellationToken cancellationToken)
where Model : class
{
var visitor = new Visitor(client, cancellationToken);
await visitor.GetOrAdd(resourceConfig);
return visitor.Result;
}

sealed class Visitor : AsyncOperationVisitor
{
public override async Task<object> Visit<Model>(ResourceConfig<Model> config)
{
Model info;
try
{
info = await config.Policy.GetAsync(GetAsyncParams.Create(
Client, config.ResourceGroupName, config.Name, CancellationToken));
}
catch (CloudException e) when (e.Response.StatusCode == HttpStatusCode.NotFound)
{
info = null;
}
if (info == null)
{
var tasks = config.Dependencies.Select(GetOrAddUntyped);
await Task.WhenAll(tasks);
return null;
}
return info;
}

public override async Task<object> Visit<Model, ParentModel>(
NestedResourceConfig<Model, ParentModel> config)
{
var parent = await GetOrAdd(config.Parent);
return parent == null ? null : config.Policy.Get(parent, config.Name);
}

public Visitor(IClient client, CancellationToken cancellationToken)
: base(client, cancellationToken)
{
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
using System.Threading;

namespace Microsoft.Azure.Commands.Common.Strategies
{
public static class GetAsyncParams
{
public static GetAsyncParams<Operations> Create<Operations>(
Operations operations,
string resourceGroupName,
string name,
CancellationToken cancellationToken)
=> new GetAsyncParams<Operations>(
operations, resourceGroupName, name, cancellationToken);
}

public class GetAsyncParams<TOperations>
{
public TOperations Operations { get; }

public string ResourceGroupName { get; }

public string Name { get; }

public CancellationToken CancellationToken { get; }

public GetAsyncParams(
TOperations operations,
string resourceGroupName,
string name,
CancellationToken cancellationToken)
{
Operations = operations;
ResourceGroupName = resourceGroupName;
Name = name;
CancellationToken = cancellationToken;
}
}
}
10 changes: 10 additions & 0 deletions src/ResourceManager/Common/Commands.Common.Strategies/IClient.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using System;

namespace Microsoft.Azure.Commands.Common.Strategies
{
public interface IClient
{
T GetClient<T>()
where T : class, IDisposable;
}
}
Loading

0 comments on commit 29af582

Please sign in to comment.