Skip to content

Commit

Permalink
Yes, it works!
Browse files Browse the repository at this point in the history
  • Loading branch information
sergey-shandar committed Nov 15, 2017
1 parent 27525ee commit 61d92df
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using Microsoft.Azure.Management.Compute;
using Microsoft.Azure.Management.Compute.Models;
using Microsoft.Azure.Management.Network.Models;
using Microsoft.Azure.Management.ResourceManager.Models;

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

public static ResourceConfig<VirtualMachine> CreateVirtualMachineConfig(
this ResourceConfig<ResourceGroup> resourceGroup, string name)
=> Policy.CreateConfig(resourceGroup, name);
this ResourceConfig<ResourceGroup> resourceGroup,
string name,
ResourceConfig<NetworkInterface> networkInterface,
string adminUsername,
string adminPassword)
=> Policy.CreateConfig(
resourceGroup,
name,
subscription => new VirtualMachine
{
OsProfile = new OSProfile
{
ComputerName = name,
WindowsConfiguration = new WindowsConfiguration
{
},
AdminUsername = adminUsername,
AdminPassword = adminPassword,
},
NetworkProfile = new NetworkProfile
{
NetworkInterfaces = new[]
{
new NetworkInterfaceReference
{
Id = networkInterface.GetId(subscription).IdToString()
}
}
},
HardwareProfile = new HardwareProfile
{
VmSize = "Standard_DS1_v2"
},
StorageProfile = new StorageProfile
{
ImageReference = new ImageReference
{
Publisher = "MicrosoftWindowsServer",
Offer = "WindowsServer",
Sku = "2016-Datacenter",
Version = "latest"
}
},
},
new[] { networkInterface });
}
}
10 changes: 9 additions & 1 deletion experiments/Azure.Experiments/Tests/Client.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Microsoft.Azure.Management.Network;
using Microsoft.Azure.Management.Compute;
using Microsoft.Azure.Management.Network;
using Microsoft.Azure.Management.ResourceManager;
using System;

Expand Down Expand Up @@ -30,6 +31,13 @@ public T GetClient<T>()
SubscriptionId = Context.SubscriptionId
} as T;
}
else if (typeof(T) == typeof(IComputeManagementClient))
{
return new ComputeManagementClient(Context.Credentials)
{
SubscriptionId = Context.SubscriptionId
} as T;
}
throw new Exception("unknown client type");
}
}
Expand Down
36 changes: 36 additions & 0 deletions experiments/Azure.Experiments/Tests/VirtualMachineTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
using Microsoft.Azure.Experiments.Compute;
using Microsoft.Azure.Experiments.Network;
using Microsoft.Azure.Experiments.ResourceManager;
using System.Threading;
using System.Threading.Tasks;
using Xunit;

namespace Microsoft.Azure.Experiments.Tests
{
public sealed class VirtualMachineTest
{
[Fact]
public async Task CreateAsyncTest()
{
var rg = ResourceGroupPolicy.CreateResourceGroupConfig("vmrg");
var vn = rg.CreateVirtualNetworkConfig("Vnni", "192.168.0.0/16");
var sn = vn.CreateSubnet("mysubnet", "192.168.1.0/24");
var pipa = rg.CreatePublicIPAddressConfig("pipavm");
var nsg = rg.CreateNetworkSecurityGroupConfig("nsgvm");
var ni = rg.CreateNetworkInterfaceConfig("nivm", sn, pipa, nsg);
var vm = rg.CreateVirtualMachineConfig("vm", ni, "MyVMUser", "@3as54dDd");

var client = new Client(Credentials.Get());
var state = await vm.GetAsync(client, new CancellationToken());
var location = state.GetLocation(rg);
var parameters = vm.GetParameters(client.Context.SubscriptionId, "eastus");
var vmc = parameters.GetOrNull(vm);
var createState = await vm.CreateOrUpdateAsync(
client, state, parameters, new CancellationToken());
var vmcc = createState.GetOrNull(vm);
Assert.Equal("eastus", vmcc.Location);
Assert.Equal("vm", vmcc.Name);
Assert.Equal(vm.GetId(client.Context.SubscriptionId).IdToString(), vmcc.Id);
}
}
}

0 comments on commit 61d92df

Please sign in to comment.