Skip to content

Commit 199077b

Browse files
Merge pull request #3 from sergey-shandar/sergey-compute-class
Sergey compute class
2 parents 4575ee6 + 315747d commit 199077b

File tree

4 files changed

+98
-68
lines changed

4 files changed

+98
-68
lines changed

experiments/Compute.Experiments/AzureRM.Compute.Experiments.Tests.ps1

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ $vmComputerUser = $credentials.vmUser;
1515
$password = ConvertTo-SecureString $vmComputerPassword -AsPlainText -Force;
1616
$vmCredential = New-Object System.Management.Automation.PSCredential ($vmComputerUser, $password);
1717

18+
New-AzVm -Name MyVM -Credential $vmCredential -WhatIf
19+
1820
# $vm = New-AzVm
1921
# $vm = New-AzVm -Credential $vmCredential
2022
$vm = New-AzVm -Name MyVM -Credential $vmCredential

experiments/Compute.Experiments/AzureRM.Compute.Experiments.psd1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
RootModule = ".\AzureRM.Compute.Experiments.psm1"
1313

1414
# Version number of this module.
15-
ModuleVersion = '1.0.4'
15+
ModuleVersion = '1.0.6'
1616

1717
# Supported PSEditions
1818
# CompatiblePSEditions = @()

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

Lines changed: 74 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -2,31 +2,36 @@
22
.ExternalHelp AzureRM.Compute.Experiments-help.xml
33
#>
44
function New-AzVm {
5-
[CmdletBinding()]
5+
[CmdletBinding(SupportsShouldProcess = $true)]
66
param (
7-
[Parameter(Mandatory=$true)][string] $Name = "VM",
7+
[Parameter(Mandatory=$true, Position=0)][string] $Name = "VM",
88
[Parameter()][PSCredential] $Credential,
99
[Parameter()][string] $ImageName = "Win2012R2Datacenter",
1010
[Parameter()][string] $ResourceGroupName,
1111
[Parameter()][string] $Location,
1212
[Parameter()][string] $VirtualNetworkName,
1313
[Parameter()][string] $PublicIpAddressName,
1414
[Parameter()][string] $SecurityGroupName
15-
# [Parameter()][string] $NetworkInterfaceName
1615
)
1716

1817
PROCESS {
1918
$rgi = [ResourceGroup]::new($ResourceGroupName);
19+
2020
$vni = [VirtualNetwork]::new($VirtualNetworkName);
2121
$piai = [PublicIpAddress]::new($PublicIpAddressName);
2222
$sgi = [SecurityGroup]::new($SecurityGroupName);
23+
24+
# we don't allow to reuse NetworkInterface so $name is $null.
2325
$nii = [NetworkInterface]::new(
24-
$null, # $NetworkInterfaceName,
26+
$null,
2527
$vni,
2628
$piai,
2729
$sgi);
30+
31+
# the purpouse of the New-AzVm cmdlet is to create (not get) a VM so $name is $null.
2832
$vmi = [VirtualMachine]::new($null, $nii, $rgi, $Credential, $ImageName, $images);
2933

34+
# infer a location
3035
$locationi = [Location]::new();
3136
if (-not $Location) {
3237
$vmi.UpdateLocation($locationi);
@@ -37,12 +42,16 @@ function New-AzVm {
3742
$locationi.Value = $Location;
3843
}
3944

40-
$resourceGroup = $rgi.GetOrCreate($Name + "ResourceGroup", $locationi.Value, $null);
41-
$vmResponse = $vmi.Create($Name, $locationi.Value, $resourceGroup.ResourceGroupName);
45+
$createParams = [CreateParams]::new($Name, $locationi.Value, $Name);
4246

43-
New-PsObject @{
44-
ResourceId = $resourceGroup.ResourceId;
45-
Response = $vmResponse;
47+
if ($PSCmdlet.ShouldProcess($Name, "Creating a virtual machine")) {
48+
$resourceGroup = $rgi.GetOrCreate($createParams);
49+
$vmResponse = $vmi.Create($createParams);
50+
51+
New-PsObject @{
52+
ResourceId = $resourceGroup.ResourceId;
53+
Response = $vmResponse;
54+
}
4655
}
4756
}
4857
}
@@ -57,6 +66,18 @@ class Location {
5766
}
5867
}
5968

69+
class CreateParams {
70+
[string] $Name;
71+
[string] $Location;
72+
[string] $ResourceGroupName;
73+
74+
CreateParams([string] $name, [string] $location, [string] $resourceGroupName) {
75+
$this.Name = $name;
76+
$this.Location = $location;
77+
$this.ResourceGroupName = $resourceGroupName;
78+
}
79+
}
80+
6081
class AzureObject {
6182
[string] $Name;
6283
[AzureObject[]] $Children;
@@ -79,7 +100,7 @@ class AzureObject {
79100
return $null;
80101
}
81102

82-
[object] Create([string] $name, [string] $location, [string] $resourceGroupName) {
103+
[object] Create([CreateParams] $p) {
83104
return $null;
84105
}
85106

@@ -96,12 +117,12 @@ class AzureObject {
96117
}
97118
}
98119

99-
[object] GetOrCreate([string] $name, [string] $location, [string] $resourceGroupName) {
120+
[object] GetOrCreate([CreateParams] $p) {
100121
if ($this.Name) {
101122
return $this.GetInfo();
102123
} else {
103-
$result = $this.Create($name, $location, $resourceGroupName);
104-
$this.Name = $name;
124+
$result = $this.Create($p);
125+
$this.Name = $p.Name;
105126
return $result;
106127
}
107128
}
@@ -115,8 +136,11 @@ class ResourceGroup: AzureObject {
115136
return Get-AzureRmResourceGroup -Name $this.Name;
116137
}
117138

118-
[object] Create([string] $name, [string] $location, [string] $resourceGroupName) {
119-
return New-AzureRmResourceGroup -Name $name -Location $location;
139+
[object] Create([CreateParams] $p) {
140+
return New-AzureRmResourceGroup `
141+
-Name $p.Name `
142+
-Location $p.Location `
143+
-WarningAction SilentlyContinue;
120144
}
121145
}
122146

@@ -133,16 +157,17 @@ class VirtualNetwork: Resource1 {
133157
return Get-AzureRmVirtualNetwork -Name $this.Name;
134158
}
135159

136-
[object] Create([string] $name, [string] $location, [string] $resourceGroupName) {
160+
[object] Create([CreateParams] $p) {
137161
$subnetConfig = New-AzureRmVirtualNetworkSubnetConfig `
138162
-Name "Subnet" `
139163
-AddressPrefix "192.168.1.0/24"
140164
return New-AzureRmVirtualNetwork `
141-
-ResourceGroupName $resourceGroupName `
142-
-Location $location `
143-
-Name $name `
165+
-ResourceGroupName $p.ResourceGroupName `
166+
-Location $p.Location `
167+
-Name $p.Name `
144168
-AddressPrefix "192.168.0.0/16" `
145-
-Subnet $subnetConfig
169+
-Subnet $subnetConfig `
170+
-WarningAction SilentlyContinue
146171
}
147172
}
148173

@@ -154,12 +179,13 @@ class PublicIpAddress: Resource1 {
154179
return Get-AzureRMPublicIpAddress -Name $this.Name;
155180
}
156181

157-
[object] Create([string] $name, [string] $location, [string] $resourceGroupName) {
182+
[object] Create([CreateParams] $p) {
158183
return New-AzureRmPublicIpAddress `
159-
-ResourceGroupName $resourceGroupName `
160-
-Location $location `
184+
-ResourceGroupName $p.ResourceGroupName `
185+
-Location $p.Location `
186+
-Name $p.Name `
161187
-AllocationMethod Static `
162-
-Name $name
188+
-WarningAction SilentlyContinue
163189
}
164190
}
165191

@@ -171,9 +197,9 @@ class SecurityGroup: Resource1 {
171197
return Get-AzureRMSecurityGroup -Name $this.Name;
172198
}
173199

174-
[object] Create([string] $name, [string] $location, [string] $resourceGroupName) {
200+
[object] Create([CreateParams] $p) {
175201
$securityRuleConfig = New-AzureRmNetworkSecurityRuleConfig `
176-
-Name $name `
202+
-Name $p.Name `
177203
-Protocol "Tcp" `
178204
-Priority 1000 `
179205
-Access "Allow" `
@@ -184,10 +210,11 @@ class SecurityGroup: Resource1 {
184210
-DestinationAddressPrefix "*"
185211

186212
return New-AzureRmNetworkSecurityGroup `
187-
-ResourceGroupName $resourceGroupName `
188-
-Location $location `
189-
-Name $name `
190-
-SecurityRules $securityRuleConfig
213+
-ResourceGroupName $p.ResourceGroupName `
214+
-Location $p.Location `
215+
-Name $p.Name `
216+
-SecurityRules $securityRuleConfig `
217+
-WarningAction SilentlyContinue
191218
}
192219
}
193220

@@ -211,17 +238,18 @@ class NetworkInterface: AzureObject {
211238
return Get-AzureRMNetworkInterface -Name $this.Name;
212239
}
213240

214-
[object] Create([string] $name, [string] $location, [string] $resourceGroupName) {
215-
$xpublicIpAddress = $this.PublicIpAddress.GetOrCreate($name, $location, $resourceGroupName);
216-
$xvirtualNetwork = $this.VirtualNetwork.GetOrCreate($name, $location, $resourceGroupName);
217-
$xsecurityGroup = $this.SecurityGroup.GetOrCreate($name, $location, $resourceGroupName);
241+
[object] Create([CreateParams] $p) {
242+
$xpublicIpAddress = $this.PublicIpAddress.GetOrCreate($p);
243+
$xvirtualNetwork = $this.VirtualNetwork.GetOrCreate($p);
244+
$xsecurityGroup = $this.SecurityGroup.GetOrCreate($p);
218245
return New-AzureRmNetworkInterface `
219-
-ResourceGroupName $resourceGroupName `
220-
-Location $location `
221-
-Name $name `
246+
-ResourceGroupName $p.ResourceGroupName `
247+
-Location $p.Location `
248+
-Name $p.Name `
222249
-PublicIpAddressId $xpublicIpAddress.Id `
223250
-SubnetId $xvirtualNetwork.Subnets[0].Id `
224-
-NetworkSecurityGroupId $xsecurityGroup.Id
251+
-NetworkSecurityGroupId $xsecurityGroup.Id `
252+
-WarningAction SilentlyContinue
225253
}
226254
}
227255

@@ -250,9 +278,8 @@ class VirtualMachine: AzureObject {
250278
return Get-AzureRMVirtualMachine -Name $this.Name;
251279
}
252280

253-
[object] Create([string] $name, [string] $location, [string] $resourceGroupName) {
254-
$networkInterfaceInstance = $this.NetworkInterface.GetOrCreate( `
255-
$name, $location, $resourceGroupName);
281+
[object] Create([CreateParams] $p) {
282+
$networkInterfaceInstance = $this.NetworkInterface.GetOrCreate($p);
256283

257284
if (-not $this.Credential) {
258285
$this.Credential = Get-Credential
@@ -264,8 +291,8 @@ class VirtualMachine: AzureObject {
264291
}
265292

266293
$vmSize = "Standard_DS2"
267-
$vmConfig = New-AzureRmVMConfig -VMName $Name -VMSize $vmSize
268-
$vmComputerName = $Name + "Computer"
294+
$vmConfig = New-AzureRmVMConfig -VMName $p.Name -VMSize $vmSize
295+
$vmComputerName = $p.Name + "Computer"
269296
switch ($vmImage.Type) {
270297
"Windows" {
271298
$vmConfig = $vmConfig | Set-AzureRmVMOperatingSystem `
@@ -291,9 +318,10 @@ class VirtualMachine: AzureObject {
291318
| Add-AzureRmVMNetworkInterface -Id $networkInterfaceInstance.Id
292319

293320
return New-AzureRmVm `
294-
-ResourceGroupName $resourceGroupName `
295-
-Location $location `
296-
-VM $vmConfig
321+
-ResourceGroupName $p.ResourceGroupName `
322+
-Location $p.Location `
323+
-VM $vmConfig `
324+
-WarningAction SilentlyContinue
297325
}
298326
}
299327

0 commit comments

Comments
 (0)