Skip to content

Commit 83d0145

Browse files
NetworkSecurityGroup
1 parent 2be7c9c commit 83d0145

File tree

5 files changed

+119
-2
lines changed

5 files changed

+119
-2
lines changed

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,15 @@ public async Task PublicIpAddressTest()
3535
var info = await pia.GetOrCreateAsync(c);
3636
}
3737

38+
[Fact]
39+
public async Task NetworkSecurityGroupTest()
40+
{
41+
var c = Credentials.Get();
42+
var rg = new ResourceGroupObject("MyNSG");
43+
var nsg = new NetworkSecurityGroupObject("MyNSG", rg);
44+
var info = await nsg.GetOrCreateAsync(c);
45+
}
46+
3847
[Fact]
3948
public async Task Test1()
4049
{

experiments/Azure.Experiments/Azure.Experiments/AzureObject.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,14 @@ protected AzureObject(string name, IEnumerable<AzureObject> dependencies)
2828
public abstract class AzureObject<T, C> : AzureObject
2929
where T: class
3030
{
31-
public async Task<T> GetOrNullAsync(Context c)
31+
public async Task<T> GetOrNullAsync(C c)
3232
{
3333
if (!IsGetCalled)
3434
{
3535
IsGetCalled = true;
3636
try
3737
{
38-
Info = await GetOrThrowAsync(CreateClient(c));
38+
Info = await GetOrThrowAsync(c);
3939
}
4040
catch (CloudException e)
4141
when (e.Response.StatusCode == HttpStatusCode.NotFound)
@@ -45,6 +45,9 @@ public async Task<T> GetOrNullAsync(Context c)
4545
return Info;
4646
}
4747

48+
public Task<T> GetOrNullAsync(Context c)
49+
=> GetOrNullAsync(CreateClient(c));
50+
4851
public async Task<T> GetOrCreateAsync(Context c)
4952
{
5053
Info = await GetOrNullAsync(c);
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
using Microsoft.Azure.Management.Network;
2+
using Microsoft.Azure.Management.Network.Models;
3+
using System.Threading.Tasks;
4+
5+
namespace Azure.Experiments
6+
{
7+
class NetworkInterfaceObject
8+
: ResourceObject<NetworkInterface, INetworkInterfacesOperations>
9+
{
10+
protected NetworkInterfaceObject(
11+
string name,
12+
ResourceGroupObject rg,
13+
VirtualNetworkObject vn,
14+
PublicIpAddressObject pia,
15+
NetworkSecurityGroupObject nsg)
16+
: base(name, rg, new AzureObject[] { vn, pia, nsg })
17+
{
18+
}
19+
20+
protected override Task<NetworkInterface> CreateAsync(
21+
INetworkInterfacesOperations c)
22+
=> c.CreateOrUpdateAsync(
23+
ResourceGroupName, Name, new NetworkInterface());
24+
25+
protected override INetworkInterfacesOperations CreateClient(Context c)
26+
=> c.CreateNetwork().NetworkInterfaces;
27+
28+
protected override Task DeleteAsync(INetworkInterfacesOperations c)
29+
=> c.DeleteAsync(ResourceGroupName, Name);
30+
31+
protected override Task<NetworkInterface> GetOrThrowAsync(
32+
INetworkInterfacesOperations c)
33+
=> c.GetAsync(ResourceGroupName, Name);
34+
}
35+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
using Microsoft.Azure.Management.Network;
2+
using Microsoft.Azure.Management.Network.Models;
3+
using System.Threading.Tasks;
4+
5+
namespace Azure.Experiments
6+
{
7+
public sealed class NetworkSecurityGroupObject
8+
: ResourceObject<NetworkSecurityGroup, INetworkSecurityGroupsOperations>
9+
{
10+
public NetworkSecurityGroupObject(
11+
string name, ResourceGroupObject rg)
12+
: base(name, rg)
13+
{
14+
}
15+
16+
protected override Task<NetworkSecurityGroup> CreateAsync(
17+
INetworkSecurityGroupsOperations c)
18+
=> c.CreateOrUpdateAsync(
19+
ResourceGroupName,
20+
Name,
21+
new NetworkSecurityGroup { Location = "eastus" });
22+
23+
protected override INetworkSecurityGroupsOperations CreateClient(
24+
Context c)
25+
=> c.CreateNetwork().NetworkSecurityGroups;
26+
27+
protected override Task DeleteAsync(INetworkSecurityGroupsOperations c)
28+
=> c.DeleteAsync(ResourceGroupName, Name);
29+
30+
protected override Task<NetworkSecurityGroup> GetOrThrowAsync(
31+
INetworkSecurityGroupsOperations c)
32+
=> c.GetAsync(ResourceGroupName, Name);
33+
}
34+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
using Microsoft.Azure.Management.Network;
2+
using System;
3+
using System.Linq;
4+
using System.Threading.Tasks;
5+
6+
namespace Azure.Experiments
7+
{
8+
sealed class SubnetObject : AzureObject<object, IVirtualNetworksOperations>
9+
{
10+
public SubnetObject(string name, VirtualNetworkObject vn)
11+
: base(name, new[] { vn })
12+
{
13+
Vn = vn;
14+
}
15+
16+
protected override Task<object> CreateAsync(IVirtualNetworksOperations c)
17+
{
18+
throw new NotImplementedException();
19+
}
20+
21+
protected override IVirtualNetworksOperations CreateClient(Context c)
22+
=> c.CreateNetwork().VirtualNetworks;
23+
24+
protected override Task DeleteAsync(IVirtualNetworksOperations c)
25+
{
26+
throw new NotImplementedException();
27+
}
28+
29+
protected override async Task<object> GetOrThrowAsync(IVirtualNetworksOperations c)
30+
=> (await Vn.GetOrNullAsync(c))
31+
?.Subnets
32+
.FirstOrDefault(s => s.Name == Name);
33+
34+
private VirtualNetworkObject Vn { get; }
35+
}
36+
}

0 commit comments

Comments
 (0)