Skip to content

Commit 6f8b6b7

Browse files
No Create, for now.
1 parent 88a5696 commit 6f8b6b7

15 files changed

+393
-228
lines changed
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
using System;
2+
using System.Collections.Concurrent;
3+
using System.Threading;
4+
using System.Threading.Tasks;
5+
6+
namespace Microsoft.Azure.Commands.Common.Strategies
7+
{
8+
sealed class AsyncOperationContext
9+
{
10+
public IClient Client { get; }
11+
12+
public CancellationToken CancellationToken { get; }
13+
14+
public IState Result => State;
15+
16+
public AsyncOperationContext(IClient client, CancellationToken cancellationToken)
17+
{
18+
Client = client;
19+
CancellationToken = cancellationToken;
20+
}
21+
22+
public async Task<Model> GetOrAdd<Model>(
23+
ResourceConfig<Model> config, Func<Task<Model>> create)
24+
where Model : class
25+
{
26+
var result = await TaskMap.GetOrAdd(
27+
config.DefaultIdStr(),
28+
async _ =>
29+
{
30+
var model = await create();
31+
if (model != null)
32+
{
33+
State.GetOrAdd(config, () => model);
34+
}
35+
return model;
36+
});
37+
return result as Model;
38+
}
39+
40+
State State { get; } = new State();
41+
42+
ConcurrentDictionary<string, Task<object>> TaskMap { get; }
43+
= new ConcurrentDictionary<string, Task<object>>();
44+
}
45+
}

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

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

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@
6767
<Reference Include="System.Xml" />
6868
</ItemGroup>
6969
<ItemGroup>
70-
<Compile Include="AsyncOperationVisitor.cs" />
70+
<Compile Include="AsyncOperationContext.cs" />
7171
<Compile Include="Compute\ComputeStrategy.cs" />
7272
<Compile Include="Compute\VirtualMachineStrategy.cs" />
7373
<Compile Include="CreateOrUpdateAsyncOperation.cs" />
@@ -96,6 +96,7 @@
9696
<Compile Include="ResourceStrategy.cs" />
9797
<Compile Include="State.cs" />
9898
<Compile Include="StateLocation.cs" />
99+
<Compile Include="Void.cs" />
99100
</ItemGroup>
100101
<ItemGroup>
101102
<None Include="MSSharedLibKey.snk" />

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

Lines changed: 161 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,165 @@
1-
using System.Linq;
1+
using System;
22
using System.Threading;
33
using System.Threading.Tasks;
44

55
namespace Microsoft.Azure.Commands.Common.Strategies
66
{
7+
/*
8+
public static class CreateOrUpdateAsyncOperation
9+
{
10+
public static async Task<IState> CreateOrUpdateAsync<Model>(
11+
this ResourceConfig<Model> config,
12+
IClient client,
13+
IState target,
14+
CancellationToken cancellationToken)
15+
where Model : class
16+
{
17+
var context = new Context(client, cancellationToken, target);
18+
}
19+
20+
sealed class Context
21+
{
22+
public AsyncOperationContext OperationContext { get; } = new AsyncOperationContext();
23+
24+
public IClient Client { get; }
25+
26+
public IState Target { get; }
27+
28+
public CancellationToken CancellationToken { get; }
29+
30+
public Context(IClient client, CancellationToken cancellationToken, IState target)
31+
{
32+
Client = client;
33+
CancellationToken = cancellationToken;
34+
Target = target;
35+
}
36+
}
37+
38+
sealed class Visitor : IResourceBaseConfigVisitor<Context, Task>
39+
{
40+
public async Task Visit<Model>(ResourceConfig<Model> config, Context context)
41+
where Model : class
42+
{
43+
var model = context.Target.Get(config);
44+
if (model != null)
45+
{
46+
await context.OperationContext.GetOrAdd(
47+
config,
48+
async () =>
49+
{
50+
foreach (var d in config.Dependencies)
51+
{
52+
}
53+
return await config.Strategy.CreateOrUpdateAsync(
54+
context.Client,
55+
CreateOrUpdateAsyncParams.Create(
56+
config.ResourceGroupName,
57+
config.Name,
58+
model,
59+
context.CancellationToken));
60+
});
61+
}
62+
}
63+
64+
public Task Visit<Model, ParentModel>(NestedResourceConfig<Model, ParentModel> config, Context context)
65+
where Model : class
66+
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+
}
135+
136+
public Context(IClient client, IState target, CancellationToken cancellationToken)
137+
{
138+
Client = client;
139+
Target = target;
140+
CancellationToken = cancellationToken;
141+
}
142+
}
143+
144+
sealed class Visitor : IResourceBaseConfigVisitor<Context, Task>
145+
{
146+
public Task Visit<Model>(ResourceConfig<Model> config, Context context)
147+
where Model : class
148+
{
149+
throw new NotImplementedException();
150+
}
151+
152+
public Task Visit<Model, ParentModel>(
153+
NestedResourceConfig<Model, ParentModel> config, Context context)
154+
where Model : class
155+
where ParentModel : class
156+
{
157+
throw new NotImplementedException();
158+
}
159+
}
160+
}
161+
162+
/*
7163
public static class CreateOrUpdateAsyncOperation
8164
{
9165
/// <summary>
@@ -32,7 +188,7 @@ sealed class CreateAsyncVisitor : AsyncOperationVisitor
32188
{
33189
public override async Task<object> Visit<Model>(ResourceConfig<Model> config)
34190
{
35-
var target = Target.GetOrNull(config);
191+
var target = Target.Get(config);
36192
if (target == null)
37193
{
38194
return null;
@@ -44,14 +200,14 @@ public override async Task<object> Visit<Model>(ResourceConfig<Model> config)
44200
CreateOrUpdateAsyncParams.Create(
45201
config.ResourceGroupName,
46202
config.Name,
47-
Target.GetOrNull(config),
203+
Target.Get(config),
48204
CancellationToken));
49205
}
50206
51207
public override async Task<object> Visit<Model, ParentModel>(
52208
NestedResourceConfig<Model, ParentModel> config)
53209
{
54-
var target = Target.GetOrNull(config);
210+
var target = Target.GetNestedResourceModel(config);
55211
if (target == null)
56212
{
57213
return null;
@@ -72,4 +228,5 @@ public CreateAsyncVisitor(
72228
IState Target { get; }
73229
}
74230
}
231+
*/
75232
}

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
using System.Collections.Generic;
1+
using System;
2+
using System.Collections.Concurrent;
3+
using System.Collections.Generic;
24
using System.Linq;
35

46
namespace Microsoft.Azure.Commands.Common.Strategies

0 commit comments

Comments
 (0)