Skip to content

Commit 29af582

Browse files
Microsoft.Azure.Commands.Common.Stratagies
1 parent 61d92df commit 29af582

22 files changed

+871
-0
lines changed
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
using System.Collections.Concurrent;
2+
using System.Threading;
3+
using System.Threading.Tasks;
4+
5+
namespace Microsoft.Azure.Commands.Common.Strategies
6+
{
7+
abstract class AsyncOperationVisitor : IResourceConfigVisitor<Task<object>>
8+
{
9+
public IClient Client { get; }
10+
11+
public CancellationToken CancellationToken { get; }
12+
13+
public State Result { get; } = new State();
14+
15+
16+
public AsyncOperationVisitor(IClient client, CancellationToken cancellationToken)
17+
{
18+
Client = client;
19+
CancellationToken = cancellationToken;
20+
}
21+
22+
public async Task<object> GetOrAddUntyped(IResourceConfig config)
23+
=> await TaskMap.GetOrAdd(
24+
config,
25+
async _ =>
26+
{
27+
var model = await config.Apply(this);
28+
Result.GetOrAddUntyped(config, () => model);
29+
return model;
30+
});
31+
32+
public async Task<Model> GetOrAdd<Model>(IResourceConfig<Model> config)
33+
where Model : class
34+
{
35+
var model = await GetOrAddUntyped(config);
36+
return model as Model;
37+
}
38+
39+
public abstract Task<object> Visit<Model>(ResourceConfig<Model> config) where Model : class;
40+
41+
public abstract Task<object> Visit<Model, ParentModel>(
42+
NestedResourceConfig<Model, ParentModel> config)
43+
where Model : class
44+
where ParentModel : class;
45+
46+
ConcurrentDictionary<IResourceConfig, Task<object>> TaskMap { get; }
47+
= new ConcurrentDictionary<IResourceConfig, Task<object>>();
48+
}
49+
}
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project ToolsVersion="14.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3+
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
4+
<PropertyGroup>
5+
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
6+
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
7+
<ProjectGuid>{EEA69772-D41B-482A-9252-2B4595C59E53}</ProjectGuid>
8+
<OutputType>Library</OutputType>
9+
<AppDesignerFolder>Properties</AppDesignerFolder>
10+
<RootNamespace>Microsoft.Azure.Commands.Common.Strategies</RootNamespace>
11+
<AssemblyName>Microsoft.Azure.Commands.Common.Strategies</AssemblyName>
12+
<TargetFrameworkVersion>v4.5.2</TargetFrameworkVersion>
13+
<FileAlignment>512</FileAlignment>
14+
</PropertyGroup>
15+
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
16+
<DebugSymbols>true</DebugSymbols>
17+
<DebugType>full</DebugType>
18+
<Optimize>false</Optimize>
19+
<OutputPath>bin\Debug\</OutputPath>
20+
<DefineConstants>DEBUG;TRACE</DefineConstants>
21+
<ErrorReport>prompt</ErrorReport>
22+
<WarningLevel>4</WarningLevel>
23+
</PropertyGroup>
24+
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
25+
<DebugType>pdbonly</DebugType>
26+
<Optimize>true</Optimize>
27+
<OutputPath>bin\Release\</OutputPath>
28+
<DefineConstants>TRACE</DefineConstants>
29+
<ErrorReport>prompt</ErrorReport>
30+
<WarningLevel>4</WarningLevel>
31+
</PropertyGroup>
32+
<ItemGroup>
33+
<Reference Include="Microsoft.Azure.Management.Compute, Version=16.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
34+
<HintPath>..\..\..\packages\Microsoft.Azure.Management.Compute.16.2.0\lib\net452\Microsoft.Azure.Management.Compute.dll</HintPath>
35+
<Private>True</Private>
36+
</Reference>
37+
<Reference Include="Microsoft.Azure.Management.Network, Version=14.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
38+
<HintPath>..\..\..\packages\Microsoft.Azure.Management.Network.14.0.0-preview\lib\net452\Microsoft.Azure.Management.Network.dll</HintPath>
39+
<Private>True</Private>
40+
</Reference>
41+
<Reference Include="Microsoft.Azure.Management.ResourceManager, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
42+
<HintPath>..\..\..\packages\Microsoft.Azure.Management.ResourceManager.1.6.0-preview\lib\net452\Microsoft.Azure.Management.ResourceManager.dll</HintPath>
43+
<Private>True</Private>
44+
</Reference>
45+
<Reference Include="Microsoft.Rest.ClientRuntime, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
46+
<HintPath>..\..\..\packages\Microsoft.Rest.ClientRuntime.2.3.9\lib\net452\Microsoft.Rest.ClientRuntime.dll</HintPath>
47+
<Private>True</Private>
48+
</Reference>
49+
<Reference Include="Microsoft.Rest.ClientRuntime.Azure, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
50+
<HintPath>..\..\..\packages\Microsoft.Rest.ClientRuntime.Azure.3.3.9\lib\net452\Microsoft.Rest.ClientRuntime.Azure.dll</HintPath>
51+
<Private>True</Private>
52+
</Reference>
53+
<Reference Include="Newtonsoft.Json, Version=6.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
54+
<HintPath>..\..\..\packages\Newtonsoft.Json.6.0.8\lib\net45\Newtonsoft.Json.dll</HintPath>
55+
<Private>True</Private>
56+
</Reference>
57+
<Reference Include="System" />
58+
<Reference Include="System.Core" />
59+
<Reference Include="System.Xml.Linq" />
60+
<Reference Include="System.Data.DataSetExtensions" />
61+
<Reference Include="Microsoft.CSharp" />
62+
<Reference Include="System.Data" />
63+
<Reference Include="System.Net.Http" />
64+
<Reference Include="System.Xml" />
65+
</ItemGroup>
66+
<ItemGroup>
67+
<Compile Include="AsyncOperationVisitor.cs" />
68+
<Compile Include="CreateOrUpdateAsyncOperation.cs" />
69+
<Compile Include="CreateOrUpdateAsyncParams.cs" />
70+
<Compile Include="Extensions.cs" />
71+
<Compile Include="GetAsyncOperation.cs" />
72+
<Compile Include="GetAsyncParams.cs" />
73+
<Compile Include="IClient.cs" />
74+
<Compile Include="IResourceConfig.cs" />
75+
<Compile Include="IResourceConfigVisitor.cs" />
76+
<Compile Include="IResourcePolicy.cs" />
77+
<Compile Include="IState.cs" />
78+
<Compile Include="NestedResourceConfig.cs" />
79+
<Compile Include="NestedResourcePolicy.cs" />
80+
<Compile Include="TargetState.cs" />
81+
<Compile Include="Properties\AssemblyInfo.cs" />
82+
<Compile Include="ResourceConfig.cs" />
83+
<Compile Include="ResourcePolicy.cs" />
84+
<Compile Include="State.cs" />
85+
<Compile Include="StateLocation.cs" />
86+
</ItemGroup>
87+
<ItemGroup>
88+
<None Include="packages.config" />
89+
</ItemGroup>
90+
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
91+
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
92+
Other similar extension points exist, see Microsoft.Common.targets.
93+
<Target Name="BeforeBuild">
94+
</Target>
95+
<Target Name="AfterBuild">
96+
</Target>
97+
-->
98+
</Project>
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
using System.Linq;
2+
using System.Threading;
3+
using System.Threading.Tasks;
4+
5+
namespace Microsoft.Azure.Commands.Common.Strategies
6+
{
7+
public static class CreateOrUpdateAsyncOperation
8+
{
9+
public static async Task<IState> CreateOrUpdateAsync<Model>(
10+
this IResourceConfig<Model> config,
11+
IClient client,
12+
IState current,
13+
IState target,
14+
CancellationToken cancellationToken)
15+
where Model : class
16+
{
17+
var visitor = new CreateAsyncVisitor(client, current, target, cancellationToken);
18+
await visitor.GetOrAdd(config);
19+
return visitor.Result;
20+
}
21+
22+
sealed class CreateAsyncVisitor : AsyncOperationVisitor
23+
{
24+
public override async Task<object> Visit<Model>(ResourceConfig<Model> config)
25+
{
26+
var current = Current.GetOrNull(config);
27+
if (current != null)
28+
{
29+
return current;
30+
}
31+
var tasks = config.Dependencies.Select(GetOrAddUntyped);
32+
await Task.WhenAll(tasks);
33+
return await config.Policy.CreateOrUpdateAsync(CreateOrUpdateAsyncParams.Create(
34+
Client,
35+
config.ResourceGroupName,
36+
config.Name,
37+
Target.GetOrNull(config),
38+
CancellationToken));
39+
}
40+
41+
public override async Task<object> Visit<Model, ParentModel>(
42+
NestedResourceConfig<Model, ParentModel> config)
43+
{
44+
var parent = await GetOrAdd(config.Parent);
45+
return config.Policy.Get(parent, config.Name);
46+
}
47+
48+
public CreateAsyncVisitor(
49+
IClient client,
50+
IState current,
51+
IState target,
52+
CancellationToken cancellationToken)
53+
: base(client, cancellationToken)
54+
{
55+
Current = current;
56+
Target = target;
57+
}
58+
59+
IState Current { get; }
60+
61+
IState Target { get; }
62+
}
63+
}
64+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
using System.Threading;
2+
3+
namespace Microsoft.Azure.Commands.Common.Strategies
4+
{
5+
public static class CreateOrUpdateAsyncParams
6+
{
7+
public static CreateOrUpdateAsyncParams<Operations, Model> Create<Operations, Model>(
8+
Operations operations,
9+
string resourceGroupName,
10+
string name,
11+
Model model,
12+
CancellationToken cancellationToken)
13+
=> new CreateOrUpdateAsyncParams<Operations, Model>(
14+
operations, resourceGroupName, name, model, cancellationToken);
15+
}
16+
17+
public sealed class CreateOrUpdateAsyncParams<TOperations, TModel>
18+
{
19+
public TOperations Operations { get; }
20+
21+
public string ResourceGroupName { get; }
22+
23+
public string Name { get; }
24+
25+
public CancellationToken CancellationToken { get; }
26+
27+
public TModel Model { get; }
28+
29+
public CreateOrUpdateAsyncParams(
30+
TOperations operations,
31+
string resourceGroupName,
32+
string name,
33+
TModel model,
34+
CancellationToken cancellationToken)
35+
{
36+
Operations = operations;
37+
ResourceGroupName = resourceGroupName;
38+
Name = name;
39+
Model = model;
40+
CancellationToken = cancellationToken;
41+
}
42+
}
43+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
using System.Collections.Generic;
2+
using System.Linq;
3+
4+
namespace Microsoft.Azure.Commands.Common.Strategies
5+
{
6+
public static class Extensions
7+
{
8+
public static IEnumerable<T> EmptyIfNull<T>(this IEnumerable<T> value)
9+
=> value ?? Enumerable.Empty<T>();
10+
11+
public static V GetOrNull<K, V>(this IDictionary<K, V> dictionary, K key)
12+
where V : class
13+
{
14+
V result;
15+
return dictionary.TryGetValue(key, out result) ? result : null;
16+
}
17+
}
18+
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
using Microsoft.Rest.Azure;
2+
using System.Linq;
3+
using System.Net;
4+
using System.Threading;
5+
using System.Threading.Tasks;
6+
7+
namespace Microsoft.Azure.Commands.Common.Strategies
8+
{
9+
public static class GetAsyncOperation
10+
{
11+
public static async Task<IState> GetAsync<Model>(
12+
this IResourceConfig<Model> resourceConfig,
13+
IClient client,
14+
CancellationToken cancellationToken)
15+
where Model : class
16+
{
17+
var visitor = new Visitor(client, cancellationToken);
18+
await visitor.GetOrAdd(resourceConfig);
19+
return visitor.Result;
20+
}
21+
22+
sealed class Visitor : AsyncOperationVisitor
23+
{
24+
public override async Task<object> Visit<Model>(ResourceConfig<Model> config)
25+
{
26+
Model info;
27+
try
28+
{
29+
info = await config.Policy.GetAsync(GetAsyncParams.Create(
30+
Client, config.ResourceGroupName, config.Name, CancellationToken));
31+
}
32+
catch (CloudException e) when (e.Response.StatusCode == HttpStatusCode.NotFound)
33+
{
34+
info = null;
35+
}
36+
if (info == null)
37+
{
38+
var tasks = config.Dependencies.Select(GetOrAddUntyped);
39+
await Task.WhenAll(tasks);
40+
return null;
41+
}
42+
return info;
43+
}
44+
45+
public override async Task<object> Visit<Model, ParentModel>(
46+
NestedResourceConfig<Model, ParentModel> config)
47+
{
48+
var parent = await GetOrAdd(config.Parent);
49+
return parent == null ? null : config.Policy.Get(parent, config.Name);
50+
}
51+
52+
public Visitor(IClient client, CancellationToken cancellationToken)
53+
: base(client, cancellationToken)
54+
{
55+
}
56+
}
57+
}
58+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
using System.Threading;
2+
3+
namespace Microsoft.Azure.Commands.Common.Strategies
4+
{
5+
public static class GetAsyncParams
6+
{
7+
public static GetAsyncParams<Operations> Create<Operations>(
8+
Operations operations,
9+
string resourceGroupName,
10+
string name,
11+
CancellationToken cancellationToken)
12+
=> new GetAsyncParams<Operations>(
13+
operations, resourceGroupName, name, cancellationToken);
14+
}
15+
16+
public class GetAsyncParams<TOperations>
17+
{
18+
public TOperations Operations { get; }
19+
20+
public string ResourceGroupName { get; }
21+
22+
public string Name { get; }
23+
24+
public CancellationToken CancellationToken { get; }
25+
26+
public GetAsyncParams(
27+
TOperations operations,
28+
string resourceGroupName,
29+
string name,
30+
CancellationToken cancellationToken)
31+
{
32+
Operations = operations;
33+
ResourceGroupName = resourceGroupName;
34+
Name = name;
35+
CancellationToken = cancellationToken;
36+
}
37+
}
38+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
using System;
2+
3+
namespace Microsoft.Azure.Commands.Common.Strategies
4+
{
5+
public interface IClient
6+
{
7+
T GetClient<T>()
8+
where T : class, IDisposable;
9+
}
10+
}

0 commit comments

Comments
 (0)