Skip to content

Commit 95173ef

Browse files
ResourceOperations
1 parent 74ff467 commit 95173ef

20 files changed

+411
-147
lines changed

experiments/Azure.Experiments/Azure.Experiments/Compute/ComputePolicy.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,20 @@
11
using Microsoft.Azure.Management.Compute;
22
using Microsoft.Azure.Management.Compute.Models;
33
using System;
4-
using System.Threading;
54
using System.Threading.Tasks;
65

76
namespace Microsoft.Azure.Experiments.Compute
87
{
98
public static class ComputePolicy
109
{
1110
public static ResourcePolicy<Config> Create<Config, Operations>(
11+
string header,
1212
Func<IComputeManagementClient, Operations> getOperations,
1313
Func<GetAsyncParams<Operations>, Task<Config>> getAsync,
1414
Func<CreateOrUpdateAsyncParams<Operations, Config>, Task<Config>> createOrUpdateAsync)
1515
where Config : Resource
1616
=> ResourcePolicy.Create(
17+
new[] { "Microsoft.Compute", header },
1718
getOperations,
1819
getAsync,
1920
createOrUpdateAsync,

experiments/Azure.Experiments/Azure.Experiments/Compute/VirtualMachinePolicy.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ public static class VirtualMachinePolicy
88
{
99
public static ResourcePolicy<VirtualMachine> Policy { get; }
1010
= ComputePolicy.Create(
11+
"virtualMachines",
1112
client => client.VirtualMachines,
1213
p => p.Operations.GetAsync(
1314
p.ResourceGroupName, p.Name, null, p.CancellationToken),

experiments/Azure.Experiments/Azure.Experiments/CreateOperation.cs

Lines changed: 0 additions & 50 deletions
This file was deleted.
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
using System.Collections.Concurrent;
2+
3+
namespace Microsoft.Azure.Experiments
4+
{
5+
public static class Parameters
6+
{
7+
public static IState GetParameters<Config>(
8+
string subscription, string location, IResourceConfig<Config> config)
9+
where Config : class
10+
{
11+
var visitor = new Visitor(subscription, location);
12+
visitor.Get(config);
13+
return visitor.Result;
14+
}
15+
16+
sealed class Visitor : IResourceConfigVisitor<object>
17+
{
18+
public Visitor(string subscription, string location)
19+
{
20+
Subscription = subscription;
21+
Location = location;
22+
}
23+
24+
public Config Get<Config>(IResourceConfig<Config> config)
25+
where Config : class
26+
=> Result.GetOrAdd(config, () => config.Apply(this) as Config);
27+
28+
public object Visit<Config>(ResourceConfig<Config> config) where Config : class
29+
{
30+
var p = config.CreateConfig(Subscription);
31+
config.Policy.SetLocation(p, Location);
32+
return p;
33+
}
34+
35+
public object Visit<Config, ParentConfig>(
36+
NestedResourceConfig<Config, ParentConfig> config)
37+
where Config : class
38+
where ParentConfig : class
39+
{
40+
var result = config.Create();
41+
config.Policy.Set(Get(config.Parent), result);
42+
return result;
43+
}
44+
45+
string Subscription { get; }
46+
47+
string Location { get; }
48+
49+
public State Result { get; } = new State();
50+
}
51+
}
52+
}
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
using Microsoft.Rest.Azure;
2+
using System.Collections.Concurrent;
3+
using System.Linq;
4+
using System.Net;
5+
using System.Threading;
6+
using System.Threading.Tasks;
7+
8+
namespace Microsoft.Azure.Experiments
9+
{
10+
public static class CurrentState
11+
{
12+
public static async Task<IState> GetState<Config>(
13+
IClient client, IResourceConfig<Config> resourceConfig)
14+
where Config : class
15+
{
16+
var visitor = new Visitor(client);
17+
await visitor.GetOrAdd(resourceConfig);
18+
return visitor.Result;
19+
}
20+
21+
sealed class Visitor : IResourceConfigVisitor<Task<object>>
22+
{
23+
public async Task<object> GetOrAddUntyped(IResourceConfig config)
24+
=> await Map.GetOrAdd(
25+
config,
26+
async _ =>
27+
{
28+
var info = await config.Apply(this);
29+
if (info != null)
30+
{
31+
Result.GetOrAddUntyped(config, () => info);
32+
}
33+
return info;
34+
});
35+
36+
public async Task<Config> GetOrAdd<Config>(IResourceConfig<Config> config)
37+
where Config : class
38+
{
39+
var result = await GetOrAddUntyped(config);
40+
return result as Config;
41+
}
42+
43+
public async Task<object> Visit<Config>(ResourceConfig<Config> config)
44+
where Config : class
45+
{
46+
Config info;
47+
try
48+
{
49+
info = await config.Policy.GetAsync(GetAsyncParams.Create(
50+
Client, config.ResourceGroupName, config.Name, new CancellationToken()));
51+
}
52+
catch (CloudException e) when (e.Response.StatusCode == HttpStatusCode.NotFound)
53+
{
54+
info = null;
55+
}
56+
if (info == null)
57+
{
58+
var tasks = config.Dependencies.Select(GetOrAddUntyped);
59+
await Task.WhenAll(tasks);
60+
return null;
61+
}
62+
return info;
63+
}
64+
65+
public async Task<object> Visit<Config, ParentConfig>(
66+
NestedResourceConfig<Config, ParentConfig> config)
67+
where Config : class
68+
where ParentConfig : class
69+
{
70+
var parent = await GetOrAdd(config.Parent);
71+
return parent == null ? null : config.Policy.Get(parent);
72+
}
73+
74+
public Visitor(IClient client)
75+
{
76+
Client = client;
77+
}
78+
79+
public State Result { get; } = new State();
80+
81+
IClient Client { get; }
82+
83+
ConcurrentDictionary<IResourceConfig, Task<object>> Map { get; }
84+
= new ConcurrentDictionary<IResourceConfig, Task<object>>();
85+
}
86+
}
87+
}

experiments/Azure.Experiments/Azure.Experiments/IResourceConfig.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
1-
namespace Microsoft.Azure.Experiments
1+
using System.Collections.Generic;
2+
3+
namespace Microsoft.Azure.Experiments
24
{
35
public interface IResourceConfigVisitor<Result>
46
{
57
Result Visit<Config>(ResourceConfig<Config> config)
68
where Config : class;
9+
710
Result Visit<Config, ParentConfig>(NestedResourceConfig<Config, ParentConfig> config)
811
where Config : class
912
where ParentConfig : class;
@@ -12,6 +15,8 @@ Result Visit<Config, ParentConfig>(NestedResourceConfig<Config, ParentConfig> co
1215
public interface IResourceConfig
1316
{
1417
Result Apply<Result>(IResourceConfigVisitor<Result> visitor);
18+
19+
IEnumerable<string> GetId(string subscription);
1520
}
1621

1722
public interface IResourceConfig<Config> : IResourceConfig

experiments/Azure.Experiments/Azure.Experiments/IState.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
{
33
public interface IState
44
{
5-
Config Get<Config>(IResourceConfig<Config> config)
5+
Config GetOrNull<Config>(IResourceConfig<Config> config)
66
where Config : class;
77
}
88
}
Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,37 @@
1-
namespace Microsoft.Azure.Experiments
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
5+
namespace Microsoft.Azure.Experiments
26
{
37
public sealed class NestedResourceConfig<Config, ParentConfig> : IResourceConfig<Config>
48
where Config : class
59
where ParentConfig : class
610
{
711
public NestedResourcePolicy<Config, ParentConfig> Policy { get; }
812

13+
public string Name { get; }
14+
915
public IResourceConfig<ParentConfig> Parent { get; }
1016

17+
public Func<Config> Create { get; }
18+
1119
public NestedResourceConfig(
12-
NestedResourcePolicy<Config, ParentConfig> policy, IResourceConfig<ParentConfig> parent)
20+
NestedResourcePolicy<Config, ParentConfig> policy,
21+
IResourceConfig<ParentConfig> parent,
22+
string name,
23+
Func<Config> create)
1324
{
1425
Policy = policy;
26+
Name = name;
1527
Parent = parent;
28+
Create = create;
1629
}
1730

1831
public Result Apply<Result>(IResourceConfigVisitor<Result> visitor)
1932
=> visitor.Visit(this);
33+
34+
public IEnumerable<string> GetId(string subscription)
35+
=> Parent.GetId(subscription).Concat(Policy.GetId(Name));
2036
}
2137
}

experiments/Azure.Experiments/Azure.Experiments/NestedResourcePolicy.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,22 @@
11
using System;
2+
using System.Collections.Generic;
23

34
namespace Microsoft.Azure.Experiments
45
{
56
public sealed class NestedResourcePolicy<Config, ParentConfig> : IResourcePolicy
67
{
8+
public Func<string, IEnumerable<string>> GetId { get; }
9+
710
public Func<ParentConfig, Config> Get { get; }
811

912
public Action<ParentConfig, Config> Set { get; }
1013

1114
public NestedResourcePolicy(
12-
Func<ParentConfig, Config> get, Action<ParentConfig, Config> set)
15+
Func<string, IEnumerable<string>> getId,
16+
Func<ParentConfig, Config> get,
17+
Action<ParentConfig, Config> set)
1318
{
19+
GetId = getId;
1420
Get = get;
1521
Set = set;
1622
}

experiments/Azure.Experiments/Azure.Experiments/Network/NetworkInterfacePolicy.cs

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,41 @@ public static class NetworkInterfacePolicy
88
{
99
public static ResourcePolicy<NetworkInterface> Policy { get; }
1010
= NetworkPolicy.Create(
11+
"networkInterfaces",
1112
client => client.NetworkInterfaces,
1213
p => p.Operations.GetAsync(
1314
p.ResourceGroupName, p.Name, null, p.CancellationToken),
1415
p => p.Operations.CreateOrUpdateAsync(
1516
p.ResourceGroupName, p.Name, p.Config, p.CancellationToken));
1617

1718
public static ResourceConfig<NetworkInterface> CreateNetworkSecurityGroupConfig(
18-
this ResourceConfig<ResourceGroup> resourceGroup, string name)
19-
=> Policy.CreateConfig(resourceGroup, name);
19+
this ResourceConfig<ResourceGroup> resourceGroup,
20+
string name,
21+
ResourceConfig<Subnet> subnet,
22+
ResourceConfig<PublicIPAddress> publicIPAddress,
23+
ResourceConfig<NetworkSecurityGroup> networkSecurityGroup)
24+
=> Policy.CreateConfig(
25+
resourceGroup,
26+
name,
27+
subscription => new NetworkInterface
28+
{
29+
IpConfigurations = new []
30+
{
31+
new NetworkInterfaceIPConfiguration
32+
{
33+
Name = name,
34+
Subnet = new Subnet { Id = subnet.GetId(subscription).IdToString() },
35+
PublicIPAddress = new PublicIPAddress
36+
{
37+
Id = publicIPAddress.GetId(subscription).IdToString()
38+
}
39+
}
40+
},
41+
NetworkSecurityGroup = new NetworkSecurityGroup
42+
{
43+
Id = networkSecurityGroup.GetId(subscription).IdToString()
44+
}
45+
},
46+
new IResourceConfig[] { subnet, publicIPAddress, networkSecurityGroup });
2047
}
2148
}

experiments/Azure.Experiments/Azure.Experiments/Network/NetworkPolicy.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,13 @@ namespace Microsoft.Azure.Experiments.Network
88
public static class NetworkPolicy
99
{
1010
public static ResourcePolicy<Config> Create<Config, Operations>(
11+
string header,
1112
Func<INetworkManagementClient, Operations> getOperations,
1213
Func<GetAsyncParams<Operations>, Task<Config>> getAsync,
1314
Func<CreateOrUpdateAsyncParams<Operations, Config>, Task<Config>> createOrUpdateAsync)
1415
where Config : Resource
1516
=> ResourcePolicy.Create(
17+
new [] { "Microsoft.Network", header },
1618
getOperations,
1719
getAsync,
1820
createOrUpdateAsync,

experiments/Azure.Experiments/Azure.Experiments/Network/NetworkSecurityGroupPolicy.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ public static class NetworkSecurityGroupPolicy
88
{
99
public static ResourcePolicy<NetworkSecurityGroup> Policy { get; }
1010
= NetworkPolicy.Create(
11+
"networkSecurityGroups",
1112
client => client.NetworkSecurityGroups,
1213
p => p.Operations.GetAsync(
1314
p.ResourceGroupName, p.Name, null, p.CancellationToken),

experiments/Azure.Experiments/Azure.Experiments/Network/PublicIPAddressPolicy.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ public static class PublicIPAddressPolicy
88
{
99
public static ResourcePolicy<PublicIPAddress> Policy { get; }
1010
= NetworkPolicy.Create(
11+
"publicIPAddresses",
1112
client => client.PublicIPAddresses,
1213
p => p.Operations.GetAsync(
1314
p.ResourceGroupName, p.Name, null, p.CancellationToken),

experiments/Azure.Experiments/Azure.Experiments/Network/VirtualNetworkPolicy.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ public static class VirtualNetworkPolicy
88
{
99
public static ResourcePolicy<VirtualNetwork> Policy { get; }
1010
= NetworkPolicy.Create(
11+
"virtualNetworks",
1112
client => client.VirtualNetworks,
1213
p => p.Operations.GetAsync(
1314
p.ResourceGroupName, p.Name, null, p.CancellationToken),

0 commit comments

Comments
 (0)