Skip to content

Commit fb1539a

Browse files
VmObject.
1 parent 5782a9b commit fb1539a

File tree

2 files changed

+98
-0
lines changed

2 files changed

+98
-0
lines changed

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,20 @@ public async Task NetworkInterfaceObject()
6767
var info = await ni.GetOrCreateAsync(c);
6868
}
6969

70+
[Fact]
71+
public async Task VmObject()
72+
{
73+
var c = Credentials.Get();
74+
var rg = new ResourceGroupObject("MyVM");
75+
var vn = new VirtualNetworkObject("MyVM", rg, "192.168.0.0/16");
76+
var subnet = new SubnetObject("MyVM", vn, "192.168.1.0/24");
77+
var pia = new PublicIpAddressObject("MyVM", rg);
78+
var nsg = new NetworkSecurityGroupObject("MyVM", rg);
79+
var ni = new NetworkInterfaceObject("MyVM", rg, subnet, pia, nsg);
80+
var vm = new VmObject("MyVM", rg, ni, "MyVMUser", "@3as54dDd");
81+
var info = await vm.GetOrCreateAsync(c);
82+
}
83+
7084
[Fact]
7185
public async Task Test1()
7286
{
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
using Microsoft.Azure.Management.Compute;
2+
using Microsoft.Azure.Management.Compute.Models;
3+
using System.Threading.Tasks;
4+
5+
namespace Azure.Experiments
6+
{
7+
public sealed class VmObject
8+
: ResourceObject<VirtualMachine, IVirtualMachinesOperations>
9+
{
10+
public VmObject(
11+
string name,
12+
ResourceGroupObject rg,
13+
NetworkInterfaceObject ni,
14+
string adminUsername,
15+
string adminPassword)
16+
: base(name, rg, new[] { ni })
17+
{
18+
AdminUsername = adminUsername;
19+
AdminPassword = adminPassword;
20+
Ni = ni;
21+
}
22+
23+
protected override Task<VirtualMachine> CreateAsync(
24+
IVirtualMachinesOperations c)
25+
=> c.CreateOrUpdateAsync(
26+
ResourceGroupName,
27+
Name,
28+
new VirtualMachine
29+
{
30+
Location = "eastus",
31+
OsProfile = new OSProfile
32+
{
33+
ComputerName = Name,
34+
WindowsConfiguration = new WindowsConfiguration
35+
{
36+
},
37+
AdminUsername = AdminUsername,
38+
AdminPassword = AdminPassword,
39+
},
40+
NetworkProfile = new NetworkProfile
41+
{
42+
NetworkInterfaces = new NetworkInterfaceReference[]
43+
{
44+
new NetworkInterfaceReference(Ni.Info.Id)
45+
}
46+
},
47+
HardwareProfile = new HardwareProfile
48+
{
49+
VmSize = "Standard_DS1_v2"
50+
},
51+
StorageProfile = new StorageProfile
52+
{
53+
ImageReference = new ImageReference
54+
{
55+
Publisher = "MicrosoftWindowsServer",
56+
Offer = "WindowsServer",
57+
Sku = "2016-Datacenter",
58+
Version = "latest"
59+
}
60+
},
61+
});
62+
63+
protected override IVirtualMachinesOperations CreateClient(Context c)
64+
=> new ComputeManagementClient(c.Credentials)
65+
{
66+
SubscriptionId = c.SubscriptionId
67+
}
68+
.VirtualMachines;
69+
70+
protected override Task DeleteAsync(IVirtualMachinesOperations c)
71+
{
72+
throw new System.NotImplementedException();
73+
}
74+
75+
protected override Task<VirtualMachine> GetOrThrowAsync(IVirtualMachinesOperations c)
76+
=> c.GetAsync(ResourceGroupName, Name);
77+
78+
private string AdminUsername { get; }
79+
80+
private string AdminPassword { get; }
81+
82+
private NetworkInterfaceObject Ni { get; }
83+
}
84+
}

0 commit comments

Comments
 (0)