Skip to content

Commit b643730

Browse files
IClient
1 parent e582289 commit b643730

File tree

9 files changed

+230
-22
lines changed

9 files changed

+230
-22
lines changed
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
using Microsoft.Azure.Management.Compute;
2+
using Microsoft.Azure.Management.Compute.Models;
3+
using System;
4+
using System.Threading.Tasks;
5+
6+
namespace Microsoft.Azure.Experiments.Compute
7+
{
8+
public static class ComputePolicy
9+
{
10+
public static ResourcePolicy<IComputeManagementClient, ResourceName, Info> Create<Operations, Info>(
11+
Func<IComputeManagementClient, Operations> getOperations,
12+
Func<Operations, ResourceName, Task<Info>> getAsync,
13+
Func<Operations, ResourceName, Info, Task<Info>> createOrUpdateAsync)
14+
where Info : Resource
15+
=> OperationsPolicy
16+
.Create(getAsync, createOrUpdateAsync)
17+
.Transform(getOperations)
18+
.CreateResourcePolicy(i => i.Location, (i, location) => i.Location = location);
19+
20+
public static ResourcePolicy<IComputeManagementClient, ResourceName, VirtualMachine> VirtualMachine
21+
{ get; }
22+
= Create(
23+
client => client.VirtualMachines,
24+
(operations, name) => operations.GetAsync(name.ResourceGroupName, name.Name),
25+
(operations, name, info)
26+
=> operations.CreateOrUpdateAsync(name.ResourceGroupName, name.Name, info));
27+
}
28+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
namespace Microsoft.Azure.Experiments
2+
{
3+
public interface IClient
4+
{
5+
Context Context { get; }
6+
7+
T GetClient<T>();
8+
}
9+
}

experiments/Azure.Experiments/Azure.Experiments/IResourcePolicy.cs

Lines changed: 0 additions & 22 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 Microsoft.Azure.Management.Network;
2+
using Microsoft.Azure.Management.Network.Models;
3+
using System;
4+
using System.Threading.Tasks;
5+
6+
namespace Microsoft.Azure.Experiments.Network
7+
{
8+
public static class NetworkPolicy
9+
{
10+
public static ResourcePolicy<INetworkManagementClient, ResourceName, Info> Create<Operations, Info>(
11+
Func<INetworkManagementClient, Operations> getOperations,
12+
Func<Operations, ResourceName, Task<Info>> getAsync,
13+
Func<Operations, ResourceName, Info, Task<Info>> createOrUpdateAsync)
14+
where Info : Resource
15+
=> OperationsPolicy
16+
.Create(getAsync, createOrUpdateAsync)
17+
.Transform(getOperations)
18+
.CreateResourcePolicy(i => i.Location, (i, location) => i.Location = location);
19+
20+
public static ResourcePolicy<INetworkManagementClient, ResourceName, NetworkInterface> NetworkInterface
21+
{ get; }
22+
= Create(
23+
client => client.NetworkInterfaces,
24+
(operations, name) => operations.GetAsync(name.ResourceGroupName, name.Name),
25+
(operations, name, info)
26+
=> operations.CreateOrUpdateAsync(name.ResourceGroupName, name.Name, info));
27+
28+
public static ResourcePolicy<INetworkManagementClient, ResourceName, NetworkSecurityGroup> NetworkSecurityGroup
29+
{ get; }
30+
= Create(
31+
client => client.NetworkSecurityGroups,
32+
(operations, name) => operations.GetAsync(name.ResourceGroupName, name.Name),
33+
(operations, name, info)
34+
=> operations.CreateOrUpdateAsync(name.ResourceGroupName, name.Name, info));
35+
36+
public static ResourcePolicy<INetworkManagementClient, ResourceName, PublicIPAddress> PublicIPAddresss
37+
{ get; }
38+
= Create(
39+
client => client.PublicIPAddresses,
40+
(operations, name) => operations.GetAsync(name.ResourceGroupName, name.Name),
41+
(operations, name, info)
42+
=> operations.CreateOrUpdateAsync(name.ResourceGroupName, name.Name, info));
43+
44+
public static ResourcePolicy<INetworkManagementClient, ResourceName, VirtualNetwork> VirtualNetwork
45+
{ get; }
46+
= Create(
47+
client => client.VirtualNetworks,
48+
(operations, name) => operations.GetAsync(name.ResourceGroupName, name.Name),
49+
(operations, name, info)
50+
=> operations.CreateOrUpdateAsync(name.ResourceGroupName, name.Name, info));
51+
}
52+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
using System;
2+
using System.Threading.Tasks;
3+
4+
namespace Microsoft.Azure.Experiments
5+
{
6+
public static class OperationsPolicy
7+
{
8+
public static OperationsPolicy<Client, Name, Info> Create<Client, Name, Info>(
9+
Func<Client, Name, Task<Info>> getAsync,
10+
Func<Client, Name, Info, Task<Info>> createOrUpdateAsync)
11+
=> new OperationsPolicy<Client, Name, Info>(getAsync, createOrUpdateAsync);
12+
}
13+
14+
public sealed class OperationsPolicy<Client, Name, Info>
15+
{
16+
public Func<Client, Name, Task<Info>> GetAsync { get; }
17+
18+
public Func<Client, Name, Info, Task<Info>> CreateOrUpdateAsync { get; }
19+
20+
public OperationsPolicy(
21+
Func<Client, Name, Task<Info>> getAsync,
22+
Func<Client, Name, Info, Task<Info>> createOrUpdateAsync)
23+
{
24+
GetAsync = getAsync;
25+
CreateOrUpdateAsync = createOrUpdateAsync;
26+
}
27+
28+
public OperationsPolicy<NewClient, Name, Info> Transform<NewClient>(Func<NewClient, Client> get)
29+
=> OperationsPolicy.Create<NewClient, Name, Info>(
30+
(client, name) => GetAsync(get(client), name),
31+
(client, name, info) => CreateOrUpdateAsync(get(client), name, info));
32+
}
33+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
using System.Collections.Generic;
2+
3+
namespace Microsoft.Azure.Experiments
4+
{
5+
public interface IResourceConfig
6+
{
7+
}
8+
9+
public static class ResourceConfig
10+
{
11+
public static ResourceConfig<Client, Name, Info> CreateResourceConfig<Client, Name, Info>(
12+
this ResourcePolicy<Client, Name, Info> policy,
13+
Name name,
14+
Info info,
15+
IEnumerable<IResourceConfig> dependencies)
16+
where Info : class
17+
=> new ResourceConfig<Client, Name, Info>(policy, name, info, dependencies);
18+
}
19+
20+
public sealed class ResourceConfig<Client, TName, TInfo>
21+
where TInfo : class
22+
{
23+
public ResourcePolicy<Client, TName, TInfo> Policy { get; }
24+
25+
public TName Name { get; }
26+
27+
public TInfo Info { get; }
28+
29+
public IEnumerable<IResourceConfig> Dependencies { get; }
30+
31+
public ResourceConfig(
32+
ResourcePolicy<Client, TName, TInfo> policy,
33+
TName name,
34+
TInfo info,
35+
IEnumerable<IResourceConfig> dependencies)
36+
{
37+
Policy = policy;
38+
Name = name;
39+
Info = info;
40+
Dependencies = dependencies;
41+
}
42+
}
43+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
using Microsoft.Azure.Management.ResourceManager;
2+
using Microsoft.Azure.Management.ResourceManager.Models;
3+
4+
namespace Microsoft.Azure.Experiments.ResourceManager
5+
{
6+
public static class ResourceManagerPolicy
7+
{
8+
public static ResourcePolicy<IResourceManagementClient, string, ResourceGroup> ResourceGroup
9+
{ get; }
10+
= OperationsPolicy
11+
.Create<IResourceGroupsOperations, string, ResourceGroup>(
12+
(operations, name) => operations.GetAsync(name),
13+
(operations, name, info) => operations.CreateOrUpdateAsync(name, info))
14+
.Transform<IResourceManagementClient>(r => r.ResourceGroups)
15+
.CreateResourcePolicy(i => i.Location, (i, location) => i.Location = location);
16+
}
17+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
namespace Microsoft.Azure.Experiments
2+
{
3+
public sealed class ResourceName
4+
{
5+
public string ResourceGroupName { get; }
6+
public string Name { get; }
7+
8+
public ResourceName(string resourceGroupName, string name)
9+
{
10+
ResourceGroupName = resourceGroupName;
11+
Name = name;
12+
}
13+
}
14+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
using System;
2+
3+
namespace Microsoft.Azure.Experiments
4+
{
5+
public static class ResourcePolicy
6+
{
7+
public static ResourcePolicy<Client, Name, Info> CreateResourcePolicy<Client, Name, Info>(
8+
this OperationsPolicy<Client, Name, Info> operationsPolicy,
9+
Func<Info, string> getLocation,
10+
Action<Info, string> setLocation)
11+
where Info : class
12+
=> new ResourcePolicy<Client, Name, Info>(operationsPolicy, getLocation, setLocation);
13+
}
14+
15+
public sealed class ResourcePolicy<Client, Name, Info>
16+
where Info : class
17+
{
18+
public OperationsPolicy<Client, Name, Info> OperationsPolicy { get; }
19+
20+
public Func<Info, string> GetLocation { get; }
21+
22+
public Action<Info, string> SetLocation { get; }
23+
24+
public ResourcePolicy(
25+
OperationsPolicy<Client, Name, Info> operationsPolicy,
26+
Func<Info, string> getLocation,
27+
Action<Info, string> setLocation)
28+
{
29+
OperationsPolicy = operationsPolicy;
30+
GetLocation = getLocation;
31+
SetLocation = setLocation;
32+
}
33+
}
34+
}

0 commit comments

Comments
 (0)