Skip to content

Commit

Permalink
NetworkSecurityGroup
Browse files Browse the repository at this point in the history
  • Loading branch information
sergey-shandar committed Oct 16, 2017
1 parent 2be7c9c commit 83d0145
Show file tree
Hide file tree
Showing 5 changed files with 119 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,15 @@ public async Task PublicIpAddressTest()
var info = await pia.GetOrCreateAsync(c);
}

[Fact]
public async Task NetworkSecurityGroupTest()
{
var c = Credentials.Get();
var rg = new ResourceGroupObject("MyNSG");
var nsg = new NetworkSecurityGroupObject("MyNSG", rg);
var info = await nsg.GetOrCreateAsync(c);
}

[Fact]
public async Task Test1()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,14 @@ protected AzureObject(string name, IEnumerable<AzureObject> dependencies)
public abstract class AzureObject<T, C> : AzureObject
where T: class
{
public async Task<T> GetOrNullAsync(Context c)
public async Task<T> GetOrNullAsync(C c)
{
if (!IsGetCalled)
{
IsGetCalled = true;
try
{
Info = await GetOrThrowAsync(CreateClient(c));
Info = await GetOrThrowAsync(c);
}
catch (CloudException e)
when (e.Response.StatusCode == HttpStatusCode.NotFound)
Expand All @@ -45,6 +45,9 @@ public async Task<T> GetOrNullAsync(Context c)
return Info;
}

public Task<T> GetOrNullAsync(Context c)
=> GetOrNullAsync(CreateClient(c));

public async Task<T> GetOrCreateAsync(Context c)
{
Info = await GetOrNullAsync(c);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
using Microsoft.Azure.Management.Network;
using Microsoft.Azure.Management.Network.Models;
using System.Threading.Tasks;

namespace Azure.Experiments
{
class NetworkInterfaceObject
: ResourceObject<NetworkInterface, INetworkInterfacesOperations>
{
protected NetworkInterfaceObject(
string name,
ResourceGroupObject rg,
VirtualNetworkObject vn,
PublicIpAddressObject pia,
NetworkSecurityGroupObject nsg)
: base(name, rg, new AzureObject[] { vn, pia, nsg })
{
}

protected override Task<NetworkInterface> CreateAsync(
INetworkInterfacesOperations c)
=> c.CreateOrUpdateAsync(
ResourceGroupName, Name, new NetworkInterface());

protected override INetworkInterfacesOperations CreateClient(Context c)
=> c.CreateNetwork().NetworkInterfaces;

protected override Task DeleteAsync(INetworkInterfacesOperations c)
=> c.DeleteAsync(ResourceGroupName, Name);

protected override Task<NetworkInterface> GetOrThrowAsync(
INetworkInterfacesOperations c)
=> c.GetAsync(ResourceGroupName, Name);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
using Microsoft.Azure.Management.Network;
using Microsoft.Azure.Management.Network.Models;
using System.Threading.Tasks;

namespace Azure.Experiments
{
public sealed class NetworkSecurityGroupObject
: ResourceObject<NetworkSecurityGroup, INetworkSecurityGroupsOperations>
{
public NetworkSecurityGroupObject(
string name, ResourceGroupObject rg)
: base(name, rg)
{
}

protected override Task<NetworkSecurityGroup> CreateAsync(
INetworkSecurityGroupsOperations c)
=> c.CreateOrUpdateAsync(
ResourceGroupName,
Name,
new NetworkSecurityGroup { Location = "eastus" });

protected override INetworkSecurityGroupsOperations CreateClient(
Context c)
=> c.CreateNetwork().NetworkSecurityGroups;

protected override Task DeleteAsync(INetworkSecurityGroupsOperations c)
=> c.DeleteAsync(ResourceGroupName, Name);

protected override Task<NetworkSecurityGroup> GetOrThrowAsync(
INetworkSecurityGroupsOperations c)
=> c.GetAsync(ResourceGroupName, Name);
}
}
36 changes: 36 additions & 0 deletions experiments/Azure.Experiments/Azure.Experiments/SubnetObject.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
using Microsoft.Azure.Management.Network;
using System;
using System.Linq;
using System.Threading.Tasks;

namespace Azure.Experiments
{
sealed class SubnetObject : AzureObject<object, IVirtualNetworksOperations>
{
public SubnetObject(string name, VirtualNetworkObject vn)
: base(name, new[] { vn })
{
Vn = vn;
}

protected override Task<object> CreateAsync(IVirtualNetworksOperations c)
{
throw new NotImplementedException();
}

protected override IVirtualNetworksOperations CreateClient(Context c)
=> c.CreateNetwork().VirtualNetworks;

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);

private VirtualNetworkObject Vn { get; }
}
}

0 comments on commit 83d0145

Please sign in to comment.