Skip to content

Commit 61d92df

Browse files
Yes, it works!
1 parent 27525ee commit 61d92df

File tree

3 files changed

+91
-3
lines changed

3 files changed

+91
-3
lines changed

experiments/Azure.Experiments/Azure.Experiments/Compute/VirtualMachinePolicy.cs

Lines changed: 46 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using Microsoft.Azure.Management.Compute;
22
using Microsoft.Azure.Management.Compute.Models;
3+
using Microsoft.Azure.Management.Network.Models;
34
using Microsoft.Azure.Management.ResourceManager.Models;
45

56
namespace Microsoft.Azure.Experiments.Compute
@@ -16,7 +17,50 @@ public static class VirtualMachinePolicy
1617
p.ResourceGroupName, p.Name, p.Config, p.CancellationToken));
1718

1819
public static ResourceConfig<VirtualMachine> CreateVirtualMachineConfig(
19-
this ResourceConfig<ResourceGroup> resourceGroup, string name)
20-
=> Policy.CreateConfig(resourceGroup, name);
20+
this ResourceConfig<ResourceGroup> resourceGroup,
21+
string name,
22+
ResourceConfig<NetworkInterface> networkInterface,
23+
string adminUsername,
24+
string adminPassword)
25+
=> Policy.CreateConfig(
26+
resourceGroup,
27+
name,
28+
subscription => new VirtualMachine
29+
{
30+
OsProfile = new OSProfile
31+
{
32+
ComputerName = name,
33+
WindowsConfiguration = new WindowsConfiguration
34+
{
35+
},
36+
AdminUsername = adminUsername,
37+
AdminPassword = adminPassword,
38+
},
39+
NetworkProfile = new NetworkProfile
40+
{
41+
NetworkInterfaces = new[]
42+
{
43+
new NetworkInterfaceReference
44+
{
45+
Id = networkInterface.GetId(subscription).IdToString()
46+
}
47+
}
48+
},
49+
HardwareProfile = new HardwareProfile
50+
{
51+
VmSize = "Standard_DS1_v2"
52+
},
53+
StorageProfile = new StorageProfile
54+
{
55+
ImageReference = new ImageReference
56+
{
57+
Publisher = "MicrosoftWindowsServer",
58+
Offer = "WindowsServer",
59+
Sku = "2016-Datacenter",
60+
Version = "latest"
61+
}
62+
},
63+
},
64+
new[] { networkInterface });
2165
}
2266
}

experiments/Azure.Experiments/Tests/Client.cs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using Microsoft.Azure.Management.Network;
1+
using Microsoft.Azure.Management.Compute;
2+
using Microsoft.Azure.Management.Network;
23
using Microsoft.Azure.Management.ResourceManager;
34
using System;
45

@@ -30,6 +31,13 @@ public T GetClient<T>()
3031
SubscriptionId = Context.SubscriptionId
3132
} as T;
3233
}
34+
else if (typeof(T) == typeof(IComputeManagementClient))
35+
{
36+
return new ComputeManagementClient(Context.Credentials)
37+
{
38+
SubscriptionId = Context.SubscriptionId
39+
} as T;
40+
}
3341
throw new Exception("unknown client type");
3442
}
3543
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
using Microsoft.Azure.Experiments.Compute;
2+
using Microsoft.Azure.Experiments.Network;
3+
using Microsoft.Azure.Experiments.ResourceManager;
4+
using System.Threading;
5+
using System.Threading.Tasks;
6+
using Xunit;
7+
8+
namespace Microsoft.Azure.Experiments.Tests
9+
{
10+
public sealed class VirtualMachineTest
11+
{
12+
[Fact]
13+
public async Task CreateAsyncTest()
14+
{
15+
var rg = ResourceGroupPolicy.CreateResourceGroupConfig("vmrg");
16+
var vn = rg.CreateVirtualNetworkConfig("Vnni", "192.168.0.0/16");
17+
var sn = vn.CreateSubnet("mysubnet", "192.168.1.0/24");
18+
var pipa = rg.CreatePublicIPAddressConfig("pipavm");
19+
var nsg = rg.CreateNetworkSecurityGroupConfig("nsgvm");
20+
var ni = rg.CreateNetworkInterfaceConfig("nivm", sn, pipa, nsg);
21+
var vm = rg.CreateVirtualMachineConfig("vm", ni, "MyVMUser", "@3as54dDd");
22+
23+
var client = new Client(Credentials.Get());
24+
var state = await vm.GetAsync(client, new CancellationToken());
25+
var location = state.GetLocation(rg);
26+
var parameters = vm.GetParameters(client.Context.SubscriptionId, "eastus");
27+
var vmc = parameters.GetOrNull(vm);
28+
var createState = await vm.CreateOrUpdateAsync(
29+
client, state, parameters, new CancellationToken());
30+
var vmcc = createState.GetOrNull(vm);
31+
Assert.Equal("eastus", vmcc.Location);
32+
Assert.Equal("vm", vmcc.Name);
33+
Assert.Equal(vm.GetId(client.Context.SubscriptionId).IdToString(), vmcc.Id);
34+
}
35+
}
36+
}

0 commit comments

Comments
 (0)