Skip to content

Commit

Permalink
it works! :-)
Browse files Browse the repository at this point in the history
  • Loading branch information
sergey-shandar committed Nov 15, 2017
1 parent 381a986 commit 27525ee
Show file tree
Hide file tree
Showing 12 changed files with 200 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public override async Task<object> Visit<Config, ParentConfig>(
NestedResourceConfig<Config, ParentConfig> config)
{
var parent = await GetOrAdd(config.Parent);
return config.Policy.Get(parent);
return config.Policy.Get(parent, config.Name);
}

public CreateAsyncVisitor(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public override async Task<object> Visit<Config, ParentConfig>(
NestedResourceConfig<Config, ParentConfig> config)
{
var parent = await GetOrAdd(config.Parent);
return parent == null ? null : config.Policy.Get(parent);
return parent == null ? null : config.Policy.Get(parent, config.Name);
}

public Visitor(IClient client, CancellationToken cancellationToken)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,18 @@

namespace Microsoft.Azure.Experiments
{
public static class NestedResourceConfig
{
public static NestedResourceConfig<Config, ParentConfig> CreateConfig<Config, ParentConfig>(
this NestedResourcePolicy<Config, ParentConfig> policy,
IResourceConfig<ParentConfig> parent,
string name,
Func<Config> create)
where Config : class
where ParentConfig : class
=> new NestedResourceConfig<Config, ParentConfig>(policy, parent, name, create);
}

public sealed class NestedResourceConfig<Config, ParentConfig> : IResourceConfig<Config>
where Config : class
where ParentConfig : class
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,32 @@ public sealed class NestedResourcePolicy<Config, ParentConfig> : IResourcePolicy
{
public Func<string, IEnumerable<string>> GetId { get; }

public Func<ParentConfig, Config> Get { get; }
public Func<ParentConfig, string, Config> Get { get; }

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

public NestedResourcePolicy(
Func<string, IEnumerable<string>> getId,
Func<ParentConfig, Config> get,
Action<ParentConfig, Config> set)
Func<ParentConfig, string, Config> get,
Action<ParentConfig, string, Config> set)
{
GetId = getId;
Get = get;
Set = set;
}
}

public static class NestedResourcePolicy
{
public static NestedResourcePolicy<Config, ParentConfig> Create<Config, ParentConfig>(
string header,
Func<ParentConfig, string, Config> get,
Action<ParentConfig, string, Config> set)
where Config : class
where ParentConfig : class
=> new NestedResourcePolicy<Config, ParentConfig>(
name => new[] { header, name},
get,
set);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ public static class NetworkInterfacePolicy
p => p.Operations.CreateOrUpdateAsync(
p.ResourceGroupName, p.Name, p.Config, p.CancellationToken));

public static ResourceConfig<NetworkInterface> CreateNetworkSecurityGroupConfig(
public static ResourceConfig<NetworkInterface> CreateNetworkInterfaceConfig(
this ResourceConfig<ResourceGroup> resourceGroup,
string name,
ResourceConfig<Subnet> subnet,
NestedResourceConfig<Subnet, VirtualNetwork> subnet,
ResourceConfig<PublicIPAddress> publicIPAddress,
ResourceConfig<NetworkSecurityGroup> networkSecurityGroup)
=> Policy.CreateConfig(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
using Microsoft.Azure.Management.Network.Models;
using System.Collections.Generic;
using System.Linq;

namespace Microsoft.Azure.Experiments.Network
{
public static class SubnetPolicy
{
public static NestedResourcePolicy<Subnet, VirtualNetwork> Policy { get; }
= NestedResourcePolicy.Create<Subnet, VirtualNetwork>(
"subnets",
(vn, name) => vn.Subnets?.FirstOrDefault(s => s.Name == name),
(vn, name, subnet) =>
{
subnet.Name = name;
if (vn.Subnets == null)
{
vn.Subnets = new List<Subnet> { subnet };
return;
}
var s = vn
.Subnets
.Select((sn, i) => new { sn, i })
.FirstOrDefault(p => p.sn.Name == name);
if (s != null)
{
vn.Subnets[s.i] = subnet;
return;
}
vn.Subnets.Add(subnet);
});

public static NestedResourceConfig<Subnet, VirtualNetwork> CreateSubnet(
this ResourceConfig<VirtualNetwork> virtualNetwork, string name, string addressPrefix)
=> Policy.CreateConfig(
virtualNetwork,
name,
() => new Subnet { Name = name, AddressPrefix = addressPrefix });
}
}
5 changes: 3 additions & 2 deletions experiments/Azure.Experiments/Azure.Experiments/Parameters.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ public Config Get<Config>(IResourceConfig<Config> config)
where Config : class
=> GetUntyped(config) as Config;

public object Visit<Config>(ResourceConfig<Config> config) where Config : class
public object Visit<Config>(ResourceConfig<Config> config)
where Config : class
{
foreach (var d in config.Dependencies)
{
Expand All @@ -47,7 +48,7 @@ public object Visit<Config, ParentConfig>(
where ParentConfig : class
{
var result = config.Create();
config.Policy.Set(Get(config.Parent), result);
config.Policy.Set(Get(config.Parent), config.Name, result);
return result;
}

Expand Down
34 changes: 34 additions & 0 deletions experiments/Azure.Experiments/Tests/NetworkInterfaceTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
using Microsoft.Azure.Experiments.Network;
using Microsoft.Azure.Experiments.ResourceManager;
using System.Threading;
using System.Threading.Tasks;
using Xunit;

namespace Microsoft.Azure.Experiments.Tests
{
public class NetworkInterfaceTest
{
[Fact]
public async Task CreateAsyncTest()
{
var rg = ResourceGroupPolicy.CreateResourceGroupConfig("nirg");
var vn = rg.CreateVirtualNetworkConfig("Vnni", "192.168.0.0/16");
var sn = vn.CreateSubnet("mysubnet", "192.168.1.0/24");
var pipa = rg.CreatePublicIPAddressConfig("pipani");
var nsg = rg.CreateNetworkSecurityGroupConfig("nsgni");
var ni = rg.CreateNetworkInterfaceConfig("ni", sn, pipa, nsg);

var client = new Client(Credentials.Get());
var state = await ni.GetAsync(client, new CancellationToken());
var location = state.GetLocation(rg);
var parameters = ni.GetParameters(client.Context.SubscriptionId, "eastus");
var nic = parameters.GetOrNull(ni);
var createState = await ni.CreateOrUpdateAsync(
client, state, parameters, new CancellationToken());
var nicc = createState.GetOrNull(ni);
Assert.Equal("eastus", nicc.Location);
Assert.Equal("ni", nicc.Name);
Assert.Equal(ni.GetId(client.Context.SubscriptionId).IdToString(), nicc.Id);
}
}
}
29 changes: 29 additions & 0 deletions experiments/Azure.Experiments/Tests/NetworkSecurityGroupTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
using Microsoft.Azure.Experiments.Network;
using Microsoft.Azure.Experiments.ResourceManager;
using System.Threading;
using System.Threading.Tasks;
using Xunit;

namespace Microsoft.Azure.Experiments.Tests
{
public class NetworkSecurityGroupTest
{
[Fact]
public async Task CreateAsyncTest()
{
var rg = ResourceGroupPolicy.CreateResourceGroupConfig("nsgrg");
var vn = rg.CreateNetworkSecurityGroupConfig("nsg");
var client = new Client(Credentials.Get());
var state = await vn.GetAsync(client, new CancellationToken());
var location = state.GetLocation(rg);
var parameters = vn.GetParameters(client.Context.SubscriptionId, "eastus");
var vnc = parameters.GetOrNull(vn);
var createState = await vn.CreateOrUpdateAsync(
client, state, parameters, new CancellationToken());
var vncc = createState.GetOrNull(vn);
Assert.Equal("eastus", vncc.Location);
Assert.Equal("nsg", vncc.Name);
Assert.Equal(vn.GetId(client.Context.SubscriptionId).IdToString(), vncc.Id);
}
}
}
29 changes: 29 additions & 0 deletions experiments/Azure.Experiments/Tests/PublicIPAddressTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
using Microsoft.Azure.Experiments.Network;
using Microsoft.Azure.Experiments.ResourceManager;
using System.Threading;
using System.Threading.Tasks;
using Xunit;

namespace Microsoft.Azure.Experiments.Tests
{
public class PublicIPAddressTest
{
[Fact]
public async Task CreateAsyncTest()
{
var rg = ResourceGroupPolicy.CreateResourceGroupConfig("piparg");
var vn = rg.CreatePublicIPAddressConfig("pipa");
var client = new Client(Credentials.Get());
var state = await vn.GetAsync(client, new CancellationToken());
var location = state.GetLocation(rg);
var parameters = vn.GetParameters(client.Context.SubscriptionId, "eastus");
var vnc = parameters.GetOrNull(vn);
var createState = await vn.CreateOrUpdateAsync(
client, state, parameters, new CancellationToken());
var vncc = createState.GetOrNull(vn);
Assert.Equal("eastus", vncc.Location);
Assert.Equal("pipa", vncc.Name);
Assert.Equal(vn.GetId(client.Context.SubscriptionId).IdToString(), vncc.Id);
}
}
}
30 changes: 30 additions & 0 deletions experiments/Azure.Experiments/Tests/SubnetTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
using Microsoft.Azure.Experiments.Network;
using Microsoft.Azure.Experiments.ResourceManager;
using System.Threading;
using System.Threading.Tasks;
using Xunit;

namespace Microsoft.Azure.Experiments.Tests
{
public class SubnetTest
{
[Fact]
public async Task CreateAsyncTest()
{
var rg = ResourceGroupPolicy.CreateResourceGroupConfig("Snrg");
var vn = rg.CreateVirtualNetworkConfig("Vn", "192.168.0.0/16");
var sn = vn.CreateSubnet("mysubnet", "192.168.1.0/24");

var client = new Client(Credentials.Get());
var state = await sn.GetAsync(client, new CancellationToken());
var location = state.GetLocation(rg);
var parameters = sn.GetParameters(client.Context.SubscriptionId, "eastus");
var snc = parameters.GetOrNull(sn);
var createState = await sn.CreateOrUpdateAsync(
client, state, parameters, new CancellationToken());
var sncc = createState.GetOrNull(sn);
Assert.Equal("mysubnet", sncc.Name);
Assert.Equal(sn.GetId(client.Context.SubscriptionId).IdToString(), sncc.Id);
}
}
}
1 change: 1 addition & 0 deletions experiments/Azure.Experiments/Tests/VirtualNetworkTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ public async Task CreateAsyncTest()
client, state, parameters, new CancellationToken());
var vncc = createState.GetOrNull(vn);
Assert.Equal("eastus", vncc.Location);
Assert.Equal("192.168.0.0/16", vncc.AddressSpace.AddressPrefixes[0]);
Assert.Equal("vn", vncc.Name);
Assert.Equal(vn.GetId(client.Context.SubscriptionId).IdToString(), vncc.Id);
}
Expand Down

0 comments on commit 27525ee

Please sign in to comment.