Skip to content

Commit 37909d0

Browse files
Operation as a separate parameter.
1 parent 2f7d5e3 commit 37909d0

15 files changed

+77
-72
lines changed

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

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

55
namespace Microsoft.Azure.Commands.Common.Strategies
66
{
7+
/// <summary>
8+
/// It's a base class for asynchronous operation visitors, such as
9+
/// CreateOrUpdateAsyncOperation and GetAsyncOperation visitors.
10+
/// </summary>
711
abstract class AsyncOperationVisitor : IResourceConfigVisitor<Task<object>>
812
{
913
public IClient Client { get; }
@@ -12,20 +16,16 @@ abstract class AsyncOperationVisitor : IResourceConfigVisitor<Task<object>>
1216

1317
public State Result { get; } = new State();
1418

15-
16-
public AsyncOperationVisitor(IClient client, CancellationToken cancellationToken)
17-
{
18-
Client = client;
19-
CancellationToken = cancellationToken;
20-
}
21-
2219
public async Task<object> GetOrAddUntyped(IResourceConfig config)
2320
=> await TaskMap.GetOrAdd(
2421
config,
2522
async _ =>
2623
{
2724
var model = await config.Apply(this);
28-
Result.GetOrAddUntyped(config, () => model);
25+
if (model != null)
26+
{
27+
Result.GetOrAddUntyped(config, () => model);
28+
}
2929
return model;
3030
});
3131

@@ -36,6 +36,12 @@ public async Task<Model> GetOrAdd<Model>(IResourceConfig<Model> config)
3636
return model as Model;
3737
}
3838

39+
protected AsyncOperationVisitor(IClient client, CancellationToken cancellationToken)
40+
{
41+
Client = client;
42+
CancellationToken = cancellationToken;
43+
}
44+
3945
public abstract Task<object> Visit<Model>(ResourceConfig<Model> config) where Model : class;
4046

4147
public abstract Task<object> Visit<Model, ParentModel>(

src/ResourceManager/Common/Commands.Common.Strategies/Compute/ComputeStrategy.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ public static class ComputePolicy
1010
public static ResourceStrategy<Model> Create<Model, Operations>(
1111
string header,
1212
Func<ComputeManagementClient, Operations> getOperations,
13-
Func<GetAsyncParams<Operations>, Task<Model>> getAsync,
14-
Func<CreateOrUpdateAsyncParams<Operations, Model>, Task<Model>> createOrUpdateAsync)
13+
Func<Operations, GetAsyncParams, Task<Model>> getAsync,
14+
Func<Operations, CreateOrUpdateAsyncParams<Model>, Task<Model>> createOrUpdateAsync)
1515
where Model : Resource
1616
=> ResourceStrategy.Create(
1717
new[] { "Microsoft.Compute", header },

src/ResourceManager/Common/Commands.Common.Strategies/Compute/VirtualMachineStrategy.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@ public static class VirtualMachineStrategy
1111
= ComputePolicy.Create(
1212
"virtualMachines",
1313
client => client.VirtualMachines,
14-
p => p.Operations.GetAsync(
14+
(o, p) => o.GetAsync(
1515
p.ResourceGroupName, p.Name, null, p.CancellationToken),
16-
p => p.Operations.CreateOrUpdateAsync(
16+
(o, p) => o.CreateOrUpdateAsync(
1717
p.ResourceGroupName, p.Name, p.Model, p.CancellationToken));
1818

1919
public static ResourceConfig<VirtualMachine> CreateVirtualMachineConfig(

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

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,16 @@ namespace Microsoft.Azure.Commands.Common.Strategies
66
{
77
public static class CreateOrUpdateAsyncOperation
88
{
9+
/// <summary>
10+
/// Asynchronous resource creation.
11+
/// </summary>
12+
/// <typeparam name="Model"></typeparam>
13+
/// <param name="config"></param>
14+
/// <param name="client"></param>
15+
/// <param name="current"></param>
16+
/// <param name="target"></param>
17+
/// <param name="cancellationToken"></param>
18+
/// <returns>An Azure state.</returns>
919
public static async Task<IState> CreateOrUpdateAsync<Model>(
1020
this IResourceConfig<Model> config,
1121
IClient client,
@@ -30,12 +40,13 @@ public override async Task<object> Visit<Model>(ResourceConfig<Model> config)
3040
}
3141
var tasks = config.Dependencies.Select(GetOrAddUntyped);
3242
await Task.WhenAll(tasks);
33-
return await config.Strategy.CreateOrUpdateAsync(CreateOrUpdateAsyncParams.Create(
43+
return await config.Strategy.CreateOrUpdateAsync(
3444
Client,
35-
config.ResourceGroupName,
36-
config.Name,
37-
Target.GetOrNull(config),
38-
CancellationToken));
45+
CreateOrUpdateAsyncParams.Create(
46+
config.ResourceGroupName,
47+
config.Name,
48+
Target.GetOrNull(config),
49+
CancellationToken));
3950
}
4051

4152
public override async Task<object> Visit<Model, ParentModel>(

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

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,14 @@ namespace Microsoft.Azure.Commands.Common.Strategies
44
{
55
public static class CreateOrUpdateAsyncParams
66
{
7-
public static CreateOrUpdateAsyncParams<Operations, Model> Create<Operations, Model>(
8-
Operations operations,
9-
string resourceGroupName,
10-
string name,
11-
Model model,
12-
CancellationToken cancellationToken)
13-
=> new CreateOrUpdateAsyncParams<Operations, Model>(
14-
operations, resourceGroupName, name, model, cancellationToken);
7+
public static CreateOrUpdateAsyncParams<Model> Create<Model>(
8+
string resourceGroupName, string name, Model model, CancellationToken cancellationToken)
9+
=> new CreateOrUpdateAsyncParams<Model>(
10+
resourceGroupName, name, model, cancellationToken);
1511
}
1612

17-
public sealed class CreateOrUpdateAsyncParams<TOperations, TModel>
13+
public sealed class CreateOrUpdateAsyncParams<TModel>
1814
{
19-
public TOperations Operations { get; }
20-
2115
public string ResourceGroupName { get; }
2216

2317
public string Name { get; }
@@ -27,13 +21,11 @@ public sealed class CreateOrUpdateAsyncParams<TOperations, TModel>
2721
public TModel Model { get; }
2822

2923
public CreateOrUpdateAsyncParams(
30-
TOperations operations,
3124
string resourceGroupName,
3225
string name,
3326
TModel model,
3427
CancellationToken cancellationToken)
3528
{
36-
Operations = operations;
3729
ResourceGroupName = resourceGroupName;
3830
Name = name;
3931
Model = model;

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

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,14 @@ namespace Microsoft.Azure.Commands.Common.Strategies
88
{
99
public static class GetAsyncOperation
1010
{
11+
/// <summary>
12+
/// Get current Azure state related to the given resource.
13+
/// </summary>
14+
/// <typeparam name="Model"></typeparam>
15+
/// <param name="resourceConfig"></param>
16+
/// <param name="client"></param>
17+
/// <param name="cancellationToken"></param>
18+
/// <returns>An Azure state.</returns>
1119
public static async Task<IState> GetAsync<Model>(
1220
this IResourceConfig<Model> resourceConfig,
1321
IClient client,
@@ -26,8 +34,10 @@ public override async Task<object> Visit<Model>(ResourceConfig<Model> config)
2634
Model info;
2735
try
2836
{
29-
info = await config.Strategy.GetAsync(GetAsyncParams.Create(
30-
Client, config.ResourceGroupName, config.Name, CancellationToken));
37+
info = await config.Strategy.GetAsync(
38+
Client,
39+
new GetAsyncParams(
40+
config.ResourceGroupName, config.Name, CancellationToken));
3141
}
3242
catch (CloudException e) when (e.Response.StatusCode == HttpStatusCode.NotFound)
3343
{

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

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,34 +2,19 @@
22

33
namespace Microsoft.Azure.Commands.Common.Strategies
44
{
5-
public static class GetAsyncParams
5+
public sealed class GetAsyncParams
66
{
7-
public static GetAsyncParams<Operations> Create<Operations>(
8-
Operations operations,
9-
string resourceGroupName,
10-
string name,
11-
CancellationToken cancellationToken)
12-
=> new GetAsyncParams<Operations>(
13-
operations, resourceGroupName, name, cancellationToken);
14-
}
15-
16-
public class GetAsyncParams<TOperations>
17-
{
18-
public TOperations Operations { get; }
19-
207
public string ResourceGroupName { get; }
218

229
public string Name { get; }
2310

2411
public CancellationToken CancellationToken { get; }
2512

2613
public GetAsyncParams(
27-
TOperations operations,
2814
string resourceGroupName,
2915
string name,
3016
CancellationToken cancellationToken)
3117
{
32-
Operations = operations;
3318
ResourceGroupName = resourceGroupName;
3419
Name = name;
3520
CancellationToken = cancellationToken;

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
namespace Microsoft.Azure.Commands.Common.Strategies
22
{
3+
/// <summary>
4+
/// An Azure state which is a dictionary of models.
5+
/// </summary>
36
public interface IState
47
{
58
Model GetOrNull<Model>(IResourceConfig<Model> config)

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@ public static class NetworkInterfaceStrategy
1010
= NetworkStrategy.Create(
1111
"networkInterfaces",
1212
client => client.NetworkInterfaces,
13-
p => p.Operations.GetAsync(
13+
(o, p) => o.GetAsync(
1414
p.ResourceGroupName, p.Name, null, p.CancellationToken),
15-
p => p.Operations.CreateOrUpdateAsync(
15+
(o, p) => o.CreateOrUpdateAsync(
1616
p.ResourceGroupName, p.Name, p.Model, p.CancellationToken));
1717

1818
public static ResourceConfig<NetworkInterface> CreateNetworkInterfaceConfig(

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@ public static class NetworkSecurityGroupStrategy
1010
= NetworkStrategy.Create(
1111
"networkSecurityGroups",
1212
client => client.NetworkSecurityGroups,
13-
p => p.Operations.GetAsync(
13+
(o, p) => o.GetAsync(
1414
p.ResourceGroupName, p.Name, null, p.CancellationToken),
15-
p => p.Operations.CreateOrUpdateAsync(
15+
(o, p) => o.CreateOrUpdateAsync(
1616
p.ResourceGroupName, p.Name, p.Model, p.CancellationToken));
1717

1818
public static ResourceConfig<NetworkSecurityGroup> CreateNetworkSecurityGroupConfig(

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ public static class NetworkStrategy
1010
public static ResourceStrategy<Model> Create<Model, Operations>(
1111
string header,
1212
Func<NetworkManagementClient, Operations> getOperations,
13-
Func<GetAsyncParams<Operations>, Task<Model>> getAsync,
14-
Func<CreateOrUpdateAsyncParams<Operations, Model>, Task<Model>> createOrUpdateAsync)
13+
Func<Operations, GetAsyncParams, Task<Model>> getAsync,
14+
Func<Operations, CreateOrUpdateAsyncParams<Model>, Task<Model>> createOrUpdateAsync)
1515
where Model : Resource
1616
=> ResourceStrategy.Create(
1717
new [] { "Microsoft.Network", header },

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@ public static class PublicIPAddressStrategy
1010
= NetworkStrategy.Create(
1111
"publicIPAddresses",
1212
client => client.PublicIPAddresses,
13-
p => p.Operations.GetAsync(
13+
(o, p) => o.GetAsync(
1414
p.ResourceGroupName, p.Name, null, p.CancellationToken),
15-
p => p.Operations.CreateOrUpdateAsync(
15+
(o, p) => o.CreateOrUpdateAsync(
1616
p.ResourceGroupName, p.Name, p.Model, p.CancellationToken));
1717

1818
public static ResourceConfig<PublicIPAddress> CreatePublicIPAddressConfig(

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@ public static class VirtualNetworkStrategy
1010
= NetworkStrategy.Create(
1111
"virtualNetworks",
1212
client => client.VirtualNetworks,
13-
p => p.Operations.GetAsync(
13+
(o, p) => o.GetAsync(
1414
p.ResourceGroupName, p.Name, null, p.CancellationToken),
15-
p => p.Operations.CreateOrUpdateAsync(
15+
(o, p) => o.CreateOrUpdateAsync(
1616
p.ResourceGroupName, p.Name, p.Model, p.CancellationToken));
1717

1818
public static ResourceConfig<VirtualNetwork> CreateVirtualNetworkConfig(

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ public static class ResourceGroupStrategy
1010
= ResourceStrategy.Create<ResourceGroup, ResourceManagementClient, IResourceGroupsOperations>(
1111
_ => Enumerable.Empty<string>(),
1212
client => client.ResourceGroups,
13-
p => p.Operations.GetAsync(p.Name, p.CancellationToken),
14-
p => p.Operations.CreateOrUpdateAsync(p.Name, p.Model, p.CancellationToken),
13+
(o, p) => o.GetAsync(p.Name, p.CancellationToken),
14+
(o, p) => o.CreateOrUpdateAsync(p.Name, p.Model, p.CancellationToken),
1515
model => model.Location,
1616
(model, location) => model.Location = location);
1717

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

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,18 +10,18 @@ public sealed class ResourceStrategy<Model> : IResourceStrategy
1010
{
1111
public Func<string, IEnumerable<string>> GetId { get; }
1212

13-
public Func<GetAsyncParams<IClient>, Task<Model>> GetAsync { get; }
13+
public Func<IClient, GetAsyncParams, Task<Model>> GetAsync { get; }
1414

15-
public Func<CreateOrUpdateAsyncParams<IClient, Model>, Task<Model>> CreateOrUpdateAsync { get; }
15+
public Func<IClient, CreateOrUpdateAsyncParams<Model>, Task<Model>> CreateOrUpdateAsync { get; }
1616

1717
public Func<Model, string> GetLocation { get; }
1818

1919
public Action<Model, string> SetLocation { get; }
2020

2121
public ResourceStrategy(
2222
Func<string, IEnumerable<string>> getId,
23-
Func<GetAsyncParams<IClient>, Task<Model>> getAsync,
24-
Func<CreateOrUpdateAsyncParams<IClient, Model>, Task<Model>> createOrUpdateAsync,
23+
Func<IClient, GetAsyncParams, Task<Model>> getAsync,
24+
Func<IClient, CreateOrUpdateAsyncParams<Model>, Task<Model>> createOrUpdateAsync,
2525
Func<Model, string> getLocation,
2626
Action<Model, string> setLocation)
2727
{
@@ -38,28 +38,26 @@ public static class ResourceStrategy
3838
public static ResourceStrategy<Model> Create<Model, Client, Operations>(
3939
Func<string, IEnumerable<string>> getId,
4040
Func<Client, Operations> getOperations,
41-
Func<GetAsyncParams<Operations>, Task<Model>> getAsync,
42-
Func<CreateOrUpdateAsyncParams<Operations, Model>, Task<Model>> createOrUpdateAsync,
41+
Func<Operations, GetAsyncParams, Task<Model>> getAsync,
42+
Func<Operations, CreateOrUpdateAsyncParams<Model>, Task<Model>> createOrUpdateAsync,
4343
Func<Model, string> getLocation,
4444
Action<Model, string> setLocation)
4545
where Client : ServiceClient<Client>
4646
{
4747
Func<IClient, Operations> toOperations = client => getOperations(client.GetClient<Client>());
4848
return new ResourceStrategy<Model>(
4949
getId,
50-
p => getAsync(GetAsyncParams.Create(
51-
toOperations(p.Operations), p.ResourceGroupName, p.Name, p.CancellationToken)),
52-
p => createOrUpdateAsync(CreateOrUpdateAsyncParams.Create(
53-
toOperations(p.Operations), p.ResourceGroupName, p.Name, p.Model, p.CancellationToken)),
50+
(client, p) => getAsync(toOperations(client), p),
51+
(client, p) => createOrUpdateAsync(toOperations(client), p),
5452
getLocation,
5553
setLocation);
5654
}
5755

5856
public static ResourceStrategy<Model> Create<Model, Client, Operations>(
5957
IEnumerable<string> headers,
6058
Func<Client, Operations> getOperations,
61-
Func<GetAsyncParams<Operations>, Task<Model>> getAsync,
62-
Func<CreateOrUpdateAsyncParams<Operations, Model>, Task<Model>> createOrUpdateAsync,
59+
Func<Operations, GetAsyncParams, Task<Model>> getAsync,
60+
Func<Operations, CreateOrUpdateAsyncParams<Model>, Task<Model>> createOrUpdateAsync,
6361
Func<Model, string> getLocation,
6462
Action<Model, string> setLocation)
6563
where Client : ServiceClient<Client>

0 commit comments

Comments
 (0)