1
- using System . Collections . Concurrent ;
2
- using System . Linq ;
1
+ using System . Linq ;
3
2
using System . Threading ;
4
3
using System . Threading . Tasks ;
5
4
@@ -8,37 +7,21 @@ namespace Microsoft.Azure.Experiments
8
7
public static class ResourceOperations
9
8
{
10
9
public static async Task < IState > CreateAsync < Config > (
11
- this IResourceConfig < Config > config , IClient client , IState current , IState parameters )
10
+ this IResourceConfig < Config > config ,
11
+ IClient client ,
12
+ IState current ,
13
+ IState parameters ,
14
+ CancellationToken cancellationToken )
12
15
where Config : class
13
16
{
14
- var visitor = new CreateAsyncVisitor ( client , current , parameters ) ;
17
+ var visitor = new CreateAsyncVisitor ( client , current , parameters , cancellationToken ) ;
15
18
await visitor . GetOrAdd ( config ) ;
16
19
return visitor . Result ;
17
20
}
18
21
19
- sealed class CreateAsyncVisitor : IResourceConfigVisitor < Task < object > >
22
+ sealed class CreateAsyncVisitor : AsyncOperation
20
23
{
21
- public async Task < object > GetOrAddUntyped ( IResourceConfig config )
22
- => await TaskMap . GetOrAdd (
23
- config ,
24
- async _ =>
25
- {
26
- var info = await config . Apply ( this ) ;
27
- if ( info != null )
28
- {
29
- Result . GetOrAddUntyped ( config , ( ) => info ) ;
30
- }
31
- return info ;
32
- } ) ;
33
-
34
- public async Task < Config > GetOrAdd < Config > ( IResourceConfig < Config > config )
35
- where Config : class
36
- {
37
- var result = await GetOrAddUntyped ( config ) ;
38
- return result as Config ;
39
- }
40
-
41
- public async Task < object > Visit < Config > ( ResourceConfig < Config > config ) where Config : class
24
+ public override async Task < object > Visit < Config > ( ResourceConfig < Config > config )
42
25
{
43
26
var current = Current . GetOrNull ( config ) ;
44
27
if ( current != null )
@@ -52,98 +35,30 @@ public async Task<object> Visit<Config>(ResourceConfig<Config> config) where Con
52
35
config . ResourceGroupName ,
53
36
config . Name ,
54
37
Parameters . GetOrNull ( config ) ,
55
- new CancellationToken ( ) ) ) ;
38
+ CancellationToken ) ) ;
56
39
}
57
40
58
- public async Task < object > Visit < Config , ParentConfig > (
41
+ public override async Task < object > Visit < Config , ParentConfig > (
59
42
NestedResourceConfig < Config , ParentConfig > config )
60
- where Config : class
61
- where ParentConfig : class
62
43
{
63
44
var parent = await GetOrAdd ( config . Parent ) ;
64
45
return config . Policy . Get ( parent ) ;
65
46
}
66
47
67
- public CreateAsyncVisitor ( IClient client , IState current , IState parameters )
48
+ public CreateAsyncVisitor (
49
+ IClient client ,
50
+ IState current ,
51
+ IState parameters ,
52
+ CancellationToken cancellationToken )
53
+ : base ( client , cancellationToken )
68
54
{
69
- Client = client ;
70
55
Current = current ;
71
56
Parameters = parameters ;
72
57
}
73
58
74
- public State Result { get ; } = new State ( ) ;
75
-
76
- IClient Client { get ; }
77
-
78
59
IState Current { get ; }
79
60
80
61
IState Parameters { get ; }
81
-
82
- ConcurrentDictionary < IResourceConfig , Task < object > > TaskMap { get ; }
83
- = new ConcurrentDictionary < IResourceConfig , Task < object > > ( ) ;
84
- }
85
- }
86
-
87
- /*
88
- public sealed class CreateOperation
89
- {
90
- public Func<IClient, IStateSet, Task> CreateAsync { get; }
91
-
92
- public IEnumerable<CreateOperation> Dependencies { get; }
93
-
94
- public CreateOperation(
95
- Func<IClient, Task> createAsync,
96
- IEnumerable<CreateOperation> dependencies)
97
- {
98
- CreateAsync = createAsync;
99
- Dependencies = dependencies;
100
- }
101
-
102
- public static CreateOperation Create<Config>(IState state, IResourceConfig<Config> config)
103
- where Config : class
104
- => new Visitor(state).Get(config);
105
-
106
- sealed class Visitor : IResourceConfigVisitor<CreateOperation>
107
- {
108
- public CreateOperation Get(IResourceConfig config)
109
- => Map.GetOrAdd(config, _ => config.Apply(this));
110
-
111
- public CreateOperation Visit<Config>(ResourceConfig<Config> config) where Config : class
112
- {
113
- var info = State.Get(config);
114
- return info == null
115
- ? new CreateOperation(
116
- async (client, state) =>
117
- {
118
- var p = config.CreateConfig(client.Context.SubscriptionId);
119
- var i = await config.Policy.CreateOrUpdateAsync(new CreateOrUpdateAsyncParams<IClient, Config>(
120
- client,
121
- config.ResourceGroupName,
122
- config.Name,
123
- p,
124
- new CancellationToken()));
125
- state.Set(config, i);
126
- },
127
- config.Dependencies.Select(d => Get(d)).Where(d => d != null))
128
- : null;
129
- }
130
-
131
- public CreateOperation Visit<Config, ParentConfig>(
132
- NestedResourceConfig<Config, ParentConfig> config)
133
- where Config : class
134
- where ParentConfig : class
135
- => Get(config.Parent);
136
-
137
- public Visitor(IStateGet state)
138
- {
139
- State = state;
140
- }
141
-
142
- IStateGet State { get; }
143
-
144
- ConcurrentDictionary<IResourceConfig, CreateOperation> Map { get; }
145
- = new ConcurrentDictionary<IResourceConfig, CreateOperation>();
146
62
}
147
63
}
148
- */
149
64
}
0 commit comments