forked from Azure/azure-powershell
-
Notifications
You must be signed in to change notification settings - Fork 2
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
74ff467
commit 95173ef
Showing
20 changed files
with
411 additions
and
147 deletions.
There are no files selected for viewing
3 changes: 2 additions & 1 deletion
3
experiments/Azure.Experiments/Azure.Experiments/Compute/ComputePolicy.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
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
50 changes: 0 additions & 50 deletions
50
experiments/Azure.Experiments/Azure.Experiments/CreateOperation.cs
This file was deleted.
Oops, something went wrong.
52 changes: 52 additions & 0 deletions
52
experiments/Azure.Experiments/Azure.Experiments/CreateParameters.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,52 @@ | ||
using System.Collections.Concurrent; | ||
|
||
namespace Microsoft.Azure.Experiments | ||
{ | ||
public static class Parameters | ||
{ | ||
public static IState GetParameters<Config>( | ||
string subscription, string location, IResourceConfig<Config> config) | ||
where Config : class | ||
{ | ||
var visitor = new Visitor(subscription, location); | ||
visitor.Get(config); | ||
return visitor.Result; | ||
} | ||
|
||
sealed class Visitor : IResourceConfigVisitor<object> | ||
{ | ||
public Visitor(string subscription, string location) | ||
{ | ||
Subscription = subscription; | ||
Location = location; | ||
} | ||
|
||
public Config Get<Config>(IResourceConfig<Config> config) | ||
where Config : class | ||
=> Result.GetOrAdd(config, () => config.Apply(this) as Config); | ||
|
||
public object Visit<Config>(ResourceConfig<Config> config) where Config : class | ||
{ | ||
var p = config.CreateConfig(Subscription); | ||
config.Policy.SetLocation(p, Location); | ||
return p; | ||
} | ||
|
||
public object Visit<Config, ParentConfig>( | ||
NestedResourceConfig<Config, ParentConfig> config) | ||
where Config : class | ||
where ParentConfig : class | ||
{ | ||
var result = config.Create(); | ||
config.Policy.Set(Get(config.Parent), result); | ||
return result; | ||
} | ||
|
||
string Subscription { get; } | ||
|
||
string Location { get; } | ||
|
||
public State Result { get; } = new State(); | ||
} | ||
} | ||
} |
87 changes: 87 additions & 0 deletions
87
experiments/Azure.Experiments/Azure.Experiments/CurrentState.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,87 @@ | ||
using Microsoft.Rest.Azure; | ||
using System.Collections.Concurrent; | ||
using System.Linq; | ||
using System.Net; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
|
||
namespace Microsoft.Azure.Experiments | ||
{ | ||
public static class CurrentState | ||
{ | ||
public static async Task<IState> GetState<Config>( | ||
IClient client, IResourceConfig<Config> resourceConfig) | ||
where Config : class | ||
{ | ||
var visitor = new Visitor(client); | ||
await visitor.GetOrAdd(resourceConfig); | ||
return visitor.Result; | ||
} | ||
|
||
sealed class Visitor : IResourceConfigVisitor<Task<object>> | ||
{ | ||
public async Task<object> GetOrAddUntyped(IResourceConfig config) | ||
=> await Map.GetOrAdd( | ||
config, | ||
async _ => | ||
{ | ||
var info = await config.Apply(this); | ||
if (info != null) | ||
{ | ||
Result.GetOrAddUntyped(config, () => info); | ||
} | ||
return info; | ||
}); | ||
|
||
public async Task<Config> GetOrAdd<Config>(IResourceConfig<Config> config) | ||
where Config : class | ||
{ | ||
var result = await GetOrAddUntyped(config); | ||
return result as Config; | ||
} | ||
|
||
public async Task<object> Visit<Config>(ResourceConfig<Config> config) | ||
where Config : class | ||
{ | ||
Config info; | ||
try | ||
{ | ||
info = await config.Policy.GetAsync(GetAsyncParams.Create( | ||
Client, config.ResourceGroupName, config.Name, new CancellationToken())); | ||
} | ||
catch (CloudException e) when (e.Response.StatusCode == HttpStatusCode.NotFound) | ||
{ | ||
info = null; | ||
} | ||
if (info == null) | ||
{ | ||
var tasks = config.Dependencies.Select(GetOrAddUntyped); | ||
await Task.WhenAll(tasks); | ||
return null; | ||
} | ||
return info; | ||
} | ||
|
||
public async Task<object> Visit<Config, ParentConfig>( | ||
NestedResourceConfig<Config, ParentConfig> config) | ||
where Config : class | ||
where ParentConfig : class | ||
{ | ||
var parent = await GetOrAdd(config.Parent); | ||
return parent == null ? null : config.Policy.Get(parent); | ||
} | ||
|
||
public Visitor(IClient client) | ||
{ | ||
Client = client; | ||
} | ||
|
||
public State Result { get; } = new State(); | ||
|
||
IClient Client { get; } | ||
|
||
ConcurrentDictionary<IResourceConfig, Task<object>> Map { get; } | ||
= new ConcurrentDictionary<IResourceConfig, Task<object>>(); | ||
} | ||
} | ||
} |
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
20 changes: 18 additions & 2 deletions
20
experiments/Azure.Experiments/Azure.Experiments/NestedResourceConfig.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 |
---|---|---|
@@ -1,21 +1,37 @@ | ||
namespace Microsoft.Azure.Experiments | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
|
||
namespace Microsoft.Azure.Experiments | ||
{ | ||
public sealed class NestedResourceConfig<Config, ParentConfig> : IResourceConfig<Config> | ||
where Config : class | ||
where ParentConfig : class | ||
{ | ||
public NestedResourcePolicy<Config, ParentConfig> Policy { get; } | ||
|
||
public string Name { get; } | ||
|
||
public IResourceConfig<ParentConfig> Parent { get; } | ||
|
||
public Func<Config> Create { get; } | ||
|
||
public NestedResourceConfig( | ||
NestedResourcePolicy<Config, ParentConfig> policy, IResourceConfig<ParentConfig> parent) | ||
NestedResourcePolicy<Config, ParentConfig> policy, | ||
IResourceConfig<ParentConfig> parent, | ||
string name, | ||
Func<Config> create) | ||
{ | ||
Policy = policy; | ||
Name = name; | ||
Parent = parent; | ||
Create = create; | ||
} | ||
|
||
public Result Apply<Result>(IResourceConfigVisitor<Result> visitor) | ||
=> visitor.Visit(this); | ||
|
||
public IEnumerable<string> GetId(string subscription) | ||
=> Parent.GetId(subscription).Concat(Policy.GetId(Name)); | ||
} | ||
} |
8 changes: 7 additions & 1 deletion
8
experiments/Azure.Experiments/Azure.Experiments/NestedResourcePolicy.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
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
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
Oops, something went wrong.