Skip to content

Commit 2b2ecd2

Browse files
GetResourceDependencies()
1 parent 4147b64 commit 2b2ecd2

File tree

8 files changed

+52
-34
lines changed

8 files changed

+52
-34
lines changed

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,8 @@
6767
<Reference Include="System.Xml" />
6868
</ItemGroup>
6969
<ItemGroup>
70+
<Compile Include="IResourceConfig.cs" />
71+
<Compile Include="IResourceConfigVisitor.cs" />
7072
<Compile Include="StateOperationContext.cs" />
7173
<Compile Include="Compute\ComputeStrategy.cs" />
7274
<Compile Include="Compute\VirtualMachineStrategy.cs" />

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

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,19 @@
1-
using Microsoft.Rest.Azure;
2-
using System.Linq;
3-
using System.Net;
1+
using System.Linq;
42
using System.Threading;
53
using System.Threading.Tasks;
64

75
namespace Microsoft.Azure.Commands.Common.Strategies
86
{
97
public static class GetStateExtensions
108
{
9+
/// <summary>
10+
/// Returns a current Azure state for the given resource (config).
11+
/// </summary>
12+
/// <typeparam name="TModel"></typeparam>
13+
/// <param name="config"></param>
14+
/// <param name="client"></param>
15+
/// <param name="cancellationToken"></param>
16+
/// <returns></returns>
1117
public static async Task<IState> GetStateAsync<TModel>(
1218
this IEntityConfig<TModel> config,
1319
IClient client,
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
using System.Collections.Generic;
2+
3+
namespace Microsoft.Azure.Commands.Common.Strategies
4+
{
5+
public interface IResourceConfig : IEntityConfig
6+
{
7+
string ResourceGroupName { get; }
8+
9+
IEnumerable<IEntityConfig> Dependencies { get; }
10+
11+
TResult Accept<TContext, TResult>(
12+
IResourceConfigVisitor<TContext, TResult> visitor, TContext context);
13+
}
14+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
namespace Microsoft.Azure.Commands.Common.Strategies
2+
{
3+
public interface IResourceConfigVisitor<TContext, TResult>
4+
{
5+
TResult Visit<TModel>(ResourceConfig<TModel> config, TContext context)
6+
where TModel : class;
7+
}
8+
}

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

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,6 @@
44

55
namespace Microsoft.Azure.Commands.Common.Strategies
66
{
7-
public interface IResourceConfig : IEntityConfig
8-
{
9-
string ResourceGroupName { get; }
10-
11-
IEnumerable<IEntityConfig> Dependencies { get; }
12-
}
13-
147
public sealed class ResourceConfig<TModel> : IEntityConfig<TModel>, IResourceConfig
158
where TModel : class
169
{
@@ -52,12 +45,16 @@ public IEnumerable<string> GetId(string subscription)
5245
}
5346
.Concat(Strategy.GetId(Name));
5447

55-
Result IEntityConfig.Accept<Context, Result>(
56-
IEntityConfigVisitor<Context, Result> visitor, Context context)
48+
TResult IEntityConfig.Accept<TContext, TResult>(
49+
IEntityConfigVisitor<TContext, TResult> visitor, TContext context)
50+
=> visitor.Visit(this, context);
51+
52+
TResult IEntityConfig<TModel>.Accept<TContext, TResult>(
53+
IEntityConfigVisitor<TModel, TContext, TResult> visitor, TContext context)
5754
=> visitor.Visit(this, context);
5855

59-
Result IEntityConfig<TModel>.Accept<Context, Result>(
60-
IEntityConfigVisitor<TModel, Context, Result> visitor, Context context)
56+
TResult IResourceConfig.Accept<TContext, TResult>(
57+
IResourceConfigVisitor<TContext, TResult> visitor, TContext context)
6158
=> visitor.Visit(this, context);
6259
}
6360
}

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,5 +70,9 @@ public static Task<TModel> CreateOrUpdateAsync<TModel>(
7070
config.Name,
7171
model,
7272
cancellationToken));
73+
74+
public static IEnumerable<IResourceConfig> GetResourceDependencies(
75+
this IResourceConfig config)
76+
=> config.Dependencies.Select(d => d.Resource);
7377
}
7478
}

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

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,6 @@ public static class TargetDependencies
77
{
88
public static IEnumerable<IResourceConfig> GetTargetDependencies(
99
this IResourceConfig config, IState target)
10-
=> config
11-
.Dependencies
12-
.Select(d => d.Resource)
13-
.Where(target.Contains);
10+
=> config.GetResourceDependencies().Where(target.Contains);
1411
}
1512
}

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

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,9 @@ await _OperationContext.GetOrAdd(
4343
async () =>
4444
{
4545
// wait for all dependencies
46-
var tasks = config.Dependencies.Select(UpdateStateAsyncDispatch);
46+
var tasks = config
47+
.GetResourceDependencies()
48+
.Select(UpdateStateAsyncDispatch);
4749
await Task.WhenAll(tasks);
4850
// call the CreateOrUpdateAsync function for the resource.
4951
return await config.CreateOrUpdateAsync(
@@ -54,26 +56,14 @@ await _OperationContext.GetOrAdd(
5456
}
5557
}
5658

57-
public Task UpdateStateAsync<TModel, TParentModel>(
58-
NestedResourceConfig<TModel, TParentModel> config)
59-
where TModel : class
60-
where TParentModel : class
61-
=> UpdateStateAsyncDispatch(config.Parent);
62-
63-
public Task UpdateStateAsyncDispatch(IEntityConfig config)
59+
public Task UpdateStateAsyncDispatch(IResourceConfig config)
6460
=> config.Accept(new UpdateStateAsyncVisitor(), this);
6561
}
6662

67-
sealed class UpdateStateAsyncVisitor : IEntityConfigVisitor<Context, Task>
63+
sealed class UpdateStateAsyncVisitor : IResourceConfigVisitor<Context, Task>
6864
{
69-
public Task Visit<TModel>(ResourceConfig<TModel> config, Context context)
70-
where TModel : class
71-
=> context.UpdateStateAsync(config);
72-
73-
public Task Visit<TModel, TParentModel>(
74-
NestedResourceConfig<TModel, TParentModel> config, Context context)
65+
public Task Visit<TModel>(ResourceConfig<TModel> config, Context context)
7566
where TModel : class
76-
where TParentModel : class
7767
=> context.UpdateStateAsync(config);
7868
}
7969
}

0 commit comments

Comments
 (0)