1
1
using Microsoft . Azure . Management . Network ;
2
+ using Microsoft . Azure . Management . Network . Models ;
2
3
using System ;
3
4
using System . Linq ;
4
5
using System . Threading . Tasks ;
5
6
6
7
namespace Azure . Experiments
7
8
{
8
- sealed class SubnetObject : AzureObject < object , IVirtualNetworksOperations >
9
+ public sealed class SubnetObject : AzureObject < Subnet , IVirtualNetworksOperations >
9
10
{
10
- public SubnetObject ( string name , VirtualNetworkObject vn )
11
+ public string AddressPrefix { get ; }
12
+
13
+ public SubnetObject ( string name , VirtualNetworkObject vn , string addressPrefix )
11
14
: base ( name , new [ ] { vn } )
12
15
{
13
16
Vn = vn ;
17
+ AddressPrefix = addressPrefix ;
14
18
}
15
19
16
- protected override Task < object > CreateAsync ( IVirtualNetworksOperations c )
20
+ protected override async Task < Subnet > CreateAsync ( IVirtualNetworksOperations c )
17
21
{
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 ) ;
19
27
}
20
28
21
29
protected override IVirtualNetworksOperations CreateClient ( Context c )
@@ -26,11 +34,12 @@ protected override Task DeleteAsync(IVirtualNetworksOperations c)
26
34
throw new NotImplementedException ( ) ;
27
35
}
28
36
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 ) ) ;
33
39
34
40
private VirtualNetworkObject Vn { get ; }
41
+
42
+ private Subnet GetSubnet ( VirtualNetwork vn )
43
+ => vn ? . Subnets . FirstOrDefault ( s => s . Name == Name ) ;
35
44
}
36
45
}
0 commit comments