11using Microsoft . Azure . Management . Network ;
2+ using Microsoft . Azure . Management . Network . Models ;
23using System ;
34using System . Linq ;
45using System . Threading . Tasks ;
56
67namespace 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