Skip to content

Commit

Permalink
Subnet
Browse files Browse the repository at this point in the history
  • Loading branch information
sergey-shandar committed Oct 16, 2017
1 parent 83d0145 commit a522a35
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,16 @@ public async Task NetworkSecurityGroupTest()
var info = await nsg.GetOrCreateAsync(c);
}

[Fact]
public async Task SubnetTest()
{
var c = Credentials.Get();
var rg = new ResourceGroupObject("MySubnet");
var vn = new VirtualNetworkObject("MySubnet", rg, "192.168.0.0/16");
var subnet = new SubnetObject("MySubnet", vn, "192.168.1.0/24");
var info = await subnet.GetOrCreateAsync(c);
}

[Fact]
public async Task Test1()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ namespace Azure.Experiments
public abstract class ResourceObject<T, C> : AzureObject<T, C>
where T : class
{
protected string ResourceGroupName { get; }
public string ResourceGroupName { get; }

protected ResourceObject(
string name,
Expand Down
25 changes: 17 additions & 8 deletions experiments/Azure.Experiments/Azure.Experiments/SubnetObject.cs
Original file line number Diff line number Diff line change
@@ -1,21 +1,29 @@
using Microsoft.Azure.Management.Network;
using Microsoft.Azure.Management.Network.Models;
using System;
using System.Linq;
using System.Threading.Tasks;

namespace Azure.Experiments
{
sealed class SubnetObject : AzureObject<object, IVirtualNetworksOperations>
public sealed class SubnetObject : AzureObject<Subnet, IVirtualNetworksOperations>
{
public SubnetObject(string name, VirtualNetworkObject vn)
public string AddressPrefix { get; }

public SubnetObject(string name, VirtualNetworkObject vn, string addressPrefix)
: base(name, new[] { vn })
{
Vn = vn;
AddressPrefix = addressPrefix;
}

protected override Task<object> CreateAsync(IVirtualNetworksOperations c)
protected override async Task<Subnet> CreateAsync(IVirtualNetworksOperations c)
{
throw new NotImplementedException();
// The Virtual Network should be created at this point.
var vn = await Vn.GetOrNullAsync(c);
vn.Subnets.Add(new Subnet { Name = Name, AddressPrefix = AddressPrefix });
vn = await c.CreateOrUpdateAsync(Vn.ResourceGroupName, Vn.Name, vn);
return GetSubnet(vn);
}

protected override IVirtualNetworksOperations CreateClient(Context c)
Expand All @@ -26,11 +34,12 @@ protected override Task DeleteAsync(IVirtualNetworksOperations c)
throw new NotImplementedException();
}

protected override async Task<object> GetOrThrowAsync(IVirtualNetworksOperations c)
=> (await Vn.GetOrNullAsync(c))
?.Subnets
.FirstOrDefault(s => s.Name == Name);
protected override async Task<Subnet> GetOrThrowAsync(IVirtualNetworksOperations c)
=> GetSubnet(await Vn.GetOrNullAsync(c));

private VirtualNetworkObject Vn { get; }

private Subnet GetSubnet(VirtualNetwork vn)
=> vn?.Subnets.FirstOrDefault(s => s.Name == Name);
}
}

0 comments on commit a522a35

Please sign in to comment.