Skip to content

Enhance Restore-AzMySqlFlexibleServer functionality with GeoRestore support #27729

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions src/MySql/MySql.Autorest/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// 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.
// Code generated by Microsoft (R) AutoRest Code Generator.Changes may cause incorrect behavior and will be lost if the code
// is regenerated.

using System;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;

[assembly: System.Reflection.AssemblyCompanyAttribute("Microsoft")]
[assembly: System.Reflection.AssemblyCopyrightAttribute("Copyright © Microsoft")]
[assembly: System.Reflection.AssemblyProductAttribute("Microsoft Azure PowerShell")]
[assembly: System.Reflection.AssemblyTitleAttribute("Microsoft Azure PowerShell - MySql")]
[assembly: System.Reflection.AssemblyFileVersionAttribute("1.3.0")]
[assembly: System.Reflection.AssemblyVersionAttribute("1.3.0")]
[assembly: System.Runtime.InteropServices.ComVisibleAttribute(false)]
[assembly: System.CLSCompliantAttribute(false)]
Original file line number Diff line number Diff line change
@@ -0,0 +1,208 @@
# ----------------------------------------------------------------------------------
#
# 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.
# ----------------------------------------------------------------------------------
$DELEGATION_SERVICE_NAME = "Microsoft.DBforMySQL/flexibleServers"
function Restore-AzMySqlFlexibleServer_GeoRestore {
[OutputType([Microsoft.Azure.PowerShell.Cmdlets.MySql.Models.Api20210501.IServerAutoGenerated])]
[CmdletBinding(PositionalBinding = $false, SupportsShouldProcess, ConfirmImpact = 'Medium')]
[Microsoft.Azure.PowerShell.Cmdlets.MySql.Description('Restore a server from an existing backup using GeoRestore')]
param(
[Parameter(Mandatory, HelpMessage = 'The name of the server.')]
[Alias('ServerName')]
[Microsoft.Azure.PowerShell.Cmdlets.MySql.Category('Path')]
[System.String]
${Name},

[Parameter(Mandatory, HelpMessage = 'The name of the resource group that contains the resource.')]
[Microsoft.Azure.PowerShell.Cmdlets.MySql.Category('Path')]
[System.String]
${ResourceGroupName},

[Parameter(HelpMessage = 'The subscription ID that identifies an Azure subscription.')]
[Microsoft.Azure.PowerShell.Cmdlets.MySql.Category('Path')]
[Microsoft.Azure.PowerShell.Cmdlets.MySql.Runtime.DefaultInfo(Script = '(Get-AzContext).Subscription.Id')]
[System.String]
${SubscriptionId},

[Parameter(Mandatory, ValueFromPipeline, HelpMessage = 'The source server object to restore from.')]
[Microsoft.Azure.PowerShell.Cmdlets.MySql.Category('Body')]
[Microsoft.Azure.PowerShell.Cmdlets.MySql.Models.Api20210501.IServerAutoGenerated]
${InputObject},

[Parameter(Mandatory, HelpMessage = 'The location to restore the server to.')]
[Microsoft.Azure.PowerShell.Cmdlets.MySql.Category('Body')]
[System.String]
${Location},

[Parameter(HelpMessage = 'The name of the sku, typically, tier + family + cores, e.g., B_Gen4_1, GP_Gen5_8.')]
[Microsoft.Azure.PowerShell.Cmdlets.MySql.Category('Body')]
[System.String]
${Sku},

[Parameter(HelpMessage = 'Application-specific metadata in the form of key-value pairs.')]
[Microsoft.Azure.PowerShell.Cmdlets.MySql.Category('Body')]
[Microsoft.Azure.PowerShell.Cmdlets.MySql.Runtime.Info(PossibleTypes = ([Microsoft.Azure.PowerShell.Cmdlets.MySql.Models.Api20171201.IServerForCreateTags]))]
[System.Collections.Hashtable]
${Tag},

[Parameter(Mandatory, HelpMessage = 'Use Geo mode to restore')]
[Microsoft.Azure.PowerShell.Cmdlets.MySql.Category('Body')]
[System.Management.Automation.SwitchParameter]
${UseGeoRestore},

[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(HelpMessage = 'Run the command as a job.')]
[Microsoft.Azure.PowerShell.Cmdlets.MySql.Category('Runtime')]
[System.Management.Automation.SwitchParameter]
${AsJob},

[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[]]
${HttpPipelineAppend},

[Parameter(DontShow)]
[ValidateNotNull()]
[Microsoft.Azure.PowerShell.Cmdlets.MySql.Category('Runtime')]
[Microsoft.Azure.PowerShell.Cmdlets.MySql.Runtime.SendAsyncStep[]]
${HttpPipelinePrepend},

[Parameter(HelpMessage = 'Run the command asynchronously.')]
[Microsoft.Azure.PowerShell.Cmdlets.MySql.Category('Runtime')]
[System.Management.Automation.SwitchParameter]
${NoWait},

[Parameter(DontShow)]
[Microsoft.Azure.PowerShell.Cmdlets.MySql.Category('Runtime')]
[System.Uri]
${Proxy},

[Parameter(DontShow)]
[ValidateNotNull()]
[Microsoft.Azure.PowerShell.Cmdlets.MySql.Category('Runtime')]
[System.Management.Automation.PSCredential]
${ProxyCredential},

[Parameter(DontShow)]
[Microsoft.Azure.PowerShell.Cmdlets.MySql.Category('Runtime')]
[System.Management.Automation.SwitchParameter]
${ProxyUseDefaultCredentials}
)

process {
try {

$PSBoundParameters.CreateMode = [Microsoft.Azure.PowerShell.Cmdlets.MySql.Support.CreateMode]::GeoRestore
$null = $PSBoundParameters.Remove('UseGeoRestore')

$server = $PSBoundParameters['InputObject']
$PSBoundParameters.SourceServerResourceId = $server.Id
$PSBoundParameters.Location = $server.Location
$null = $PSBoundParameters.Remove('InputObject')

if ($PSBoundParameters.ContainsKey('Location')) {
$PSBoundParameters.Location = $PSBoundParameters['Location']
$null = $PSBoundParameters.Remove('Location')
}

if ($PSBoundParameters.ContainsKey('Zone')) {
$PSBoundParameters.AvailabilityZone = $PSBoundParameters.Zone
$null = $PSBoundParameters.Remove('Zone')
}

if ($PSBoundParameters.ContainsKey('Sku')) {
$PSBoundParameters.SkuName = $PSBoundParameters['Sku']
$null = $PSBoundParameters.Remove('Sku')
}

if ($PSBoundParameters.ContainsKey('Subnet')) {
if (!(Get-Module -ListAvailable -Name Az.Network)) {
throw 'Please install Az.Network module by entering "Install-Module -Name Az.Network"'
}
else {
Import-Module -Name Az.Network
}
$VnetId = [string]::Join("/", $PSBoundParameters.Subnet.split("/")[0..8])
$SubnetModel = Get-AzVirtualNetworkSubnetConfig -ResourceId $PSBoundParameters.Subnet
$Delegations = Get-AzDelegation -Subnet $SubnetModel
if ($null -ne $Delegations) {
# Valid but incorrect delegation
$Delegations | ForEach-Object { if ($PSItem.ServiceName -ne $DELEGATION_SERVICE_NAME) {
$Msg = "Can not use subnet with existing delegations other than {0}" -f $DELEGATION_SERVICE_NAME
throw $Msg
} }
}
else {
# Valid but no delegation
throw "Add delegation " + $DELEGATION_SERVICE_NAME + " to the subnet."
}
$PSBoundParameters.NetworkDelegatedSubnetResourceId = $PSBoundParameters.Subnet
$null = $PSBoundParameters.Remove('Subnet')
}

if ($PSBoundParameters.ContainsKey('PrivateDnsZone')) {
if (!(Get-Module -ListAvailable -Name Az.PrivateDns)) {
throw 'Please install Az.Network module by entering "Install-Module -Name Az.PrivateDns"'
}
else {
Import-Module -Name Az.PrivateDns
}
if ([string]::IsNullOrEmpty($VnetId)) {
if ($server.NetworkPublicNetworkAccess -eq 'Enabled') {
throw "The source server is not private access enabled. Specify subnet ID if you want to restore it to public access server"
}
$VnetId = [string]::Join("/", $server.NetworkDelegatedSubnetResourceId.split("/")[0..8])
}
$ZoneName = $PSBoundParameters["PrivateDnsZone"].split("/")[-1]
$DnsResourceGroup = $PSBoundParameters["PrivateDnsZone"].split("/")[4]
$Links = Get-AzPrivateDnsVirtualNetworkLink -ZoneName $ZoneName -ResourceGroupName $DnsResourceGroup
$LinkedFlag = $false
foreach ($Link in $Links) {
if ($Link.VirtualNetworkId -eq $VnetId) {
$LinkedFlag = $true
break
}
}
if (!$LinkedFlag) {
Write-Host "Adding virtual network link to the DNS zone..."
New-AzPrivateDnsVirtualNetworkLink -ZoneName $ZoneName -ResourceGroupName $DnsResourceGroup -Name $PSBoundParameters["Name"] -VirtualNetworkId $VnetId
}
$PSBoundParameters.NetworkPrivateDnsZoneResourceId = $PSBoundParameters.PrivateDnsZone
$null = $PSBoundParameters.Remove('PrivateDnsZone')

}

if ($PSBoundParameters.ContainsKey('Tag')) {
$PSBoundParameters.Tag = $PSBoundParameters['Tag']
$null = $PSBoundParameters.Remove('Tag')
}

Az.MySql.internal\New-AzMySqlFlexibleServer @PSBoundParameters
}
catch {
throw
}
}
}
4 changes: 2 additions & 2 deletions src/MySql/MySql.Autorest/docs/Az.MySql.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
Module Name: Az.MySql
Module Guid: 9e6f7276-93b8-48dc-8c37-abd95fe7d667
Module Guid: 21aee4b9-13aa-41fa-993f-0ea403b3fa72
Download Help Link: https://learn.microsoft.com/powershell/module/az.mysql
Help Version: 1.0.0.0
Locale: en-US
Expand Down Expand Up @@ -102,7 +102,7 @@ Restarts a server.
Restarts a server.

### [Restore-AzMySqlFlexibleServer](Restore-AzMySqlFlexibleServer.md)
Restore a server from an existing backup
Restore a server from an existing backup using GeoRestore

### [Restore-AzMySqlServer](Restore-AzMySqlServer.md)
Restore a server from an existing backup
Expand Down
Loading