|
| 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 | +$DELEGATION_SERVICE_NAME = "Microsoft.DBforMySQL/flexibleServers" |
| 15 | +function Restore-AzMySqlFlexibleServer_GeoRestore { |
| 16 | + [OutputType([Microsoft.Azure.PowerShell.Cmdlets.MySql.Models.Api20210501.IServerAutoGenerated])] |
| 17 | + [CmdletBinding(PositionalBinding = $false, SupportsShouldProcess, ConfirmImpact = 'Medium')] |
| 18 | + [Microsoft.Azure.PowerShell.Cmdlets.MySql.Description('Restore a server from an existing backup using GeoRestore')] |
| 19 | + param( |
| 20 | + [Parameter(Mandatory, HelpMessage = 'The name of the server.')] |
| 21 | + [Alias('ServerName')] |
| 22 | + [Microsoft.Azure.PowerShell.Cmdlets.MySql.Category('Path')] |
| 23 | + [System.String] |
| 24 | + ${Name}, |
| 25 | + |
| 26 | + [Parameter(Mandatory, HelpMessage = 'The name of the resource group that contains the resource.')] |
| 27 | + [Microsoft.Azure.PowerShell.Cmdlets.MySql.Category('Path')] |
| 28 | + [System.String] |
| 29 | + ${ResourceGroupName}, |
| 30 | + |
| 31 | + [Parameter(HelpMessage = 'The subscription ID that identifies an Azure subscription.')] |
| 32 | + [Microsoft.Azure.PowerShell.Cmdlets.MySql.Category('Path')] |
| 33 | + [Microsoft.Azure.PowerShell.Cmdlets.MySql.Runtime.DefaultInfo(Script = '(Get-AzContext).Subscription.Id')] |
| 34 | + [System.String] |
| 35 | + ${SubscriptionId}, |
| 36 | + |
| 37 | + [Parameter(Mandatory, ValueFromPipeline, HelpMessage = 'The source server object to restore from.')] |
| 38 | + [Microsoft.Azure.PowerShell.Cmdlets.MySql.Category('Body')] |
| 39 | + [Microsoft.Azure.PowerShell.Cmdlets.MySql.Models.Api20210501.IServerAutoGenerated] |
| 40 | + ${InputObject}, |
| 41 | + |
| 42 | + [Parameter(Mandatory, HelpMessage = 'The location to restore the server to.')] |
| 43 | + [Microsoft.Azure.PowerShell.Cmdlets.MySql.Category('Body')] |
| 44 | + [System.String] |
| 45 | + ${Location}, |
| 46 | + |
| 47 | + [Parameter(HelpMessage = 'The name of the sku, typically, tier + family + cores, e.g., B_Gen4_1, GP_Gen5_8.')] |
| 48 | + [Microsoft.Azure.PowerShell.Cmdlets.MySql.Category('Body')] |
| 49 | + [System.String] |
| 50 | + ${Sku}, |
| 51 | + |
| 52 | + [Parameter(HelpMessage = 'Application-specific metadata in the form of key-value pairs.')] |
| 53 | + [Microsoft.Azure.PowerShell.Cmdlets.MySql.Category('Body')] |
| 54 | + [Microsoft.Azure.PowerShell.Cmdlets.MySql.Runtime.Info(PossibleTypes = ([Microsoft.Azure.PowerShell.Cmdlets.MySql.Models.Api20171201.IServerForCreateTags]))] |
| 55 | + [System.Collections.Hashtable] |
| 56 | + ${Tag}, |
| 57 | + |
| 58 | + [Parameter(Mandatory, HelpMessage = 'Use Geo mode to restore')] |
| 59 | + [Microsoft.Azure.PowerShell.Cmdlets.MySql.Category('Body')] |
| 60 | + [System.Management.Automation.SwitchParameter] |
| 61 | + ${UseGeoRestore}, |
| 62 | + |
| 63 | + [Parameter(HelpMessage = 'The credentials, account, tenant, and subscription used for communication with Azure.')] |
| 64 | + [Alias('AzureRMContext', 'AzureCredential')] |
| 65 | + [ValidateNotNull()] |
| 66 | + [Microsoft.Azure.PowerShell.Cmdlets.MySql.Category('Azure')] |
| 67 | + [System.Management.Automation.PSObject] |
| 68 | + ${DefaultProfile}, |
| 69 | + |
| 70 | + [Parameter(HelpMessage = 'Run the command as a job.')] |
| 71 | + [Microsoft.Azure.PowerShell.Cmdlets.MySql.Category('Runtime')] |
| 72 | + [System.Management.Automation.SwitchParameter] |
| 73 | + ${AsJob}, |
| 74 | + |
| 75 | + [Parameter(DontShow, HelpMessage = 'Wait for .NET debugger to attach.')] |
| 76 | + [Microsoft.Azure.PowerShell.Cmdlets.MySql.Category('Runtime')] |
| 77 | + [System.Management.Automation.SwitchParameter] |
| 78 | + ${Break}, |
| 79 | + |
| 80 | + [Parameter(DontShow)] |
| 81 | + [ValidateNotNull()] |
| 82 | + [Microsoft.Azure.PowerShell.Cmdlets.MySql.Category('Runtime')] |
| 83 | + [Microsoft.Azure.PowerShell.Cmdlets.MySql.Runtime.SendAsyncStep[]] |
| 84 | + ${HttpPipelineAppend}, |
| 85 | + |
| 86 | + [Parameter(DontShow)] |
| 87 | + [ValidateNotNull()] |
| 88 | + [Microsoft.Azure.PowerShell.Cmdlets.MySql.Category('Runtime')] |
| 89 | + [Microsoft.Azure.PowerShell.Cmdlets.MySql.Runtime.SendAsyncStep[]] |
| 90 | + ${HttpPipelinePrepend}, |
| 91 | + |
| 92 | + [Parameter(HelpMessage = 'Run the command asynchronously.')] |
| 93 | + [Microsoft.Azure.PowerShell.Cmdlets.MySql.Category('Runtime')] |
| 94 | + [System.Management.Automation.SwitchParameter] |
| 95 | + ${NoWait}, |
| 96 | + |
| 97 | + [Parameter(DontShow)] |
| 98 | + [Microsoft.Azure.PowerShell.Cmdlets.MySql.Category('Runtime')] |
| 99 | + [System.Uri] |
| 100 | + ${Proxy}, |
| 101 | + |
| 102 | + [Parameter(DontShow)] |
| 103 | + [ValidateNotNull()] |
| 104 | + [Microsoft.Azure.PowerShell.Cmdlets.MySql.Category('Runtime')] |
| 105 | + [System.Management.Automation.PSCredential] |
| 106 | + ${ProxyCredential}, |
| 107 | + |
| 108 | + [Parameter(DontShow)] |
| 109 | + [Microsoft.Azure.PowerShell.Cmdlets.MySql.Category('Runtime')] |
| 110 | + [System.Management.Automation.SwitchParameter] |
| 111 | + ${ProxyUseDefaultCredentials} |
| 112 | + ) |
| 113 | + |
| 114 | + process { |
| 115 | + try { |
| 116 | + |
| 117 | + $PSBoundParameters.CreateMode = [Microsoft.Azure.PowerShell.Cmdlets.MySql.Support.CreateMode]::GeoRestore |
| 118 | + $null = $PSBoundParameters.Remove('UseGeoRestore') |
| 119 | + |
| 120 | + $server = $PSBoundParameters['InputObject'] |
| 121 | + $PSBoundParameters.SourceServerResourceId = $server.Id |
| 122 | + $PSBoundParameters.Location = $server.Location |
| 123 | + $null = $PSBoundParameters.Remove('InputObject') |
| 124 | + |
| 125 | + if ($PSBoundParameters.ContainsKey('Location')) { |
| 126 | + $PSBoundParameters.Location = $PSBoundParameters['Location'] |
| 127 | + $null = $PSBoundParameters.Remove('Location') |
| 128 | + } |
| 129 | + |
| 130 | + if ($PSBoundParameters.ContainsKey('Zone')) { |
| 131 | + $PSBoundParameters.AvailabilityZone = $PSBoundParameters.Zone |
| 132 | + $null = $PSBoundParameters.Remove('Zone') |
| 133 | + } |
| 134 | + |
| 135 | + if ($PSBoundParameters.ContainsKey('Sku')) { |
| 136 | + $PSBoundParameters.SkuName = $PSBoundParameters['Sku'] |
| 137 | + $null = $PSBoundParameters.Remove('Sku') |
| 138 | + } |
| 139 | + |
| 140 | + if ($PSBoundParameters.ContainsKey('Subnet')) { |
| 141 | + if (!(Get-Module -ListAvailable -Name Az.Network)) { |
| 142 | + throw 'Please install Az.Network module by entering "Install-Module -Name Az.Network"' |
| 143 | + } |
| 144 | + else { |
| 145 | + Import-Module -Name Az.Network |
| 146 | + } |
| 147 | + $VnetId = [string]::Join("/", $PSBoundParameters.Subnet.split("/")[0..8]) |
| 148 | + $SubnetModel = Get-AzVirtualNetworkSubnetConfig -ResourceId $PSBoundParameters.Subnet |
| 149 | + $Delegations = Get-AzDelegation -Subnet $SubnetModel |
| 150 | + if ($null -ne $Delegations) { |
| 151 | + # Valid but incorrect delegation |
| 152 | + $Delegations | ForEach-Object { if ($PSItem.ServiceName -ne $DELEGATION_SERVICE_NAME) { |
| 153 | + $Msg = "Can not use subnet with existing delegations other than {0}" -f $DELEGATION_SERVICE_NAME |
| 154 | + throw $Msg |
| 155 | + } } |
| 156 | + } |
| 157 | + else { |
| 158 | + # Valid but no delegation |
| 159 | + throw "Add delegation " + $DELEGATION_SERVICE_NAME + " to the subnet." |
| 160 | + } |
| 161 | + $PSBoundParameters.NetworkDelegatedSubnetResourceId = $PSBoundParameters.Subnet |
| 162 | + $null = $PSBoundParameters.Remove('Subnet') |
| 163 | + } |
| 164 | + |
| 165 | + if ($PSBoundParameters.ContainsKey('PrivateDnsZone')) { |
| 166 | + if (!(Get-Module -ListAvailable -Name Az.PrivateDns)) { |
| 167 | + throw 'Please install Az.Network module by entering "Install-Module -Name Az.PrivateDns"' |
| 168 | + } |
| 169 | + else { |
| 170 | + Import-Module -Name Az.PrivateDns |
| 171 | + } |
| 172 | + if ([string]::IsNullOrEmpty($VnetId)) { |
| 173 | + if ($server.NetworkPublicNetworkAccess -eq 'Enabled') { |
| 174 | + throw "The source server is not private access enabled. Specify subnet ID if you want to restore it to public access server" |
| 175 | + } |
| 176 | + $VnetId = [string]::Join("/", $server.NetworkDelegatedSubnetResourceId.split("/")[0..8]) |
| 177 | + } |
| 178 | + $ZoneName = $PSBoundParameters["PrivateDnsZone"].split("/")[-1] |
| 179 | + $DnsResourceGroup = $PSBoundParameters["PrivateDnsZone"].split("/")[4] |
| 180 | + $Links = Get-AzPrivateDnsVirtualNetworkLink -ZoneName $ZoneName -ResourceGroupName $DnsResourceGroup |
| 181 | + $LinkedFlag = $false |
| 182 | + foreach ($Link in $Links) { |
| 183 | + if ($Link.VirtualNetworkId -eq $VnetId) { |
| 184 | + $LinkedFlag = $true |
| 185 | + break |
| 186 | + } |
| 187 | + } |
| 188 | + if (!$LinkedFlag) { |
| 189 | + Write-Host "Adding virtual network link to the DNS zone..." |
| 190 | + New-AzPrivateDnsVirtualNetworkLink -ZoneName $ZoneName -ResourceGroupName $DnsResourceGroup -Name $PSBoundParameters["Name"] -VirtualNetworkId $VnetId |
| 191 | + } |
| 192 | + $PSBoundParameters.NetworkPrivateDnsZoneResourceId = $PSBoundParameters.PrivateDnsZone |
| 193 | + $null = $PSBoundParameters.Remove('PrivateDnsZone') |
| 194 | + |
| 195 | + } |
| 196 | + |
| 197 | + if ($PSBoundParameters.ContainsKey('Tag')) { |
| 198 | + $PSBoundParameters.Tag = $PSBoundParameters['Tag'] |
| 199 | + $null = $PSBoundParameters.Remove('Tag') |
| 200 | + } |
| 201 | + |
| 202 | + Az.MySql.internal\New-AzMySqlFlexibleServer @PSBoundParameters |
| 203 | + } |
| 204 | + catch { |
| 205 | + throw |
| 206 | + } |
| 207 | + } |
| 208 | +} |
0 commit comments