Skip to content

Commit 4147b64

Browse files
address comments.
1 parent 186926e commit 4147b64

File tree

11 files changed

+172
-139
lines changed

11 files changed

+172
-139
lines changed

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -67,14 +67,14 @@
6767
<Reference Include="System.Xml" />
6868
</ItemGroup>
6969
<ItemGroup>
70-
<Compile Include="AsyncOperationContext.cs" />
70+
<Compile Include="StateOperationContext.cs" />
7171
<Compile Include="Compute\ComputeStrategy.cs" />
7272
<Compile Include="Compute\VirtualMachineStrategy.cs" />
73-
<Compile Include="CreateOrUpdateAsyncExtensions.cs" />
73+
<Compile Include="UpdateStateExtensions.cs" />
7474
<Compile Include="CreateOrUpdateAsyncParams.cs" />
7575
<Compile Include="EntityConfigExtensions.cs" />
7676
<Compile Include="Extensions.cs" />
77-
<Compile Include="GetAsyncOperation.cs" />
77+
<Compile Include="GetStateExtensions.cs" />
7878
<Compile Include="GetAsyncParams.cs" />
7979
<Compile Include="IClient.cs" />
8080
<Compile Include="IEntityConfig.cs" />
@@ -99,7 +99,7 @@
9999
<Compile Include="ResourceConfig.cs" />
100100
<Compile Include="ResourceStrategy.cs" />
101101
<Compile Include="State.cs" />
102-
<Compile Include="StateLocation.cs" />
102+
<Compile Include="LocationExtensions.cs" />
103103
<Compile Include="Void.cs" />
104104
</ItemGroup>
105105
<ItemGroup>

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

Lines changed: 0 additions & 76 deletions
This file was deleted.
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
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 GetStateExtensions
10+
{
11+
public static async Task<IState> GetStateAsync<TModel>(
12+
this IEntityConfig<TModel> config,
13+
IClient client,
14+
CancellationToken cancellationToken)
15+
where TModel : class
16+
{
17+
var context = new StateOperationContext(client, cancellationToken);
18+
await context.GetStateAsyncDispatch(config);
19+
return context.Result;
20+
}
21+
22+
static Task GetStateAsyncDispatch(this StateOperationContext context, IEntityConfig config)
23+
=> config.Accept(new GetStateAsyncVisitor(), context);
24+
25+
static async Task GetStateAsync<TModel>(
26+
this StateOperationContext context, ResourceConfig<TModel> config)
27+
where TModel : class
28+
=> await context.GetOrAdd(
29+
config,
30+
async () =>
31+
{
32+
var info = await config.GetAsync(context.Client, context.CancellationToken);
33+
// Get state of dependencies if the resource doesn't exist
34+
if (info == null)
35+
{
36+
var tasks = config.Dependencies.Select(context.GetStateAsyncDispatch);
37+
await Task.WhenAll(tasks);
38+
}
39+
return info;
40+
});
41+
42+
static Task GetStateAsync<TModel, TParentModel>(
43+
this StateOperationContext context, NestedResourceConfig<TModel, TParentModel> config)
44+
where TModel : class
45+
where TParentModel : class
46+
=> context.GetStateAsyncDispatch(config.Parent);
47+
48+
sealed class GetStateAsyncVisitor : IEntityConfigVisitor<StateOperationContext, Task>
49+
{
50+
public Task Visit<TModel>(
51+
ResourceConfig<TModel> config, StateOperationContext context)
52+
where TModel : class
53+
=> context.GetStateAsync(config);
54+
55+
public Task Visit<TModel, TParentModel>(
56+
NestedResourceConfig<TModel, TParentModel> config, StateOperationContext context)
57+
where TModel : class
58+
where TParentModel : class
59+
=> context.GetStateAsync(config);
60+
}
61+
}
62+
}

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
using Microsoft.Rest;
2-
using System;
32

43
namespace Microsoft.Azure.Commands.Common.Strategies
54
{

src/ResourceManager/Common/Commands.Common.Strategies/StateLocation.cs renamed to src/ResourceManager/Common/Commands.Common.Strategies/LocationExtensions.cs

Lines changed: 29 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
namespace Microsoft.Azure.Commands.Common.Strategies
55
{
6-
public static class StateLocation
6+
public static class LocationExtensions
77
{
88
/// <summary>
99
/// Get the best location for the given entity from the given state.
@@ -12,29 +12,43 @@ public static class StateLocation
1212
/// <param name="config"></param>
1313
/// <returns></returns>
1414
public static string GetLocation(this IState state, IEntityConfig config)
15-
=> config.Accept(new GetLocationVisitor(), state)?.Location;
15+
=> state.GetDependencyLocationDispatch(config)?.Location;
1616

17-
sealed class GetLocationVisitor : IEntityConfigVisitor<IState, DependencyLocation>
17+
static DependencyLocation GetDependencyLocationDispatch(this IState state, IEntityConfig config)
18+
=> config.Accept(new GetDependencyLocationVisitor(), state);
19+
20+
static DependencyLocation GetDependencyLocation<TModel>(
21+
this IState state, ResourceConfig<TModel> config)
22+
where TModel : class
23+
{
24+
var info = state.Get(config);
25+
return info != null
26+
? new DependencyLocation(
27+
config.Strategy.GetLocation(info),
28+
typeof(TModel) != typeof(ResourceGroup))
29+
: config
30+
.Dependencies
31+
.Select(state.GetDependencyLocationDispatch)
32+
.Aggregate(null as DependencyLocation, Merge);
33+
}
34+
35+
static DependencyLocation GetDependencyLocation<TModel, TParentModel>(
36+
this IState state, NestedResourceConfig<TModel, TParentModel> config)
37+
where TModel : class
38+
where TParentModel : class
39+
=> config.Parent.Accept(new GetDependencyLocationVisitor(), state);
40+
41+
sealed class GetDependencyLocationVisitor : IEntityConfigVisitor<IState, DependencyLocation>
1842
{
1943
public DependencyLocation Visit<TModel>(ResourceConfig<TModel> config, IState state)
2044
where TModel : class
21-
{
22-
var info = state.Get(config);
23-
return info != null
24-
? new DependencyLocation(
25-
config.Strategy.GetLocation(info),
26-
typeof(TModel) != typeof(ResourceGroup))
27-
: config
28-
.Dependencies
29-
.Select(c => c.Accept(this, state))
30-
.Aggregate(null as DependencyLocation, Merge);
31-
}
45+
=> state.GetDependencyLocation(config);
3246

3347
public DependencyLocation Visit<TModel, TParentModel>(
3448
NestedResourceConfig<TModel, TParentModel> config, IState state)
3549
where TModel : class
3650
where TParentModel : class
37-
=> config.Parent.Accept(this, state);
51+
=> state.GetDependencyLocation(config);
3852
}
3953

4054
sealed class DependencyLocation

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,11 @@ public NestedResourceConfig(
3535
public IEnumerable<string> GetId(string subscription)
3636
=> Parent.GetId(subscription).Concat(Strategy.GetId(Name));
3737

38-
public TResult Accept<TContext, TResult>(
38+
TResult IEntityConfig.Accept<TContext, TResult>(
3939
IEntityConfigVisitor<TContext, TResult> visitor, TContext context)
4040
=> visitor.Visit(this, context);
4141

42-
public TResult Accept<TContext, TResult>(
42+
TResult IEntityConfig<TModel>.Accept<TContext, TResult>(
4343
IEntityConfigVisitor<TModel, TContext, TResult> visitor, TContext context)
4444
=> visitor.Visit(this, context);
4545
}

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

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -42,14 +42,6 @@ public ResourceConfig(
4242
Dependencies = dependencies;
4343
}
4444

45-
public Result Accept<Context, Result>(
46-
IEntityConfigVisitor<Context, Result> visitor, Context context)
47-
=> visitor.Visit(this, context);
48-
49-
public Result Accept<Context, Result>(
50-
IEntityConfigVisitor<TModel, Context, Result> visitor, Context context)
51-
=> visitor.Visit(this, context);
52-
5345
public IEnumerable<string> GetId(string subscription)
5446
=> new[]
5547
{
@@ -59,5 +51,13 @@ public IEnumerable<string> GetId(string subscription)
5951
ResourceGroupName
6052
}
6153
.Concat(Strategy.GetId(Name));
54+
55+
Result IEntityConfig.Accept<Context, Result>(
56+
IEntityConfigVisitor<Context, Result> visitor, Context context)
57+
=> visitor.Visit(this, context);
58+
59+
Result IEntityConfig<TModel>.Accept<Context, Result>(
60+
IEntityConfigVisitor<TModel, Context, Result> visitor, Context context)
61+
=> visitor.Visit(this, context);
6262
}
6363
}

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

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
using Microsoft.Azure.Management.ResourceManager.Models;
2+
using Microsoft.Rest.Azure;
23
using System;
34
using System.Collections.Generic;
45
using System.Linq;
6+
using System.Net;
7+
using System.Threading;
8+
using System.Threading.Tasks;
59

610
namespace Microsoft.Azure.Commands.Common.Strategies
711
{
@@ -33,5 +37,38 @@ public static ResourceConfig<TModel> CreateConfig<TModel>(
3337
name,
3438
createModel,
3539
dependencies.EmptyIfNull().Concat(new[] { resourceGroup }));
40+
41+
public static async Task<TModel> GetAsync<TModel>(
42+
this ResourceConfig<TModel> config,
43+
IClient client,
44+
CancellationToken cancellationToken)
45+
where TModel : class
46+
{
47+
try
48+
{
49+
return await config.Strategy.GetAsync(
50+
client,
51+
new GetAsyncParams(config.ResourceGroupName, config.Name, cancellationToken));
52+
}
53+
catch (CloudException e)
54+
when (e.Response.StatusCode == HttpStatusCode.NotFound)
55+
{
56+
return null;
57+
}
58+
}
59+
60+
public static Task<TModel> CreateOrUpdateAsync<TModel>(
61+
this ResourceConfig<TModel> config,
62+
IClient client,
63+
TModel model,
64+
CancellationToken cancellationToken)
65+
where TModel : class
66+
=> config.Strategy.CreateOrUpdateAsync(
67+
client,
68+
CreateOrUpdateAsyncParams.Create(
69+
config.ResourceGroupName,
70+
config.Name,
71+
model,
72+
cancellationToken));
3673
}
3774
}

src/ResourceManager/Common/Commands.Common.Strategies/AsyncOperationContext.cs renamed to src/ResourceManager/Common/Commands.Common.Strategies/StateOperationContext.cs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,21 +8,26 @@ namespace Microsoft.Azure.Commands.Common.Strategies
88
/// <summary>
99
/// Context for asyncronous operations, such as GetAsync or CreateOrUpdateAsync.
1010
/// </summary>
11-
public sealed class AsyncOperationContext
11+
public sealed class StateOperationContext
1212
{
1313
public IClient Client { get; }
1414

1515
public CancellationToken CancellationToken { get; }
1616

1717
public IState Result => _Result;
1818

19-
public AsyncOperationContext(IClient client, CancellationToken cancellationToken)
19+
readonly State _Result = new State();
20+
21+
readonly ConcurrentDictionary<string, Task> _TaskMap
22+
= new ConcurrentDictionary<string, Task>();
23+
24+
public StateOperationContext(IClient client, CancellationToken cancellationToken)
2025
{
2126
Client = client;
2227
CancellationToken = cancellationToken;
2328
}
2429

25-
public async Task<TModel> GetOrAddAsync<TModel>(
30+
public async Task<TModel> GetOrAdd<TModel>(
2631
ResourceConfig<TModel> config, Func<Task<TModel>> operation)
2732
where TModel : class
2833
=> await _TaskMap.GetOrAddWithCast(
@@ -37,10 +42,5 @@ public async Task<TModel> GetOrAddAsync<TModel>(
3742
}
3843
return model;
3944
});
40-
41-
readonly State _Result = new State();
42-
43-
readonly ConcurrentDictionary<string, Task> _TaskMap
44-
= new ConcurrentDictionary<string, Task>();
4545
}
4646
}

0 commit comments

Comments
 (0)