|
| 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 | +function Restore-AzPostgreSqlFlexibleServer_GeoRestore { |
| 16 | + [OutputType([Microsoft.Azure.PowerShell.Cmdlets.PostgreSql.Models.Api20210601.IServerAutoGenerated])] |
| 17 | + [CmdletBinding(PositionalBinding = $false, SupportsShouldProcess, ConfirmImpact = 'Medium')] |
| 18 | + [Microsoft.Azure.PowerShell.Cmdlets.PostgreSql.Description('Restore a PostgreSQL flexible server using Geo-restore')] |
| 19 | + param( |
| 20 | + [Parameter(Mandatory, HelpMessage = 'The name of the server to restore.')] |
| 21 | + [Alias('ServerName')] |
| 22 | + [Microsoft.Azure.PowerShell.Cmdlets.PostgreSql.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.PostgreSql.Category('Path')] |
| 28 | + [System.String] |
| 29 | + ${ResourceGroupName}, |
| 30 | + |
| 31 | + [Parameter(HelpMessage = 'The subscription ID that identifies an Azure subscription.')] |
| 32 | + [Microsoft.Azure.PowerShell.Cmdlets.PostgreSql.Category('Path')] |
| 33 | + [Microsoft.Azure.PowerShell.Cmdlets.PostgreSql.Runtime.DefaultInfo(Script = '(Get-AzContext).Subscription.Id')] |
| 34 | + [System.String] |
| 35 | + ${SubscriptionId}, |
| 36 | + |
| 37 | + [Parameter(Mandatory, HelpMessage = 'The point in time to restore from (ISO8601 format), e.g., 2017-04-26T02:10:00+08:00.')] |
| 38 | + [Microsoft.Azure.PowerShell.Cmdlets.PostgreSql.Category('Body')] |
| 39 | + [System.DateTime] |
| 40 | + ${RestorePointInTime}, |
| 41 | + |
| 42 | + [Parameter(Mandatory, HelpMessage = 'The name of the source server to restore from.')] |
| 43 | + [Microsoft.Azure.PowerShell.Cmdlets.PostgreSql.Category('Path')] |
| 44 | + [System.String] |
| 45 | + ${SourceServerName}, |
| 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.PostgreSql.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.PostgreSql.Category('Body')] |
| 54 | + [Microsoft.Azure.PowerShell.Cmdlets.PostgreSql.Runtime.Info(PossibleTypes = ([Microsoft.Azure.PowerShell.Cmdlets.PostgreSql.Models.Api20171201.IServerForCreateTags]))] |
| 55 | + [System.Collections.Hashtable] |
| 56 | + ${Tag}, |
| 57 | + |
| 58 | + [Parameter(Mandatory, HelpMessage = 'Use Geo mode to restore')] |
| 59 | + [Microsoft.Azure.PowerShell.Cmdlets.PostgreSql.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.PostgreSql.Category('Azure')] |
| 67 | + [System.Management.Automation.PSObject] |
| 68 | + ${DefaultProfile}, |
| 69 | + |
| 70 | + [Parameter(HelpMessage = 'Run the command as a job.')] |
| 71 | + [Microsoft.Azure.PowerShell.Cmdlets.PostgreSql.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.PostgreSql.Category('Runtime')] |
| 77 | + [System.Management.Automation.SwitchParameter] |
| 78 | + ${Break}, |
| 79 | + |
| 80 | + [Parameter(DontShow)] |
| 81 | + [ValidateNotNull()] |
| 82 | + [Microsoft.Azure.PowerShell.Cmdlets.PostgreSql.Category('Runtime')] |
| 83 | + [Microsoft.Azure.PowerShell.Cmdlets.PostgreSql.Runtime.SendAsyncStep[]] |
| 84 | + ${HttpPipelineAppend}, |
| 85 | + |
| 86 | + [Parameter(DontShow)] |
| 87 | + [ValidateNotNull()] |
| 88 | + [Microsoft.Azure.PowerShell.Cmdlets.PostgreSql.Category('Runtime')] |
| 89 | + [Microsoft.Azure.PowerShell.Cmdlets.PostgreSql.Runtime.SendAsyncStep[]] |
| 90 | + ${HttpPipelinePrepend}, |
| 91 | + |
| 92 | + [Parameter(HelpMessage = 'Run the command asynchronously.')] |
| 93 | + [Microsoft.Azure.PowerShell.Cmdlets.PostgreSql.Category('Runtime')] |
| 94 | + [System.Management.Automation.SwitchParameter] |
| 95 | + ${NoWait}, |
| 96 | + |
| 97 | + [Parameter(DontShow)] |
| 98 | + [Microsoft.Azure.PowerShell.Cmdlets.PostgreSql.Category('Runtime')] |
| 99 | + [System.Uri] |
| 100 | + ${Proxy}, |
| 101 | + |
| 102 | + [Parameter(DontShow)] |
| 103 | + [ValidateNotNull()] |
| 104 | + [Microsoft.Azure.PowerShell.Cmdlets.PostgreSql.Category('Runtime')] |
| 105 | + [System.Management.Automation.PSCredential] |
| 106 | + ${ProxyCredential}, |
| 107 | + |
| 108 | + [Parameter(DontShow)] |
| 109 | + [Microsoft.Azure.PowerShell.Cmdlets.PostgreSql.Category('Runtime')] |
| 110 | + [System.Management.Automation.SwitchParameter] |
| 111 | + ${ProxyUseDefaultCredentials} |
| 112 | + ) |
| 113 | + |
| 114 | + process { |
| 115 | + try { |
| 116 | + $PSBoundParameters.CreateMode = [Microsoft.Azure.PowerShell.Cmdlets.PostgreSql.Support.CreateMode]::GeoRestore |
| 117 | + $null = $PSBoundParameters.Remove('UseGeoRestore') |
| 118 | + |
| 119 | + $server = Get-AzPostgreSqlFlexibleServer -ResourceGroupName $PSBoundParameters.ResourceGroupName -Name $PSBoundParameters.SourceServerName |
| 120 | + $null = $PSBoundParameters.Remove('SourceServerName') |
| 121 | + |
| 122 | + $PSBoundParameters.PointInTimeUtc = $PSBoundParameters["RestorePointInTime"] |
| 123 | + $null = $PSBoundParameters.Remove('RestorePointInTime') |
| 124 | + |
| 125 | + $PSBoundParameters.SourceServerResourceId = $server.Id |
| 126 | + |
| 127 | + $LocationParts = ForEach ($part in $server.Location.Split(" ")) { $part.ToLower() } |
| 128 | + $PSBoundParameters.Location = $LocationParts -Join "" |
| 129 | + |
| 130 | + |
| 131 | + if ($PSBoundParameters.ContainsKey('Sku')) { |
| 132 | + $PSBoundParameters.SkuName = $PSBoundParameters.Sku |
| 133 | + $null = $PSBoundParameters.Remove('Sku') |
| 134 | + } |
| 135 | + |
| 136 | + # Retrieve the paired regions for the source server's location |
| 137 | + $SourceRegion = $server.Location |
| 138 | + $PairedRegion = (Get-AzLocation | Where-Object { $_.Providers -contains "Microsoft.DBforPostgreSQL" -and $_.DisplayName -eq $SourceRegion }).PairedRegion.Name |
| 139 | + if (-not $PairedRegion) { |
| 140 | + throw "No paired regions found for the source region: $SourceRegion" |
| 141 | + } |
| 142 | + $PSBoundParameters.Location = $PairedRegion |
| 143 | + |
| 144 | + if ($PSBoundParameters.ContainsKey('Subnet')) { |
| 145 | + if ($server.NetworkPublicNetworkAccess -eq 'Enabled') { |
| 146 | + throw "The source server is not private access enabled." |
| 147 | + } |
| 148 | + if (!(Get-Module -ListAvailable -Name Az.Network)) { |
| 149 | + throw 'Please install Az.Network module by entering "Install-Module -Name Az.Network"' |
| 150 | + } |
| 151 | + else { |
| 152 | + Import-Module -Name Az.Network |
| 153 | + } |
| 154 | + $VnetId = [string]::Join("/", $PSBoundParameters.Subnet.split("/")[0..8]) |
| 155 | + $SubnetModel = Get-AzVirtualNetworkSubnetConfig -ResourceId $PSBoundParameters.Subnet |
| 156 | + $Delegations = Get-AzDelegation -Subnet $SubnetModel |
| 157 | + if ($null -ne $Delegations) { |
| 158 | + # Valid but incorrect delegation |
| 159 | + $Delegations | ForEach-Object { if ($PSItem.ServiceName -ne $DELEGATION_SERVICE_NAME) { |
| 160 | + $Msg = "Can not use subnet with existing delegations other than {0}" -f $DELEGATION_SERVICE_NAME |
| 161 | + throw $Msg |
| 162 | + } } |
| 163 | + } |
| 164 | + else { |
| 165 | + # Valid but no delegation |
| 166 | + throw "Add delegation " + $DELEGATION_SERVICE_NAME + " to the subnet." |
| 167 | + } |
| 168 | + $PSBoundParameters.NetworkDelegatedSubnetResourceId = $PSBoundParameters.Subnet |
| 169 | + $null = $PSBoundParameters.Remove('Subnet') |
| 170 | + } |
| 171 | + |
| 172 | + if ($PSBoundParameters.ContainsKey('PrivateDnsZone')) { |
| 173 | + if ($server.NetworkPublicNetworkAccess -eq 'Enabled') { |
| 174 | + throw "The source server is not private access enabled." |
| 175 | + } |
| 176 | + if (!(Get-Module -ListAvailable -Name Az.PrivateDns)) { |
| 177 | + throw 'Please install Az.Network module by entering "Install-Module -Name Az.PrivateDns"' |
| 178 | + } |
| 179 | + else { |
| 180 | + Import-Module -Name Az.PrivateDns |
| 181 | + } |
| 182 | + if ([string]::IsNullOrEmpty($VnetId)) { |
| 183 | + $VnetId = [string]::Join("/", $server.NetworkDelegatedSubnetResourceId.split("/")[0..8]) |
| 184 | + } |
| 185 | + $ZoneName = $PSBoundParameters["PrivateDnsZone"].split("/")[-1] |
| 186 | + $DnsResourceGroup = $PSBoundParameters["PrivateDnsZone"].split("/")[4] |
| 187 | + $Links = Get-AzPrivateDnsVirtualNetworkLink -ZoneName $ZoneName -ResourceGroupName $DnsResourceGroup |
| 188 | + $LinkedFlag = $false |
| 189 | + foreach ($Link in $Links) { |
| 190 | + if ($Link.VirtualNetworkId -eq $VnetId) { |
| 191 | + $LinkedFlag = $true |
| 192 | + break |
| 193 | + } |
| 194 | + } |
| 195 | + if (!$LinkedFlag) { |
| 196 | + Write-Host "Adding virtual network link to the DNS zone..." |
| 197 | + New-AzPrivateDnsVirtualNetworkLink -ZoneName $ZoneName -ResourceGroupName $DnsResourceGroup -Name $PSBoundParameters["Name"] -VirtualNetworkId $VnetId |
| 198 | + } |
| 199 | + $PSBoundParameters.NetworkPrivateDnsZoneArmResourceId = $PSBoundParameters.PrivateDnsZone |
| 200 | + $null = $PSBoundParameters.Remove('PrivateDnsZone') |
| 201 | + } |
| 202 | + |
| 203 | + if ($PSBoundParameters.ContainsKey('Tag')) { |
| 204 | + $PSBoundParameters.Tag = $PSBoundParameters.Tag |
| 205 | + } |
| 206 | + |
| 207 | + |
| 208 | + Az.PostgreSql.internal\New-AzPostgreSqlFlexibleServer @PSBoundParameters |
| 209 | + } |
| 210 | + catch { |
| 211 | + throw |
| 212 | + } |
| 213 | + } |
| 214 | +} |
0 commit comments