forked from Azure/azure-powershell
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
013ac18
commit 186926e
Showing
26 changed files
with
409 additions
and
356 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
83 changes: 83 additions & 0 deletions
83
src/ResourceManager/Common/Commands.Common.Strategies/CreateOrUpdateAsyncExtensions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
using System.Linq; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
|
||
namespace Microsoft.Azure.Commands.Common.Strategies | ||
{ | ||
public static class CreateOrUpdateAsyncExtensions | ||
{ | ||
public static async Task<IState> CreateOrUpdateAsync<TModel>( | ||
this ResourceConfig<TModel> config, | ||
IClient client, | ||
IState target, | ||
CancellationToken cancellationToken) | ||
where TModel : class | ||
{ | ||
var context = new Context(new AsyncOperationContext(client, cancellationToken), target); | ||
await context.CreateOrUpdateAsync(config); | ||
return context.Result; | ||
} | ||
|
||
sealed class Context | ||
{ | ||
public IState Result => _OperationContext.Result; | ||
|
||
readonly AsyncOperationContext _OperationContext; | ||
|
||
readonly IState _Target; | ||
|
||
public Context(AsyncOperationContext operationContext, IState target) | ||
{ | ||
_OperationContext = operationContext; | ||
_Target = target; | ||
} | ||
|
||
public async Task CreateOrUpdateAsync<TModel>(ResourceConfig<TModel> config) | ||
where TModel : class | ||
{ | ||
var model = _Target.Get(config); | ||
if (model != null) | ||
{ | ||
await _OperationContext.GetOrAddAsync( | ||
config, | ||
async () => | ||
{ | ||
// wait for all dependencies | ||
var tasks = config.Dependencies.Select(CreateOrUpdateAsyncDispatch); | ||
await Task.WhenAll(tasks); | ||
// call the main function. | ||
return await config.Strategy.CreateOrUpdateAsync( | ||
_OperationContext.Client, | ||
CreateOrUpdateAsyncParams.Create( | ||
config.ResourceGroupName, | ||
config.Name, | ||
model, | ||
_OperationContext.CancellationToken)); | ||
}); | ||
} | ||
} | ||
|
||
public Task CreateOrUpdateAsync<TModel, TParentModel>( | ||
NestedResourceConfig<TModel, TParentModel> config) | ||
where TModel : class | ||
where TParentModel : class | ||
=> CreateOrUpdateAsyncDispatch(config.Parent); | ||
|
||
public Task CreateOrUpdateAsyncDispatch(IEntityConfig config) | ||
=> config.Accept(new CreateOrUpdateAsyncVisitor(), this); | ||
} | ||
|
||
sealed class CreateOrUpdateAsyncVisitor : IEntityConfigVisitor<Context, Task> | ||
{ | ||
public Task Visit<TModel>(ResourceConfig<TModel> config, Context context) | ||
where TModel : class | ||
=> context.CreateOrUpdateAsync(config); | ||
|
||
public Task Visit<TModel, TParentModel>( | ||
NestedResourceConfig<TModel, TParentModel> config, Context context) | ||
where TModel : class | ||
where TParentModel : class | ||
=> context.CreateOrUpdateAsync(config); | ||
} | ||
} | ||
} |
80 changes: 0 additions & 80 deletions
80
src/ResourceManager/Common/Commands.Common.Strategies/CreateOrUpdateAsyncOperation.cs
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
13 changes: 13 additions & 0 deletions
13
src/ResourceManager/Common/Commands.Common.Strategies/EntityConfigExtensions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
using System.Collections.Generic; | ||
|
||
namespace Microsoft.Azure.Commands.Common.Strategies | ||
{ | ||
public static class EntityConfigExtensions | ||
{ | ||
public static string IdToString(this IEnumerable<string> id) | ||
=> "/" + string.Join("/", id); | ||
|
||
public static string DefaultIdStr(this IEntityConfig config) | ||
=> config.GetId(string.Empty).IdToString(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.