diff --git a/experiments/Compute.Experiments/AzureRM.Compute.Experiments.Tests.ps1 b/experiments/Compute.Experiments/AzureRM.Compute.Experiments.Tests.ps1 index 7129be2d677d..4c821faadc15 100644 --- a/experiments/Compute.Experiments/AzureRM.Compute.Experiments.Tests.ps1 +++ b/experiments/Compute.Experiments/AzureRM.Compute.Experiments.Tests.ps1 @@ -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 @@ -37,4 +37,4 @@ $vm # Write-Host "" # clean-up -Remove-AzureRmResourceGroup -ResourceId $vm.ResourceGroupId \ No newline at end of file +Remove-AzureRmResourceGroup -Name $vm.ResourceGroupName \ No newline at end of file diff --git a/experiments/Compute.Experiments/AzureRM.Compute.Experiments.psm1 b/experiments/Compute.Experiments/AzureRM.Compute.Experiments.psm1 index b82bfef37acf..4a74079d3d3d 100644 --- a/experiments/Compute.Experiments/AzureRM.Compute.Experiments.psm1 +++ b/experiments/Compute.Experiments/AzureRM.Compute.Experiments.psm1 @@ -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", @@ -47,7 +47,7 @@ function New-AzVm { # we don't allow to reuse NetworkInterface so $name is $null. $nii = [NetworkInterface]::new( - $null, + $Name, $rgi, $subnet, $piai, @@ -55,7 +55,7 @@ function New-AzVm { # 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, @@ -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) { @@ -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) } } } @@ -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 } @@ -143,6 +134,8 @@ class AzureObject { [string] $Name; [AzureObject[]] $Children; [int] $Priority; + + [bool] $GetInfoCalled = $false; [object] $info = $null; AzureObject([string] $name, [AzureObject[]] $children) { @@ -157,6 +150,10 @@ class AzureObject { $this.Priority++ } + [string] GetResourceType() { + return $null + } + [object] GetInfoOrThrow([object] $context) { return $null } @@ -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 { @@ -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 } } @@ -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 ` @@ -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 ` @@ -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 @@ -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 ` @@ -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 @@ -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 ` @@ -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 @@ -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" ` @@ -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 ` @@ -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) } } @@ -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 @@ -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 ` @@ -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 @@ -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 ` @@ -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 } }