Skip to content

Commit 59b95eb

Browse files
ShawnXxymsJinLei
andauthored
Add Geo-restore functionality to Az.PostgreSql module (Azure#27760)
Co-authored-by: Jin Lei <54836179+msJinLei@users.noreply.github.com>
1 parent ab58e35 commit 59b95eb

12 files changed

+472
-39
lines changed
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the Apache License, Version 2.0 (the ""License"");
3+
// you may not use this file except in compliance with the License.
4+
// You may obtain a copy of the License at
5+
// http://www.apache.org/licenses/LICENSE-2.0
6+
// Unless required by applicable law or agreed to in writing, software
7+
// distributed under the License is distributed on an ""AS IS"" BASIS,
8+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
9+
// See the License for the specific language governing permissions and
10+
// limitations under the License.
11+
// Code generated by Microsoft (R) AutoRest Code Generator.Changes may cause incorrect behavior and will be lost if the code
12+
// is regenerated.
13+
14+
using System;
15+
using System.Reflection;
16+
using System.Runtime.CompilerServices;
17+
using System.Runtime.InteropServices;
18+
19+
[assembly: System.Reflection.AssemblyCompanyAttribute("Microsoft")]
20+
[assembly: System.Reflection.AssemblyCopyrightAttribute("Copyright © Microsoft")]
21+
[assembly: System.Reflection.AssemblyProductAttribute("Microsoft Azure PowerShell")]
22+
[assembly: System.Reflection.AssemblyTitleAttribute("Microsoft Azure PowerShell - PostgreSql")]
23+
[assembly: System.Reflection.AssemblyFileVersionAttribute("1.2.0")]
24+
[assembly: System.Reflection.AssemblyVersionAttribute("1.2.0")]
25+
[assembly: System.Runtime.InteropServices.ComVisibleAttribute(false)]
26+
[assembly: System.CLSCompliantAttribute(false)]
Lines changed: 214 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,214 @@
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+
}

src/PostgreSql/PostgreSql.Autorest/docs/Az.PostgreSql.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
---
22
Module Name: Az.PostgreSql
3-
Module Guid: 85809807-c6ca-41fb-b667-7ac040e536a4
3+
Module Guid: b58e4c63-c46d-424a-9b42-50f1324245ab
44
Download Help Link: https://learn.microsoft.com/powershell/module/az.postgresql
55
Help Version: 1.0.0.0
66
Locale: en-US
@@ -93,7 +93,7 @@ Restarts a server.
9393
Restarts a server.
9494

9595
### [Restore-AzPostgreSqlFlexibleServer](Restore-AzPostgreSqlFlexibleServer.md)
96-
Restore a server from an existing backup
96+
Restore a PostgreSQL flexible server using Geo-restore
9797

9898
### [Restore-AzPostgreSqlServer](Restore-AzPostgreSqlServer.md)
9999
Restore a server from an existing backup

src/PostgreSql/PostgreSql.Autorest/docs/Restore-AzPostgreSqlFlexibleServer.md

Lines changed: 61 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,26 @@ schema: 2.0.0
88
# Restore-AzPostgreSqlFlexibleServer
99

1010
## SYNOPSIS
11-
Restore a server from an existing backup
11+
Restore a PostgreSQL flexible server using Geo-restore
1212

1313
## SYNTAX
1414

15+
### PointInTimeRestore (Default)
1516
```
1617
Restore-AzPostgreSqlFlexibleServer -Name <String> -ResourceGroupName <String> -SourceServerName <String>
1718
-RestorePointInTime <DateTime> [-SubscriptionId <String>] [-PrivateDnsZone <String>] [-Subnet <String>]
1819
[-Zone <String>] [-DefaultProfile <PSObject>] [-AsJob] [-NoWait] [-Confirm] [-WhatIf] [<CommonParameters>]
1920
```
2021

22+
### GeoRestore
23+
```
24+
Restore-AzPostgreSqlFlexibleServer -Name <String> -ResourceGroupName <String> -SourceServerName <String>
25+
-RestorePointInTime <DateTime> -UseGeoRestore [-SubscriptionId <String>] [-Sku <String>] [-Tag <Hashtable>]
26+
[-DefaultProfile <PSObject>] [-AsJob] [-NoWait] [-Confirm] [-WhatIf] [<CommonParameters>]
27+
```
28+
2129
## DESCRIPTION
22-
Restore a server from an existing backup
30+
Restore a PostgreSQL flexible server using Geo-restore
2331

2432
## EXAMPLES
2533

@@ -87,7 +95,7 @@ Accept wildcard characters: False
8795
```
8896
8997
### -Name
90-
The name of the server.
98+
The name of the server to restore.
9199
92100
```yaml
93101
Type: System.String
@@ -125,7 +133,7 @@ The suffix of dns zone has to be same as that of fully qualified domain of the s
125133
126134
```yaml
127135
Type: System.String
128-
Parameter Sets: (All)
136+
Parameter Sets: PointInTimeRestore
129137
Aliases:
130138

131139
Required: False
@@ -136,7 +144,7 @@ Accept wildcard characters: False
136144
```
137145
138146
### -ResourceGroupName
139-
The name of the resource group that contains the resource, You can obtain this value from the Azure Resource Manager API or the portal.
147+
The name of the resource group that contains the resource.
140148
141149
```yaml
142150
Type: System.String
@@ -165,8 +173,23 @@ Accept pipeline input: False
165173
Accept wildcard characters: False
166174
```
167175
176+
### -Sku
177+
The name of the sku, typically, tier + family + cores, e.g., B_Gen4_1, GP_Gen5_8.
178+
179+
```yaml
180+
Type: System.String
181+
Parameter Sets: GeoRestore
182+
Aliases:
183+
184+
Required: False
185+
Position: Named
186+
Default value: None
187+
Accept pipeline input: False
188+
Accept wildcard characters: False
189+
```
190+
168191
### -SourceServerName
169-
The name of the source server.
192+
The name of the source server to restore from.
170193
171194
```yaml
172195
Type: System.String
@@ -187,7 +210,7 @@ After delegation, this subnet cannot be used for any other type of Azure resourc
187210
188211
```yaml
189212
Type: System.String
190-
Parameter Sets: (All)
213+
Parameter Sets: PointInTimeRestore
191214
Aliases:
192215

193216
Required: False
@@ -212,12 +235,42 @@ Accept pipeline input: False
212235
Accept wildcard characters: False
213236
```
214237
238+
### -Tag
239+
Application-specific metadata in the form of key-value pairs.
240+
241+
```yaml
242+
Type: System.Collections.Hashtable
243+
Parameter Sets: GeoRestore
244+
Aliases:
245+
246+
Required: False
247+
Position: Named
248+
Default value: None
249+
Accept pipeline input: False
250+
Accept wildcard characters: False
251+
```
252+
253+
### -UseGeoRestore
254+
Use Geo mode to restore
255+
256+
```yaml
257+
Type: System.Management.Automation.SwitchParameter
258+
Parameter Sets: GeoRestore
259+
Aliases:
260+
261+
Required: True
262+
Position: Named
263+
Default value: None
264+
Accept pipeline input: False
265+
Accept wildcard characters: False
266+
```
267+
215268
### -Zone
216269
Availability zone into which to provision the resource.
217270
218271
```yaml
219272
Type: System.String
220-
Parameter Sets: (All)
273+
Parameter Sets: PointInTimeRestore
221274
Aliases:
222275

223276
Required: False
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
{
2-
"generate_Id": "4c0ec4db-1f6f-432c-91e4-6c48fa388d57"
2+
"generate_Id": "f0b983f7-1a63-4b8a-b719-bcd0e1c97929"
33
}

0 commit comments

Comments
 (0)