Skip to content

Commit 74ff467

Browse files
CreaOperation, StateLocation
1 parent b18f0d1 commit 74ff467

File tree

2 files changed

+124
-0
lines changed

2 files changed

+124
-0
lines changed
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
using System.Collections.Concurrent;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
5+
namespace Microsoft.Azure.Experiments
6+
{
7+
public sealed class CreateOperation
8+
{
9+
public IEnumerable<CreateOperation> Dependencies { get; }
10+
11+
public CreateOperation(IEnumerable<CreateOperation> dependencies)
12+
{
13+
Dependencies = dependencies;
14+
}
15+
16+
public static CreateOperation Create<Config>(IState state, IResourceConfig<Config> config)
17+
where Config : class
18+
=> new Visitor(state).Get(config);
19+
20+
sealed class Visitor : IResourceConfigVisitor<CreateOperation>
21+
{
22+
public CreateOperation Get(IResourceConfig config)
23+
=> Map.GetOrAdd(config, _ => config.Apply(this));
24+
25+
public CreateOperation Visit<Config>(ResourceConfig<Config> config) where Config : class
26+
{
27+
var info = State.Get(config);
28+
return info == null
29+
? new CreateOperation(config.Dependencies.Select(d => Get(d)))
30+
: null;
31+
}
32+
33+
public CreateOperation Visit<Config, ParentConfig>(
34+
NestedResourceConfig<Config, ParentConfig> config)
35+
where Config : class
36+
where ParentConfig : class
37+
=> Get(config.Parent);
38+
39+
public Visitor(IState state)
40+
{
41+
State = state;
42+
}
43+
44+
IState State { get; }
45+
46+
ConcurrentDictionary<IResourceConfig, CreateOperation> Map { get; }
47+
= new ConcurrentDictionary<IResourceConfig, CreateOperation>();
48+
}
49+
}
50+
}
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
using Microsoft.Azure.Management.ResourceManager.Models;
2+
using System.Linq;
3+
4+
namespace Microsoft.Azure.Experiments
5+
{
6+
public static class StateLocation
7+
{
8+
public static string GetLocation(this IState state, IResourceConfig config)
9+
=> config.Apply(new Visitor(state))?.Location;
10+
11+
class DependencyLocation
12+
{
13+
public string Location { get; }
14+
15+
public bool IsCompulsory { get; }
16+
17+
public DependencyLocation(string location, bool isCompulsory)
18+
{
19+
Location = location;
20+
IsCompulsory = isCompulsory;
21+
}
22+
}
23+
24+
static DependencyLocation Merge(this DependencyLocation a, DependencyLocation b)
25+
{
26+
if (a == null)
27+
{
28+
return b;
29+
}
30+
if (b == null)
31+
{
32+
return a;
33+
}
34+
35+
if (a.IsCompulsory != b.IsCompulsory)
36+
{
37+
return a.IsCompulsory ? b : a;
38+
}
39+
40+
// a.IsCompulsory == b.IsCompulsory
41+
return a.Location == b.Location ? a : new DependencyLocation(null, a.IsCompulsory);
42+
}
43+
44+
sealed class Visitor : IResourceConfigVisitor<DependencyLocation>
45+
{
46+
public DependencyLocation Visit<Config>(ResourceConfig<Config> config)
47+
where Config : class
48+
{
49+
var info = State.Get(config);
50+
return info != null
51+
? new DependencyLocation(
52+
config.Policy.GetLocation(info),
53+
typeof(Config) != typeof(ResourceGroup))
54+
: config
55+
.Dependencies
56+
.Select(c => c.Apply(this))
57+
.Aggregate((DependencyLocation)null, Merge);
58+
}
59+
60+
public DependencyLocation Visit<Config, ParentConfig>(
61+
NestedResourceConfig<Config, ParentConfig> config)
62+
where Config : class
63+
where ParentConfig : class
64+
=> config.Parent.Apply(this);
65+
66+
public Visitor(IState state)
67+
{
68+
State = state;
69+
}
70+
71+
IState State { get; }
72+
}
73+
}
74+
}

0 commit comments

Comments
 (0)