Skip to content

Commit 96972d2

Browse files
classes
1 parent 6571649 commit 96972d2

File tree

1 file changed

+150
-63
lines changed

1 file changed

+150
-63
lines changed

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

Lines changed: 150 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,55 @@ function New-AzVm {
44
[Parameter()][PSCredential] $Credential,
55
[Parameter()][string] $Name = "VM",
66
[Parameter()][string] $ImageName = "Win2012R2Datacenter",
7-
[Parameter()][string] $ResourceGroupName = $Name + "ResourceGroup",
7+
[Parameter()][string] $ResourceGroupName,
88
[Parameter()][string] $Location = "eastus",
9-
[Parameter()][string] $VirtualNetworkName = $Name + "VirtualNetwork",
10-
[Parameter()][string] $PublicIpAddressName = $Name + "PublicIpAddress",
11-
[Parameter()][string] $SecurityGroupName = $Name + "SecurityGroup",
12-
[Parameter()][string] $NetworkInterfaceName = $Name + "NetworkInterface"
9+
[Parameter()][string] $VirtualNetworkName,
10+
[Parameter()][string] $PublicIpAddressName,
11+
[Parameter()][string] $SecurityGroupName,
12+
[Parameter()][string] $NetworkInterfaceName
1313
)
1414

1515
PROCESS {
16+
$rgi = [ResourceGroup]::new($ResourceGroupName)
17+
$nii = [NetworkInterface]::new(
18+
$NetworkInterfaceName,
19+
[VirtualNetwork]::new($VirtualNetworkName),
20+
[PublicIpAddress]::new($PublicIpAddressName),
21+
[SecurityGroup]::new($SecurityGroupName)
22+
);
23+
$vmi = [VirtualMachine]::new($null, $nii, $rgi);
24+
25+
$locationi = [Location]::new();
26+
if (-not $Location) {
27+
$vmi.UpdateLocation($locationi);
28+
if (-not $locationi.Value) {
29+
$locationi.Value = "eastus";
30+
}
31+
} else {
32+
$locationi.Value = $Location;
33+
}
34+
35+
# Resource Group
36+
$resourceGroup = $rgi.GetOrCreate($Name + "ResourceGroup", $locationi.Value);
37+
1638
if (-not $Credential) {
1739
$Credential = Get-Credential
1840
}
41+
if (-not $ResourceGroupName) {
42+
$ResourceGroupName = $Name + "ResourceGroup";
43+
}
44+
if (-not $VirtualNetworkName) {
45+
$VirtualNetworkName = $Name + "VirtualNetwork";
46+
}
47+
if (-not $PublicIpAddressName) {
48+
$PublicIpAddressName = $Name + "PublicIpAddress";
49+
}
50+
if (-not $SecurityGroupName) {
51+
$SecurityGroupName = $Name + "SecurityGroup";
52+
}
53+
if (-not $NetworkInterfaceName) {
54+
$NetworkInterfaceName = $Name + "NetworkInterface"
55+
}
1956

2057
# Find VM Image
2158
$vmImage = $images | Where-Object { $_.Name -eq $ImageName } | Select-Object -First 1
@@ -24,7 +61,7 @@ function New-AzVm {
2461
}
2562

2663
# Resource Group
27-
$resourceGroup = Set-ResourceGroup -Name $ResourceGroupName -Location $Location
64+
# $resourceGroup = Set-ResourceGroup -Name $ResourceGroupName -Location $Location
2865

2966
# Virtual Network
3067
$virtualNetworkAddressPrefix = "192.168.0.0/16"
@@ -139,83 +176,133 @@ function Set-ResourceGroup {
139176
}
140177
}
141178

142-
class Common {
143-
[string] $Location;
144-
[string] $ResourceGroupName;
179+
class Location {
180+
[int] $Priority;
181+
[string] $Value;
145182

146-
[void] Update([string] $location, [string] $resourceGroupName) {
147-
if (-not $this.Location) {
148-
$this.Location = $location
149-
}
150-
if (-not $this.ResourceGroupName) {
151-
$this.ResourceGroupName = $resourceGroupName
152-
}
183+
Location() {
184+
$this.Priority = 0;
185+
$this.Value = $null;
153186
}
154187
}
155188

156-
class VirtualNetwork {
189+
class AzureObject {
157190
[string] $Name;
191+
[AzureObject[]] $Children;
192+
[int] $Priority;
158193

159-
[bool] UpdateCommon([Common] $common) {
160-
if ($this.Name) {
161-
$virtualNetwork = Get-AzureRMVirtualNetwork `
162-
| Where-Object { $_.Name -eq $Name } `
163-
| Select-Object -First 1 -Wait;
164-
$common.Update($virtualNetwork.Location, $virtualNetwork.ResourceGroupName)
165-
return $true
194+
AzureObject([string] $name, [AzureObject[]] $children) {
195+
$this.Name = $name;
196+
$this.Children = $children;
197+
$this.Priority = 0;
198+
foreach ($child in $this.Children) {
199+
if ($this.Priority -lt $child.Priority) {
200+
$this.Priority = $child.Priority;
201+
}
166202
}
167-
return $false
203+
$this.Priority++;
168204
}
169-
}
170205

171-
class PublicIpAddress {
172-
[string] $Name;
206+
# This function should be called only when $this.Name is not $null.
207+
[object] GetInfo() {
208+
return $null;
209+
}
173210

174-
[bool] UpdateCommon([Common] $common) {
211+
[object] Create([string] $name, [string] $location) {
212+
return $null;
213+
}
214+
215+
[void] UpdateLocation([Location] $location) {
216+
if ($this.Priority -gt $location.Priority) {
217+
if ($this.Name) {
218+
$location.Value = $this.GetInfo().Location;
219+
$location.Priority = $this.Priority;
220+
} else {
221+
foreach ($child in $this.Children) {
222+
$child.UpdateLocation($location);
223+
}
224+
}
225+
}
226+
}
227+
228+
[object] GetOrCreate([string] $name, [string] $location) {
175229
if ($this.Name) {
176-
$virtualNetwork = Get-AzureRMPublicIpAddress `
177-
| Where-Object { $_.Name -eq $Name } `
178-
| Select-Object -First 1 -Wait;
179-
$common.Update($virtualNetwork.Location, $virtualNetwork.ResourceGroupName)
180-
return $true
230+
return $this.GetInfo();
231+
} else {
232+
$result = $this.Create($name, $location);
233+
$this.Name = $name;
234+
return $result;
181235
}
182-
return $false
183236
}
184237
}
185238

186-
class SecurityGroup {
187-
[string] $Name;
239+
class ResourceGroup: AzureObject {
240+
ResourceGroup([string] $name): base($name, @()) {
241+
}
188242

189-
[bool] UpdateCommon([Common] $common) {
190-
if ($this.Name) {
191-
$virtualNetwork = Get-AzureRMSecurityGroup `
192-
| Where-Object { $_.Name -eq $Name } `
193-
| Select-Object -First 1 -Wait;
194-
$common.Update($virtualNetwork.Location, $virtualNetwork.ResourceGroupName);
195-
return $true;
196-
}
197-
return $false;
243+
[object] GetInfo() {
244+
return Get-AzureRmResourceGroup -Name $this.Name;
245+
}
246+
247+
[object] Create([string] $name, [string] $location) {
248+
return New-AzureRmResourceGroup -Name $name -Location $location;
198249
}
199250
}
200251

201-
class NetworkInterface {
202-
[string] $Name;
203-
[VirtualNetwork] $VirtualNetwork;
204-
[PublicIpAddress] $PublicIpAddress;
205-
[SecurityGroup] $SecurityGroupName;
252+
class Resource1: AzureObject {
253+
Resource1([string] $name): base($name, @([ResourceGroup]::new($null))) {
254+
}
255+
}
206256

207-
[bool] UpdateCommon([Common] $common) {
208-
if ($this.Name) {
209-
$networkInterface = Get-AzureRMNetworkInterface `
210-
| Where-Object { $_.Name -eq $Name } `
211-
| Select-Object -First 1 -Wait;
212-
$common.Update($networkInterface.Location, $networkInterface.ResourceGroupName);
213-
return $true;
214-
} else {
215-
return $this.VirtualNetwork.UpdateCommon($common) `
216-
-or $this.PublicIpAddress.UpdateCommon($common) `
217-
-or $this.SecurityGroup.UpdateCommon($common);
218-
}
257+
class VirtualNetwork: Resource1 {
258+
VirtualNetwork([string] $name): base($name) {
259+
}
260+
261+
[object] GetInfo() {
262+
return Get-AzureRmVirtualNetwork -Name $this.Name;
263+
}
264+
}
265+
266+
class PublicIpAddress: Resource1 {
267+
PublicIpAddress([string] $name): base($name) {
268+
}
269+
270+
[object] GetInfo() {
271+
return Get-AzureRMPublicIpAddress -Name $this.Name;
272+
}
273+
}
274+
275+
class SecurityGroup: Resource1 {
276+
SecurityGroup([string] $name): base($name) {
277+
}
278+
279+
[object] GetInfo() {
280+
return Get-AzureRMSecurityGroup -Name $this.Name;
281+
}
282+
}
283+
284+
class NetworkInterface: AzureObject {
285+
NetworkInterface(
286+
[string] $name,
287+
[VirtualNetwork] $virtualNetwork,
288+
[PublicIpAddress] $publicIpAddress,
289+
[SecurityGroup] $securityGroup
290+
): base($name, @($virtualNetwork, $publicIpAddress, $securityGroup)) {
291+
}
292+
293+
[object] GetInfo() {
294+
return Get-AzureRMNetworkInterface -Name $this.Name;
295+
}
296+
}
297+
298+
class VirtualMachine: AzureObject {
299+
VirtualMachine(
300+
[string] $name, [NetworkInterface] $networkInterface, [ResourceGroup] $resourceGroup
301+
): base($name, @($networkInterface, $resourceGroup)) {
302+
}
303+
304+
[object] GetInfo() {
305+
return Get-AzureRMVirtualMachine -Name $this.Name;
219306
}
220307
}
221308

0 commit comments

Comments
 (0)