Skip to content

Commit

Permalink
IClient
Browse files Browse the repository at this point in the history
  • Loading branch information
sergey-shandar committed Nov 9, 2017
1 parent e582289 commit b643730
Show file tree
Hide file tree
Showing 9 changed files with 230 additions and 22 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
using Microsoft.Azure.Management.Compute;
using Microsoft.Azure.Management.Compute.Models;
using System;
using System.Threading.Tasks;

namespace Microsoft.Azure.Experiments.Compute
{
public static class ComputePolicy
{
public static ResourcePolicy<IComputeManagementClient, ResourceName, Info> Create<Operations, Info>(
Func<IComputeManagementClient, Operations> getOperations,
Func<Operations, ResourceName, Task<Info>> getAsync,
Func<Operations, ResourceName, Info, Task<Info>> createOrUpdateAsync)
where Info : Resource
=> OperationsPolicy
.Create(getAsync, createOrUpdateAsync)
.Transform(getOperations)
.CreateResourcePolicy(i => i.Location, (i, location) => i.Location = location);

public static ResourcePolicy<IComputeManagementClient, ResourceName, VirtualMachine> VirtualMachine
{ get; }
= Create(
client => client.VirtualMachines,
(operations, name) => operations.GetAsync(name.ResourceGroupName, name.Name),
(operations, name, info)
=> operations.CreateOrUpdateAsync(name.ResourceGroupName, name.Name, info));
}
}
9 changes: 9 additions & 0 deletions experiments/Azure.Experiments/Azure.Experiments/IClient.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace Microsoft.Azure.Experiments
{
public interface IClient
{
Context Context { get; }

T GetClient<T>();
}
}
22 changes: 0 additions & 22 deletions experiments/Azure.Experiments/Azure.Experiments/IResourcePolicy.cs

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
using Microsoft.Azure.Management.Network;
using Microsoft.Azure.Management.Network.Models;
using System;
using System.Threading.Tasks;

namespace Microsoft.Azure.Experiments.Network
{
public static class NetworkPolicy
{
public static ResourcePolicy<INetworkManagementClient, ResourceName, Info> Create<Operations, Info>(
Func<INetworkManagementClient, Operations> getOperations,
Func<Operations, ResourceName, Task<Info>> getAsync,
Func<Operations, ResourceName, Info, Task<Info>> createOrUpdateAsync)
where Info : Resource
=> OperationsPolicy
.Create(getAsync, createOrUpdateAsync)
.Transform(getOperations)
.CreateResourcePolicy(i => i.Location, (i, location) => i.Location = location);

public static ResourcePolicy<INetworkManagementClient, ResourceName, NetworkInterface> NetworkInterface
{ get; }
= Create(
client => client.NetworkInterfaces,
(operations, name) => operations.GetAsync(name.ResourceGroupName, name.Name),
(operations, name, info)
=> operations.CreateOrUpdateAsync(name.ResourceGroupName, name.Name, info));

public static ResourcePolicy<INetworkManagementClient, ResourceName, NetworkSecurityGroup> NetworkSecurityGroup
{ get; }
= Create(
client => client.NetworkSecurityGroups,
(operations, name) => operations.GetAsync(name.ResourceGroupName, name.Name),
(operations, name, info)
=> operations.CreateOrUpdateAsync(name.ResourceGroupName, name.Name, info));

public static ResourcePolicy<INetworkManagementClient, ResourceName, PublicIPAddress> PublicIPAddresss
{ get; }
= Create(
client => client.PublicIPAddresses,
(operations, name) => operations.GetAsync(name.ResourceGroupName, name.Name),
(operations, name, info)
=> operations.CreateOrUpdateAsync(name.ResourceGroupName, name.Name, info));

public static ResourcePolicy<INetworkManagementClient, ResourceName, VirtualNetwork> VirtualNetwork
{ get; }
= Create(
client => client.VirtualNetworks,
(operations, name) => operations.GetAsync(name.ResourceGroupName, name.Name),
(operations, name, info)
=> operations.CreateOrUpdateAsync(name.ResourceGroupName, name.Name, info));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
using System;
using System.Threading.Tasks;

namespace Microsoft.Azure.Experiments
{
public static class OperationsPolicy
{
public static OperationsPolicy<Client, Name, Info> Create<Client, Name, Info>(
Func<Client, Name, Task<Info>> getAsync,
Func<Client, Name, Info, Task<Info>> createOrUpdateAsync)
=> new OperationsPolicy<Client, Name, Info>(getAsync, createOrUpdateAsync);
}

public sealed class OperationsPolicy<Client, Name, Info>
{
public Func<Client, Name, Task<Info>> GetAsync { get; }

public Func<Client, Name, Info, Task<Info>> CreateOrUpdateAsync { get; }

public OperationsPolicy(
Func<Client, Name, Task<Info>> getAsync,
Func<Client, Name, Info, Task<Info>> createOrUpdateAsync)
{
GetAsync = getAsync;
CreateOrUpdateAsync = createOrUpdateAsync;
}

public OperationsPolicy<NewClient, Name, Info> Transform<NewClient>(Func<NewClient, Client> get)
=> OperationsPolicy.Create<NewClient, Name, Info>(
(client, name) => GetAsync(get(client), name),
(client, name, info) => CreateOrUpdateAsync(get(client), name, info));
}
}
43 changes: 43 additions & 0 deletions experiments/Azure.Experiments/Azure.Experiments/ResourceConfig.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
using System.Collections.Generic;

namespace Microsoft.Azure.Experiments
{
public interface IResourceConfig
{
}

public static class ResourceConfig
{
public static ResourceConfig<Client, Name, Info> CreateResourceConfig<Client, Name, Info>(
this ResourcePolicy<Client, Name, Info> policy,
Name name,
Info info,
IEnumerable<IResourceConfig> dependencies)
where Info : class
=> new ResourceConfig<Client, Name, Info>(policy, name, info, dependencies);
}

public sealed class ResourceConfig<Client, TName, TInfo>
where TInfo : class
{
public ResourcePolicy<Client, TName, TInfo> Policy { get; }

public TName Name { get; }

public TInfo Info { get; }

public IEnumerable<IResourceConfig> Dependencies { get; }

public ResourceConfig(
ResourcePolicy<Client, TName, TInfo> policy,
TName name,
TInfo info,
IEnumerable<IResourceConfig> dependencies)
{
Policy = policy;
Name = name;
Info = info;
Dependencies = dependencies;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using Microsoft.Azure.Management.ResourceManager;
using Microsoft.Azure.Management.ResourceManager.Models;

namespace Microsoft.Azure.Experiments.ResourceManager
{
public static class ResourceManagerPolicy
{
public static ResourcePolicy<IResourceManagementClient, string, ResourceGroup> ResourceGroup
{ get; }
= OperationsPolicy
.Create<IResourceGroupsOperations, string, ResourceGroup>(
(operations, name) => operations.GetAsync(name),
(operations, name, info) => operations.CreateOrUpdateAsync(name, info))
.Transform<IResourceManagementClient>(r => r.ResourceGroups)
.CreateResourcePolicy(i => i.Location, (i, location) => i.Location = location);
}
}
14 changes: 14 additions & 0 deletions experiments/Azure.Experiments/Azure.Experiments/ResourceName.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
namespace Microsoft.Azure.Experiments
{
public sealed class ResourceName
{
public string ResourceGroupName { get; }
public string Name { get; }

public ResourceName(string resourceGroupName, string name)
{
ResourceGroupName = resourceGroupName;
Name = name;
}
}
}
34 changes: 34 additions & 0 deletions experiments/Azure.Experiments/Azure.Experiments/ResourcePolicy.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
using System;

namespace Microsoft.Azure.Experiments
{
public static class ResourcePolicy
{
public static ResourcePolicy<Client, Name, Info> CreateResourcePolicy<Client, Name, Info>(
this OperationsPolicy<Client, Name, Info> operationsPolicy,
Func<Info, string> getLocation,
Action<Info, string> setLocation)
where Info : class
=> new ResourcePolicy<Client, Name, Info>(operationsPolicy, getLocation, setLocation);
}

public sealed class ResourcePolicy<Client, Name, Info>
where Info : class
{
public OperationsPolicy<Client, Name, Info> OperationsPolicy { get; }

public Func<Info, string> GetLocation { get; }

public Action<Info, string> SetLocation { get; }

public ResourcePolicy(
OperationsPolicy<Client, Name, Info> operationsPolicy,
Func<Info, string> getLocation,
Action<Info, string> setLocation)
{
OperationsPolicy = operationsPolicy;
GetLocation = getLocation;
SetLocation = setLocation;
}
}
}

0 comments on commit b643730

Please sign in to comment.