Skip to content

Commit 3f78229

Browse files
VM creation using classes
1 parent 144b3a6 commit 3f78229

File tree

1 file changed

+64
-51
lines changed

1 file changed

+64
-51
lines changed

experiments/Compute.Experiments/AzureRM.Compute.Experiments.psm1

Lines changed: 64 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ function New-AzVm {
2222
$vni,
2323
$piai,
2424
$sgi);
25-
$vmi = [VirtualMachine]::new($null, $nii, $rgi);
25+
$vmi = [VirtualMachine]::new($null, $nii, $rgi, $Credential, $ImageName, $images);
2626

2727
$locationi = [Location]::new();
2828
if (-not $Location) {
@@ -35,57 +35,11 @@ function New-AzVm {
3535
}
3636

3737
$resourceGroup = $rgi.GetOrCreate($Name + "ResourceGroup", $locationi.Value, $null);
38-
$networkInterface = $nii.GetOrCreate($Name, $locationi.Value, $resourceGroup.ResourceGroupName);
39-
40-
if (-not $Credential) {
41-
$Credential = Get-Credential
42-
}
43-
if (-not $ResourceGroupName) {
44-
$ResourceGroupName = $Name + "ResourceGroup";
45-
}
46-
47-
# Find VM Image
48-
$vmImage = $images | Where-Object { $_.Name -eq $ImageName } | Select-Object -First 1
49-
if (-not $vmImage) {
50-
throw "Unknown image: " + $ImageName
51-
}
52-
53-
# VM
54-
$vmSize = "Standard_DS2"
55-
$vmConfig = New-AzureRmVMConfig -VMName $Name -VMSize $vmSize
56-
$vmComputerName = $Name + "Computer"
57-
switch ($vmImage.Type) {
58-
"Windows" {
59-
$vmConfig = $vmConfig | Set-AzureRmVMOperatingSystem `
60-
-Windows `
61-
-ComputerName $vmComputerName `
62-
-Credential $Credential
63-
}
64-
"Linux" {
65-
$vmConfig = $vmConfig | Set-AzureRmVMOperatingSystem `
66-
-Linux `
67-
-ComputerName $vmComputerName `
68-
-Credential $Credential
69-
}
70-
}
71-
72-
$vmImageImage = $vmImage.Image
73-
$vmConfig = $vmConfig `
74-
| Set-AzureRmVMSourceImage `
75-
-PublisherName $vmImageImage.publisher `
76-
-Offer $vmImageImage.offer `
77-
-Skus $vmImageImage.sku `
78-
-Version $vmImageImage.version `
79-
| Add-AzureRmVMNetworkInterface -Id $networkInterface.Id
80-
81-
$response = New-AzureRmVm `
82-
-ResourceGroupName $ResourceGroupName `
83-
-Location $Location `
84-
-VM $vmConfig
38+
$vmResponse = $vmi.Create($Name, $locationi.Value, $resourceGroup.ResourceGroupName);
8539

8640
New-PsObject @{
8741
ResourceId = $resourceGroup.ResourceId;
88-
Response = $response
42+
Response = $vmResponse
8943
}
9044
}
9145
}
@@ -285,16 +239,75 @@ class NetworkInterface: AzureObject {
285239
}
286240

287241
class VirtualMachine: AzureObject {
242+
[NetworkInterface] $NetworkInterface;
243+
[pscredential] $Credential;
244+
[string] $ImageName;
245+
[object] $Images;
246+
288247
VirtualMachine(
289248
[string] $name,
290249
[NetworkInterface] $networkInterface,
291-
[ResourceGroup] $resourceGroup
292-
): base($name, @($networkInterface, $resourceGroup)) {
250+
[ResourceGroup] $resourceGroup,
251+
[PSCredential] $credential,
252+
[string] $imageName,
253+
[object] $images):
254+
base($name, @($networkInterface, $resourceGroup)) {
255+
256+
$this.Credential = $credential;
257+
$this.ImageName = $imageName;
258+
$this.NetworkInterface = $networkInterface;
259+
$this.Images = $images;
293260
}
294261

295262
[object] GetInfo() {
296263
return Get-AzureRMVirtualMachine -Name $this.Name;
297264
}
265+
266+
[object] Create([string] $name, [string] $location, [string] $resourceGroupName) {
267+
$networkInterfaceInstance = $this.NetworkInterface.GetOrCreate( `
268+
$name, $location, $resourceGroupName);
269+
270+
if (-not $this.Credential) {
271+
$this.Credential = Get-Credential
272+
}
273+
274+
$vmImage = $this.Images | Where-Object { $_.Name -eq $this.ImageName } | Select-Object -First 1
275+
if (-not $vmImage) {
276+
throw "Unknown image: " + $this.ImageName
277+
}
278+
279+
$vmSize = "Standard_DS2"
280+
$vmConfig = New-AzureRmVMConfig -VMName $Name -VMSize $vmSize
281+
$vmComputerName = $Name + "Computer"
282+
switch ($vmImage.Type) {
283+
"Windows" {
284+
$vmConfig = $vmConfig | Set-AzureRmVMOperatingSystem `
285+
-Windows `
286+
-ComputerName $vmComputerName `
287+
-Credential $this.Credential
288+
}
289+
"Linux" {
290+
$vmConfig = $vmConfig | Set-AzureRmVMOperatingSystem `
291+
-Linux `
292+
-ComputerName $vmComputerName `
293+
-Credential $this.Credential
294+
}
295+
}
296+
297+
$vmImageImage = $vmImage.Image
298+
$vmConfig = $vmConfig `
299+
| Set-AzureRmVMSourceImage `
300+
-PublisherName $vmImageImage.publisher `
301+
-Offer $vmImageImage.offer `
302+
-Skus $vmImageImage.sku `
303+
-Version $vmImageImage.version `
304+
| Add-AzureRmVMNetworkInterface -Id $networkInterfaceInstance.Id
305+
306+
return New-AzureRmVm `
307+
-ResourceGroupName $resourceGroupName `
308+
-Location $location `
309+
-VM $vmConfig
310+
}
298311
}
299312
function New-PsObject {
300313
param([hashtable] $property)

0 commit comments

Comments
 (0)