Skip to content

Commit

Permalink
new
Browse files Browse the repository at this point in the history
  • Loading branch information
sergey-shandar committed Nov 22, 2017
1 parent 6f8b6b7 commit 2c8aeb9
Show file tree
Hide file tree
Showing 15 changed files with 108 additions and 208 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

namespace Microsoft.Azure.Commands.Common.Strategies
{
sealed class AsyncOperationContext
public sealed class AsyncOperationContext
{
public IClient Client { get; }

Expand All @@ -19,7 +19,7 @@ public AsyncOperationContext(IClient client, CancellationToken cancellationToken
CancellationToken = cancellationToken;
}

public async Task<Model> GetOrAdd<Model>(
public async Task<Model> GetOrAddAsync<Model>(
ResourceConfig<Model> config, Func<Task<Model>> create)
where Model : class
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,11 +76,12 @@
<Compile Include="GetAsyncOperation.cs" />
<Compile Include="GetAsyncParams.cs" />
<Compile Include="IClient.cs" />
<Compile Include="IReport.cs" />
<Compile Include="IResourceBaseConfig.cs" />
<Compile Include="IResourceBaseConfigVisitor.cs" />
<Compile Include="IResourceBaseStrategy.cs" />
<Compile Include="IState.cs" />
<Compile Include="TargetDependencies.cs" />
<Compile Include="TypedDictionary.cs" />
<Compile Include="NestedResourceConfig.cs" />
<Compile Include="NestedResourceStrategy.cs" />
<Compile Include="Network\NetworkInterfaceStrategy.cs" />
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
using System;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;

namespace Microsoft.Azure.Commands.Common.Strategies
{
/*
public static class CreateOrUpdateAsyncOperation
{
public static async Task<IState> CreateOrUpdateAsync<Model>(
Expand All @@ -14,219 +13,67 @@ public static async Task<IState> CreateOrUpdateAsync<Model>(
CancellationToken cancellationToken)
where Model : class
{
var context = new Context(client, cancellationToken, target);
var context = new Context(
new AsyncOperationContext(client, cancellationToken), target);
await context.CreateOrUpdateAsync(config);
return context.OperationContext.Result;
}

sealed class Context
{
public AsyncOperationContext OperationContext { get; } = new AsyncOperationContext();
public IClient Client { get; }
public AsyncOperationContext OperationContext { get; }

public IState Target { get; }

public CancellationToken CancellationToken { get; }
public Context(IClient client, CancellationToken cancellationToken, IState target)
public Context(AsyncOperationContext operationContext, IState target)
{
Client = client;
CancellationToken = cancellationToken;
OperationContext = operationContext;
Target = target;
}
}

sealed class Visitor : IResourceBaseConfigVisitor<Context, Task>
{
public async Task Visit<Model>(ResourceConfig<Model> config, Context context)
public async Task CreateOrUpdateAsync<Model>(ResourceConfig<Model> config)
where Model : class
{
var model = context.Target.Get(config);
var model = Target.Get(config);
if (model != null)
{
await context.OperationContext.GetOrAdd(
await OperationContext.GetOrAddAsync(
config,
async () =>
{
foreach (var d in config.Dependencies)
{
}
var tasks = config.Dependencies.Select(CreateOrUpdateAsync);
await Task.WhenAll(tasks);
return await config.Strategy.CreateOrUpdateAsync(
context.Client,
OperationContext.Client,
CreateOrUpdateAsyncParams.Create(
config.ResourceGroupName,
config.Name,
model,
context.CancellationToken));
OperationContext.CancellationToken));
});
}
}

public Task Visit<Model, ParentModel>(NestedResourceConfig<Model, ParentModel> config, Context context)
public Task CreateOrUpdateAsync<Model, ParentModel>(NestedResourceConfig<Model, ParentModel> config)
where Model : class
where ParentModel : class
{
throw new NotImplementedException();
}
}
}
/*
public static class CreateOrUpdateAsyncOperation
{
public static async Task<IState> CreateOrUpdateAsync<Model>(
this ResourceConfig<Model> config,
IClient client,
IState target,
CancellationToken cancellationToken)
where Model : class
{
var context = new AsyncOperationContext();
var model = target.Get(config);
if (model != null)
{
await context.GetOrAdd(
config,
async () =>
{
// config.Dependencies
return await config.Strategy.CreateOrUpdateAsync(
client,
CreateOrUpdateAsyncParams.Create(
config.ResourceGroupName,
config.Name,
model,
cancellationToken));
});
}
return context.Result;
}
sealed class Context
{
AsyncOperationContext OperationContext { get; } = new AsyncOperationContext();
IClient Client { get; }
IState Target { get; }
CancellationToken CancellationToken { get; }
public async Task CreateOrUpdateAsync<Model>(ResourceConfig<Model> config)
where Model : class
{
var model = Target.Get(config);
if (model != null)
{
await OperationContext.GetOrAdd(
config,
async () =>
{
// config.Dependencies
return await config.Strategy.CreateOrUpdateAsync(
Client,
CreateOrUpdateAsyncParams.Create(
config.ResourceGroupName,
config.Name,
model,
CancellationToken));
});
}
}
=> CreateOrUpdateAsync(config.Parent);

public Context(IClient client, IState target, CancellationToken cancellationToken)
{
Client = client;
Target = target;
CancellationToken = cancellationToken;
}
public Task CreateOrUpdateAsync(IResourceBaseConfig config)
=> config.Accept(new Visitor(), this);
}

sealed class Visitor : IResourceBaseConfigVisitor<Context, Task>
{
public Task Visit<Model>(ResourceConfig<Model> config, Context context)
where Model : class
{
throw new NotImplementedException();
}
=> context.CreateOrUpdateAsync(config);

public Task Visit<Model, ParentModel>(
NestedResourceConfig<Model, ParentModel> config, Context context)
where Model : class
where ParentModel : class
{
throw new NotImplementedException();
}
}
}
/*
public static class CreateOrUpdateAsyncOperation
{
/// <summary>
/// Asynchronous resource creation.
/// </summary>
/// <typeparam name="Model"></typeparam>
/// <param name="config"></param>
/// <param name="client"></param>
/// <param name="current"></param>
/// <param name="target"></param>
/// <param name="cancellationToken"></param>
/// <returns>An Azure state.</returns>
public static async Task<IState> CreateOrUpdateAsync<Model>(
this IResourceBaseConfig<Model> config,
IClient client,
IState target,
CancellationToken cancellationToken)
where Model : class
{
var visitor = new CreateAsyncVisitor(client, target, cancellationToken);
await visitor.GetOrAdd(config);
return visitor.Result;
}
sealed class CreateAsyncVisitor : AsyncOperationVisitor
{
public override async Task<object> Visit<Model>(ResourceConfig<Model> config)
{
var target = Target.Get(config);
if (target == null)
{
return null;
}
var tasks = config.Dependencies.Select(GetOrAddUntyped);
await Task.WhenAll(tasks);
return await config.Strategy.CreateOrUpdateAsync(
Client,
CreateOrUpdateAsyncParams.Create(
config.ResourceGroupName,
config.Name,
Target.Get(config),
CancellationToken));
}
public override async Task<object> Visit<Model, ParentModel>(
NestedResourceConfig<Model, ParentModel> config)
{
var target = Target.GetNestedResourceModel(config);
if (target == null)
{
return null;
}
var parent = await GetOrAdd(config.Parent);
return config.Strategy.Get(parent, config.Name);
}
public CreateAsyncVisitor(
IClient client,
IState target,
CancellationToken cancellationToken)
: base(client, cancellationToken)
{
Target = target;
}
IState Target { get; }
=> context.CreateOrUpdateAsync(config);
}
}
*/
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,9 @@ public static V GetOrNull<K, V>(this IDictionary<K, V> dictionary, K key)
V result;
return dictionary.TryGetValue(key, out result) ? result : null;
}

public static V GetOrAdd<K, V, T>(this ConcurrentDictionary<K, T> dictionary, K key, Func<V> add)
where V : T
=> (V)dictionary.GetOrAdd(key, _ => add());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,11 @@ public static async Task<IState> GetAsync<Model>(
}

static Task AddStateAsync(this AsyncOperationContext context, IResourceBaseConfig config)
=> config.Accept(new Visitor(), context);
=> config.Accept(new AddStateAsyncVisitor(), context);

sealed class Visitor : IResourceBaseConfigVisitor<AsyncOperationContext, Task>
{
public async Task Visit<Model>(ResourceConfig<Model> config, AsyncOperationContext context)
where Model : class
=> await context.GetOrAdd(
static async Task AddStateAsync<Model>(this AsyncOperationContext context, ResourceConfig<Model> config)
where Model : class
=> await context.GetOrAddAsync(
config,
async () =>
{
Expand All @@ -40,7 +38,7 @@ public async Task Visit<Model>(ResourceConfig<Model> config, AsyncOperationConte
config.Name,
context.CancellationToken));
}
catch (CloudException e)
catch (CloudException e)
when (e.Response.StatusCode == HttpStatusCode.NotFound)
{
info = null;
Expand All @@ -54,11 +52,24 @@ public async Task Visit<Model>(ResourceConfig<Model> config, AsyncOperationConte
return info;
});

static Task AddStateAsync<Model, ParentModel>(
AsyncOperationContext context, NestedResourceConfig<Model, ParentModel> config)
where Model : class
where ParentModel : class
=> context.AddStateAsync(config.Parent);

sealed class AddStateAsyncVisitor : IResourceBaseConfigVisitor<AsyncOperationContext, Task>
{
public Task Visit<Model>(
ResourceConfig<Model> config, AsyncOperationContext context)
where Model : class
=> context.AddStateAsync(config);

public Task Visit<Model, ParentModel>(
NestedResourceConfig<Model, ParentModel> config, AsyncOperationContext context)
where Model : class
where ParentModel : class
=> context.AddStateAsync(config.Parent);
=> context.AddStateAsync(config);
}
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ Result Accept<Context, Result>(
IResourceBaseConfigVisitor<Context, Result> visitor, Context context);

IEnumerable<string> GetId(string subscription);

IResourceConfig Resource { get; }
}

public interface IResourceBaseConfig<Model> : IResourceBaseConfig
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@ public interface IState
{
Model Get<Model>(ResourceConfig<Model> config)
where Model : class;

bool Contains(IResourceBaseConfig config);
}

public static class StateExtension
public static class StateExtensions
{
public static Model Get<Model, ParentModel>(
this IState state, NestedResourceConfig<Model, ParentModel> config)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ public sealed class NestedResourceConfig<Model, ParentModel> : IResourceBaseConf

IResourceBaseStrategy IResourceBaseConfig.Strategy => Strategy;

public IResourceConfig Resource => Parent.Resource;

public NestedResourceConfig(
NestedResourceStrategy<Model, ParentModel> strategy,
IResourceBaseConfig<ParentModel> parent,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using Microsoft.Azure.Management.Network;
using Microsoft.Azure.Management.Network.Models;
using Microsoft.Azure.Management.ResourceManager.Models;
using System;

namespace Microsoft.Azure.Commands.Common.Strategies.Network
{
Expand Down
Loading

0 comments on commit 2c8aeb9

Please sign in to comment.