|
| 1 | +# ---------------------------------------------------------------------------------- |
| 2 | +# |
| 3 | +# Copyright Microsoft Corporation |
| 4 | +# Licensed under the Apache License, Version 2.0 (the 'License'); |
| 5 | +# you may not use this file except in compliance with the License. |
| 6 | +# You may obtain a copy of the License at |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# Unless required by applicable law or agreed to in writing, software |
| 9 | +# distributed under the License is distributed on an 'AS IS' BASIS, |
| 10 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 | +# See the License for the specific language governing permissions and |
| 12 | +# limitations under the License. |
| 13 | +# ---------------------------------------------------------------------------------- |
| 14 | + |
| 15 | +<# |
| 16 | + .SYNOPSIS |
| 17 | + Creates a resource group for tests |
| 18 | +#> |
| 19 | +function Create-ResourceGroupForTest ($location = 'westus') |
| 20 | +{ |
| 21 | + $rgName = Get-ResourceGroupNameForTest |
| 22 | + $rg = New-AzResourceGroup -Name $rgName -Location $location -Force |
| 23 | + return $rg |
| 24 | +} |
| 25 | + |
| 26 | +<# |
| 27 | + .SYNOPSIS |
| 28 | + Removes the resource group that was used for testing |
| 29 | +#> |
| 30 | +function Remove-ResourceGroupForTest ($rg) |
| 31 | +{ |
| 32 | + Remove-AzResourceGroup -Name $rg.ResourceGroupName -Force |
| 33 | +} |
| 34 | + |
| 35 | +<# |
| 36 | + .SYNOPSIS |
| 37 | + Creates a virtual machine |
| 38 | +#> |
| 39 | +function Create-VM( |
| 40 | + [string] $resourceGroupName, |
| 41 | + [string] $vmName, |
| 42 | + [string] $location) |
| 43 | +{ |
| 44 | + $subnetName = $vmName + "subnet" |
| 45 | + $vnetName= $vmName + "vnet" |
| 46 | + $pipName = $vmName + "pip" |
| 47 | + # Create a subnet configuration |
| 48 | + $subnetConfig = New-AzVirtualNetworkSubnetConfig -Name $subnetName -AddressPrefix 192.168.1.0/24 |
| 49 | + |
| 50 | + # Create a virtual network |
| 51 | + $vnet = New-AzVirtualNetwork -ResourceGroupName $resourceGroupName -Location $location ` |
| 52 | + -Name $vnetName -AddressPrefix 192.168.0.0/16 -Subnet $subnetConfig |
| 53 | + $vnet = Get-AzVirtualNetwork -ResourceGroupName $resourceGroupName -Name $vnetName |
| 54 | + |
| 55 | + # Create a public IP address and specify a DNS name |
| 56 | + $pip = New-AzPublicIpAddress -ResourceGroupName $resourceGroupName -Location $location ` |
| 57 | + -AllocationMethod Static -IdleTimeoutInMinutes 4 -Name $pipName |
| 58 | + $pip = Get-AzPublicIpAddress -ResourceGroupName $resourceGroupName -Name $pipName |
| 59 | + |
| 60 | + # Rule to allow remote desktop (RDP) |
| 61 | + $nsgRuleRDP = New-AzNetworkSecurityRuleConfig -Name 'RDPRule' -Protocol Tcp ` |
| 62 | + -Direction Inbound -Priority 1000 -SourceAddressPrefix * -SourcePortRange * ` |
| 63 | + -DestinationAddressPrefix * -DestinationPortRange 3389 -Access Allow |
| 64 | + |
| 65 | + # Rule to allow SQL Server connections on port 1433 |
| 66 | + $nsgRuleSQL = New-AzNetworkSecurityRuleConfig -Name 'MSSQLRule' -Protocol Tcp ` |
| 67 | + -Direction Inbound -Priority 1001 -SourceAddressPrefix * -SourcePortRange * ` |
| 68 | + -DestinationAddressPrefix * -DestinationPortRange 1433 -Access Allow |
| 69 | + |
| 70 | + # Create the network security group |
| 71 | + $nsgName = $vmName + 'nsg' |
| 72 | + $nsg = New-AzNetworkSecurityGroup -ResourceGroupName $resourceGroupName ` |
| 73 | + -Location $location -Name $nsgName ` |
| 74 | + -SecurityRules $nsgRuleRDP,$nsgRuleSQL |
| 75 | + $nsg = Get-AzNetworkSecurityGroup -ResourceGroupName $resourceGroupName -Name $nsgName |
| 76 | + |
| 77 | + $interfaceName = $vmName + 'int' |
| 78 | + $subnetId = $vnet.Subnets[0].Id |
| 79 | + |
| 80 | + $interface = New-AzNetworkInterface -Name $interfaceName ` |
| 81 | + -ResourceGroupName $resourceGroupName -Location $location ` |
| 82 | + -SubnetId $subnetId -PublicIpAddressId $pip.Id ` |
| 83 | + -NetworkSecurityGroupId $nsg.Id |
| 84 | + $interface = Get-AzNetworkInterface -ResourceGroupName $resourceGroupName -Name $interfaceName |
| 85 | + |
| 86 | + $cred = Get-DefaultCredentialForTest |
| 87 | + |
| 88 | + # Create a virtual machine configuration |
| 89 | + $vmConfig = New-AzVMConfig -VMName $vmName -VMSize Standard_DS13_V2 | |
| 90 | + Set-AzVMOperatingSystem -Windows -ComputerName $vmName -Credential $cred -ProvisionVMAgent -EnableAutoUpdate | |
| 91 | + Set-AzVMSourceImage -PublisherName 'MicrosoftSQLServer' -Offer 'SQL2017-WS2016' -Skus 'Enterprise' -Version 'latest' | |
| 92 | + Add-AzVMNetworkInterface -Id $interface.Id |
| 93 | + |
| 94 | + return New-AzVM -ResourceGroupName $resourceGroupName -Location $location -VM $vmConfig |
| 95 | +} |
| 96 | + |
| 97 | +<# |
| 98 | + .SYNOPSIS |
| 99 | + Creates default credentials for testing |
| 100 | +#> |
| 101 | +function Get-DefaultCredentialForTest() |
| 102 | +{ |
| 103 | + $user = Get-DefaultUser |
| 104 | + $pswd = Get-DefaultPassword |
| 105 | + $securePswd = ConvertTo-SecureString -String $pswd -AsPlainText -Force |
| 106 | + return New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $user, $securePswd |
| 107 | +} |
| 108 | + |
| 109 | +function Get-LocationForTest() |
| 110 | +{ |
| 111 | + $location = Get-Location -providerNamespace "Microsoft.SqlVirtualMachine" -resourceType "SqlVirtualMachines" -preferredLocation "East US" |
| 112 | + return $location |
| 113 | +} |
| 114 | + |
| 115 | +function Get-ResourceGroupNameForTest() |
| 116 | +{ |
| 117 | + $nr = getAssetName "rg-" |
| 118 | + return $nr |
| 119 | +} |
| 120 | + |
| 121 | +function Get-SqlVirtualMachineGroupName() |
| 122 | +{ |
| 123 | + $nr = getAssetName "psgr" |
| 124 | + return $nr |
| 125 | +} |
| 126 | + |
| 127 | +function Get-DefaultUser() |
| 128 | +{ |
| 129 | + return 'myvmadmin' |
| 130 | +} |
| 131 | + |
| 132 | +function Get-DefaultSqlService() |
| 133 | +{ |
| 134 | + return 'sqlservice' |
| 135 | +} |
| 136 | + |
| 137 | +function Get-DefaultPassword() |
| 138 | +{ |
| 139 | + return getAssetName "Sql1@" |
| 140 | +} |
| 141 | + |
| 142 | +function Get-DomainForTest() |
| 143 | +{ |
| 144 | + return 'Domain' |
| 145 | +} |
| 146 | + |
| 147 | +function Get-StorageaccountNameForTest() |
| 148 | +{ |
| 149 | + $nr = getAssetName 'st' |
| 150 | + return $nr |
| 151 | +} |
| 152 | + |
| 153 | +<# |
| 154 | + .SYNOPSIS |
| 155 | + Checks that the sql virtual machines provided are equal |
| 156 | +#> |
| 157 | +function Validate-SqlVirtualMachine($sqlvm1, $sqlvm2) |
| 158 | +{ |
| 159 | + # tmp prevents output of assert to be returned if true |
| 160 | + $tmp = Assert-NotNull $sqlvm1 |
| 161 | + $tmp = Assert-NotNull $sqlvm2 |
| 162 | + |
| 163 | + $tmp = Assert-AreEqual $sqlvm1.ResourceId $sqlvm2.ResourceId |
| 164 | + $tmp = Assert-AreEqual $sqlvm1.Name $sqlvm2.Name |
| 165 | + $tmp = Assert-AreEqual $sqlvm1.ResourceGroupName $sqlvm2.ResourceGroupName |
| 166 | + $tmp = Assert-AreEqual $sqlvm1.SqlManagementType $sqlvm2.SqlManagementType |
| 167 | + $tmp = Assert-AreEqual $sqlvm1.LicenseType $sqlvm2.LicenseType |
| 168 | + $tmp = Assert-AreEqual $sqlvm1.Offer $sqlvm2.Offer |
| 169 | + $tmp = Assert-AreEqual $sqlvm1.Sku $sqlvm2.Sku |
| 170 | +} |
| 171 | + |
| 172 | +<# |
| 173 | + .SYNOPSIS |
| 174 | + Checks that the sql virtual machine groups provided are equal |
| 175 | +#> |
| 176 | +function Validate-SqlVirtualMachineGroup($group1, $group2) |
| 177 | +{ |
| 178 | + $tmp = Assert-NotNull $group1 |
| 179 | + $tmp = Assert-NotNull $group2 |
| 180 | + |
| 181 | + $tmp = Assert-AreEqual $group1.ResourceId $group2.ResourceId |
| 182 | + $tmp = Assert-AreEqual $group1.Name $group2.Name |
| 183 | + $tmp = Assert-AreEqual $group1.ResourceGroupName $group2.ResourceGroupName |
| 184 | + $tmp = Assert-AreEqual $group1.Offer $group2.Offer |
| 185 | + $tmp = Assert-AreEqual $group1.Sku $group2.Sku |
| 186 | +} |
| 187 | + |
| 188 | +<# |
| 189 | + .SYNOPSIS |
| 190 | + Gets a default WsfcDomainProfile for testing |
| 191 | +#> |
| 192 | +function Get-WsfcDomainProfileForTest( |
| 193 | + [string] $resourceGroupName, |
| 194 | + [string] $location, |
| 195 | + [string] $user, |
| 196 | + [string] $sqllogin, |
| 197 | + [string] $domainName, |
| 198 | + [string] $blobAccount, |
| 199 | + [string] $storageAccountKey |
| 200 | +) |
| 201 | +{ |
| 202 | + $props = @{ |
| 203 | + DomainFqdn = $domainName + '.com' |
| 204 | + ClusterOperatorAccount = $user + '@' + $domainName + '.com' |
| 205 | + ClusterBootstrapAccount = $user + '@' + $domainName + '.com' |
| 206 | + |
| 207 | + SqlServiceAccount = $sqllogin + '@' + $domainName + '.com' |
| 208 | + StorageAccountUrl = $blobAccount |
| 209 | + StorageAccountPrimaryKey = $storageAccountKey |
| 210 | + } |
| 211 | + return new-object Microsoft.Azure.Management.SqlVirtualMachine.Models.WsfcDomainProfile -Property $props |
| 212 | +} |
| 213 | + |
| 214 | +<# |
| 215 | + .SYNOPSIS |
| 216 | + Creates a sql virtual machine |
| 217 | +#> |
| 218 | +function Create-SqlVM ( |
| 219 | + [string] $resourceGroupName, |
| 220 | + [string] $vmName, |
| 221 | + [string] $location |
| 222 | +) |
| 223 | +{ |
| 224 | + $vm = Create-VM $resourceGroupName $vmName $location |
| 225 | + $sqlvm = New-AzSqlVM -ResourceGroupName $resourceGroupName -Name $vmName -LicenseType 'PAYG' -Sku 'Enterprise' -Location $location |
| 226 | + $sqlvm = Get-AzSqlVM -ResourceGroupName $resourceGroupName -Name $vmName |
| 227 | + return $sqlvm |
| 228 | +} |
| 229 | + |
| 230 | +<# |
| 231 | + .SYNOPSIS |
| 232 | + Creates a sql virtual machine group |
| 233 | +#> |
| 234 | +function Create-SqlVMGroup( |
| 235 | + [string] $resourceGroupName, |
| 236 | + [string] $groupName, |
| 237 | + [string] $location |
| 238 | +) |
| 239 | +{ |
| 240 | + $storageAccountName = Get-StorageaccountNameForTest |
| 241 | + $storageAccount = New-AzStorageAccount -ResourceGroupName $resourceGroupName -Name $storageAccountName -Location $location -Type Standard_LRS -Kind StorageV2 |
| 242 | + $storageAccount = Get-AzStorageAccount -ResourceGroupName $resourceGroupName -Name $storageAccountName |
| 243 | + $tmp = Assert-NotNull $storageAccount |
| 244 | + $tmp = Assert-NotNull $storageAccount.PrimaryEndpoints |
| 245 | + $tmp = Assert-NotNull $storageAccount.PrimaryEndpoints.Blob |
| 246 | + |
| 247 | + $storageAccountKey = (Get-AzStorageAccountKey -ResourceGroupName $resourceGroupName -Name $storageAccountName).Value[0] |
| 248 | + $blobAccount = $storageAccount.PrimaryEndpoints.Blob |
| 249 | + |
| 250 | + $user = Get-DefaultUser |
| 251 | + $domain = Get-DomainForTest |
| 252 | + $sqllogin = Get-DefaultSqlService |
| 253 | + $profile = Get-WsfcDomainProfileForTest $resourceGroupName $location $user $sqllogin $domain $blobAccount $storageAccountKey |
| 254 | + |
| 255 | + $secureKey = ConvertTo-SecureString $profile.StorageAccountPrimaryKey -AsPlainText -Force |
| 256 | + |
| 257 | + $group = New-AzSqlVMGroup -ResourceGroupName $resourceGroupName -Name $groupName -Location $location -ClusterOperatorAccount $profile.ClusterOperatorAccount ` |
| 258 | + -ClusterBootstrapAccount $profile.ClusterBootstrapAccount ` |
| 259 | + -SqlServiceAccount $profile.SqlServiceAccount -StorageAccountUrl $profile.StorageAccountUrl ` |
| 260 | + -StorageAccountPrimaryKey $secureKey -DomainFqdn $profile.DomainFqdn ` |
| 261 | + -Offer 'SQL2017-WS2016' -Sku 'Enterprise' |
| 262 | + $group = Get-AzSqlVMGroup -ResourceGroupName $resourceGroupName -Name $groupName |
| 263 | + return $group |
| 264 | +} |
0 commit comments