Skip to content

Commit

Permalink
GetAsync
Browse files Browse the repository at this point in the history
  • Loading branch information
sergey-shandar committed Oct 26, 2017
1 parent 4a41002 commit 3eaa6c0
Show file tree
Hide file tree
Showing 8 changed files with 55 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,9 @@ public VirtualMachineParameters(
Ni = ni;
}

public override Task<VirtualMachine> GetAsync(Context context)
public override Task<VirtualMachine> GetAsync(GetContext context)
=> context
.Context
.CreateCompute()
.VirtualMachines
.GetAsync(ResourceGroup.Name, Name);
Expand Down
37 changes: 37 additions & 0 deletions experiments/Azure.Experiments/Azure.Experiments/GetContext.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
using System.Collections.Concurrent;
using System.Threading.Tasks;

namespace Microsoft.Azure.Experiments
{
public sealed class GetContext
{
public Context Context { get; }

public GetContext(Context context)
{
Context = context;
}

public async Task<T> GetOrNullAsync<T>(Parameters<T> parameters)
{
var result = await Map.GetOrAdd(
parameters, _ => GetObjectOrNullAsync(parameters));
return (T)result;
}

private async Task<object> GetObjectOrNullAsync<T>(Parameters<T> parameters)
{
try
{
return await parameters.GetAsync(this);
}
catch
{
return null;
}
}

private ConcurrentDictionary<Parameters, Task<object>> Map { get; }
= new ConcurrentDictionary<Parameters, Task<object>>();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,9 @@ public NetworkInterfaceParameters(
Pia = pia;
}

public override Task<NetworkInterface> GetAsync(Context context)
public override Task<NetworkInterface> GetAsync(GetContext context)
=> context
.Context
.CreateNetwork()
.NetworkInterfaces
.GetAsync(ResourceGroup.Name, Name);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,9 @@ public NetworkSecurityGroupParameters(
{
}

public override Task<NetworkSecurityGroup> GetAsync(Context context)
public override Task<NetworkSecurityGroup> GetAsync(GetContext context)
=> context
.Context
.CreateNetwork()
.NetworkSecurityGroups
.GetAsync(ResourceGroup.Name, Name);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,9 @@ public PublicIpAddressParameters(
{
}

public override Task<PublicIPAddress> GetAsync(Context context)
public override Task<PublicIPAddress> GetAsync(GetContext context)
=> context
.Context
.CreateNetwork()
.PublicIPAddresses
.GetAsync(ResourceGroup.Name, Name);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System.Threading.Tasks;
using Microsoft.Azure.Management.Network.Models;
using System.Linq;

namespace Microsoft.Azure.Experiments.Network
{
Expand All @@ -13,5 +14,11 @@ public SubnetParameters(
{
VirtualNetwork = virtualNetwork;
}

public override async Task<Subnet> GetAsync(GetContext context)
{
var virtualNetwork = await context.GetOrNullAsync(VirtualNetwork);
return virtualNetwork?.Subnets.FirstOrDefault(s => s.Name == Name);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,9 @@ public VirtualNetworkParameters(
{
}

public override Task<VirtualNetwork> GetAsync(Context context)
public override Task<VirtualNetwork> GetAsync(GetContext context)
=> context
.Context
.CreateNetwork()
.VirtualNetworks
.GetAsync(ResourceGroup.Name, Name);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,6 @@ protected Parameters(string name, IEnumerable<Parameters> parameters)
{
}

public abstract Task<T> GetAsync(Context context);
public abstract Task<T> GetAsync(GetContext context);
}
}

0 comments on commit 3eaa6c0

Please sign in to comment.