Skip to content

Commit

Permalink
VmObject.
Browse files Browse the repository at this point in the history
  • Loading branch information
sergey-shandar committed Oct 18, 2017
1 parent 5782a9b commit fb1539a
Show file tree
Hide file tree
Showing 2 changed files with 98 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,20 @@ public async Task NetworkInterfaceObject()
var info = await ni.GetOrCreateAsync(c);
}

[Fact]
public async Task VmObject()
{
var c = Credentials.Get();
var rg = new ResourceGroupObject("MyVM");
var vn = new VirtualNetworkObject("MyVM", rg, "192.168.0.0/16");
var subnet = new SubnetObject("MyVM", vn, "192.168.1.0/24");
var pia = new PublicIpAddressObject("MyVM", rg);
var nsg = new NetworkSecurityGroupObject("MyVM", rg);
var ni = new NetworkInterfaceObject("MyVM", rg, subnet, pia, nsg);
var vm = new VmObject("MyVM", rg, ni, "MyVMUser", "@3as54dDd");
var info = await vm.GetOrCreateAsync(c);
}

[Fact]
public async Task Test1()
{
Expand Down
84 changes: 84 additions & 0 deletions experiments/Azure.Experiments/Azure.Experiments/VmObject.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
using Microsoft.Azure.Management.Compute;
using Microsoft.Azure.Management.Compute.Models;
using System.Threading.Tasks;

namespace Azure.Experiments
{
public sealed class VmObject
: ResourceObject<VirtualMachine, IVirtualMachinesOperations>
{
public VmObject(
string name,
ResourceGroupObject rg,
NetworkInterfaceObject ni,
string adminUsername,
string adminPassword)
: base(name, rg, new[] { ni })
{
AdminUsername = adminUsername;
AdminPassword = adminPassword;
Ni = ni;
}

protected override Task<VirtualMachine> CreateAsync(
IVirtualMachinesOperations c)
=> c.CreateOrUpdateAsync(
ResourceGroupName,
Name,
new VirtualMachine
{
Location = "eastus",
OsProfile = new OSProfile
{
ComputerName = Name,
WindowsConfiguration = new WindowsConfiguration
{
},
AdminUsername = AdminUsername,
AdminPassword = AdminPassword,
},
NetworkProfile = new NetworkProfile
{
NetworkInterfaces = new NetworkInterfaceReference[]
{
new NetworkInterfaceReference(Ni.Info.Id)
}
},
HardwareProfile = new HardwareProfile
{
VmSize = "Standard_DS1_v2"
},
StorageProfile = new StorageProfile
{
ImageReference = new ImageReference
{
Publisher = "MicrosoftWindowsServer",
Offer = "WindowsServer",
Sku = "2016-Datacenter",
Version = "latest"
}
},
});

protected override IVirtualMachinesOperations CreateClient(Context c)
=> new ComputeManagementClient(c.Credentials)
{
SubscriptionId = c.SubscriptionId
}
.VirtualMachines;

protected override Task DeleteAsync(IVirtualMachinesOperations c)
{
throw new System.NotImplementedException();
}

protected override Task<VirtualMachine> GetOrThrowAsync(IVirtualMachinesOperations c)
=> c.GetAsync(ResourceGroupName, Name);

private string AdminUsername { get; }

private string AdminPassword { get; }

private NetworkInterfaceObject Ni { get; }
}
}

0 comments on commit fb1539a

Please sign in to comment.