Skip to content

Commit

Permalink
Added cmdlet Test-AzMySqlFlexibleServerConnect
Browse files Browse the repository at this point in the history
  • Loading branch information
msJinLei committed Feb 2, 2021
1 parent 2b73fd2 commit 3222864
Show file tree
Hide file tree
Showing 15 changed files with 1,091 additions and 30 deletions.
13 changes: 6 additions & 7 deletions src/MySql/Az.MySql.psd1
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#
# Generated by: Microsoft Corporation
#
# Generated on: 1/14/2021
# Generated on: 2/2/2021
#

@{
Expand Down Expand Up @@ -45,7 +45,7 @@ PowerShellVersion = '5.1'
DotNetFrameworkVersion = '4.7.2'

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

# Processor architecture (None, X86, Amd64) required by this module
# ProcessorArchitecture = ''
Expand Down Expand Up @@ -89,8 +89,9 @@ FunctionsToExport = 'Get-AzMySqlConfiguration', 'Get-AzMySqlConnectionString',
'Remove-AzMySqlVirtualNetworkRule', 'Restart-AzMySqlFlexibleServer',
'Restart-AzMySqlServer', 'Restore-AzMySqlFlexibleServer',
'Restore-AzMySqlServer', 'Start-AzMySqlFlexibleServer',
'Stop-AzMySqlFlexibleServer', 'Update-AzMySqlConfiguration',
'Update-AzMySqlFirewallRule', 'Update-AzMySqlFlexibleServer',
'Stop-AzMySqlFlexibleServer', 'Test-AzMySqlFlexibleServerConnect',
'Update-AzMySqlConfiguration', 'Update-AzMySqlFirewallRule',
'Update-AzMySqlFlexibleServer',
'Update-AzMySqlFlexibleServerConfiguration',
'Update-AzMySqlFlexibleServerDatabase',
'Update-AzMySqlFlexibleServerFirewallRule', 'Update-AzMySqlServer',
Expand Down Expand Up @@ -132,9 +133,7 @@ PrivateData = @{
# IconUri = ''

# ReleaseNotes of this module
ReleaseNotes = '* Updated New-AzMySqlFlexibleServer cmdlet to provide easy resource group and network management within RDBMS flexible server management
* Added new cmdlets Get-AzMySqlFlexibleServerLocationBasedCapability and Get-AzMySqlConnectionString
* Added parameter MinimalTlsVersion'
# ReleaseNotes = ''

# Prerelease string of this module
# Prerelease = ''
Expand Down
1 change: 1 addition & 0 deletions src/MySql/ChangeLog.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
- Additional information about change #1
-->
## Upcoming Release
* Added cmdlet `Test-AzMySqlFlexibleServerConnect`

## Version 0.4.0
* Updated New-AzMySqlFlexibleServer cmdlet to provide easy resource group and network management within RDBMS flexible server management
Expand Down
5 changes: 2 additions & 3 deletions src/MySql/custom/New-AzMySqlFlexibleServer.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,6 @@ process {
$Password = Get-GeneratePassword
$PSBoundParameters.AdministratorLoginPassword = $Password | ConvertTo-SecureString -AsPlainText -Force
}
$PSBoundParameters.AdministratorLoginPassword = . "$PSScriptRoot/../utils/Unprotect-SecureString.ps1" $PSBoundParameters['AdministratorLoginPassword']

Import-Module -Name Az.Resources
if(!$PSBoundParameters.ContainsKey('ResourceGroupName')) {
Expand Down Expand Up @@ -308,7 +307,7 @@ process {
$Server.FirewallRuleName = $FirewallRuleName
}
$Server.DatabaseName = $DEFAULT_DB_NAME
$Server.SecuredPassword = $PSBoundParameters.AdministratorLoginPassword | ConvertTo-SecureString -AsPlainText -Force
$Server.SecuredPassword = $PSBoundParameters.AdministratorLoginPassword

return $Server

Expand Down Expand Up @@ -526,7 +525,7 @@ function CreateFirewallRule($Parameters) {
}
return $FirewallRule.Name
}
elseif ($Parameters.ContainsKey('PublicAccess') -And $Parameters.PublicAccess.ToLower() -ne 'none') {
elseif ($Parameters.ContainsKey('PublicAccess') -And $Parameters.PublicAccess.ToLower() -eq 'none') {
Write-Host "No firewall rule was set"
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/MySql/custom/New-AzMySqlServer.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ function New-AzMySqlServer {
$Parameter.Property.AdministratorLogin = $PSBoundParameters['AdministratorUserName']
$null = $PSBoundParameters.Remove('AdministratorUserName')

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

$PSBoundParameters.Add('Parameter', $Parameter)
Expand Down
164 changes: 164 additions & 0 deletions src/MySql/custom/Test-AzMySqlFlexibleServerConnect.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,164 @@
# ----------------------------------------------------------------------------------
#
# Copyright Microsoft Corporation
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
# http://www.apache.org/licenses/LICENSE-2.0
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ----------------------------------------------------------------------------------

function Test-AzMySqlFlexibleServerConnect {
[OutputType([System.String])]
[CmdletBinding(DefaultParameterSetName='Test', PositionalBinding=$false)]
[Microsoft.Azure.PowerShell.Cmdlets.MySql.Description('Test out the connection to the database server')]
param(
[Parameter(ParameterSetName='Test', Mandatory, HelpMessage = 'The name of the server to connect.')]
[Parameter(ParameterSetName='TestAndQuery', Mandatory, HelpMessage = 'The name of the server to connect.')]
[Alias('ServerName')]
[Microsoft.Azure.PowerShell.Cmdlets.MySql.Category('Path')]
[System.String]
${Name},

[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.')]
[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.')]
[Microsoft.Azure.PowerShell.Cmdlets.MySql.Category('Path')]
[System.String]
${ResourceGroupName},

[Parameter(HelpMessage = 'The database name to connect.')]
[Microsoft.Azure.PowerShell.Cmdlets.MySql.Category('Path')]
[System.String]
${DatabaseName},

[Parameter(ParameterSetName='TestViaIdentityAndQuery', Mandatory, HelpMessage = 'The query for the database to test')]
[Parameter(ParameterSetName='TestAndQuery', Mandatory, HelpMessage = 'The query for the database to test')]
[Microsoft.Azure.PowerShell.Cmdlets.MySql.Category('Path')]
[System.String]
${QueryText},

[Parameter(ParameterSetName='TestViaIdentity', Mandatory, ValueFromPipeline, HelpMessage = 'The server to connect.')]
[Parameter(ParameterSetName='TestViaIdentityAndQuery', Mandatory, ValueFromPipeline, HelpMessage = 'The server to connect.')]
[Microsoft.Azure.PowerShell.Cmdlets.MySql.Category('Body')]
[Microsoft.Azure.PowerShell.Cmdlets.MySql.Models.IMySqlIdentity]
${InputObject},

[Parameter(HelpMessage = 'Administrator username for the server. Once set, it cannot be changed.')]
[Microsoft.Azure.PowerShell.Cmdlets.MySql.Category('Body')]
[System.String]
${AdministratorUserName},

[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.')]
[Microsoft.Azure.PowerShell.Cmdlets.MySql.Category('Body')]
[System.Security.SecureString]
[ValidateNotNullOrEmpty()]
${AdministratorLoginPassword},

[Parameter(HelpMessage = 'The credentials, account, tenant, and subscription used for communication with Azure.')]
[Alias('AzureRMContext', 'AzureCredential')]
[ValidateNotNull()]
[Microsoft.Azure.PowerShell.Cmdlets.MySql.Category('Azure')]
[System.Management.Automation.PSObject]
${DefaultProfile},

[Parameter(DontShow, HelpMessage = 'Wait for .NET debugger to attach.')]
[Microsoft.Azure.PowerShell.Cmdlets.MySql.Category('Runtime')]
[System.Management.Automation.SwitchParameter]
${Break},

[Parameter(DontShow)]
[ValidateNotNull()]
[Microsoft.Azure.PowerShell.Cmdlets.MySql.Category('Runtime')]
[Microsoft.Azure.PowerShell.Cmdlets.MySql.Runtime.SendAsyncStep[]]
# SendAsync Pipeline Steps to be appended to the front of the pipeline.
${HttpPipelineAppend},

[Parameter(DontShow)]
[ValidateNotNull()]
[Microsoft.Azure.PowerShell.Cmdlets.MySql.Category('Runtime')]
[Microsoft.Azure.PowerShell.Cmdlets.MySql.Runtime.SendAsyncStep[]]
# SendAsync Pipeline Steps to be prepended to the front of the pipeline.
${HttpPipelinePrepend},

[Parameter(DontShow)]
[Microsoft.Azure.PowerShell.Cmdlets.MySql.Category('Runtime')]
[System.Uri]
# The URI for the proxy server to use.
${Proxy},

[Parameter(DontShow)]
[ValidateNotNull()]
[Microsoft.Azure.PowerShell.Cmdlets.MySql.Category('Runtime')]
[System.Management.Automation.PSCredential]
# Credentials for a proxy server to use for the remote call.
${ProxyCredential},

[Parameter(DontShow)]
[Microsoft.Azure.PowerShell.Cmdlets.MySql.Category('Runtime')]
[System.Management.Automation.SwitchParameter]
# Use the default credentials for the proxy.
${ProxyUseDefaultCredentials}
)

process {
if (!(Get-Module -ListAvailable -Name SimplySQL)){
Write-Error "This cmdlet requires SimplySQL module. Please install the module first by running Install-Module -Name SimplySQL."
exit
}
Import-Module SimplySQL

$Query = [string]::Empty
if ($PSBoundParameters.ContainsKey('QueryText')) {
$Query = $PSBoundParameters.QueryText
$null = $PSBoundParameters.Remove('QueryText')
}

$DatabaseName = [string]::Empty
if ($PSBoundParameters.ContainsKey('DatabaseName')) {
$DatabaseName = $PSBoundParameters.DatabaseName
$null = $PSBoundParameters.Remove('DatabaseName')
}

$Password = $PSBoundParameters['AdministratorLoginPassword']
$null = $PSBoundParameters.Remove('AdministratorLoginPassword')

$AdministratorUserName = [string]::Empty
if ($PSBoundParameters.ContainsKey('AdministratorUserName')) {
$AdministratorUserName = $PSBoundParameters.AdministratorUserName
$null = $PSBoundParameters.Remove('AdministratorUserName')
}

$Server = Az.MySql\Get-AzMySqlFlexibleServer @PSBoundParameters
$HostAddr = $Server.FullyQualifiedDomainName

if ([string]::IsNullOrEmpty($AdministratorUserName)) {
$AdministratorUserName = $Server.AdministratorLogin
}

try {
if ([string]::IsNullOrEmpty($DatabaseName)){
Open-MySqlConnection -Database $DatabaseName -Server $HostAddr -UserName $AdministratorUserName -Password $Password -SSLMode Required -WarningAction 'silentlycontinue'
}
else {
Open-MySqlConnection -Server $HostAddr -UserName $AdministratorUserName -Password $Password -SSLMode Required -WarningAction 'silentlycontinue'
}
} catch {
Write-Host $_.Exception.GetType().FullName
Write-Host $_.Exception.Message
exit
}

if (![string]::IsNullOrEmpty($Query)) {
Invoke-SqlQuery -Query $Query -WarningAction 'silentlycontinue'
}
else {
$Msg = "The connection testing to {0} was successful!" -f $HostAddr
Write-Host $Msg
}
}
}

4 changes: 0 additions & 4 deletions src/MySql/custom/Update-AzMySqlFlexibleServer.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -161,10 +161,6 @@ function Update-AzMySqlFlexibleServer {

process {
try {
if ($PSBoundParameters.ContainsKey('AdministratorLoginPassword')) {
$PSBoundParameters.AdministratorLoginPassword = . "$PSScriptRoot/../utils/Unprotect-SecureString.ps1" $PSBoundParameters['AdministratorLoginPassword']
}

if ($PSBoundParameters.ContainsKey('StorageInMb')) {
$PSBoundParameters.StorageProfileStorageMb = $PSBoundParameters['StorageInMb']
$null = $PSBoundParameters.Remove('StorageInMb')
Expand Down
6 changes: 0 additions & 6 deletions src/MySql/custom/Update-AzMySqlServer.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -165,12 +165,6 @@ function Update-AzMySqlServer {

process {
try {
if ($PSBoundParameters.ContainsKey('AdministratorLoginPassword')) {
$password = . "$PSScriptRoot/../utils/Unprotect-SecureString.ps1" $PSBoundParameters['AdministratorLoginPassword']
$PSBoundParameters.Add('AdministratorLoginPassword', $password)
$null = $PSBoundParameters.Remove('AdministratorLoginPassword')
}

if ($PSBoundParameters.ContainsKey('StorageInMb')) {
$PSBoundParameters.Add('StorageProfileStorageMb', $PSBoundParameters['StorageInMb'])
$null = $PSBoundParameters.Remove('StorageInMb')
Expand Down
44 changes: 44 additions & 0 deletions src/MySql/examples/Test-AzMySqlFlexibleServerConnect.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
### Example 1: Test connection by name
```powershell
PS C:\> Get-AzMySqlFlexibleServerConnect -ResourceGroupName PowershellMySqlTest -Name mysql-test -AdministratorLoginPassword $password
The connection testing to mysql-test.database.azure.com was successful!
```

Test connection by the resource group and the server name

### Example 2: Test connection by identity
```powershell
PS C:\> Get-AzMySqlFlexibleServer -ResourceGroupName PowershellMySqlTest -ServerName mysql-test | Get-AzMySqlFlexibleServerConnect -AdministratorLoginPassword $password
The connection testing to mysql-test.database.azure.com was successful!
```

Test connection by the identity

### Example 3: Test query by name
```powershell
PS C:\> Get-AzMySqlFlexibleServerConnect -ResourceGroupName PowershellMySqlTest -Name mysql-test -AdministratorLoginPassword $password -Query "SELECT * FROM test"
col
-----
1
2
3
```

Test a query by the resource group and the server name

### Example 4: Test connection by identity
```powershell
PS C:\> Get-AzMySqlFlexibleServer -ResourceGroupName PowershellMySqlTest -ServerName mysql-test | Get-AzMySqlFlexibleServerConnect -Query "SELECT * FROM test" -AdministratorLoginPassword $password
col
-----
1
2
3
```

Test a query by the identity

Loading

0 comments on commit 3222864

Please sign in to comment.