Skip to content

Commit

Permalink
GetDependecyLocation
Browse files Browse the repository at this point in the history
  • Loading branch information
sergey-shandar committed Oct 31, 2017
1 parent bc0fb77 commit 7832679
Show file tree
Hide file tree
Showing 13 changed files with 134 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,8 @@ protected override Task<VirtualMachine> GetAsync(
.CreateCompute()
.VirtualMachines
.GetAsync(ResourceGroup.Name, Name);

public override string GetLocation(VirtualMachine value)
=> value.Location;
}
}
7 changes: 7 additions & 0 deletions experiments/Azure.Experiments/Azure.Experiments/Context.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using Microsoft.Azure.Management.Compute;
using Microsoft.Azure.Management.Network;
using Microsoft.Azure.Management.ResourceManager;
using Microsoft.Rest;

namespace Microsoft.Azure.Experiments
Expand Down Expand Up @@ -28,5 +29,11 @@ public ComputeManagementClient CreateCompute()
{
SubscriptionId = SubscriptionId
};

public ResourceManagementClient CreateResource()
=> new ResourceManagementClient(Credentials)
{
SubscriptionId = SubscriptionId
};
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
namespace Microsoft.Azure.Experiments
{
public sealed class DependencyLocation
{
public string Location { get; }

public bool IsCommon { get; }

public DependencyLocation(string location, bool isCommon)
{
Location = location;
IsCommon = isCommon;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
using System;

namespace Microsoft.Azure.Experiments
{
public static class DependencyLocationExtensions
{
public static DependencyLocation Best(this DependencyLocation a, DependencyLocation b)
{
if (a == null)
{
return b;
}
if (b == null)
{
return a;
}

// a != null
// b != null
if (!a.IsCommon)
{
return b;
}
if (!b.IsCommon)
{
return a;
}

// a.IsCommon == true
// b.IsCommon == true
if (a.Location != b.Location)
{
throw new Exception("dependent resources have different locations");
}
return a;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
namespace Microsoft.Azure.Experiments.Network
{
public sealed class NetworkInterfaceParameters
: ResourceParameters<NetworkInterface>
: NetworkParameters<NetworkInterface>
{
public SubnetParameters Subnet { get; }

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using Microsoft.Azure.Management.Network.Models;
using System.Collections.Generic;

namespace Microsoft.Azure.Experiments.Network
{
public abstract class NetworkParameters<T> : ResourceParameters<T>
where T : Resource
{
public NetworkParameters(
string name,
ResourceGroupParameters resourceGroup,
IEnumerable<Parameters> dependencies)
: base(name, resourceGroup, dependencies)
{
}

public sealed override string GetLocation(T value)
=> value.Location;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
namespace Microsoft.Azure.Experiments.Network
{
public sealed class NetworkSecurityGroupParameters
: ResourceParameters<NetworkSecurityGroup>
: NetworkParameters<NetworkSecurityGroup>
{
public NetworkSecurityGroupParameters(
string name, ResourceGroupParameters resourceGroup)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
namespace Microsoft.Azure.Experiments.Network
{
public sealed class PublicIpAddressParameters
: ResourceParameters<PublicIPAddress>
: NetworkParameters<PublicIPAddress>
{
public PublicIpAddressParameters(
string name, ResourceGroupParameters resourceGroup)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ public sealed class SubnetParameters : Parameters<Subnet>
{
public VirtualNetworkParameters VirtualNetwork { get; }

public override bool HasCommonLocation => true;

public SubnetParameters(
string name, VirtualNetworkParameters virtualNetwork)
: base(name, new[] { virtualNetwork })
Expand All @@ -22,5 +24,8 @@ protected override async Task<Subnet> GetAsync(
await VirtualNetwork.GetOrNullAsync(context, getParameters);
return virtualNetwork?.Subnets.FirstOrDefault(s => s.Name == Name);
}

public override string GetLocation(Subnet value)
=> null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
namespace Microsoft.Azure.Experiments.Network
{
public sealed class VirtualNetworkParameters
: ResourceParameters<VirtualNetwork>
: NetworkParameters<VirtualNetwork>
{
public VirtualNetworkParameters(
string name, ResourceGroupParameters resourceGroup)
Expand Down
26 changes: 25 additions & 1 deletion experiments/Azure.Experiments/Azure.Experiments/Parameters.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@ public static IEnumerable<Parameters> NoDependencies

public IEnumerable<Parameters> Dependencies { get; }

public abstract bool HasCommonLocation { get; }

public abstract Task<DependencyLocation> GetDependencyLocation(
Context context, IGetParameters getParameters);

protected Parameters(string name, IEnumerable<Parameters> dependencies)
{
Name = name;
Expand All @@ -30,7 +35,8 @@ protected Parameters(string name, IEnumerable<Parameters> parameters)
{
}

public Task<T> GetOrNullAsync(Context context, IGetParameters getParameters)
public Task<T> GetOrNullAsync(
Context context, IGetParameters getParameters)
=> getParameters.GetOrAdd(
this,
async () =>
Expand All @@ -49,6 +55,24 @@ public Task<T> GetOrNullAsync(Context context, IGetParameters getParameters)
protected abstract Task<T> GetAsync(
Context context, IGetParameters getParameters);

public abstract string GetLocation(T value);

public sealed override async Task<DependencyLocation> GetDependencyLocation(
Context context, IGetParameters getParameters)
{
var info = await GetOrNullAsync(context, getParameters);
var location = info == null ? null : GetLocation(info);
if (location == null)
{
var tasks = Dependencies.Select(
d => d.GetDependencyLocation(context, getParameters));
var dependencyLocations = await Task.WhenAll(tasks);
return dependencyLocations.Aggregate(
(DependencyLocation)null, DependencyLocationExtensions.Best);
}
return new DependencyLocation(location, HasCommonLocation);
}

//protected abstract Task<T> CreateAsync(
// Context context, ICreateParameters createParameters);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,22 @@
namespace Microsoft.Azure.Experiments
using Microsoft.Azure.Management.ResourceManager;
using Microsoft.Azure.Management.ResourceManager.Models;
using System.Threading.Tasks;

namespace Microsoft.Azure.Experiments
{
public sealed class ResourceGroupParameters : Parameters
public sealed class ResourceGroupParameters : Parameters<ResourceGroup>
{
public ResourceGroupParameters(string name) : base(name, NoDependencies)
{
}

public override bool HasCommonLocation => false;

public override string GetLocation(ResourceGroup value)
=> value.Location;

protected override Task<ResourceGroup> GetAsync(
Context context, IGetParameters getParameters)
=> context.CreateResource().ResourceGroups.GetAsync(Name);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ public abstract class ResourceParameters<T> : Parameters<T>
{
public ResourceGroupParameters ResourceGroup { get; }

public sealed override bool HasCommonLocation => true;

public ResourceParameters(
string name,
ResourceGroupParameters resourceGroup,
Expand Down

0 comments on commit 7832679

Please sign in to comment.