Skip to content

Commit

Permalink
Set-ResourceGroup
Browse files Browse the repository at this point in the history
  • Loading branch information
sergey-shandar committed Sep 6, 2017
1 parent 02692a7 commit a321a65
Show file tree
Hide file tree
Showing 3 changed files with 103 additions and 32 deletions.
88 changes: 72 additions & 16 deletions experiments/Compute.Experiments/Az.Compute.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -89,16 +89,33 @@ function New-AzVm {
# Create!

Write-Info "Ensuring resource group...";
$rg = Use-ResourceGroup -ResourceGroup $ResourceGroup -Location $Location;
$rg = Use-ResourceGroup `
-ResourceGroup $ResourceGroup `
-Location $Location;

Write-Info "Ensuring Vnet...";
$vnet = Use-Vnet -Name $VnetName -ResourceGroup $ResourceGroup -Location $Location -SubnetAddressPrefix $SubnetAddressPrefix -VnetAddressPrefix $VnetAddressPrefix;
$vnet = Use-Vnet `
-Name $VnetName `
-ResourceGroup $ResourceGroup `
-Location $Location `
-SubnetAddressPrefix $SubnetAddressPrefix `
-VnetAddressPrefix $VnetAddressPrefix;

Write-Info "Ensuring public IP...";
$pip = Use-Pip -Name $PublicIpName -ResourceGroup $ResourceGroup -Location $Location -DnsLabel $PublicIpDnsLabel -AllocationMethod $PublicIpAllocationMethod -IdleTimeoutInMinutes $PublicIpIdleTimeoutInMinutes;
$pip = Use-Pip `
-Name $PublicIpName `
-ResourceGroup $ResourceGroup `
-Location $Location `
-DnsLabel $PublicIpDnsLabel `
-AllocationMethod $PublicIpAllocationMethod `
-IdleTimeoutInMinutes $PublicIpIdleTimeoutInMinutes;

Write-Info "Ensuring NSG...";
$nsg = Use-Nsg -Name $NsgName -ResourceGroup $ResourceGroup -Location $Location -OpenPorts $NsgOpenPorts;
$nsg = Use-Nsg `
-Name $NsgName `
-ResourceGroup $ResourceGroup `
-Location $Location `
-OpenPorts $NsgOpenPorts;

Write-Info "Ensuring NIC...";
$nic = Use-Nic `
Expand All @@ -113,10 +130,14 @@ function New-AzVm {
# https://docs.microsoft.com/en-us/powershell/module/azurerm.compute/set-azurermvmosdisk?view=azurermps-4.2.0

# Create a virtual machine configuration
$vmConfig = New-AzureRmVMConfig -VMName $Name -VMSize $Size | `
Set-AzureRmVMOperatingSystem -Windows -ComputerName $Name -Credential $creds | `
Set-AzureRmVMSourceImage -PublisherName MicrosoftWindowsServer -Offer WindowsServer -Skus 2016-Datacenter -Version latest | `
Add-AzureRmVMNetworkInterface -Id $nic.Id
$vmConfig = New-AzureRmVMConfig -VMName $Name -VMSize $Size `
| Set-AzureRmVMOperatingSystem -Windows -ComputerName $Name -Credential $creds `
| Set-AzureRmVMSourceImage `
-PublisherName MicrosoftWindowsServer `
-Offer WindowsServer `
-Skus 2016-Datacenter `
-Version latest `
| Add-AzureRmVMNetworkInterface -Id $nic.Id

# Create a virtual machine
$vm = New-AzureRmVM -ResourceGroupName $resourceGroup -Location $location -VM $vmConfig
Expand Down Expand Up @@ -148,7 +169,9 @@ function Use-ResourceGroup {
[Parameter(Mandatory=$true)] [string] $Location
)

$rg = Get-AzureRmResourceGroup | Where-Object { $_.ResourceGroupName -eq $ResourceGroup } | Select-Object -First 1 -Wait;
$rg = Get-AzureRmResourceGroup `
| Where-Object { $_.ResourceGroupName -eq $ResourceGroup } `
| Select-Object -First 1 -Wait;

if($rg -eq $null) {
return New-AzureRmResourceGroup -Name $ResourceGroup -Location $Location;
Expand All @@ -170,10 +193,17 @@ function Use-Vnet {

if($vnet -eq $null) {
# Create a subnet configuration.
$subnetConfig = New-AzureRmVirtualNetworkSubnetConfig -Name "$($Name)Subnet" -AddressPrefix $SubnetAddressPrefix;
$subnetConfig = New-AzureRmVirtualNetworkSubnetConfig `
-Name "$($Name)Subnet" `
-AddressPrefix $SubnetAddressPrefix;

# Create a virtual network.
return New-AzureRmVirtualNetwork -ResourceGroupName $ResourceGroup -Location $Location -Name $Name -AddressPrefix $VnetAddressPrefix -Subnet $subnetConfig
return New-AzureRmVirtualNetwork `
-ResourceGroupName $ResourceGroup `
-Location $Location `
-Name $Name `
-AddressPrefix $VnetAddressPrefix `
-Subnet $subnetConfig
} else {
return $vnet;
}
Expand All @@ -193,7 +223,13 @@ function Use-Pip {

if($pip -eq $null) {
# Create a public IP address and specify a DNS name.
return New-AzureRmPublicIpAddress -ResourceGroupName $ResourceGroup -Location $Location -Name $Name -DomainNameLabel $DnsLabel -AllocationMethod $AllocationMethod -IdleTimeoutInMinutes $IdleTimeoutInMinutes;
return New-AzureRmPublicIpAddress `
-ResourceGroupName $ResourceGroup `
-Location $Location `
-Name $Name `
-DomainNameLabel $DnsLabel `
-AllocationMethod $AllocationMethod `
-IdleTimeoutInMinutes $IdleTimeoutInMinutes;
} else {
return $pip;
}
Expand All @@ -210,19 +246,33 @@ function Use-Nsg {
$nsg = Get-AzureRmNetworkSecurityGroup | Where-Object { $_.Name -eq $Name } | Select-Object -First 1 -Wait;

if($nsg -eq $null) {
$rules = New-Object "System.Collections.Generic.List[Microsoft.Azure.Commands.Network.Models.PSSecurityRule]";
$rules = New-Object `
"System.Collections.Generic.List[Microsoft.Azure.Commands.Network.Models.PSSecurityRule]";
$priority = 1000;

foreach($port in $OpenPorts)
{
$nsgRule = New-AzureRmNetworkSecurityRuleConfig -Name myNetworkSecurityGroupRuleRDP -Protocol Tcp -Direction Inbound -Priority $priority -SourceAddressPrefix * -SourcePortRange * -DestinationAddressPrefix * -DestinationPortRange $port -Access Allow;
$nsgRule = New-AzureRmNetworkSecurityRuleConfig `
-Name myNetworkSecurityGroupRuleRDP `
-Protocol Tcp `
-Direction Inbound `
-Priority $priority `
-SourceAddressPrefix * `
-SourcePortRange * `
-DestinationAddressPrefix * `
-DestinationPortRange $port `
-Access Allow;
$rules.Add($nsgRule);

$priority--;
}

# Create an NSG.
return New-AzureRmNetworkSecurityGroup -ResourceGroupName $ResourceGroup -Location $Location -Name $Name -SecurityRules $rules;
return New-AzureRmNetworkSecurityGroup `
-ResourceGroupName $ResourceGroup `
-Location $Location `
-Name $Name `
-SecurityRules $rules;
} else {
return $nsg;
}
Expand All @@ -242,7 +292,13 @@ function Use-Nic {

if($nic -eq $null) {
# Create a virtual network card and associate with public IP address and NSG
return New-AzureRmNetworkInterface -Name $Name -ResourceGroupName $resourceGroup -Location $location -SubnetId $SubnetId -PublicIpAddressId $PublicIpAddressId -NetworkSecurityGroupId $NetworkSecurityGroupId.ToString();
return New-AzureRmNetworkInterface `
-Name $Name `
-ResourceGroupName $resourceGroup `
-Location $location `
-SubnetId $SubnetId `
-PublicIpAddressId $PublicIpAddressId `
-NetworkSecurityGroupId $NetworkSecurityGroupId.ToString();
} else {
return $nic;
}
Expand Down
43 changes: 29 additions & 14 deletions experiments/Compute.Experiments/AzureRM.Compute.Experiments.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,8 @@ function New-AzVm {
throw "Unknown image: " + $ImageName
}

# Location
# $Location = (Get-AzureRmLocation | Select-Object -First 1 -Wait).Location

# Resource Group
$resource = New-AzureRmResourceGroup -Name $ResourceGroupName -Location $Location
$resourceGroup = Set-ResourceGroup -Name $ResourceGroupName -Location $Location

# Virtual Network
$virtualNetworkAddressPrefix = "192.168.0.0/16"
Expand Down Expand Up @@ -87,20 +84,20 @@ function New-AzVm {
-NetworkSecurityGroupId $securityGroup.Id

# VM
$vm = @{ Name = $Name; Size = "Standard_DS2" }
$vmConfig = New-AzureRmVMConfig -VMName $vm.Name -VMSize $vm.Size
$vmComputer = $vm.Name
$vmSize = "Standard_DS2"
$vmConfig = New-AzureRmVMConfig -VMName $Name -VMSize $vmSize
$vmComputerName = $Name + "Computer"
switch ($vmImage.Type) {
"Windows" {
$vmConfig = $vmConfig | Set-AzureRmVMOperatingSystem `
-Windows `
-ComputerName $vmComputer `
-ComputerName $vmComputerName `
-Credential $Credential
}
"Linux" {
$vmConfig = $vmConfig | Set-AzureRmVMOperatingSystem `
-Linux `
-ComputerName $vmComputer `
-ComputerName $vmComputerName `
-Credential $Credential
}
}
Expand All @@ -114,16 +111,34 @@ function New-AzVm {
-Version $vmImageImage.version `
| Add-AzureRmVMNetworkInterface -Id $networkInterface.Id

$response = New-AzureRmVm `
-ResourceGroupName $ResourceGroupName `
-Location $Location `
-VM $vmConfig

New-PsObject @{
ResourceId = $resource.ResourceId;
Response = New-AzureRmVm `
-ResourceGroupName $ResourceGroupName `
-Location $Location `
-VM $vmConfig
ResourceId = $resourceGroup.ResourceId;
Response = $response
}
}
}

function Set-ResourceGroup {
param(
[parameter(Mandatory = $true)][string]$Name,
[parameter(Mandatory = $true)][string]$Location
)

$resourceGroup = Get-AzureRmResourceGroup `
| Where-Object { $_.ResourceGroupName -eq $Name } `
| Select-Object -First 1 -Wait;
if ($resourceGroup) {
$resourceGroup;
} else {
New-AzureRmResourceGroup -Name $ResourceGroupName -Location $Location
}
}

function New-PsObject {
param([hashtable] $property)

Expand Down
4 changes: 2 additions & 2 deletions experiments/Compute.Experiments/publish-dev.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ $out = "..\build\AzureRM.Compute.Experiments\"
$repository = "sergey"
$dep = @("AzureRM.Resources", "AzureRM.Network", "AzureRM.Compute")
mkdir $out
copy .\AzureRM.Compute.Experiments.psd1 $out
copy .\AzureRM.Compute.Experiments.psm1 $out
Copy-Item .\AzureRM.Compute.Experiments.psd1 $out
Copy-Item .\AzureRM.Compute.Experiments.psm1 $out
foreach ($d in $dep) {
Install-Module $d -Repository $repository
}
Expand Down

0 comments on commit a321a65

Please sign in to comment.