Skip to content

Commit 8a017cf

Browse files
Update to latest CosmosDB client libraray and allow ServerVersion to … (#14235)
* Update to latest CosmosDB client libraray and allow ServerVersion to be updated for CosmosDB accounts * Add 4.0 ServerVersion in argument completion * Added ServerVersion change description to ChangeLog.md * Adding backup interval and retention parameters to database account create and update cmdlets * Regenerating test record with the new parameters * PR feedback Co-authored-by: Kalyan Khandrika <[email protected]>
1 parent 2941472 commit 8a017cf

File tree

12 files changed

+599
-386
lines changed

12 files changed

+599
-386
lines changed

src/CosmosDB/CosmosDB.Test/ScenarioTests/AccountTests.ps1

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,15 @@
1414

1515
function Test-AccountRelatedCmdlets
1616
{
17-
$rgName = "CosmosDBResourceGroup89"
17+
$rgName = "CosmosDBResourceGroup91"
1818
$location = "East US"
1919
$locationlist = "East US", "West US"
2020
$locationlist2 = "East US", "UK South", "UK West", "South India"
2121
$locationlist3 = "West US", "East US"
2222

2323
$resourceGroup = New-AzResourceGroup -ResourceGroupName $rgName -Location $location
2424

25-
$cosmosDBAccountName = "cosmosdb67"
25+
$cosmosDBAccountName = "cosmosdb678901"
2626

2727
#use an existing account with the following information for Account Update Operations
2828
$cosmosDBExistingAccountName = "dbaccount30"
@@ -34,7 +34,7 @@ function Test-AccountRelatedCmdlets
3434
$networkAclBypass = "AzureServices"
3535
$networkAclBypassResourceId = "/subscriptions/subId/resourcegroups/rgName/providers/Microsoft.Synapse/workspaces/workspaceName"
3636

37-
$cosmosDBAccount = New-AzCosmosDBAccount -ResourceGroupName $rgName -Name $cosmosDBAccountName -DefaultConsistencyLevel "BoundedStaleness" -MaxStalenessIntervalInSeconds 10 -MaxStalenessPrefix 20 -Location $location -IpRule $IpRule -Tag $tags -EnableVirtualNetwork -EnableMultipleWriteLocations -EnableAutomaticFailover -ApiKind "MongoDB" -PublicNetworkAccess $publicNetworkAccess -EnableFreeTier 0 -EnableAnalyticalStorage 0 -ServerVersion "3.2" -NetworkAclBypass $NetworkAclBypass
37+
$cosmosDBAccount = New-AzCosmosDBAccount -ResourceGroupName $rgName -Name $cosmosDBAccountName -DefaultConsistencyLevel "BoundedStaleness" -MaxStalenessIntervalInSeconds 10 -MaxStalenessPrefix 20 -Location $location -IpRule $IpRule -Tag $tags -EnableVirtualNetwork -EnableMultipleWriteLocations -EnableAutomaticFailover -ApiKind "MongoDB" -PublicNetworkAccess $publicNetworkAccess -EnableFreeTier 0 -EnableAnalyticalStorage 0 -ServerVersion "3.2" -NetworkAclBypass $NetworkAclBypass -BackupRetentionIntervalInHours 16 -BackupIntervalInMinutes 480
3838

3939
Assert-AreEqual $cosmosDBAccountName $cosmosDBAccount.Name
4040
Assert-AreEqual "BoundedStaleness" $cosmosDBAccount.ConsistencyPolicy.DefaultConsistencyLevel
@@ -49,6 +49,8 @@ function Test-AccountRelatedCmdlets
4949
Assert-AreEqual $cosmosDBAccount.EnableFreeTier 0
5050
Assert-AreEqual $cosmosDBAccount.NetworkAclBypass $NetworkAclBypass
5151
Assert-AreEqual $cosmosDBAccount.NetworkAclBypassResourceIds.Count 0
52+
Assert-AreEqual $cosmosDBAccount.BackupPolicy.BackupIntervalInMinutes 480
53+
Assert-AreEqual $cosmosDBAccount.BackupPolicy.BackupRetentionIntervalInHours 16
5254

5355
# create an existing database
5456
Try {
@@ -58,9 +60,9 @@ function Test-AccountRelatedCmdlets
5860
Assert-AreEqual $_.Exception.Message ("Resource with Name " + $cosmosDBAccountName + " already exists.")
5961
}
6062

61-
$updatedCosmosDBAccount = Update-AzCosmosDBAccount -ResourceGroupName $existingResourceGroupName -Name $cosmosDBExistingAccountName -DefaultConsistencyLevel "BoundedStaleness" -MaxStalenessIntervalInSeconds 10 -MaxStalenessPrefix 20 -IpRule $IpRule -Tag $tags -EnableVirtualNetwork 1 -EnableAutomaticFailover 1 -PublicNetworkAccess $publicNetworkAccess -NetworkAclBypass $NetworkAclBypass -NetworkAclBypassResourceId $networkAclBypassResourceId
63+
$updatedCosmosDBAccount = Update-AzCosmosDBAccount -ResourceGroupName $rgName -Name $cosmosDBAccountName -DefaultConsistencyLevel "BoundedStaleness" -MaxStalenessIntervalInSeconds 10 -MaxStalenessPrefix 20 -IpRule $IpRule -Tag $tags -EnableVirtualNetwork 1 -EnableAutomaticFailover 1 -PublicNetworkAccess $publicNetworkAccess -NetworkAclBypass $NetworkAclBypass -NetworkAclBypassResourceId $networkAclBypassResourceId -BackupRetentionIntervalInHours 8 -BackupIntervalInMinutes 240
6264

63-
Assert-AreEqual $cosmosDBExistingAccountName $updatedCosmosDBAccount.Name
65+
Assert-AreEqual $cosmosDBAccountName $updatedCosmosDBAccount.Name
6466
Assert-AreEqual "BoundedStaleness" $updatedCosmosDBAccount.ConsistencyPolicy.DefaultConsistencyLevel
6567
Assert-AreEqual 10 $updatedCosmosDBAccount.ConsistencyPolicy.MaxIntervalInSeconds
6668
Assert-AreEqual 20 $updatedCosmosDBAccount.ConsistencyPolicy.MaxStalenessPrefix
@@ -69,6 +71,8 @@ function Test-AccountRelatedCmdlets
6971
Assert-AreEqual $updatedCosmosDBAccount.PublicNetworkAccess $publicNetworkAccess
7072
Assert-AreEqual $updatedCosmosDBAccount.NetworkAclBypass $NetworkAclBypass
7173
Assert-AreEqual $updatedCosmosDBAccount.NetworkAclBypassResourceIds.Count 1
74+
Assert-AreEqual $updatedCosmosDBAccount.BackupPolicy.BackupIntervalInMinutes 240
75+
Assert-AreEqual $updatedCosmosDBAccount.BackupPolicy.BackupRetentionIntervalInHours 8
7276

7377
$cosmosDBAccountKey = Get-AzCosmosDBAccountKey -Name $cosmosDBAccountName -ResourceGroupName $rgname
7478
Assert-NotNull $cosmosDBAccountKey

src/CosmosDB/CosmosDB.Test/SessionRecords/Microsoft.Azure.Commands.CosmosDB.Test.ScenarioTests.ScenarioTest.AccountTests/TestAccountRelatedCmdlets.json

Lines changed: 365 additions & 365 deletions
Large diffs are not rendered by default.

src/CosmosDB/CosmosDB/ChangeLog.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020

2121
## Upcoming Release
2222
* Introduced NetworkAclBypass and NetworkAclBypassResourceIds for Database Account cmdlets.
23+
* Introduced ServerVersion parameter to Update-AzCosmosDBAccount.
24+
* Introduced BackupInterval and BackupRetention for Database Account cmdlets
2325

2426
## Version 1.0.0
2527
* General availability of 'Az.CosmosDB' module

src/CosmosDB/CosmosDB/CosmosDBAccount/NewAzCosmosDBAccount.cs

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,10 +50,6 @@ public class NewAzCosmosDBAccount : NewOrUpdateAzCosmosDBAccount
5050
[Parameter(Mandatory = false, HelpMessage = Constants.EnableFreeTierHelpMessage)]
5151
public bool? EnableFreeTier { get; set; }
5252

53-
[Parameter(Mandatory = false, HelpMessage = Constants.ServerVersionHelpMessage)]
54-
[PSArgumentCompleter(SDKModel.ServerVersion.ThreeFullStopTwo, SDKModel.ServerVersion.ThreeFullStopSix)]
55-
public string ServerVersion { get; set; }
56-
5753
[Parameter(Mandatory = false, HelpMessage = Constants.LocationHelpMessage)]
5854
[ValidateNotNullOrEmpty]
5955
public string[] Location { get; set; }
@@ -215,6 +211,18 @@ public override void ExecuteCmdlet()
215211

216212
databaseAccountCreateUpdateParameters.Kind = ApiKind;
217213

214+
if (BackupIntervalInMinutes.HasValue || BackupRetentionIntervalInHours.HasValue)
215+
{
216+
databaseAccountCreateUpdateParameters.BackupPolicy = new PeriodicModeBackupPolicy()
217+
{
218+
PeriodicModeProperties = new PeriodicModeProperties()
219+
{
220+
BackupIntervalInMinutes = BackupIntervalInMinutes,
221+
BackupRetentionIntervalInHours = BackupRetentionIntervalInHours
222+
}
223+
};
224+
}
225+
218226
if (ShouldProcess(Name, "Creating Database Account"))
219227
{
220228
DatabaseAccountGetResults cosmosDBAccount = CosmosDBManagementClient.DatabaseAccounts.CreateOrUpdateWithHttpMessagesAsync(ResourceGroupName, Name, databaseAccountCreateUpdateParameters).GetAwaiter().GetResult().Body;

src/CosmosDB/CosmosDB/CosmosDBAccount/NewOrUpdateAzCosmosDBAccount.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,16 @@ public class NewOrUpdateAzCosmosDBAccount : AzureCosmosDBCmdletBase
8181
[ValidateNotNullOrEmpty]
8282
public string[] NetworkAclBypassResourceId { get; set; }
8383

84+
[Parameter(Mandatory = false, HelpMessage = Constants.ServerVersionHelpMessage)]
85+
[PSArgumentCompleter(SDKModel.ServerVersion.ThreeFullStopTwo, SDKModel.ServerVersion.ThreeFullStopSix, SDKModel.ServerVersion.FourFullStopZero)]
86+
public string ServerVersion { get; set; }
87+
88+
[Parameter(Mandatory = false, HelpMessage = Constants.BackupIntervalInMinHelpMessage)]
89+
public int? BackupIntervalInMinutes { get; set; }
90+
91+
[Parameter(Mandatory = false, HelpMessage = Constants.BackupRetentionInHoursHelpMessage)]
92+
public int? BackupRetentionIntervalInHours { get; set; }
93+
8494
public ConsistencyPolicy PopoulateConsistencyPolicy(string DefaultConsistencyLevel, int? MaxStalenessIntervalInSeconds, int? MaxStalenessPrefix)
8595
{
8696
ConsistencyPolicy consistencyPolicy = new ConsistencyPolicy();

src/CosmosDB/CosmosDB/CosmosDBAccount/UpdateAzCosmosDBAccount.cs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,12 +140,42 @@ public override void ExecuteCmdlet()
140140
databaseAccountUpdateParameters.IpRules = base.PopulateIpRules(IpRule);
141141
}
142142

143+
if (ServerVersion != null)
144+
{
145+
if (databaseAccountUpdateParameters.ApiProperties == null)
146+
{
147+
databaseAccountUpdateParameters.ApiProperties = new ApiProperties();
148+
}
149+
150+
databaseAccountUpdateParameters.ApiProperties.ServerVersion = ServerVersion;
151+
}
152+
143153
if (NetworkAclBypassResourceId != null)
144154
{
145155
Collection<string> networkAclBypassResourceId = new Collection<string>(NetworkAclBypassResourceId);
146156
databaseAccountUpdateParameters.NetworkAclBypassResourceIds = networkAclBypassResourceId;
147157
}
148158

159+
if (BackupIntervalInMinutes.HasValue || BackupRetentionIntervalInHours.HasValue)
160+
{
161+
if (readDatabase.BackupPolicy is PeriodicModeBackupPolicy)
162+
{
163+
databaseAccountUpdateParameters.BackupPolicy = new PeriodicModeBackupPolicy()
164+
{
165+
PeriodicModeProperties = new PeriodicModeProperties()
166+
{
167+
BackupIntervalInMinutes = BackupIntervalInMinutes,
168+
BackupRetentionIntervalInHours = BackupRetentionIntervalInHours
169+
}
170+
};
171+
}
172+
else
173+
{
174+
WriteWarning("Can accept BackupInterval or BackupRetention parameters only for accounts with PeriodicMode backup policy");
175+
return;
176+
}
177+
}
178+
149179
if (ShouldProcess(Name, "Updating Database Account"))
150180
{
151181
DatabaseAccountGetResults cosmosDBAccount = CosmosDBManagementClient.DatabaseAccounts.UpdateWithHttpMessagesAsync(ResourceGroupName, Name, databaseAccountUpdateParameters).GetAwaiter().GetResult().Body;

src/CosmosDB/CosmosDB/Helpers/Constants.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,9 @@ internal static class Constants
5656
public const string NetworkAclBypassHelpMessage = "Whether or not Network Acl Bypass is enabled for this account for Synapse Link. Possible values include: 'None', 'AzureServices'.";
5757
public const string NetworkAclBypassResourceIdHelpMessage = "List of Resource Ids to allow Network Acl Bypass for Synapse Link.";
5858

59+
//Backup specific help messages
60+
public const string BackupIntervalInMinHelpMessage = "The interval(in minutes) with which backup are taken (only for accounts with periodic mode backups)";
61+
public const string BackupRetentionInHoursHelpMessage = "The time(in hours) for which each backup is retained (only for accounts with periodic mode backups)";
5962

6063
//Sql cmdlets help messages
6164
public const string DatabaseNameHelpMessage = "Database name.";

src/CosmosDB/CosmosDB/Models/DatabaseAccount/PSApiProperties.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public PSApiProperties(ApiProperties apiProperties)
3030
//
3131
// Summary:
3232
// Gets or sets describes the ServerVersion of an a MongoDB account. Possible values
33-
// include: '3.2', '3.6'
33+
// include: '3.2', '3.6', '4.0'
3434
public string ServerVersion { get; set; }
3535
}
3636
}
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
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+
using Microsoft.Azure.Commands.CosmosDB.Helpers;
16+
using Microsoft.Azure.Commands.ResourceManager.Common.ArgumentCompleters;
17+
using Microsoft.Azure.Management.CosmosDB.Models;
18+
using System.Management.Automation;
19+
20+
namespace Microsoft.Azure.Commands.CosmosDB.Models
21+
{
22+
public class PSBackupPolicy
23+
{
24+
public static readonly string PeriodicModeBackupType = "Periodic";
25+
26+
public PSBackupPolicy()
27+
{
28+
}
29+
30+
public PSBackupPolicy(BackupPolicy backupPolicy)
31+
{
32+
if (backupPolicy is PeriodicModeBackupPolicy)
33+
{
34+
PeriodicModeBackupPolicy periodicModeBackupPolicy = backupPolicy as PeriodicModeBackupPolicy;
35+
BackupIntervalInMinutes = periodicModeBackupPolicy.PeriodicModeProperties.BackupIntervalInMinutes;
36+
BackupRetentionIntervalInHours = periodicModeBackupPolicy.PeriodicModeProperties.BackupRetentionIntervalInHours;
37+
BackupType = PeriodicModeBackupType;
38+
}
39+
else
40+
{
41+
return;
42+
}
43+
}
44+
45+
public int? BackupIntervalInMinutes { get; set; }
46+
47+
public int? BackupRetentionIntervalInHours { get; set; }
48+
49+
public string BackupType { get; set; }
50+
51+
public BackupPolicy ToSDKModel()
52+
{
53+
if (BackupType.Equals(PSBackupPolicy.PeriodicModeBackupType))
54+
{
55+
PeriodicModeBackupPolicy periodicModeBackupPolicy = new PeriodicModeBackupPolicy
56+
{
57+
PeriodicModeProperties = new PeriodicModeProperties()
58+
{
59+
BackupIntervalInMinutes = BackupIntervalInMinutes,
60+
BackupRetentionIntervalInHours = BackupRetentionIntervalInHours
61+
}
62+
};
63+
64+
return periodicModeBackupPolicy;
65+
}
66+
else
67+
{
68+
return null;
69+
}
70+
}
71+
}
72+
}

src/CosmosDB/CosmosDB/Models/DatabaseAccount/PSDatabaseAccountGetResults.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ public PSDatabaseAccountGetResults(DatabaseAccountGetResults databaseAccountGetR
6060
EnableAnalyticalStorage = databaseAccountGetResults.EnableAnalyticalStorage;
6161
NetworkAclBypass = databaseAccountGetResults.NetworkAclBypass;
6262
NetworkAclBypassResourceIds = databaseAccountGetResults.NetworkAclBypassResourceIds;
63+
BackupPolicy = new PSBackupPolicy(databaseAccountGetResults.BackupPolicy);
6364
}
6465

6566
//
@@ -190,5 +191,9 @@ public PSDatabaseAccountGetResults(DatabaseAccountGetResults databaseAccountGetR
190191
// Summary:
191192
// Gets or sets list of Network Acl Bypass Resource Ids.
192193
public IList<string> NetworkAclBypassResourceIds { get; set; }
194+
//
195+
// Summary:
196+
// Gets or sets the backup policy of the database account.
197+
public PSBackupPolicy BackupPolicy { get; set; }
193198
}
194199
}

src/CosmosDB/CosmosDB/help/New-AzCosmosDBAccount.md

Lines changed: 39 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,15 @@ Create a new CosmosDB Account.
1414

1515
```
1616
New-AzCosmosDBAccount [-EnableAutomaticFailover] [-EnableMultipleWriteLocations] [-EnableVirtualNetwork]
17-
[-ApiKind <String>] [-DisableKeyBasedMetadataWriteAccess] [-EnableFreeTier <Boolean>]
18-
[-ServerVersion <String>] [-Location <String[]>] [-LocationObject <PSLocation[]>] -ResourceGroupName <String>
19-
-Name <String> [-DefaultConsistencyLevel <String>] [-IpRule <String[]>]
20-
[-MaxStalenessIntervalInSeconds <Int32>] [-MaxStalenessPrefix <Int32>] [-Tag <Hashtable>]
21-
[-VirtualNetworkRule <String[]>] [-VirtualNetworkRuleObject <PSVirtualNetworkRule[]>]
22-
[-PublicNetworkAccess <String>] [-KeyVaultKeyUri <String>] [-EnableAnalyticalStorage <Boolean>] [-AsJob]
23-
[-NetworkAclBypass <String>] [-NetworkAclBypassResourceId <String[]>]
24-
[-DefaultProfile <IAzureContextContainer>] [-WhatIf] [-Confirm] [<CommonParameters>]
17+
[-ApiKind <String>] [-DisableKeyBasedMetadataWriteAccess] [-EnableFreeTier <Boolean>] [-Location <String[]>]
18+
[-LocationObject <PSLocation[]>] -ResourceGroupName <String> -Name <String>
19+
[-DefaultConsistencyLevel <String>] [-IpRule <String[]>] [-MaxStalenessIntervalInSeconds <Int32>]
20+
[-MaxStalenessPrefix <Int32>] [-Tag <Hashtable>] [-VirtualNetworkRule <String[]>]
21+
[-VirtualNetworkRuleObject <PSVirtualNetworkRule[]>] [-PublicNetworkAccess <String>]
22+
[-KeyVaultKeyUri <String>] [-EnableAnalyticalStorage <Boolean>] [-AsJob] [-NetworkAclBypass <String>]
23+
[-NetworkAclBypassResourceId <String[]>] [-ServerVersion <String>] [-BackupIntervalInMinutes <Int32>]
24+
[-BackupRetentionIntervalInHours <Int32>] [-DefaultProfile <IAzureContextContainer>] [-WhatIf] [-Confirm]
25+
[<CommonParameters>]
2526
```
2627

2728
## DESCRIPTION
@@ -92,6 +93,36 @@ Accept pipeline input: False
9293
Accept wildcard characters: False
9394
```
9495
96+
### -BackupIntervalInMinutes
97+
The interval(in minutes) with which backup are taken (only for accounts with periodic mode backups)
98+
99+
```yaml
100+
Type: Int32
101+
Parameter Sets: (All)
102+
Aliases:
103+
104+
Required: False
105+
Position: Named
106+
Default value: None
107+
Accept pipeline input: False
108+
Accept wildcard characters: False
109+
```
110+
111+
### -BackupRetentionIntervalInHours
112+
The time(in hours) for which each backup is retained (only for accounts with periodic mode backups)
113+
114+
```yaml
115+
Type: Int32
116+
Parameter Sets: (All)
117+
Aliases:
118+
119+
Required: False
120+
Position: Named
121+
Default value: None
122+
Accept pipeline input: False
123+
Accept wildcard characters: False
124+
```
125+
95126
### -Confirm
96127
Prompts you for confirmation before running the cmdlet.
97128

0 commit comments

Comments
 (0)