Skip to content

Commit

Permalink
ShouldProcess
Browse files Browse the repository at this point in the history
  • Loading branch information
sergey-shandar committed Nov 28, 2017
1 parent 901660a commit ace1441
Show file tree
Hide file tree
Showing 15 changed files with 64 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@
<Compile Include="Compute\Images.cs" />
<Compile Include="IResourceConfig.cs" />
<Compile Include="IResourceConfigVisitor.cs" />
<Compile Include="IShouldProcess.cs" />
<Compile Include="StateOperationContext.cs" />
<Compile Include="Compute\ComputeStrategy.cs" />
<Compile Include="Compute\VirtualMachineStrategy.cs" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,14 @@ namespace Microsoft.Azure.Commands.Common.Strategies.Compute
public static class ComputePolicy
{
public static ResourceStrategy<TModel> Create<TModel, TOperations>(
string type,
string header,
Func<ComputeManagementClient, TOperations> getOperations,
Func<TOperations, GetAsyncParams, Task<TModel>> getAsync,
Func<TOperations, CreateOrUpdateAsyncParams<TModel>, Task<TModel>> createOrUpdateAsync)
where TModel : Resource
=> ResourceStrategy.Create(
type,
new[] { "Microsoft.Compute", header },
getOperations,
getAsync,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ public static class VirtualMachineStrategy
{
public static ResourceStrategy<VirtualMachine> Strategy { get; }
= ComputePolicy.Create(
"virtual machine",
"virtualMachines",
client => client.VirtualMachines,
(o, p) => o.GetAsync(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace Microsoft.Azure.Commands.Common.Strategies
{
public interface IShouldProcess
{
bool ShouldCreate<TModel>(ResourceConfig<TModel> config, TModel model)
where TModel : class;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ public static class NetworkInterfaceStrategy
{
public static ResourceStrategy<NetworkInterface> Strategy { get; }
= NetworkStrategy.Create(
"network interface",
"networkInterfaces",
client => client.NetworkInterfaces,
(o, p) => o.GetAsync(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ public static class NetworkSecurityGroupStrategy
{
public static ResourceStrategy<NetworkSecurityGroup> Strategy { get; }
= NetworkStrategy.Create(
"network security group",
"networkSecurityGroups",
client => client.NetworkSecurityGroups,
(o, p) => o.GetAsync(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,14 @@ namespace Microsoft.Azure.Commands.Common.Strategies.Network
public static class NetworkStrategy
{
public static ResourceStrategy<TModel> Create<TModel, TOperations>(
string type,
string header,
Func<NetworkManagementClient, TOperations> getOperations,
Func<TOperations, GetAsyncParams, Task<TModel>> getAsync,
Func<TOperations, CreateOrUpdateAsyncParams<TModel>, Task<TModel>> createOrUpdateAsync)
where TModel : Resource
=> ResourceStrategy.Create(
type,
new [] { "Microsoft.Network", header },
getOperations,
getAsync,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ public static class PublicIPAddressStrategy
{
public static ResourceStrategy<PublicIPAddress> Strategy { get; }
= NetworkStrategy.Create(
"public IP address",
"publicIPAddresses",
client => client.PublicIPAddresses,
(o, p) => o.GetAsync(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ public static class SubnetPolicy
public static NestedResourceStrategy<Subnet, VirtualNetwork> Strategy { get; }
= NestedResourceStrategy.Create<Subnet, VirtualNetwork>(
"subnets",
(vn, name) => vn.Subnets?.FirstOrDefault(s => s.Name == name),
(vn, name) => vn.Subnets?.FirstOrDefault(s => s?.Name == name),
(vn, name, subnet) =>
{
subnet.Name = name;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ public static class VirtualNetworkStrategy
{
public static ResourceStrategy<VirtualNetwork> Strategy { get; }
= NetworkStrategy.Create(
"virtual network",
"virtualNetworks",
client => client.VirtualNetworks,
(o, p) => o.GetAsync(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ public static class ResourceGroupStrategy
{
public static ResourceStrategy<ResourceGroup> Strategy { get; }
= ResourceStrategy.Create(
"resource group",
_ => Enumerable.Empty<string>(),
(ResourceManagementClient client) => client.ResourceGroups,
(o, p) => o.GetAsync(p.Name, p.CancellationToken),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ namespace Microsoft.Azure.Commands.Common.Strategies
{
public sealed class ResourceStrategy<TModel> : IEntityStrategy
{
public string Type { get; }

public Func<string, IEnumerable<string>> GetId { get; }

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

public ResourceStrategy(
string type,
Func<string, IEnumerable<string>> getId,
Func<IClient, GetAsyncParams, Task<TModel>> getAsync,
Func<IClient, CreateOrUpdateAsyncParams<TModel>, Task<TModel>> createOrUpdateAsync,
Func<TModel, string> getLocation,
Action<TModel, string> setLocation)
{
Type = type;
GetId = getId;
GetAsync = getAsync;
CreateOrUpdateAsync = createOrUpdateAsync;
Expand All @@ -37,6 +41,7 @@ public ResourceStrategy(
public static class ResourceStrategy
{
public static ResourceStrategy<TModel> Create<TModel, TClient, TOperation>(
string type,
Func<string, IEnumerable<string>> getId,
Func<TClient, TOperation> getOperations,
Func<TOperation, GetAsyncParams, Task<TModel>> getAsync,
Expand All @@ -47,6 +52,7 @@ public static ResourceStrategy<TModel> Create<TModel, TClient, TOperation>(
{
Func<IClient, TOperation> toOperations = client => getOperations(client.GetClient<TClient>());
return new ResourceStrategy<TModel>(
type,
getId,
(client, p) => getAsync(toOperations(client), p),
(client, p) => createOrUpdateAsync(toOperations(client), p),
Expand All @@ -55,6 +61,7 @@ public static ResourceStrategy<TModel> Create<TModel, TClient, TOperation>(
}

public static ResourceStrategy<TModel> Create<TModel, TClient, TOperation>(
string type,
IEnumerable<string> headers,
Func<TClient, TOperation> getOperations,
Func<TOperation, GetAsyncParams, Task<TModel>> getAsync,
Expand All @@ -63,6 +70,7 @@ public static ResourceStrategy<TModel> Create<TModel, TClient, TOperation>(
Action<TModel, string> setLocation)
where TClient : ServiceClient<TClient>
=> Create(
type,
name => new[] { "providers" }.Concat(headers).Concat(new[] { name }),
getOperations,
getAsync,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@ public static TModel Get<TModel, TParentModel>(
this IState state, NestedResourceConfig<TModel, TParentModel> config)
where TModel : class
where TParentModel : class
=> config.Strategy.Get(state.GetDispatch(config.Parent), config.Name);
{
var parentModel = state.GetDispatch(config.Parent);
return parentModel == null ? null : config.Strategy.Get(parentModel, config.Name);
}

/// <summary>
/// Get a model of the given entity model from the given state.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,12 @@ public static async Task<IState> UpdateStateAsync<TModel>(
this ResourceConfig<TModel> config,
IClient client,
IState target,
CancellationToken cancellationToken)
CancellationToken cancellationToken,
IShouldProcess shouldProcess)
where TModel : class
{
var context = new Context(new StateOperationContext(client, cancellationToken), target);
var context = new Context(
new StateOperationContext(client, cancellationToken), target, shouldProcess);
await context.UpdateStateAsync(config);
return context.Result;
}
Expand All @@ -27,10 +29,14 @@ sealed class Context

readonly IState _Target;

public Context(StateOperationContext operationContext, IState target)
readonly IShouldProcess _ShouldProcess;

public Context(
StateOperationContext operationContext, IState target, IShouldProcess shouldProcess)
{
_OperationContext = operationContext;
_Target = target;
_ShouldProcess = shouldProcess;
}

public async Task UpdateStateAsync<TModel>(ResourceConfig<TModel> config)
Expand All @@ -49,16 +55,16 @@ await _OperationContext.GetOrAdd(
.Select(UpdateStateAsyncDispatch);
await Task.WhenAll(tasks);
// call the CreateOrUpdateAsync function for the resource.
try
if (_ShouldProcess.ShouldCreate(config, model))
{
return await config.CreateOrUpdateAsync(
_OperationContext.Client,
model,
_OperationContext.CancellationToken);
}
catch (Exception e)
else
{
throw e;
return null;
}
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,20 @@ public T GetClient<T>()
Context, AzureEnvironment.Endpoint.ResourceManager);
}

private sealed class ShouldProcessType : IShouldProcess
{
readonly Cmdlet _Cmdlet;

public ShouldProcessType(Cmdlet cmdlet)
{
_Cmdlet = cmdlet;
}

public bool ShouldCreate<TModel>(ResourceConfig<TModel> config, TModel model)
where TModel : class
=> _Cmdlet.ShouldProcess(config.Name + " " + config.Strategy.Type, VerbsCommon.New);
}

public void StrategyExecuteCmdlet()
{
ResourceGroupName = ResourceGroupName ?? Name;
Expand Down Expand Up @@ -240,14 +254,12 @@ public void StrategyExecuteCmdlet()

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

if (ShouldProcess(Name, VerbsCommon.New))
{
var result = virtualMachine
.UpdateStateAsync(client, target, new CancellationToken())
.GetAwaiter()
.GetResult();
WriteObject(result);
}
var result = virtualMachine
.UpdateStateAsync(
client, target, new CancellationToken(), new ShouldProcessType(this))
.GetAwaiter()
.GetResult();
WriteObject(result);
}

public void DefaultExecuteCmdlet()
Expand Down

0 comments on commit ace1441

Please sign in to comment.