Skip to content

Commit

Permalink
Network and Compute, policies.
Browse files Browse the repository at this point in the history
  • Loading branch information
sergey-shandar committed Oct 24, 2017
1 parent 7b3275b commit 94b26c2
Show file tree
Hide file tree
Showing 17 changed files with 99 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,9 @@ protected AzureObject(string name, IEnumerable<AzureObject> dependencies)
}
}

public abstract class AzureObject<T> : AzureObject
public abstract class AzureObject<T, P> : AzureObject
where T: class
where P: struct, IInfoPolicy<T>
{
public T Info { get; private set; }

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using Microsoft.Azure.Management.Compute.Models;

namespace Azure.Experiments.Compute
{
public struct ComputePolicy<T> : IInfoPolicy<T>
where T : Resource
{
public string GetLocation(T value)
=> value.Location;
}
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
using Microsoft.Azure.Management.Compute;
using Azure.Experiments.Network;
using Microsoft.Azure.Management.Compute;
using Microsoft.Azure.Management.Compute.Models;
using System.Threading.Tasks;

namespace Azure.Experiments
namespace Azure.Experiments.Compute
{
public sealed class VmObject
: ResourceObject<VirtualMachine>
public sealed class VirtualMachineObject
: ResourceObject<VirtualMachine, ComputePolicy<VirtualMachine>>
{
public VmObject(
public VirtualMachineObject(
Context c,
string name,
ResourceGroupObject rg,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace Azure.Experiments
{
public interface IInfoPolicy<T>
{
string GetLocation(T value);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@
using Microsoft.Azure.Management.Network.Models;
using System.Threading.Tasks;

namespace Azure.Experiments
namespace Azure.Experiments.Network
{
public sealed class NetworkInterfaceObject
: ResourceObject<NetworkInterface>
: NetworkObject<NetworkInterface>
{
public NetworkInterfaceObject(
INetworkManagementClient client,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using System.Collections.Generic;
using Microsoft.Azure.Management.Network.Models;

namespace Azure.Experiments.Network
{
public abstract class NetworkObject<T> : ResourceObject<T, NetworkPolicy<T>>
where T : Resource
{
protected NetworkObject(string name, ResourceGroupObject rg) : base(name, rg)
{
}

protected NetworkObject(
string name,
ResourceGroupObject rg,
IEnumerable<AzureObject> dependencies) : base(name, rg, dependencies)
{
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using Microsoft.Azure.Management.Network.Models;

namespace Azure.Experiments.Network
{
public struct NetworkPolicy<T> : IInfoPolicy<T>
where T : Resource
{
public string GetLocation(T value)
=> value.Location;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@
using Microsoft.Azure.Management.Network.Models;
using System.Threading.Tasks;

namespace Azure.Experiments
namespace Azure.Experiments.Network
{
public sealed class NetworkSecurityGroupObject
: ResourceObject<NetworkSecurityGroup>
: NetworkObject<NetworkSecurityGroup>
{
public NetworkSecurityGroupObject(
INetworkManagementClient client,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@
using Microsoft.Azure.Management.Network.Models;
using System.Threading.Tasks;

namespace Azure.Experiments
namespace Azure.Experiments.Network
{
public sealed class PublicIpAddressObject :
ResourceObject<PublicIPAddress>
NetworkObject<PublicIPAddress>
{
public PublicIpAddressObject(
INetworkManagementClient client,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
using System.Linq;
using System.Threading.Tasks;

namespace Azure.Experiments
namespace Azure.Experiments.Network
{
public sealed class SubnetObject : AzureObject<Subnet>
public sealed class SubnetObject : AzureObject<Subnet, SubnetPolicy>
{
public string AddressPrefix { get; }

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using Microsoft.Azure.Management.Network.Models;

namespace Azure.Experiments
{
public struct SubnetPolicy : IInfoPolicy<Subnet>
{
public string GetLocation(Subnet value) => null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@
using Microsoft.Azure.Management.Network.Models;
using System.Threading.Tasks;

namespace Azure.Experiments
namespace Azure.Experiments.Network
{
public sealed class VirtualNetworkObject :
ResourceObject<VirtualNetwork>
NetworkObject<VirtualNetwork>
{
public VirtualNetworkObject(
INetworkManagementClient client,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
namespace Azure.Experiments
{
public sealed class ResourceGroupObject : AzureObject<
ResourceGroup>
ResourceGroup, ResourceGroupPolicy>
{
public ResourceGroupObject(Context client, string name)
: base(name, NoDependencies)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using Microsoft.Azure.Management.ResourceManager.Models;

namespace Azure.Experiments
{
public struct ResourceGroupPolicy : IInfoPolicy<ResourceGroup>
{
public string GetLocation(ResourceGroup value)
=> value.Location;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@

namespace Azure.Experiments
{
public abstract class ResourceObject<T> : AzureObject<T>
public abstract class ResourceObject<T, P> : AzureObject<T, P>
where T : class
where P : struct, IInfoPolicy<T>
{
public string ResourceGroupName { get; }

Expand Down
4 changes: 3 additions & 1 deletion experiments/Azure.Experiments/Tests/ComputeTest.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
using Xunit;
using Microsoft.Azure.Management.Compute;
using System.Threading.Tasks;
using Azure.Experiments.Network;
using Azure.Experiments.Compute;

namespace Azure.Experiments.Tests
{
Expand Down Expand Up @@ -79,7 +81,7 @@ public async Task VmObject()
var pia = new PublicIpAddressObject(network, "MyVM", rg);
var nsg = new NetworkSecurityGroupObject(network, "MyVM", rg);
var ni = new NetworkInterfaceObject(network, "MyVM", rg, subnet, pia, nsg);
var vm = new VmObject(c, "MyVM", rg, ni, "MyVMUser", "@3as54dDd");
var vm = new VirtualMachineObject(c, "MyVM", rg, ni, "MyVMUser", "@3as54dDd");
var info = await vm.GetOrCreateAsync();
}

Expand Down
12 changes: 7 additions & 5 deletions experiments/Azure.Experiments/Tests/UnitTests.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using Microsoft.Rest;
using Azure.Experiments.Compute;
using Azure.Experiments.Network;
using Microsoft.Rest;
using Xunit;

namespace Azure.Experiments.Tests
Expand All @@ -8,22 +10,22 @@ public class UnitTests
private static Context C { get; }
= new Context(new TokenCredentials("a"), string.Empty);

//[Fact]
[Fact]
public void ResourceGroupObjectTest()
{
var rg = new ResourceGroupObject(C, "My");
Assert.Equal(0, rg.Priority);
}

//[Fact]
[Fact]
public void VirtualNetworkObjectTest()
{
var rg = new ResourceGroupObject(C, "My1");
var vn = new VirtualNetworkObject(C.CreateNetwork(), "My1", rg, "192.168.0.0/16");
Assert.Equal(1, vn.Priority);
}

//[Fact]
[Fact]
public void PublicIpAddressObjectTest()
{
var rg = new ResourceGroupObject(C, "MyPIA");
Expand Down Expand Up @@ -72,7 +74,7 @@ public void VmObjectTest()
var pia = new PublicIpAddressObject(network, "MyVM", rg);
var nsg = new NetworkSecurityGroupObject(network, "MyVM", rg);
var ni = new NetworkInterfaceObject(network, "MyVM", rg, subnet, pia, nsg);
var vm = new VmObject(C, "MyVM", rg, ni, "MyVMUser", "@3as54dDd");
var vm = new VirtualMachineObject(C, "MyVM", rg, ni, "MyVMUser", "@3as54dDd");
Assert.Equal(4, vm.Priority);
}
}
Expand Down

0 comments on commit 94b26c2

Please sign in to comment.