Skip to content

Commit ace1441

Browse files
ShouldProcess
1 parent 901660a commit ace1441

15 files changed

+64
-16
lines changed

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@
7171
<Compile Include="Compute\Images.cs" />
7272
<Compile Include="IResourceConfig.cs" />
7373
<Compile Include="IResourceConfigVisitor.cs" />
74+
<Compile Include="IShouldProcess.cs" />
7475
<Compile Include="StateOperationContext.cs" />
7576
<Compile Include="Compute\ComputeStrategy.cs" />
7677
<Compile Include="Compute\VirtualMachineStrategy.cs" />

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,14 @@ namespace Microsoft.Azure.Commands.Common.Strategies.Compute
88
public static class ComputePolicy
99
{
1010
public static ResourceStrategy<TModel> Create<TModel, TOperations>(
11+
string type,
1112
string header,
1213
Func<ComputeManagementClient, TOperations> getOperations,
1314
Func<TOperations, GetAsyncParams, Task<TModel>> getAsync,
1415
Func<TOperations, CreateOrUpdateAsyncParams<TModel>, Task<TModel>> createOrUpdateAsync)
1516
where TModel : Resource
1617
=> ResourceStrategy.Create(
18+
type,
1719
new[] { "Microsoft.Compute", header },
1820
getOperations,
1921
getAsync,

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ public static class VirtualMachineStrategy
99
{
1010
public static ResourceStrategy<VirtualMachine> Strategy { get; }
1111
= ComputePolicy.Create(
12+
"virtual machine",
1213
"virtualMachines",
1314
client => client.VirtualMachines,
1415
(o, p) => o.GetAsync(
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 IShouldProcess
4+
{
5+
bool ShouldCreate<TModel>(ResourceConfig<TModel> config, TModel model)
6+
where TModel : class;
7+
}
8+
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ public static class NetworkInterfaceStrategy
88
{
99
public static ResourceStrategy<NetworkInterface> Strategy { get; }
1010
= NetworkStrategy.Create(
11+
"network interface",
1112
"networkInterfaces",
1213
client => client.NetworkInterfaces,
1314
(o, p) => o.GetAsync(

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ public static class NetworkSecurityGroupStrategy
99
{
1010
public static ResourceStrategy<NetworkSecurityGroup> Strategy { get; }
1111
= NetworkStrategy.Create(
12+
"network security group",
1213
"networkSecurityGroups",
1314
client => client.NetworkSecurityGroups,
1415
(o, p) => o.GetAsync(

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,14 @@ namespace Microsoft.Azure.Commands.Common.Strategies.Network
88
public static class NetworkStrategy
99
{
1010
public static ResourceStrategy<TModel> Create<TModel, TOperations>(
11+
string type,
1112
string header,
1213
Func<NetworkManagementClient, TOperations> getOperations,
1314
Func<TOperations, GetAsyncParams, Task<TModel>> getAsync,
1415
Func<TOperations, CreateOrUpdateAsyncParams<TModel>, Task<TModel>> createOrUpdateAsync)
1516
where TModel : Resource
1617
=> ResourceStrategy.Create(
18+
type,
1719
new [] { "Microsoft.Network", header },
1820
getOperations,
1921
getAsync,

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ public static class PublicIPAddressStrategy
88
{
99
public static ResourceStrategy<PublicIPAddress> Strategy { get; }
1010
= NetworkStrategy.Create(
11+
"public IP address",
1112
"publicIPAddresses",
1213
client => client.PublicIPAddresses,
1314
(o, p) => o.GetAsync(

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ public static class SubnetPolicy
99
public static NestedResourceStrategy<Subnet, VirtualNetwork> Strategy { get; }
1010
= NestedResourceStrategy.Create<Subnet, VirtualNetwork>(
1111
"subnets",
12-
(vn, name) => vn.Subnets?.FirstOrDefault(s => s.Name == name),
12+
(vn, name) => vn.Subnets?.FirstOrDefault(s => s?.Name == name),
1313
(vn, name, subnet) =>
1414
{
1515
subnet.Name = name;

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ public static class VirtualNetworkStrategy
99
{
1010
public static ResourceStrategy<VirtualNetwork> Strategy { get; }
1111
= NetworkStrategy.Create(
12+
"virtual network",
1213
"virtualNetworks",
1314
client => client.VirtualNetworks,
1415
(o, p) => o.GetAsync(

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ public static class ResourceGroupStrategy
88
{
99
public static ResourceStrategy<ResourceGroup> Strategy { get; }
1010
= ResourceStrategy.Create(
11+
"resource group",
1112
_ => Enumerable.Empty<string>(),
1213
(ResourceManagementClient client) => client.ResourceGroups,
1314
(o, p) => o.GetAsync(p.Name, p.CancellationToken),

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ namespace Microsoft.Azure.Commands.Common.Strategies
88
{
99
public sealed class ResourceStrategy<TModel> : IEntityStrategy
1010
{
11+
public string Type { get; }
12+
1113
public Func<string, IEnumerable<string>> GetId { get; }
1214

1315
public Func<IClient, GetAsyncParams, Task<TModel>> GetAsync { get; }
@@ -20,12 +22,14 @@ public Func<IClient, CreateOrUpdateAsyncParams<TModel>, Task<TModel>> CreateOrUp
2022
public Action<TModel, string> SetLocation { get; }
2123

2224
public ResourceStrategy(
25+
string type,
2326
Func<string, IEnumerable<string>> getId,
2427
Func<IClient, GetAsyncParams, Task<TModel>> getAsync,
2528
Func<IClient, CreateOrUpdateAsyncParams<TModel>, Task<TModel>> createOrUpdateAsync,
2629
Func<TModel, string> getLocation,
2730
Action<TModel, string> setLocation)
2831
{
32+
Type = type;
2933
GetId = getId;
3034
GetAsync = getAsync;
3135
CreateOrUpdateAsync = createOrUpdateAsync;
@@ -37,6 +41,7 @@ public ResourceStrategy(
3741
public static class ResourceStrategy
3842
{
3943
public static ResourceStrategy<TModel> Create<TModel, TClient, TOperation>(
44+
string type,
4045
Func<string, IEnumerable<string>> getId,
4146
Func<TClient, TOperation> getOperations,
4247
Func<TOperation, GetAsyncParams, Task<TModel>> getAsync,
@@ -47,6 +52,7 @@ public static ResourceStrategy<TModel> Create<TModel, TClient, TOperation>(
4752
{
4853
Func<IClient, TOperation> toOperations = client => getOperations(client.GetClient<TClient>());
4954
return new ResourceStrategy<TModel>(
55+
type,
5056
getId,
5157
(client, p) => getAsync(toOperations(client), p),
5258
(client, p) => createOrUpdateAsync(toOperations(client), p),
@@ -55,6 +61,7 @@ public static ResourceStrategy<TModel> Create<TModel, TClient, TOperation>(
5561
}
5662

5763
public static ResourceStrategy<TModel> Create<TModel, TClient, TOperation>(
64+
string type,
5865
IEnumerable<string> headers,
5966
Func<TClient, TOperation> getOperations,
6067
Func<TOperation, GetAsyncParams, Task<TModel>> getAsync,
@@ -63,6 +70,7 @@ public static ResourceStrategy<TModel> Create<TModel, TClient, TOperation>(
6370
Action<TModel, string> setLocation)
6471
where TClient : ServiceClient<TClient>
6572
=> Create(
73+
type,
6674
name => new[] { "providers" }.Concat(headers).Concat(new[] { name }),
6775
getOperations,
6876
getAsync,

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,10 @@ public static TModel Get<TModel, TParentModel>(
1616
this IState state, NestedResourceConfig<TModel, TParentModel> config)
1717
where TModel : class
1818
where TParentModel : class
19-
=> config.Strategy.Get(state.GetDispatch(config.Parent), config.Name);
19+
{
20+
var parentModel = state.GetDispatch(config.Parent);
21+
return parentModel == null ? null : config.Strategy.Get(parentModel, config.Name);
22+
}
2023

2124
/// <summary>
2225
/// Get a model of the given entity model from the given state.

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

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,12 @@ public static async Task<IState> UpdateStateAsync<TModel>(
1111
this ResourceConfig<TModel> config,
1212
IClient client,
1313
IState target,
14-
CancellationToken cancellationToken)
14+
CancellationToken cancellationToken,
15+
IShouldProcess shouldProcess)
1516
where TModel : class
1617
{
17-
var context = new Context(new StateOperationContext(client, cancellationToken), target);
18+
var context = new Context(
19+
new StateOperationContext(client, cancellationToken), target, shouldProcess);
1820
await context.UpdateStateAsync(config);
1921
return context.Result;
2022
}
@@ -27,10 +29,14 @@ sealed class Context
2729

2830
readonly IState _Target;
2931

30-
public Context(StateOperationContext operationContext, IState target)
32+
readonly IShouldProcess _ShouldProcess;
33+
34+
public Context(
35+
StateOperationContext operationContext, IState target, IShouldProcess shouldProcess)
3136
{
3237
_OperationContext = operationContext;
3338
_Target = target;
39+
_ShouldProcess = shouldProcess;
3440
}
3541

3642
public async Task UpdateStateAsync<TModel>(ResourceConfig<TModel> config)
@@ -49,16 +55,16 @@ await _OperationContext.GetOrAdd(
4955
.Select(UpdateStateAsyncDispatch);
5056
await Task.WhenAll(tasks);
5157
// call the CreateOrUpdateAsync function for the resource.
52-
try
58+
if (_ShouldProcess.ShouldCreate(config, model))
5359
{
5460
return await config.CreateOrUpdateAsync(
5561
_OperationContext.Client,
5662
model,
5763
_OperationContext.CancellationToken);
5864
}
59-
catch (Exception e)
65+
else
6066
{
61-
throw e;
67+
return null;
6268
}
6369
});
6470
}

src/ResourceManager/Compute/Commands.Compute/VirtualMachine/Operation/NewAzureVMCommand.cs

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,20 @@ public T GetClient<T>()
184184
Context, AzureEnvironment.Endpoint.ResourceManager);
185185
}
186186

187+
private sealed class ShouldProcessType : IShouldProcess
188+
{
189+
readonly Cmdlet _Cmdlet;
190+
191+
public ShouldProcessType(Cmdlet cmdlet)
192+
{
193+
_Cmdlet = cmdlet;
194+
}
195+
196+
public bool ShouldCreate<TModel>(ResourceConfig<TModel> config, TModel model)
197+
where TModel : class
198+
=> _Cmdlet.ShouldProcess(config.Name + " " + config.Strategy.Type, VerbsCommon.New);
199+
}
200+
187201
public void StrategyExecuteCmdlet()
188202
{
189203
ResourceGroupName = ResourceGroupName ?? Name;
@@ -240,14 +254,12 @@ public void StrategyExecuteCmdlet()
240254

241255
var target = virtualMachine.GetTargetState(current, client.SubscriptionId, Location);
242256

243-
if (ShouldProcess(Name, VerbsCommon.New))
244-
{
245-
var result = virtualMachine
246-
.UpdateStateAsync(client, target, new CancellationToken())
247-
.GetAwaiter()
248-
.GetResult();
249-
WriteObject(result);
250-
}
257+
var result = virtualMachine
258+
.UpdateStateAsync(
259+
client, target, new CancellationToken(), new ShouldProcessType(this))
260+
.GetAwaiter()
261+
.GetResult();
262+
WriteObject(result);
251263
}
252264

253265
public void DefaultExecuteCmdlet()

0 commit comments

Comments
 (0)