Skip to content

Commit a522a35

Browse files
Subnet
1 parent 83d0145 commit a522a35

File tree

3 files changed

+28
-9
lines changed

3 files changed

+28
-9
lines changed

experiments/Azure.Experiments/Azure.Experiments.Tests/ComputeTest.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,16 @@ public async Task NetworkSecurityGroupTest()
4444
var info = await nsg.GetOrCreateAsync(c);
4545
}
4646

47+
[Fact]
48+
public async Task SubnetTest()
49+
{
50+
var c = Credentials.Get();
51+
var rg = new ResourceGroupObject("MySubnet");
52+
var vn = new VirtualNetworkObject("MySubnet", rg, "192.168.0.0/16");
53+
var subnet = new SubnetObject("MySubnet", vn, "192.168.1.0/24");
54+
var info = await subnet.GetOrCreateAsync(c);
55+
}
56+
4757
[Fact]
4858
public async Task Test1()
4959
{

experiments/Azure.Experiments/Azure.Experiments/ResourceObject.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ namespace Azure.Experiments
66
public abstract class ResourceObject<T, C> : AzureObject<T, C>
77
where T : class
88
{
9-
protected string ResourceGroupName { get; }
9+
public string ResourceGroupName { get; }
1010

1111
protected ResourceObject(
1212
string name,
Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,29 @@
11
using Microsoft.Azure.Management.Network;
2+
using Microsoft.Azure.Management.Network.Models;
23
using System;
34
using System.Linq;
45
using System.Threading.Tasks;
56

67
namespace Azure.Experiments
78
{
8-
sealed class SubnetObject : AzureObject<object, IVirtualNetworksOperations>
9+
public sealed class SubnetObject : AzureObject<Subnet, IVirtualNetworksOperations>
910
{
10-
public SubnetObject(string name, VirtualNetworkObject vn)
11+
public string AddressPrefix { get; }
12+
13+
public SubnetObject(string name, VirtualNetworkObject vn, string addressPrefix)
1114
: base(name, new[] { vn })
1215
{
1316
Vn = vn;
17+
AddressPrefix = addressPrefix;
1418
}
1519

16-
protected override Task<object> CreateAsync(IVirtualNetworksOperations c)
20+
protected override async Task<Subnet> CreateAsync(IVirtualNetworksOperations c)
1721
{
18-
throw new NotImplementedException();
22+
// The Virtual Network should be created at this point.
23+
var vn = await Vn.GetOrNullAsync(c);
24+
vn.Subnets.Add(new Subnet { Name = Name, AddressPrefix = AddressPrefix });
25+
vn = await c.CreateOrUpdateAsync(Vn.ResourceGroupName, Vn.Name, vn);
26+
return GetSubnet(vn);
1927
}
2028

2129
protected override IVirtualNetworksOperations CreateClient(Context c)
@@ -26,11 +34,12 @@ protected override Task DeleteAsync(IVirtualNetworksOperations c)
2634
throw new NotImplementedException();
2735
}
2836

29-
protected override async Task<object> GetOrThrowAsync(IVirtualNetworksOperations c)
30-
=> (await Vn.GetOrNullAsync(c))
31-
?.Subnets
32-
.FirstOrDefault(s => s.Name == Name);
37+
protected override async Task<Subnet> GetOrThrowAsync(IVirtualNetworksOperations c)
38+
=> GetSubnet(await Vn.GetOrNullAsync(c));
3339

3440
private VirtualNetworkObject Vn { get; }
41+
42+
private Subnet GetSubnet(VirtualNetwork vn)
43+
=> vn?.Subnets.FirstOrDefault(s => s.Name == Name);
3544
}
3645
}

0 commit comments

Comments
 (0)