Skip to content

Commit

Permalink
No Create, for now.
Browse files Browse the repository at this point in the history
  • Loading branch information
sergey-shandar committed Nov 20, 2017
1 parent 88a5696 commit 6f8b6b7
Show file tree
Hide file tree
Showing 15 changed files with 393 additions and 228 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
using System;
using System.Collections.Concurrent;
using System.Threading;
using System.Threading.Tasks;

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

public CancellationToken CancellationToken { get; }

public IState Result => State;

public AsyncOperationContext(IClient client, CancellationToken cancellationToken)
{
Client = client;
CancellationToken = cancellationToken;
}

public async Task<Model> GetOrAdd<Model>(
ResourceConfig<Model> config, Func<Task<Model>> create)
where Model : class
{
var result = await TaskMap.GetOrAdd(
config.DefaultIdStr(),
async _ =>
{
var model = await create();
if (model != null)
{
State.GetOrAdd(config, () => model);
}
return model;
});
return result as Model;
}

State State { get; } = new State();

ConcurrentDictionary<string, Task<object>> TaskMap { get; }
= new ConcurrentDictionary<string, Task<object>>();
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="AsyncOperationVisitor.cs" />
<Compile Include="AsyncOperationContext.cs" />
<Compile Include="Compute\ComputeStrategy.cs" />
<Compile Include="Compute\VirtualMachineStrategy.cs" />
<Compile Include="CreateOrUpdateAsyncOperation.cs" />
Expand Down Expand Up @@ -96,6 +96,7 @@
<Compile Include="ResourceStrategy.cs" />
<Compile Include="State.cs" />
<Compile Include="StateLocation.cs" />
<Compile Include="Void.cs" />
</ItemGroup>
<ItemGroup>
<None Include="MSSharedLibKey.snk" />
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,165 @@
using System.Linq;
using System;
using System.Threading;
using System.Threading.Tasks;

namespace Microsoft.Azure.Commands.Common.Strategies
{
/*
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 Context(client, cancellationToken, target);
}
sealed class Context
{
public AsyncOperationContext OperationContext { get; } = new AsyncOperationContext();
public IClient Client { get; }
public IState Target { get; }
public CancellationToken CancellationToken { get; }
public Context(IClient client, CancellationToken cancellationToken, IState target)
{
Client = client;
CancellationToken = cancellationToken;
Target = target;
}
}
sealed class Visitor : IResourceBaseConfigVisitor<Context, Task>
{
public async Task Visit<Model>(ResourceConfig<Model> config, Context context)
where Model : class
{
var model = context.Target.Get(config);
if (model != null)
{
await context.OperationContext.GetOrAdd(
config,
async () =>
{
foreach (var d in config.Dependencies)
{
}
return await config.Strategy.CreateOrUpdateAsync(
context.Client,
CreateOrUpdateAsyncParams.Create(
config.ResourceGroupName,
config.Name,
model,
context.CancellationToken));
});
}
}
public Task Visit<Model, ParentModel>(NestedResourceConfig<Model, ParentModel> config, Context context)
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));
});
}
}
public Context(IClient client, IState target, CancellationToken cancellationToken)
{
Client = client;
Target = target;
CancellationToken = cancellationToken;
}
}
sealed class Visitor : IResourceBaseConfigVisitor<Context, Task>
{
public Task Visit<Model>(ResourceConfig<Model> config, Context context)
where Model : class
{
throw new NotImplementedException();
}
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>
Expand Down Expand Up @@ -32,7 +188,7 @@ sealed class CreateAsyncVisitor : AsyncOperationVisitor
{
public override async Task<object> Visit<Model>(ResourceConfig<Model> config)
{
var target = Target.GetOrNull(config);
var target = Target.Get(config);
if (target == null)
{
return null;
Expand All @@ -44,14 +200,14 @@ public override async Task<object> Visit<Model>(ResourceConfig<Model> config)
CreateOrUpdateAsyncParams.Create(
config.ResourceGroupName,
config.Name,
Target.GetOrNull(config),
Target.Get(config),
CancellationToken));
}
public override async Task<object> Visit<Model, ParentModel>(
NestedResourceConfig<Model, ParentModel> config)
{
var target = Target.GetOrNull(config);
var target = Target.GetNestedResourceModel(config);
if (target == null)
{
return null;
Expand All @@ -72,4 +228,5 @@ public CreateAsyncVisitor(
IState Target { get; }
}
}
*/
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using System.Collections.Generic;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;

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

0 comments on commit 6f8b6b7

Please sign in to comment.