Skip to content

Commit f2ccdd3

Browse files
msJinLeiwyunchi-ms
andauthored
Add cmdlet Test-AzPostgreSqlFlexibleServerConnect (#14096)
* Add cmdlet Test-AzPostgreSqlFlexibleServerConnect * Update ChangeLog.md Co-authored-by: Yunchi Wang <[email protected]>
1 parent 930959c commit f2ccdd3

15 files changed

+1070
-24
lines changed

src/PostgreSql/Az.PostgreSql.psd1

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
#
44
# Generated by: Microsoft Corporation
55
#
6-
# Generated on: 1/14/2021
6+
# Generated on: 2/2/2021
77
#
88

99
@{
@@ -45,7 +45,7 @@ PowerShellVersion = '5.1'
4545
DotNetFrameworkVersion = '4.7.2'
4646

4747
# Minimum version of the common language runtime (CLR) required by this module. This prerequisite is valid for the PowerShell Desktop edition only.
48-
# CLRVersion = ''
48+
# ClrVersion = ''
4949

5050
# Processor architecture (None, X86, Amd64) required by this module
5151
# ProcessorArchitecture = ''
@@ -90,6 +90,7 @@ FunctionsToExport = 'Get-AzPostgreSqlConfiguration', 'Get-AzPostgreSqlConnection
9090
'Restore-AzPostgreSqlFlexibleServer', 'Restore-AzPostgreSqlServer',
9191
'Start-AzPostgreSqlFlexibleServer',
9292
'Stop-AzPostgreSqlFlexibleServer',
93+
'Test-AzPostgreSqlFlexibleServerConnect',
9394
'Update-AzPostgreSqlConfiguration',
9495
'Update-AzPostgreSqlFirewallRule',
9596
'Update-AzPostgreSqlFlexibleServer',
@@ -134,8 +135,7 @@ PrivateData = @{
134135
# IconUri = ''
135136

136137
# ReleaseNotes of this module
137-
ReleaseNotes = '* Add parameter MinimalTlsVersion
138-
* First version of flexible server'
138+
# ReleaseNotes = ''
139139

140140
# Prerelease string of this module
141141
# Prerelease = ''

src/PostgreSql/ChangeLog.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
- Additional information about change #1
1919
-->
2020
## Upcoming Release
21+
* Added cmdlet `Test-AzPostgreSqlFlexibleServerConnect`
2122

2223
## Version 0.3.0
2324
* Add parameter MinimalTlsVersion

src/PostgreSql/custom/New-AzPostgreSqlFlexibleServer.ps1

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,6 @@ function New-AzPostgreSqlFlexibleServer {
192192
$Password = Get-GeneratePassword
193193
$PSBoundParameters.AdministratorLoginPassword = $Password | ConvertTo-SecureString -AsPlainText -Force
194194
}
195-
$PSBoundParameters.AdministratorLoginPassword = . "$PSScriptRoot/../utils/Unprotect-SecureString.ps1" $PSBoundParameters['AdministratorLoginPassword']
196195

197196
Import-Module -Name Az.Resources
198197

@@ -304,7 +303,7 @@ function New-AzPostgreSqlFlexibleServer {
304303
if (![string]::IsNullOrEmpty($FirewallRuleName)) {
305304
$Server.FirewallRuleName = $FirewallRuleName
306305
}
307-
$Server.SecuredPassword = $PSBoundParameters.AdministratorLoginPassword | ConvertTo-SecureString -AsPlainText -Force
306+
$Server.SecuredPassword = $PSBoundParameters.AdministratorLoginPassword
308307

309308
return $Server
310309
} catch {
@@ -527,7 +526,7 @@ function CreateFirewallRule($FirewallRuleParameters) {
527526
}
528527
return $FirewallRule.Name
529528
}
530-
elseif ($Parameters.ContainsKey('PublicAccess') -And $Parameters.PublicAccess.ToLower() -ne 'none') {
529+
elseif ($FirewallRuleParameters.ContainsKey('PublicAccess') -And $FirewallRuleParameters.PublicAccess.ToLower() -eq 'none') {
531530
Write-Host "No firewall rule was set"
532531
}
533532

src/PostgreSql/custom/New-AzPostgreSqlServer.ps1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,7 @@ function New-AzPostgreSqlServer {
224224
$Parameter.Property.AdministratorLogin = $PSBoundParameters['AdministratorUserName']
225225
$null = $PSBoundParameters.Remove('AdministratorUserName')
226226

227-
$Parameter.Property.AdministratorLoginPassword = . "$PSScriptRoot/../utils/Unprotect-SecureString.ps1" $PSBoundParameters['AdministratorLoginPassword']
227+
$Parameter.Property.AdministratorLoginPassword = $PSBoundParameters['AdministratorLoginPassword']
228228
$null = $PSBoundParameters.Remove('AdministratorLoginPassword')
229229

230230
$PSBoundParameters.Add('Parameter', $Parameter)
Lines changed: 165 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,165 @@
1+
2+
# ----------------------------------------------------------------------------------
3+
#
4+
# Copyright Microsoft Corporation
5+
# Licensed under the Apache License, Version 2.0 (the "License");
6+
# you may not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
# http://www.apache.org/licenses/LICENSE-2.0
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
# ----------------------------------------------------------------------------------
15+
16+
function Test-AzPostgreSqlFlexibleServerConnect {
17+
[OutputType([System.String])]
18+
[CmdletBinding(DefaultParameterSetName='Test', PositionalBinding=$false)]
19+
[Microsoft.Azure.PowerShell.Cmdlets.PostgreSql.Description('Test out the connection to the database server')]
20+
param(
21+
[Parameter(ParameterSetName='Test', Mandatory, HelpMessage = 'The name of the server to connect.')]
22+
[Parameter(ParameterSetName='TestAndQuery', Mandatory, HelpMessage = 'The name of the server to connect.')]
23+
[Alias('ServerName')]
24+
[Microsoft.Azure.PowerShell.Cmdlets.PostgreSql.Category('Path')]
25+
[System.String]
26+
${Name},
27+
28+
[Parameter(ParameterSetName='Test', Mandatory, HelpMessage = 'The name of the resource group that contains the resource, You can obtain this value from the Azure Resource Manager API or the portal.')]
29+
[Parameter(ParameterSetName='TestAndQuery', Mandatory, HelpMessage = 'The name of the resource group that contains the resource, You can obtain this value from the Azure Resource Manager API or the portal.')]
30+
[Microsoft.Azure.PowerShell.Cmdlets.PostgreSql.Category('Path')]
31+
[System.String]
32+
${ResourceGroupName},
33+
34+
[Parameter(HelpMessage = 'The database name to connect.')]
35+
[Microsoft.Azure.PowerShell.Cmdlets.PostgreSql.Category('Path')]
36+
[System.String]
37+
${DatabaseName},
38+
39+
[Parameter(ParameterSetName='TestViaIdentityAndQuery', Mandatory, HelpMessage = 'The query for the database to test')]
40+
[Parameter(ParameterSetName='TestAndQuery', Mandatory, HelpMessage = 'The query for the database to test')]
41+
[Microsoft.Azure.PowerShell.Cmdlets.PostgreSql.Category('Path')]
42+
[System.String]
43+
${QueryText},
44+
45+
[Parameter(ParameterSetName='TestViaIdentity', Mandatory, ValueFromPipeline, HelpMessage = 'The server to connect.')]
46+
[Parameter(ParameterSetName='TestViaIdentityAndQuery', Mandatory, ValueFromPipeline, HelpMessage = 'The server to connect.')]
47+
[Microsoft.Azure.PowerShell.Cmdlets.PostgreSql.Category('Body')]
48+
[Microsoft.Azure.PowerShell.Cmdlets.PostgreSql.Models.IPostgreSqlIdentity]
49+
${InputObject},
50+
51+
[Parameter(HelpMessage = 'Administrator username for the server. Once set, it cannot be changed.')]
52+
[Microsoft.Azure.PowerShell.Cmdlets.PostgreSql.Category('Body')]
53+
[System.String]
54+
${AdministratorUserName},
55+
56+
[Parameter(Mandatory, HelpMessage = 'The password of the administrator. Minimum 8 characters and maximum 128 characters. Password must contain characters from three of the following categories: English uppercase letters, English lowercase letters, numbers, and non-alphanumeric characters.')]
57+
[Microsoft.Azure.PowerShell.Cmdlets.PostgreSql.Category('Body')]
58+
[System.Security.SecureString]
59+
[ValidateNotNullOrEmpty()]
60+
${AdministratorLoginPassword},
61+
62+
[Parameter(HelpMessage = 'The credentials, account, tenant, and subscription used for communication with Azure.')]
63+
[Alias('AzureRMContext', 'AzureCredential')]
64+
[ValidateNotNull()]
65+
[Microsoft.Azure.PowerShell.Cmdlets.PostgreSql.Category('Azure')]
66+
[System.Management.Automation.PSObject]
67+
${DefaultProfile},
68+
69+
[Parameter(DontShow, HelpMessage = 'Wait for .NET debugger to attach.')]
70+
[Microsoft.Azure.PowerShell.Cmdlets.PostgreSql.Category('Runtime')]
71+
[System.Management.Automation.SwitchParameter]
72+
${Break},
73+
74+
[Parameter(DontShow)]
75+
[ValidateNotNull()]
76+
[Microsoft.Azure.PowerShell.Cmdlets.PostgreSql.Category('Runtime')]
77+
[Microsoft.Azure.PowerShell.Cmdlets.PostgreSql.Runtime.SendAsyncStep[]]
78+
# SendAsync Pipeline Steps to be appended to the front of the pipeline.
79+
${HttpPipelineAppend},
80+
81+
[Parameter(DontShow)]
82+
[ValidateNotNull()]
83+
[Microsoft.Azure.PowerShell.Cmdlets.PostgreSql.Category('Runtime')]
84+
[Microsoft.Azure.PowerShell.Cmdlets.PostgreSql.Runtime.SendAsyncStep[]]
85+
# SendAsync Pipeline Steps to be prepended to the front of the pipeline.
86+
${HttpPipelinePrepend},
87+
88+
[Parameter(DontShow)]
89+
[Microsoft.Azure.PowerShell.Cmdlets.PostgreSql.Category('Runtime')]
90+
[System.Uri]
91+
# The URI for the proxy server to use.
92+
${Proxy},
93+
94+
[Parameter(DontShow)]
95+
[ValidateNotNull()]
96+
[Microsoft.Azure.PowerShell.Cmdlets.PostgreSql.Category('Runtime')]
97+
[System.Management.Automation.PSCredential]
98+
# Credentials for a proxy server to use for the remote call.
99+
${ProxyCredential},
100+
101+
[Parameter(DontShow)]
102+
[Microsoft.Azure.PowerShell.Cmdlets.PostgreSql.Category('Runtime')]
103+
[System.Management.Automation.SwitchParameter]
104+
# Use the default credentials for the proxy.
105+
${ProxyUseDefaultCredentials}
106+
)
107+
108+
process {
109+
if (!(Get-Module -ListAvailable -Name SimplySQL)){
110+
Write-Error "This cmdlet requires SimplySQL module. Please install the module first by running Install-Module -Name SimplySQL."
111+
exit
112+
}
113+
Import-Module SimplySQL
114+
115+
$Query = [string]::Empty
116+
if ($PSBoundParameters.ContainsKey('QueryText')) {
117+
$Query = $PSBoundParameters.QueryText
118+
$null = $PSBoundParameters.Remove('QueryText')
119+
}
120+
121+
$DatabaseName = [string]::Empty
122+
if ($PSBoundParameters.ContainsKey('DatabaseName')) {
123+
$DatabaseName = $PSBoundParameters.DatabaseName
124+
$null = $PSBoundParameters.Remove('DatabaseName')
125+
}
126+
127+
$AdministratorUserName = [string]::Empty
128+
if ($PSBoundParameters.ContainsKey('AdministratorUserName')) {
129+
$AdministratorUserName = $PSBoundParameters.AdministratorUserName
130+
$null = $PSBoundParameters.Remove('AdministratorUserName')
131+
}
132+
133+
$Password = $PSBoundParameters['AdministratorLoginPassword']
134+
$null = $PSBoundParameters.Remove('AdministratorLoginPassword')
135+
136+
137+
$Server = Az.PostgreSql\Get-AzPostgreSqlFlexibleServer @PSBoundParameters
138+
$HostAddr = $Server.FullyQualifiedDomainName
139+
if ([string]::IsNullOrEmpty($AdministratorUserName)) {
140+
$AdministratorUserName = $Server.AdministratorLogin
141+
}
142+
143+
try {
144+
if ([string]::IsNullOrEmpty($DatabaseName)){
145+
Open-PostGreConnection -Server $HostAddr -UserName $AdministratorUserName -Password $Password -RequireSSL -WarningAction 'silentlycontinue'
146+
}
147+
else {
148+
Open-PostGreConnection -Database $DatabaseName -Server $HostAddr -UserName $AdministratorUserName -Password $Password -RequireSSL -WarningAction 'silentlycontinue'
149+
}
150+
151+
} catch {
152+
Write-Host $_.Exception.GetType().FullName
153+
Write-Host $_.Exception.Message
154+
exit
155+
}
156+
157+
if (![string]::IsNullOrEmpty($Query)) {
158+
Invoke-SqlQuery -Query $Query -WarningAction 'silentlycontinue'
159+
}
160+
else {
161+
$Msg = "The connection testing to {0} was successful!" -f $HostAddr
162+
Write-Host $Msg
163+
}
164+
}
165+
}

src/PostgreSql/custom/Update-AzPostgreSqlFlexibleServer.ps1

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -166,12 +166,6 @@ function Update-AzPostgreSqlFlexibleServer {
166166

167167
process {
168168
try {
169-
if ($PSBoundParameters.ContainsKey('AdministratorLoginPassword')) {
170-
$bStr = . "$PSScriptRoot/../utils/Unprotect-SecureString.ps1" $PSBoundParameters['AdministratorLoginPassword']
171-
$null = $PSBoundParameters.Remove('AdministratorLoginPassword')
172-
$PSBoundParameters.Add('AdministratorLoginPassword', $bStr)
173-
}
174-
175169
if ($PSBoundParameters.ContainsKey('StorageInMb')) {
176170
$PSBoundParameters.StorageProfileStorageMb = $PSBoundParameters['StorageInMb']
177171
$null = $PSBoundParameters.Remove('StorageInMb')

src/PostgreSql/custom/Update-AzPostgreSqlServer.ps1

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -170,12 +170,6 @@ function Update-AzPostgreSqlServer {
170170

171171
process {
172172
try {
173-
if ($PSBoundParameters.ContainsKey('AdministratorLoginPassword')) {
174-
$bStr = . "$PSScriptRoot/../utils/Unprotect-SecureString.ps1" $PSBoundParameters['AdministratorLoginPassword']
175-
$null = $PSBoundParameters.Remove('AdministratorLoginPassword')
176-
$PSBoundParameters.Add('AdministratorLoginPassword', $bStr)
177-
}
178-
179173
if ($PSBoundParameters.ContainsKey('StorageInMb')) {
180174
$PSBoundParameters.Add('StorageProfileStorageMb', $PSBoundParameters['StorageInMb'])
181175
$null = $PSBoundParameters.Remove('StorageInMb')
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
### Example 1: Test connection by name
2+
```powershell
3+
PS C:\> Get-AzPostgreSqlFlexibleServerConnect -ResourceGroupName PowershellPostgreSqlTest -Name postgresql-test -AdministratorLoginPassword $password
4+
5+
The connection testing to postgresql-test.database.azure.com was successful!
6+
```
7+
8+
Test connection by the resource group and the server name
9+
10+
### Example 2: Test connection by identity
11+
```powershell
12+
PS C:\> Get-AzPostgreSqlFlexibleServer -ResourceGroupName PowershellPostgreSqlTest -ServerName postgresql-test | Get-AzPostgreSqlFlexibleServerConnect -AdministratorLoginPassword $password
13+
14+
The connection testing to postgresql-test.database.azure.com was successful!
15+
```
16+
17+
Test connection by the identity
18+
19+
### Example 3: Test query by name
20+
```powershell
21+
PS C:\> Get-AzPostgreSqlFlexibleServerConnect -ResourceGroupName PowershellPostgreSqlTest -Name postgresql-test -AdministratorLoginPassword $password -Query "SELECT * FROM test"
22+
23+
col
24+
-----
25+
1
26+
2
27+
3
28+
```
29+
30+
Test a query by the resource group and the server name
31+
32+
### Example 4: Test connection by identity
33+
```powershell
34+
PS C:\> Get-AzPostgreSqlFlexibleServer -ResourceGroupName PowershellPostgreSqlTest -ServerName postgresql-test | Get-AzPostgreSqlFlexibleServerConnect -Query "SELECT * FROM test" -AdministratorLoginPassword $password
35+
36+
col
37+
-----
38+
1
39+
2
40+
3
41+
```
42+
43+
Test a query by the identity

0 commit comments

Comments
 (0)