Skip to content

Commit 29bfe01

Browse files
MehaKaushikMeha Kaushik
andauthored
[CosmosDB] Bug Fixes and Enhancements (Azure#11513)
* fixing location issue * more fixes * Adding tests * Updated help and Account update and creation * fixing location issue * more fixes * Adding tests * Updated help and Account update and creation * Renaming GlobalDocumentDB to Sql * changes in changelog.md and renaming apikind GlobalDocumentDB to Sql * Fix indentation in ChangeLog Co-authored-by: Meha Kaushik <[email protected]>
1 parent 744ae6d commit 29bfe01

File tree

15 files changed

+1578
-59
lines changed

15 files changed

+1578
-59
lines changed

src/CosmosDB/CosmosDB.Test/ScenarioTests/AccountTests.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,5 +47,12 @@ public void TestAccountRelatedCmdletsUsingObject()
4747
{
4848
TestController.NewInstance.RunPowerShellTest(_logger, "Test-AccountRelatedCmdletsUsingObject");
4949
}
50+
51+
[Fact]
52+
[Trait(Category.AcceptanceType, Category.CheckIn)]
53+
public void TestAddRegionOperation()
54+
{
55+
TestController.NewInstance.RunPowerShellTest(_logger, "Test-AddRegionOperation");
56+
}
5057
}
5158
}

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

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,4 +166,23 @@ function Test-AccountRelatedCmdletsUsingObject
166166

167167
$IsAccountDeleted = Remove-AzCosmosDBAccount -InputObject $cosmosDBAccount -PassThru
168168
Assert-AreEqual $IsAccountDeleted true
169+
}
170+
171+
function Test-AddRegionOperation
172+
{
173+
$rgName = "CosmosDBResourceGroup2"
174+
$location = "East US"
175+
$locationlist = "East US", "West US"
176+
$cosmosDBAccountName = "testupdateregion"
177+
$resourceGroup = New-AzResourceGroup -ResourceGroupName $rgName -Location $location
178+
179+
$cosmosDBAccount = New-AzCosmosDBAccount -ResourceGroupName $rgName -Name $cosmosDBAccountName -Location $location -EnableMultipleWriteLocations -EnableAutomaticFailover
180+
do
181+
{
182+
$cosmosDBAccount = Get-AzCosmosDBAccount -ResourceGroupName $rgName -Name $cosmosDBAccountName
183+
} while ($cosmosDBAccount.ProvisioningState -ne "Succeeded")
184+
185+
$updatedCosmosDBAccount = Update-AzCosmosDBAccountRegion -ResourceGroupName $rgName -Name $cosmosDBAccountName -Location $locationlist
186+
$updatedCosmosDBAccount = Get-AzCosmosDBAccount -ResourceGroupName $rgName -Name $cosmosDBAccountName
187+
Assert-AreEqual $cosmosDBAccount.Locations.Count $updatedCosmosDBAccount.Locations.Count - 1
169188
}

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

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

src/CosmosDB/CosmosDB/ChangeLog.md

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,16 @@
1919
-->
2020

2121
## Upcoming Release
22+
* Changes in New-AzCosmosDBAccount and Update-AzCosmosDBAccount
23+
- Allows empty string as a value for IpRangeFilter
24+
- Renamed ApiKind value GlobalDocumentDB to Sql.
25+
- Added parameter DisableKeyBasedMetadataWriteAccess
2226
* Introduces cmdlets to update throughput for Sql Database and Container, Cassandra Keyspace and Table, MongoDB Database and Collection, Gremlin Database and Graph and Table.
23-
- Update-AzCosmosDBSqlContainerThroughput, Update-AzCosmosDBSqlDatabaseThroughput,
24-
Update-AzCosmosDBMongoDBCollectionThroughput, Update-AzCosmosDBMongoDBDatabaseThroughput,
25-
Update-AzCosmosDBGremlinGraphThroughput, Update-AzCosmosDBGremlinDatabaseThroughput,
26-
Update-AzCosmosDBCassandraTableThroughput, Update-AzCosmosDBCassandraKeyspaceThroughput,
27-
Update-AzCosmosDBTableThroughput
28-
27+
- Update-AzCosmosDBSqlContainerThroughput, Update-AzCosmosDBSqlDatabaseThroughput,
28+
Update-AzCosmosDBMongoDBCollectionThroughput, Update-AzCosmosDBMongoDBDatabaseThroughput,
29+
Update-AzCosmosDBGremlinGraphThroughput, Update-AzCosmosDBGremlinDatabaseThroughput,
30+
Update-AzCosmosDBCassandraTableThroughput, Update-AzCosmosDBCassandraKeyspaceThroughput,
31+
Update-AzCosmosDBTableThroughput
2932
## Version 0.1.3
3033
* Allowing Account Creation for API Types: Gremlin, Cassandra and Table.
3134
* Bug Fixes

src/CosmosDB/CosmosDB/CosmosDBAccount/NewAzCosmosDBAccount.cs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
using System.Collections;
1717
using System.Collections.Generic;
1818
using System.Collections.ObjectModel;
19+
using System.Linq;
1920
using System.Management.Automation;
2021
using Microsoft.Azure.Commands.CosmosDB.Helpers;
2122
using Microsoft.Azure.Commands.CosmosDB.Models;
@@ -51,7 +52,7 @@ public class NewAzCosmosDBAccount : AzureCosmosDBCmdletBase
5152
public SwitchParameter EnableVirtualNetwork { get; set; }
5253

5354
[Parameter(Mandatory = false, HelpMessage = Constants.IpRangeFilterHelpMessage)]
54-
[ValidateNotNullOrEmpty]
55+
[ValidateNotNull]
5556
public string[] IpRangeFilter { get; set; }
5657

5758
[Parameter(Mandatory = false, HelpMessage = Constants.LocationHelpMessage)]
@@ -81,9 +82,12 @@ public class NewAzCosmosDBAccount : AzureCosmosDBCmdletBase
8182
public PSVirtualNetworkRule[] VirtualNetworkRuleObject { get; set; }
8283

8384
[Parameter(Mandatory = false, HelpMessage = Constants.ApiKindHelpMessage)]
84-
[PSArgumentCompleter("GlobalDocumentDB", "MongoDB", "Gremlin", "Cassandra", "Table")]
85+
[PSArgumentCompleter("Sql", "MongoDB", "Gremlin", "Cassandra", "Table")]
8586
public string ApiKind { get; set; }
8687

88+
[Parameter(Mandatory = false, HelpMessage = Constants.DisableKeyBasedMetadataWriteAccessHelpMessage)]
89+
public SwitchParameter DisableKeyBasedMetadataWriteAccess { get; set; }
90+
8791
[Parameter(Mandatory = false, HelpMessage = Constants.AsJobHelpMessage)]
8892
public SwitchParameter AsJob { get; set; }
8993

@@ -191,26 +195,20 @@ public override void ExecuteCmdlet()
191195
string IpRangeFilterAsString = null;
192196
if (IpRangeFilter != null && IpRangeFilter.Length > 0)
193197
{
194-
for (int i = 0; i < IpRangeFilter.Length; i++)
195-
{
196-
if (i == 0)
197-
{
198-
IpRangeFilterAsString = IpRangeFilter[0];
199-
}
200-
else
201-
IpRangeFilterAsString = string.Concat(IpRangeFilterAsString, ",", IpRangeFilter[i]);
202-
}
198+
IpRangeFilterAsString = IpRangeFilter?.Aggregate(string.Empty, (output, next) => string.Concat(output, (!string.IsNullOrWhiteSpace(output) && !string.IsNullOrWhiteSpace(next) ? "," : string.Empty), next)) ?? string.Empty;
203199
}
204200

205201
DatabaseAccountCreateUpdateParameters databaseAccountCreateUpdateParameters = new DatabaseAccountCreateUpdateParameters(locations:LocationCollection, location: writeLocation, name:Name, consistencyPolicy:consistencyPolicy, tags:tags, ipRangeFilter:IpRangeFilterAsString);
206202
databaseAccountCreateUpdateParameters.EnableMultipleWriteLocations = EnableMultipleWriteLocations;
207203
databaseAccountCreateUpdateParameters.IsVirtualNetworkFilterEnabled = EnableVirtualNetwork;
208204
databaseAccountCreateUpdateParameters.EnableAutomaticFailover = EnableAutomaticFailover;
209205
databaseAccountCreateUpdateParameters.VirtualNetworkRules = virtualNetworkRule;
206+
databaseAccountCreateUpdateParameters.DisableKeyBasedMetadataWriteAccess = DisableKeyBasedMetadataWriteAccess;
207+
databaseAccountCreateUpdateParameters.IpRangeFilter = IpRangeFilterAsString;
210208

211209
if (!string.IsNullOrEmpty(ApiKind))
212210
{
213-
if (!ApiKind.Equals("GlobalDocumentDB", StringComparison.OrdinalIgnoreCase) && !ApiKind.Equals("MongoDB", StringComparison.OrdinalIgnoreCase))
211+
if (!ApiKind.Equals("MongoDB", StringComparison.OrdinalIgnoreCase))
214212
{
215213
switch (ApiKind)
216214
{
@@ -223,6 +221,8 @@ public override void ExecuteCmdlet()
223221
case "Table":
224222
databaseAccountCreateUpdateParameters.Capabilities = new List<Capability> { new Capability { Name = "EnableTable" } };
225223
break;
224+
case "Sql":
225+
break;
226226
}
227227

228228
ApiKind = null;

src/CosmosDB/CosmosDB/CosmosDBAccount/UpdateAzCosmosDBAccount.cs

Lines changed: 26 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -51,16 +51,16 @@ public class UpdateAzCosmosDBAccount : AzureCosmosDBCmdletBase
5151
public string DefaultConsistencyLevel { get; set; }
5252

5353
[Parameter(Mandatory = false, HelpMessage = Constants.EnableAutomaticFailoverHelpMessage)]
54-
public bool EnableAutomaticFailover { get; set; }
54+
public bool? EnableAutomaticFailover { get; set; }
5555

5656
[Parameter(Mandatory = false, HelpMessage = Constants.EnableMultipleWriteLocationsHelpMessage)]
57-
public bool EnableMultipleWriteLocations { get; set; }
57+
public bool? EnableMultipleWriteLocations { get; set; }
5858

5959
[Parameter(Mandatory = false, HelpMessage = Constants.EnableVirtualNetworkHelpMessage)]
60-
public bool EnableVirtualNetwork { get; set; }
60+
public bool? EnableVirtualNetwork { get; set; }
6161

6262
[Parameter(Mandatory = false, HelpMessage = Constants.IpRangeFilterHelpMessage)]
63-
[ValidateNotNullOrEmpty]
63+
[ValidateNotNull]
6464
public string[] IpRangeFilter { get; set; }
6565

6666
[Parameter(Mandatory = false, HelpMessage = Constants.MaxStalenessIntervalInSecondsHelpMessage)]
@@ -81,6 +81,9 @@ public class UpdateAzCosmosDBAccount : AzureCosmosDBCmdletBase
8181
[ValidateNotNullOrEmpty]
8282
public PSVirtualNetworkRule[] VirtualNetworkRuleObject { get; set; }
8383

84+
[Parameter(Mandatory = false, HelpMessage = Constants.DisableKeyBasedMetadataWriteAccessHelpMessage)]
85+
public bool? DisableKeyBasedMetadataWriteAccess { get; set; }
86+
8487
[Parameter(Mandatory = false, HelpMessage = Constants.AsJobHelpMessage)]
8588
public SwitchParameter AsJob { get; set; }
8689

@@ -103,10 +106,23 @@ public override void ExecuteCmdlet()
103106

104107
DatabaseAccountGetResults readDatabase = CosmosDBManagementClient.DatabaseAccounts.GetWithHttpMessagesAsync(ResourceGroupName, Name).GetAwaiter().GetResult().Body;
105108

106-
DatabaseAccountUpdateParameters databaseAccountUpdateParameters = new DatabaseAccountUpdateParameters(locations: readDatabase.ReadLocations, location: readDatabase.WriteLocations.ElementAt(0).LocationName);
107-
databaseAccountUpdateParameters.EnableMultipleWriteLocations = EnableMultipleWriteLocations;
108-
databaseAccountUpdateParameters.IsVirtualNetworkFilterEnabled = EnableVirtualNetwork;
109-
databaseAccountUpdateParameters.EnableAutomaticFailover = EnableAutomaticFailover;
109+
DatabaseAccountUpdateParameters databaseAccountUpdateParameters = new DatabaseAccountUpdateParameters(locations: readDatabase.Locations, location: readDatabase.WriteLocations.ElementAt(0).LocationName);
110+
if (EnableMultipleWriteLocations != null)
111+
{
112+
databaseAccountUpdateParameters.EnableMultipleWriteLocations = EnableMultipleWriteLocations;
113+
}
114+
if (EnableVirtualNetwork != null)
115+
{
116+
databaseAccountUpdateParameters.IsVirtualNetworkFilterEnabled = EnableVirtualNetwork;
117+
}
118+
if (EnableAutomaticFailover != null)
119+
{
120+
databaseAccountUpdateParameters.EnableAutomaticFailover = EnableAutomaticFailover;
121+
}
122+
if (DisableKeyBasedMetadataWriteAccess != null)
123+
{
124+
databaseAccountUpdateParameters.DisableKeyBasedMetadataWriteAccess = DisableKeyBasedMetadataWriteAccess;
125+
}
110126

111127
if (!string.IsNullOrEmpty(DefaultConsistencyLevel))
112128
{
@@ -172,20 +188,9 @@ public override void ExecuteCmdlet()
172188
databaseAccountUpdateParameters.VirtualNetworkRules = virtualNetworkRule;
173189
}
174190

175-
if (IpRangeFilter != null && IpRangeFilter.Length > 0)
191+
if (IpRangeFilter != null)
176192
{
177-
string IpRangeFilterAsString = null;
178-
179-
for (int i = 0; i < IpRangeFilter.Length; i++)
180-
{
181-
if(i==0)
182-
{
183-
IpRangeFilterAsString = IpRangeFilter[0];
184-
}
185-
else
186-
IpRangeFilterAsString = string.Concat(IpRangeFilterAsString, ",", IpRangeFilter[i]);
187-
}
188-
193+
string IpRangeFilterAsString = IpRangeFilter?.Aggregate(string.Empty, (output, next) => string.Concat(output, (!string.IsNullOrWhiteSpace(output) && !string.IsNullOrWhiteSpace(next) ? "," : string.Empty), next)) ?? string.Empty;
189194
databaseAccountUpdateParameters.IpRangeFilter = IpRangeFilterAsString;
190195
}
191196

src/CosmosDB/CosmosDB/CosmosDBAccount/UpdateAzCosmosDBAccountRegion.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,9 +76,11 @@ public override void ExecuteCmdlet()
7676
{
7777
if (Location != null && Location.Length > 0)
7878
{
79+
int failoverPriority = 0;
7980
foreach (string location in Location)
8081
{
81-
locations.Add(new Location(location));
82+
locations.Add(new Location(locationName: location, failoverPriority: failoverPriority));
83+
failoverPriority++;
8284
}
8385
}
8486
if (LocationObject != null && LocationObject.Length > 0)

src/CosmosDB/CosmosDB/Helpers/Constants.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,10 @@ internal static class Constants
4545
public const string VirtualNetworkRuleObjectHelpMessage = "Array of PSVirtualNetworkRuleObjects for virtual network.";
4646
public const string VirtualNetworkRuleIdHelpMessage = "Resource ID of a subnet, for example: /subscriptions/{subscriptionId}/resourceGroups/{groupName}/providers/Microsoft.Network/virtualNetworks/{virtualNetworkName}/subnets/{subnetName}";
4747
public const string IgnoreMissingVNetServiceEndpointHelpMessage = "Boolean to indicate if to create firewall rule before the virtual network has vnet service endpoint enabled.";
48-
public const string ApiKindHelpMessage = "The type of Cosmos DB database account to create. Accepted values: GlobalDocumentDB, Sql, MongoDB, Gremlin, Table, Cassandra. Default value: GlobalDocumentDB ";
48+
public const string ApiKindHelpMessage = "The type of Cosmos DB database account to create. Accepted values: Sql, MongoDB, Gremlin, Table, Cassandra. Default value: Sql ";
4949
public const string AccountKeyTypeHelpMessage = "Value from: {ConnectionStrings, Keys, ReadOnlyKeys}. Default is Keys.";
50-
50+
public const string DisableKeyBasedMetadataWriteAccessHelpMessage = "Disable write operations on metadata resources (databases, containers, throughput) via account keys";
51+
5152
//Sql cmdlets help messages
5253
public const string DatabaseNameHelpMessage = "Database name.";
5354
public const string ContainerNameHelpMessage = "Container name.";

src/CosmosDB/CosmosDB/Models/PSDatabaseAccount.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ public PSDatabaseAccount(DatabaseAccountGetResults databaseAccountGetResults)
3131
Location = databaseAccountGetResults.Location;
3232
EnableCassandraConnector = databaseAccountGetResults.EnableCassandraConnector;
3333
FailoverPolicies = databaseAccountGetResults.FailoverPolicies;
34+
Locations = databaseAccountGetResults.Locations;
3435
ReadLocations = databaseAccountGetResults.ReadLocations;
3536
WriteLocations = databaseAccountGetResults.WriteLocations;
3637
Capabilities = databaseAccountGetResults.Capabilities;

src/CosmosDB/CosmosDB/Models/PSIndexingPolicy.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,10 @@ public PSIndexingPolicy()
2525

2626
public PSIndexingPolicy(IndexingPolicy indexingPolicy)
2727
{
28-
Automatic = indexingPolicy.Automatic;
29-
IndexingMode = indexingPolicy.IndexingMode;
28+
Automatic = indexingPolicy?.Automatic;
29+
IndexingMode = indexingPolicy?.IndexingMode;
3030

31-
if (indexingPolicy.IncludedPaths != null)
31+
if (indexingPolicy?.IncludedPaths != null)
3232
{
3333
IncludedPaths = new List<PSIncludedPath>();
3434
foreach (IncludedPath includedPath in indexingPolicy.IncludedPaths)
@@ -37,7 +37,7 @@ public PSIndexingPolicy(IndexingPolicy indexingPolicy)
3737
}
3838
}
3939

40-
if (indexingPolicy.ExcludedPaths != null)
40+
if (indexingPolicy?.ExcludedPaths != null)
4141
{
4242
ExcludedPaths = new List<PSExcludedPath>();
4343
foreach (ExcludedPath excludedPath in indexingPolicy.ExcludedPaths)
@@ -46,7 +46,7 @@ public PSIndexingPolicy(IndexingPolicy indexingPolicy)
4646
}
4747
}
4848

49-
if (indexingPolicy.CompositeIndexes != null)
49+
if (indexingPolicy?.CompositeIndexes != null)
5050
{
5151
CompositeIndexes = new List<IList<PSCompositePath>>();
5252
foreach (IList<CompositePath> compositePathList in indexingPolicy.CompositeIndexes)
@@ -60,7 +60,7 @@ public PSIndexingPolicy(IndexingPolicy indexingPolicy)
6060
}
6161
}
6262

63-
if (indexingPolicy.SpatialIndexes != null)
63+
if (indexingPolicy?.SpatialIndexes != null)
6464
{
6565
SpatialIndexes = new List<PSSpatialSpec>();
6666
foreach (SpatialSpec spatialSpec in indexingPolicy.SpatialIndexes)

src/CosmosDB/CosmosDB/Models/PSUniqueKeyPolicy.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public PSUniqueKeyPolicy()
2626
public PSUniqueKeyPolicy(UniqueKeyPolicy uniqueKey)
2727
{
2828
UniqueKeys = new List<PSUniqueKey>();
29-
if (uniqueKey.UniqueKeys != null)
29+
if (uniqueKey?.UniqueKeys != null)
3030
{
3131
foreach (UniqueKey key in uniqueKey.UniqueKeys)
3232
{

src/CosmosDB/CosmosDB/SQL/GetAzCosmosDBSqlContainer.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ public override void ExecuteCmdlet()
6868
if (Detailed)
6969
{
7070
ThroughputSettingsGetResults throughputSettingsGetResults = CosmosDBManagementClient.SqlResources.GetSqlContainerThroughputWithHttpMessagesAsync(ResourceGroupName, AccountName, DatabaseName, Name).GetAwaiter().GetResult().Body;
71-
WriteObject(throughputSettingsGetResults);
71+
WriteObject(new PSThroughputSettingsGetResults(throughputSettingsGetResults));
7272
}
7373
}
7474
else

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

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ New-AzCosmosDBAccount -ResourceGroupName <String> -Name <String> [-DefaultConsis
1717
[-EnableAutomaticFailover] [-EnableMultipleWriteLocations] [-EnableVirtualNetwork] [-IpRangeFilter <String[]>]
1818
[-Location <String[]>] [-LocationObject <PSLocation[]>] [-MaxStalenessIntervalInSeconds <Int32>]
1919
[-MaxStalenessPrefix <Int32>] [-Tag <Hashtable>] [-VirtualNetworkRule <String[]>]
20-
[-VirtualNetworkRuleObject <PSVirtualNetworkRule[]>] [-ApiKind <String>] [-AsJob]
21-
[-DefaultProfile <IAzureContextContainer>] [-WhatIf] [-Confirm] [<CommonParameters>]
20+
[-VirtualNetworkRuleObject <PSVirtualNetworkRule[]>] [-ApiKind <String>] [-DisableKeyBasedMetadataWriteAccess]
21+
[-AsJob] [-DefaultProfile <IAzureContextContainer>] [-WhatIf] [-Confirm] [<CommonParameters>]
2222
```
2323

2424
## DESCRIPTION
@@ -133,6 +133,21 @@ Accept pipeline input: False
133133
Accept wildcard characters: False
134134
```
135135
136+
### -DisableKeyBasedMetadataWriteAccess
137+
Disable write operations on metadata resources (databases, containers, throughput) via account keys
138+
139+
```yaml
140+
Type: SwitchParameter
141+
Parameter Sets: (All)
142+
Aliases:
143+
144+
Required: False
145+
Position: Named
146+
Default value: None
147+
Accept pipeline input: False
148+
Accept wildcard characters: False
149+
```
150+
136151
### -EnableAutomaticFailover
137152
Enables automatic failover of the write region in the rare event that the region is unavailable due to an outage.
138153
Automatic failover will result in a new write region for the account and is chosen based on the failover priorities configured for the account.

0 commit comments

Comments
 (0)