Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ $vmComputerUser = $credentials.vmUser;
$password = ConvertTo-SecureString $vmComputerPassword -AsPlainText -Force;
$vmCredential = New-Object System.Management.Automation.PSCredential ($vmComputerUser, $password);

New-AzVm -Name MyVM -Credential $vmCredential -WhatIf
# New-AzVm -Name MyVM -Credential $vmCredential -WhatIf

# $job = New-AzVm -Name MyVMA -Credential $vmCredential -AsJob
# Receive-Job $job
Expand All @@ -37,4 +37,4 @@ $vm
# Write-Host "</async>"

# clean-up
Remove-AzureRmResourceGroup -ResourceId $vm.ResourceGroupId
Remove-AzureRmResourceGroup -Name $vm.ResourceGroupName
132 changes: 82 additions & 50 deletions experiments/Compute.Experiments/AzureRM.Compute.Experiments.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,20 @@ function New-AzVm {
[Parameter(Mandatory=$true, Position=0)][string] $Name = "VM",
[Parameter(Mandatory=$true)][PSCredential] $Credential,

[Parameter()][string] $ResourceGroupName,
[Parameter()][string] $ResourceGroupName = $Name,
[Parameter()][string] $Location,

[Parameter()][string] $VirtualNetworkName,
[Parameter()][string] $VirtualNetworkName = $Name,
[Parameter()][string] $AddressPrefix = "192.168.0.0/16",

[Parameter()][string] $SubnetName,
[Parameter()][string] $SubnetName = $Name,
[Parameter()][string] $SubnetAddressPrefix = "192.168.1.0/24",

[Parameter()][string] $PublicIpAddressName,
[Parameter()][string] $PublicIpAddressName = $Name,
[Parameter()][string] $DomainNameLabel = $Name,
[Parameter()][string] $AllocationMethod = "Static",

[Parameter()][string] $SecurityGroupName,
[Parameter()][string] $SecurityGroupName = $Name,
[Parameter()][int[]] $OpenPorts = @(3389, 5985),

[Parameter()][string] $ImageName = "Win2016Datacenter",
Expand All @@ -47,15 +47,15 @@ function New-AzVm {

# we don't allow to reuse NetworkInterface so $name is $null.
$nii = [NetworkInterface]::new(
$null,
$Name,
$rgi,
$subnet,
$piai,
$sgi)

# the purpouse of the New-AzVm cmdlet is to create (not get) a VM so $name is $null.
$vmi = [VirtualMachine]::new(
$null,
$Name,
$rgi,
$nii,
$Credential,
Expand All @@ -74,7 +74,7 @@ function New-AzVm {
$locationi.Value = $Location
}

$createParams = [CreateParams]::new($Name, $locationi.Value, $context)
$createParams = [CreateParams]::new($locationi.Value, $context)

if ($PSCmdlet.ShouldProcess($Name, "Creating a virtual machine")) {
if ($AsJob) {
Expand All @@ -91,13 +91,7 @@ function New-AzVm {
}
return Start-Job $script -ArgumentList $arguments
} else {
# Force to create Resource Group before anything else.
$rg = $rgi.GetOrCreate($createParams)
$vmResponse = $vmi.Create($createParams)
return [PSAzureVm]::new(
$rg.ResourceId,
$VirtualMachine.Name
)
return $vmi.GetOrCreate($createParams)
}
}
}
Expand All @@ -124,16 +118,13 @@ class Location {
}

class CreateParams {
[string] $Name;
[string] $Location;
[object] $Context;

CreateParams(
[string] $name,
[string] $location,
[object] $context)
{
$this.Name = $name
$this.Location = $location
$this.Context = $context
}
Expand All @@ -143,6 +134,8 @@ class AzureObject {
[string] $Name;
[AzureObject[]] $Children;
[int] $Priority;

[bool] $GetInfoCalled = $false;
[object] $info = $null;

AzureObject([string] $name, [AzureObject[]] $children) {
Expand All @@ -157,6 +150,10 @@ class AzureObject {
$this.Priority++
}

[string] GetResourceType() {
return $null
}

[object] GetInfoOrThrow([object] $context) {
return $null
}
Expand All @@ -165,9 +162,9 @@ class AzureObject {
return $null
}

# This function should be called only when $this.Name is not $null.
[object] GetInfo([object] $context) {
if (!$this.Info) {
if (!$this.GetInfoCalled) {
$this.GetInfoCalled = $true
try {
$this.Info = $this.GetInfoOrThrow($context)
} catch {
Expand Down Expand Up @@ -198,15 +195,8 @@ class AzureObject {
if ($i) {
return $i
}
if ($this.Name) {
$p = [CreateParams]::new(
$this.Name,
$p.Location,
$p.Context
)
}
Write-Host ("creating " + $this.GetResourceType() + ": " + $this.Name)
$this.Info = $this.Create($p)
$this.Name = $p.Name
return $this.Info
}
}
Expand All @@ -215,6 +205,10 @@ class ResourceGroup: AzureObject {
ResourceGroup([string] $name): base($name, @()) {
}

[string] GetResourceType() {
return "Resource Group"
}

[object] GetInfoOrThrow([object] $context) {
return Get-AzureRmResourceGroup `
-Name $this.Name `
Expand All @@ -224,7 +218,7 @@ class ResourceGroup: AzureObject {

[object] Create([CreateParams] $p) {
return New-AzureRmResourceGroup `
-Name $p.Name `
-Name $this.Name `
-Location $p.Location `
-AzureRmContext $p.Context `
-WarningAction SilentlyContinue `
Expand Down Expand Up @@ -266,8 +260,13 @@ class VirtualNetwork: Resource1 {
$this.AddressPrefix = $addressPrefix
}

[string] GetResourceType() {
return "Virtual Network"
}

[object] GetInfoOrThrow([object] $context) {
return Get-AzureRmVirtualNetwork `
-ResourceGroupName $this.ResourceGroup.Name `
-Name $this.Name `
-AzureRmContext $context `
-ErrorAction Stop
Expand All @@ -277,7 +276,7 @@ class VirtualNetwork: Resource1 {
return New-AzureRmVirtualNetwork `
-ResourceGroupName $this.GetResourceGroupName($p) `
-Location $p.Location `
-Name $p.Name `
-Name $this.Name `
-AddressPrefix $this.AddressPrefix `
-AzureRmContext $p.Context `
-WarningAction SilentlyContinue `
Expand All @@ -299,8 +298,13 @@ class PublicIpAddress: Resource1 {
$this.AllocationMethod = $allocationMethod
}

[string] GetResourceType() {
return "Public IP Address"
}

[object] GetInfoOrThrow([object] $context) {
return Get-AzureRMPublicIpAddress `
-ResourceGroupName $this.ResourceGroup.Name `
-Name $this.Name `
-AzureRmContext $context `
-ErrorAction Stop
Expand All @@ -310,7 +314,7 @@ class PublicIpAddress: Resource1 {
return New-AzureRmPublicIpAddress `
-ResourceGroupName $this.GetResourceGroupName($p) `
-Location $p.Location `
-Name $p.Name `
-Name $this.Name `
-DomainNameLabel $this.DomainNameLabel.ToLower() `
-AllocationMethod $this.AllocationMethod `
-AzureRmContext $p.Context `
Expand All @@ -330,8 +334,13 @@ class SecurityGroup: Resource1 {
$this.OpenPorts = $OpenPorts
}

[string] GetResourceType() {
return "Security Group"
}

[object] GetInfoOrThrow([object] $context) {
return Get-AzureRMSecurityGroup `
return Get-AzureRmNetworkSecurityGroup `
-ResourceGroupName $this.ResourceGroup.Name `
-Name $this.Name `
-AzureRmContext $context `
-ErrorAction Stop
Expand All @@ -342,7 +351,7 @@ class SecurityGroup: Resource1 {
"System.Collections.Generic.List[Microsoft.Azure.Commands.Network.Models.PSSecurityRule]"
$priority = 1000
foreach ($port in $this.OpenPorts) {
$name = $p.Name + $port
$name = $this.Name + $port
$securityRuleConfig = New-AzureRmNetworkSecurityRuleConfig `
-Name $name `
-Protocol "Tcp" `
Expand All @@ -360,7 +369,7 @@ class SecurityGroup: Resource1 {
return New-AzureRmNetworkSecurityGroup `
-ResourceGroupName $this.GetResourceGroupName($p) `
-Location $p.Location `
-Name $p.Name `
-Name $this.Name `
-SecurityRules $rules `
-AzureRmContext $p.Context `
-WarningAction SilentlyContinue `
Expand All @@ -378,28 +387,38 @@ class Subnet: AzureObject {
$this.SubnetAddressPrefix = $subnetAddressPrefix
}

[string] GetResourceType() {
return "Subnet"
}

[object] GetInfoFromVirtualNetworkInfo([object] $virtualNetworkInfo) {
return $virtualNetworkInfo `
| Get-AzureRmVirtualNetworkSubnetConfig -Name $this.Name -ErrorAction Stop
}

[object] GetInfoOrThrow([object] $context) {
$virutalNetworkInfo = $this.VirtualNetwork.GetInfo($context)
if (!$virutalNetworkInfo) {
return $null
$virtualNetworkInfo = $this.VirtualNetwork.GetInfo($context)
if ($virtualNetworkInfo) {
return $this.GetInfoFromVirtualNetworkInfo($virtualNetworkInfo)
}
return $virutalNetworkInfo `
| Get-AzureRmVirtualNetworkSubnetConfig -Name $this.Name -ErrorAction Stop
return $null
}

[object] Create([CreateParams] $p) {
$virtualNetworkInfo = $this.VirtualNetwork.GetOrCreate($p)
Add-AzureRmVirtualNetworkSubnetConfig `
try {
return $this.GetInfoFromVirtualNetworkInfo($virtualNetworkInfo)
} catch {
}
$virtualNetworkInfo = Add-AzureRmVirtualNetworkSubnetConfig `
-VirtualNetwork $virtualNetworkInfo `
-Name $p.Name `
-Name $this.Name `
-AddressPrefix $this.SubnetAddressPrefix
$virtualNetworkInfo = Set-AzureRmVirtualNetwork `
-VirtualNetwork $virtualNetworkInfo `
-AzureRmContext $p.Context `
-ErrorAction Stop
return Get-AzureRmVirtualNetworkSubnetConfig `
-VirtualNetwork $virtualNetworkInfo `
-Name $p.Name
return $this.GetInfoFromVirtualNetworkInfo($virtualNetworkInfo)
}
}

Expand All @@ -420,8 +439,13 @@ class NetworkInterface: Resource1 {
$this.SecurityGroup = $securityGroup
}

[string] GetResourceType() {
return "Network Interface"
}

[object] GetInfoOrThrow([object] $context) {
return Get-AzureRMNetworkInterface `
-ResourceGroupName $this.ResourceGroup.Name `
-Name $this.Name `
-AzureRmContext $context `
-ErrorAction Stop
Expand All @@ -434,7 +458,7 @@ class NetworkInterface: Resource1 {
return New-AzureRmNetworkInterface `
-ResourceGroupName $this.GetResourceGroupName($p) `
-Location $p.Location `
-Name $p.Name `
-Name $this.Name `
-PublicIpAddressId $publicIpAddressInfo.Id `
-SubnetId $subnetInfo.Id `
-NetworkSecurityGroupId $securityGroupInfo.Id `
Expand Down Expand Up @@ -468,8 +492,13 @@ class VirtualMachine: Resource1 {
$this.Size = $size
}

[string] GetResourceType() {
return "Virtual Machine"
}

[object] GetInfoOrThrow([object] $context) {
return Get-AzureRMVirtualMachine `
return Get-AzureRmVM `
-ResourceGroupName $this.ResourceGroup.Name `
-Name $this.Name `
-AzureRmContext $context `
-ErrorAction Stop
Expand All @@ -483,8 +512,8 @@ class VirtualMachine: Resource1 {
throw "Unknown image: " + $this.ImageName
}

$vmConfig = New-AzureRmVMConfig -VMName $p.Name -VMSize $this.Size -ErrorAction Stop
$vmComputerName = $p.Name
$vmConfig = New-AzureRmVMConfig -VMName $this.Name -VMSize $this.Size -ErrorAction Stop
$vmComputerName = $this.Name
switch ($vmImage.Type) {
"Windows" {
$vmConfig = $vmConfig | Set-AzureRmVMOperatingSystem `
Expand Down Expand Up @@ -514,13 +543,16 @@ class VirtualMachine: Resource1 {
-Id $networkInterfaceInstance.Id `
-ErrorAction Stop

return New-AzureRmVm `
-ResourceGroupName $this.GetResourceGroupName($p) `
$rgName = $this.GetResourceGroupName($p)
New-AzureRmVm `
-ResourceGroupName $rgName `
-Location $p.Location `
-VM $vmConfig `
-AzureRmContext $p.Context `
-WarningAction SilentlyContinue `
-ErrorAction Stop

return Get-AzureRmVM -ResourceGroupName $rgName -Name $this.Name
}
}

Expand Down