Skip to content

Commit 2c8aeb9

Browse files
new
1 parent 6f8b6b7 commit 2c8aeb9

15 files changed

+108
-208
lines changed

src/ResourceManager/Common/Commands.Common.Strategies/AsyncOperationContext.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
namespace Microsoft.Azure.Commands.Common.Strategies
77
{
8-
sealed class AsyncOperationContext
8+
public sealed class AsyncOperationContext
99
{
1010
public IClient Client { get; }
1111

@@ -19,7 +19,7 @@ public AsyncOperationContext(IClient client, CancellationToken cancellationToken
1919
CancellationToken = cancellationToken;
2020
}
2121

22-
public async Task<Model> GetOrAdd<Model>(
22+
public async Task<Model> GetOrAddAsync<Model>(
2323
ResourceConfig<Model> config, Func<Task<Model>> create)
2424
where Model : class
2525
{

src/ResourceManager/Common/Commands.Common.Strategies/Commands.Common.Strategies.csproj

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,11 +76,12 @@
7676
<Compile Include="GetAsyncOperation.cs" />
7777
<Compile Include="GetAsyncParams.cs" />
7878
<Compile Include="IClient.cs" />
79-
<Compile Include="IReport.cs" />
8079
<Compile Include="IResourceBaseConfig.cs" />
8180
<Compile Include="IResourceBaseConfigVisitor.cs" />
8281
<Compile Include="IResourceBaseStrategy.cs" />
8382
<Compile Include="IState.cs" />
83+
<Compile Include="TargetDependencies.cs" />
84+
<Compile Include="TypedDictionary.cs" />
8485
<Compile Include="NestedResourceConfig.cs" />
8586
<Compile Include="NestedResourceStrategy.cs" />
8687
<Compile Include="Network\NetworkInterfaceStrategy.cs" />
Lines changed: 21 additions & 174 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
1-
using System;
1+
using System.Linq;
22
using System.Threading;
33
using System.Threading.Tasks;
44

55
namespace Microsoft.Azure.Commands.Common.Strategies
66
{
7-
/*
87
public static class CreateOrUpdateAsyncOperation
98
{
109
public static async Task<IState> CreateOrUpdateAsync<Model>(
@@ -14,219 +13,67 @@ public static async Task<IState> CreateOrUpdateAsync<Model>(
1413
CancellationToken cancellationToken)
1514
where Model : class
1615
{
17-
var context = new Context(client, cancellationToken, target);
16+
var context = new Context(
17+
new AsyncOperationContext(client, cancellationToken), target);
18+
await context.CreateOrUpdateAsync(config);
19+
return context.OperationContext.Result;
1820
}
1921

2022
sealed class Context
2123
{
22-
public AsyncOperationContext OperationContext { get; } = new AsyncOperationContext();
23-
24-
public IClient Client { get; }
24+
public AsyncOperationContext OperationContext { get; }
2525

2626
public IState Target { get; }
2727

28-
public CancellationToken CancellationToken { get; }
29-
30-
public Context(IClient client, CancellationToken cancellationToken, IState target)
28+
public Context(AsyncOperationContext operationContext, IState target)
3129
{
32-
Client = client;
33-
CancellationToken = cancellationToken;
30+
OperationContext = operationContext;
3431
Target = target;
3532
}
36-
}
3733

38-
sealed class Visitor : IResourceBaseConfigVisitor<Context, Task>
39-
{
40-
public async Task Visit<Model>(ResourceConfig<Model> config, Context context)
34+
public async Task CreateOrUpdateAsync<Model>(ResourceConfig<Model> config)
4135
where Model : class
4236
{
43-
var model = context.Target.Get(config);
37+
var model = Target.Get(config);
4438
if (model != null)
4539
{
46-
await context.OperationContext.GetOrAdd(
40+
await OperationContext.GetOrAddAsync(
4741
config,
4842
async () =>
4943
{
50-
foreach (var d in config.Dependencies)
51-
{
52-
}
44+
var tasks = config.Dependencies.Select(CreateOrUpdateAsync);
45+
await Task.WhenAll(tasks);
5346
return await config.Strategy.CreateOrUpdateAsync(
54-
context.Client,
47+
OperationContext.Client,
5548
CreateOrUpdateAsyncParams.Create(
5649
config.ResourceGroupName,
5750
config.Name,
5851
model,
59-
context.CancellationToken));
52+
OperationContext.CancellationToken));
6053
});
6154
}
6255
}
6356

64-
public Task Visit<Model, ParentModel>(NestedResourceConfig<Model, ParentModel> config, Context context)
57+
public Task CreateOrUpdateAsync<Model, ParentModel>(NestedResourceConfig<Model, ParentModel> config)
6558
where Model : class
6659
where ParentModel : class
67-
{
68-
throw new NotImplementedException();
69-
}
70-
}
71-
}
72-
73-
/*
74-
public static class CreateOrUpdateAsyncOperation
75-
{
76-
public static async Task<IState> CreateOrUpdateAsync<Model>(
77-
this ResourceConfig<Model> config,
78-
IClient client,
79-
IState target,
80-
CancellationToken cancellationToken)
81-
where Model : class
82-
{
83-
var context = new AsyncOperationContext();
84-
var model = target.Get(config);
85-
if (model != null)
86-
{
87-
await context.GetOrAdd(
88-
config,
89-
async () =>
90-
{
91-
// config.Dependencies
92-
return await config.Strategy.CreateOrUpdateAsync(
93-
client,
94-
CreateOrUpdateAsyncParams.Create(
95-
config.ResourceGroupName,
96-
config.Name,
97-
model,
98-
cancellationToken));
99-
});
100-
}
101-
return context.Result;
102-
}
103-
104-
sealed class Context
105-
{
106-
AsyncOperationContext OperationContext { get; } = new AsyncOperationContext();
107-
108-
IClient Client { get; }
109-
110-
IState Target { get; }
111-
112-
CancellationToken CancellationToken { get; }
113-
114-
public async Task CreateOrUpdateAsync<Model>(ResourceConfig<Model> config)
115-
where Model : class
116-
{
117-
var model = Target.Get(config);
118-
if (model != null)
119-
{
120-
await OperationContext.GetOrAdd(
121-
config,
122-
async () =>
123-
{
124-
// config.Dependencies
125-
return await config.Strategy.CreateOrUpdateAsync(
126-
Client,
127-
CreateOrUpdateAsyncParams.Create(
128-
config.ResourceGroupName,
129-
config.Name,
130-
model,
131-
CancellationToken));
132-
});
133-
}
134-
}
60+
=> CreateOrUpdateAsync(config.Parent);
13561

136-
public Context(IClient client, IState target, CancellationToken cancellationToken)
137-
{
138-
Client = client;
139-
Target = target;
140-
CancellationToken = cancellationToken;
141-
}
62+
public Task CreateOrUpdateAsync(IResourceBaseConfig config)
63+
=> config.Accept(new Visitor(), this);
14264
}
14365

14466
sealed class Visitor : IResourceBaseConfigVisitor<Context, Task>
14567
{
14668
public Task Visit<Model>(ResourceConfig<Model> config, Context context)
14769
where Model : class
148-
{
149-
throw new NotImplementedException();
150-
}
70+
=> context.CreateOrUpdateAsync(config);
15171

15272
public Task Visit<Model, ParentModel>(
15373
NestedResourceConfig<Model, ParentModel> config, Context context)
15474
where Model : class
15575
where ParentModel : class
156-
{
157-
throw new NotImplementedException();
158-
}
159-
}
160-
}
161-
162-
/*
163-
public static class CreateOrUpdateAsyncOperation
164-
{
165-
/// <summary>
166-
/// Asynchronous resource creation.
167-
/// </summary>
168-
/// <typeparam name="Model"></typeparam>
169-
/// <param name="config"></param>
170-
/// <param name="client"></param>
171-
/// <param name="current"></param>
172-
/// <param name="target"></param>
173-
/// <param name="cancellationToken"></param>
174-
/// <returns>An Azure state.</returns>
175-
public static async Task<IState> CreateOrUpdateAsync<Model>(
176-
this IResourceBaseConfig<Model> config,
177-
IClient client,
178-
IState target,
179-
CancellationToken cancellationToken)
180-
where Model : class
181-
{
182-
var visitor = new CreateAsyncVisitor(client, target, cancellationToken);
183-
await visitor.GetOrAdd(config);
184-
return visitor.Result;
185-
}
186-
187-
sealed class CreateAsyncVisitor : AsyncOperationVisitor
188-
{
189-
public override async Task<object> Visit<Model>(ResourceConfig<Model> config)
190-
{
191-
var target = Target.Get(config);
192-
if (target == null)
193-
{
194-
return null;
195-
}
196-
var tasks = config.Dependencies.Select(GetOrAddUntyped);
197-
await Task.WhenAll(tasks);
198-
return await config.Strategy.CreateOrUpdateAsync(
199-
Client,
200-
CreateOrUpdateAsyncParams.Create(
201-
config.ResourceGroupName,
202-
config.Name,
203-
Target.Get(config),
204-
CancellationToken));
205-
}
206-
207-
public override async Task<object> Visit<Model, ParentModel>(
208-
NestedResourceConfig<Model, ParentModel> config)
209-
{
210-
var target = Target.GetNestedResourceModel(config);
211-
if (target == null)
212-
{
213-
return null;
214-
}
215-
var parent = await GetOrAdd(config.Parent);
216-
return config.Strategy.Get(parent, config.Name);
217-
}
218-
219-
public CreateAsyncVisitor(
220-
IClient client,
221-
IState target,
222-
CancellationToken cancellationToken)
223-
: base(client, cancellationToken)
224-
{
225-
Target = target;
226-
}
227-
228-
IState Target { get; }
76+
=> context.CreateOrUpdateAsync(config);
22977
}
23078
}
231-
*/
23279
}

src/ResourceManager/Common/Commands.Common.Strategies/Extensions.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,5 +16,9 @@ public static V GetOrNull<K, V>(this IDictionary<K, V> dictionary, K key)
1616
V result;
1717
return dictionary.TryGetValue(key, out result) ? result : null;
1818
}
19+
20+
public static V GetOrAdd<K, V, T>(this ConcurrentDictionary<K, T> dictionary, K key, Func<V> add)
21+
where V : T
22+
=> (V)dictionary.GetOrAdd(key, _ => add());
1923
}
2024
}

src/ResourceManager/Common/Commands.Common.Strategies/GetAsyncOperation.cs

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,11 @@ public static async Task<IState> GetAsync<Model>(
2020
}
2121

2222
static Task AddStateAsync(this AsyncOperationContext context, IResourceBaseConfig config)
23-
=> config.Accept(new Visitor(), context);
23+
=> config.Accept(new AddStateAsyncVisitor(), context);
2424

25-
sealed class Visitor : IResourceBaseConfigVisitor<AsyncOperationContext, Task>
26-
{
27-
public async Task Visit<Model>(ResourceConfig<Model> config, AsyncOperationContext context)
28-
where Model : class
29-
=> await context.GetOrAdd(
25+
static async Task AddStateAsync<Model>(this AsyncOperationContext context, ResourceConfig<Model> config)
26+
where Model : class
27+
=> await context.GetOrAddAsync(
3028
config,
3129
async () =>
3230
{
@@ -40,7 +38,7 @@ public async Task Visit<Model>(ResourceConfig<Model> config, AsyncOperationConte
4038
config.Name,
4139
context.CancellationToken));
4240
}
43-
catch (CloudException e)
41+
catch (CloudException e)
4442
when (e.Response.StatusCode == HttpStatusCode.NotFound)
4543
{
4644
info = null;
@@ -54,11 +52,24 @@ public async Task Visit<Model>(ResourceConfig<Model> config, AsyncOperationConte
5452
return info;
5553
});
5654

55+
static Task AddStateAsync<Model, ParentModel>(
56+
AsyncOperationContext context, NestedResourceConfig<Model, ParentModel> config)
57+
where Model : class
58+
where ParentModel : class
59+
=> context.AddStateAsync(config.Parent);
60+
61+
sealed class AddStateAsyncVisitor : IResourceBaseConfigVisitor<AsyncOperationContext, Task>
62+
{
63+
public Task Visit<Model>(
64+
ResourceConfig<Model> config, AsyncOperationContext context)
65+
where Model : class
66+
=> context.AddStateAsync(config);
67+
5768
public Task Visit<Model, ParentModel>(
5869
NestedResourceConfig<Model, ParentModel> config, AsyncOperationContext context)
5970
where Model : class
6071
where ParentModel : class
61-
=> context.AddStateAsync(config.Parent);
72+
=> context.AddStateAsync(config);
6273
}
6374
}
6475
}

src/ResourceManager/Common/Commands.Common.Strategies/IReport.cs

Lines changed: 0 additions & 8 deletions
This file was deleted.

src/ResourceManager/Common/Commands.Common.Strategies/IResourceBaseConfig.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ Result Accept<Context, Result>(
1212
IResourceBaseConfigVisitor<Context, Result> visitor, Context context);
1313

1414
IEnumerable<string> GetId(string subscription);
15+
16+
IResourceConfig Resource { get; }
1517
}
1618

1719
public interface IResourceBaseConfig<Model> : IResourceBaseConfig

src/ResourceManager/Common/Commands.Common.Strategies/IState.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,11 @@ public interface IState
77
{
88
Model Get<Model>(ResourceConfig<Model> config)
99
where Model : class;
10+
11+
bool Contains(IResourceBaseConfig config);
1012
}
1113

12-
public static class StateExtension
14+
public static class StateExtensions
1315
{
1416
public static Model Get<Model, ParentModel>(
1517
this IState state, NestedResourceConfig<Model, ParentModel> config)

src/ResourceManager/Common/Commands.Common.Strategies/NestedResourceConfig.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ public sealed class NestedResourceConfig<Model, ParentModel> : IResourceBaseConf
3030

3131
IResourceBaseStrategy IResourceBaseConfig.Strategy => Strategy;
3232

33+
public IResourceConfig Resource => Parent.Resource;
34+
3335
public NestedResourceConfig(
3436
NestedResourceStrategy<Model, ParentModel> strategy,
3537
IResourceBaseConfig<ParentModel> parent,

src/ResourceManager/Common/Commands.Common.Strategies/Network/VirtualNetworkStrategy.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using Microsoft.Azure.Management.Network;
22
using Microsoft.Azure.Management.Network.Models;
33
using Microsoft.Azure.Management.ResourceManager.Models;
4+
using System;
45

56
namespace Microsoft.Azure.Commands.Common.Strategies.Network
67
{

0 commit comments

Comments
 (0)