Skip to content

Commit

Permalink
Update to latest CosmosDB client libraray and allow ServerVersion to … (
Browse files Browse the repository at this point in the history
#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]>
  • Loading branch information
craigtmc-msft and kavskalyan authored Feb 23, 2021
1 parent 2941472 commit 8a017cf
Show file tree
Hide file tree
Showing 12 changed files with 599 additions and 386 deletions.
14 changes: 9 additions & 5 deletions src/CosmosDB/CosmosDB.Test/ScenarioTests/AccountTests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,15 @@

function Test-AccountRelatedCmdlets
{
$rgName = "CosmosDBResourceGroup89"
$rgName = "CosmosDBResourceGroup91"
$location = "East US"
$locationlist = "East US", "West US"
$locationlist2 = "East US", "UK South", "UK West", "South India"
$locationlist3 = "West US", "East US"

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

$cosmosDBAccountName = "cosmosdb67"
$cosmosDBAccountName = "cosmosdb678901"

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

$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
$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

Assert-AreEqual $cosmosDBAccountName $cosmosDBAccount.Name
Assert-AreEqual "BoundedStaleness" $cosmosDBAccount.ConsistencyPolicy.DefaultConsistencyLevel
Expand All @@ -49,6 +49,8 @@ function Test-AccountRelatedCmdlets
Assert-AreEqual $cosmosDBAccount.EnableFreeTier 0
Assert-AreEqual $cosmosDBAccount.NetworkAclBypass $NetworkAclBypass
Assert-AreEqual $cosmosDBAccount.NetworkAclBypassResourceIds.Count 0
Assert-AreEqual $cosmosDBAccount.BackupPolicy.BackupIntervalInMinutes 480
Assert-AreEqual $cosmosDBAccount.BackupPolicy.BackupRetentionIntervalInHours 16

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

$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
$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

Assert-AreEqual $cosmosDBExistingAccountName $updatedCosmosDBAccount.Name
Assert-AreEqual $cosmosDBAccountName $updatedCosmosDBAccount.Name
Assert-AreEqual "BoundedStaleness" $updatedCosmosDBAccount.ConsistencyPolicy.DefaultConsistencyLevel
Assert-AreEqual 10 $updatedCosmosDBAccount.ConsistencyPolicy.MaxIntervalInSeconds
Assert-AreEqual 20 $updatedCosmosDBAccount.ConsistencyPolicy.MaxStalenessPrefix
Expand All @@ -69,6 +71,8 @@ function Test-AccountRelatedCmdlets
Assert-AreEqual $updatedCosmosDBAccount.PublicNetworkAccess $publicNetworkAccess
Assert-AreEqual $updatedCosmosDBAccount.NetworkAclBypass $NetworkAclBypass
Assert-AreEqual $updatedCosmosDBAccount.NetworkAclBypassResourceIds.Count 1
Assert-AreEqual $updatedCosmosDBAccount.BackupPolicy.BackupIntervalInMinutes 240
Assert-AreEqual $updatedCosmosDBAccount.BackupPolicy.BackupRetentionIntervalInHours 8

$cosmosDBAccountKey = Get-AzCosmosDBAccountKey -Name $cosmosDBAccountName -ResourceGroupName $rgname
Assert-NotNull $cosmosDBAccountKey
Expand Down

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions src/CosmosDB/CosmosDB/ChangeLog.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@

## Upcoming Release
* Introduced NetworkAclBypass and NetworkAclBypassResourceIds for Database Account cmdlets.
* Introduced ServerVersion parameter to Update-AzCosmosDBAccount.
* Introduced BackupInterval and BackupRetention for Database Account cmdlets

## Version 1.0.0
* General availability of 'Az.CosmosDB' module
Expand Down
16 changes: 12 additions & 4 deletions src/CosmosDB/CosmosDB/CosmosDBAccount/NewAzCosmosDBAccount.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,6 @@ public class NewAzCosmosDBAccount : NewOrUpdateAzCosmosDBAccount
[Parameter(Mandatory = false, HelpMessage = Constants.EnableFreeTierHelpMessage)]
public bool? EnableFreeTier { get; set; }

[Parameter(Mandatory = false, HelpMessage = Constants.ServerVersionHelpMessage)]
[PSArgumentCompleter(SDKModel.ServerVersion.ThreeFullStopTwo, SDKModel.ServerVersion.ThreeFullStopSix)]
public string ServerVersion { get; set; }

[Parameter(Mandatory = false, HelpMessage = Constants.LocationHelpMessage)]
[ValidateNotNullOrEmpty]
public string[] Location { get; set; }
Expand Down Expand Up @@ -215,6 +211,18 @@ public override void ExecuteCmdlet()

databaseAccountCreateUpdateParameters.Kind = ApiKind;

if (BackupIntervalInMinutes.HasValue || BackupRetentionIntervalInHours.HasValue)
{
databaseAccountCreateUpdateParameters.BackupPolicy = new PeriodicModeBackupPolicy()
{
PeriodicModeProperties = new PeriodicModeProperties()
{
BackupIntervalInMinutes = BackupIntervalInMinutes,
BackupRetentionIntervalInHours = BackupRetentionIntervalInHours
}
};
}

if (ShouldProcess(Name, "Creating Database Account"))
{
DatabaseAccountGetResults cosmosDBAccount = CosmosDBManagementClient.DatabaseAccounts.CreateOrUpdateWithHttpMessagesAsync(ResourceGroupName, Name, databaseAccountCreateUpdateParameters).GetAwaiter().GetResult().Body;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,16 @@ public class NewOrUpdateAzCosmosDBAccount : AzureCosmosDBCmdletBase
[ValidateNotNullOrEmpty]
public string[] NetworkAclBypassResourceId { get; set; }

[Parameter(Mandatory = false, HelpMessage = Constants.ServerVersionHelpMessage)]
[PSArgumentCompleter(SDKModel.ServerVersion.ThreeFullStopTwo, SDKModel.ServerVersion.ThreeFullStopSix, SDKModel.ServerVersion.FourFullStopZero)]
public string ServerVersion { get; set; }

[Parameter(Mandatory = false, HelpMessage = Constants.BackupIntervalInMinHelpMessage)]
public int? BackupIntervalInMinutes { get; set; }

[Parameter(Mandatory = false, HelpMessage = Constants.BackupRetentionInHoursHelpMessage)]
public int? BackupRetentionIntervalInHours { get; set; }

public ConsistencyPolicy PopoulateConsistencyPolicy(string DefaultConsistencyLevel, int? MaxStalenessIntervalInSeconds, int? MaxStalenessPrefix)
{
ConsistencyPolicy consistencyPolicy = new ConsistencyPolicy();
Expand Down
30 changes: 30 additions & 0 deletions src/CosmosDB/CosmosDB/CosmosDBAccount/UpdateAzCosmosDBAccount.cs
Original file line number Diff line number Diff line change
Expand Up @@ -140,12 +140,42 @@ public override void ExecuteCmdlet()
databaseAccountUpdateParameters.IpRules = base.PopulateIpRules(IpRule);
}

if (ServerVersion != null)
{
if (databaseAccountUpdateParameters.ApiProperties == null)
{
databaseAccountUpdateParameters.ApiProperties = new ApiProperties();
}

databaseAccountUpdateParameters.ApiProperties.ServerVersion = ServerVersion;
}

if (NetworkAclBypassResourceId != null)
{
Collection<string> networkAclBypassResourceId = new Collection<string>(NetworkAclBypassResourceId);
databaseAccountUpdateParameters.NetworkAclBypassResourceIds = networkAclBypassResourceId;
}

if (BackupIntervalInMinutes.HasValue || BackupRetentionIntervalInHours.HasValue)
{
if (readDatabase.BackupPolicy is PeriodicModeBackupPolicy)
{
databaseAccountUpdateParameters.BackupPolicy = new PeriodicModeBackupPolicy()
{
PeriodicModeProperties = new PeriodicModeProperties()
{
BackupIntervalInMinutes = BackupIntervalInMinutes,
BackupRetentionIntervalInHours = BackupRetentionIntervalInHours
}
};
}
else
{
WriteWarning("Can accept BackupInterval or BackupRetention parameters only for accounts with PeriodicMode backup policy");
return;
}
}

if (ShouldProcess(Name, "Updating Database Account"))
{
DatabaseAccountGetResults cosmosDBAccount = CosmosDBManagementClient.DatabaseAccounts.UpdateWithHttpMessagesAsync(ResourceGroupName, Name, databaseAccountUpdateParameters).GetAwaiter().GetResult().Body;
Expand Down
3 changes: 3 additions & 0 deletions src/CosmosDB/CosmosDB/Helpers/Constants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,9 @@ internal static class Constants
public const string NetworkAclBypassHelpMessage = "Whether or not Network Acl Bypass is enabled for this account for Synapse Link. Possible values include: 'None', 'AzureServices'.";
public const string NetworkAclBypassResourceIdHelpMessage = "List of Resource Ids to allow Network Acl Bypass for Synapse Link.";

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

//Sql cmdlets help messages
public const string DatabaseNameHelpMessage = "Database name.";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public PSApiProperties(ApiProperties apiProperties)
//
// Summary:
// Gets or sets describes the ServerVersion of an a MongoDB account. Possible values
// include: '3.2', '3.6'
// include: '3.2', '3.6', '4.0'
public string ServerVersion { get; set; }
}
}
72 changes: 72 additions & 0 deletions src/CosmosDB/CosmosDB/Models/DatabaseAccount/PSBackupPolicy.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
// ----------------------------------------------------------------------------------
//
// Copyright Microsoft Corporation
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// ----------------------------------------------------------------------------------

using Microsoft.Azure.Commands.CosmosDB.Helpers;
using Microsoft.Azure.Commands.ResourceManager.Common.ArgumentCompleters;
using Microsoft.Azure.Management.CosmosDB.Models;
using System.Management.Automation;

namespace Microsoft.Azure.Commands.CosmosDB.Models
{
public class PSBackupPolicy
{
public static readonly string PeriodicModeBackupType = "Periodic";

public PSBackupPolicy()
{
}

public PSBackupPolicy(BackupPolicy backupPolicy)
{
if (backupPolicy is PeriodicModeBackupPolicy)
{
PeriodicModeBackupPolicy periodicModeBackupPolicy = backupPolicy as PeriodicModeBackupPolicy;
BackupIntervalInMinutes = periodicModeBackupPolicy.PeriodicModeProperties.BackupIntervalInMinutes;
BackupRetentionIntervalInHours = periodicModeBackupPolicy.PeriodicModeProperties.BackupRetentionIntervalInHours;
BackupType = PeriodicModeBackupType;
}
else
{
return;
}
}

public int? BackupIntervalInMinutes { get; set; }

public int? BackupRetentionIntervalInHours { get; set; }

public string BackupType { get; set; }

public BackupPolicy ToSDKModel()
{
if (BackupType.Equals(PSBackupPolicy.PeriodicModeBackupType))
{
PeriodicModeBackupPolicy periodicModeBackupPolicy = new PeriodicModeBackupPolicy
{
PeriodicModeProperties = new PeriodicModeProperties()
{
BackupIntervalInMinutes = BackupIntervalInMinutes,
BackupRetentionIntervalInHours = BackupRetentionIntervalInHours
}
};

return periodicModeBackupPolicy;
}
else
{
return null;
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ public PSDatabaseAccountGetResults(DatabaseAccountGetResults databaseAccountGetR
EnableAnalyticalStorage = databaseAccountGetResults.EnableAnalyticalStorage;
NetworkAclBypass = databaseAccountGetResults.NetworkAclBypass;
NetworkAclBypassResourceIds = databaseAccountGetResults.NetworkAclBypassResourceIds;
BackupPolicy = new PSBackupPolicy(databaseAccountGetResults.BackupPolicy);
}

//
Expand Down Expand Up @@ -190,5 +191,9 @@ public PSDatabaseAccountGetResults(DatabaseAccountGetResults databaseAccountGetR
// Summary:
// Gets or sets list of Network Acl Bypass Resource Ids.
public IList<string> NetworkAclBypassResourceIds { get; set; }
//
// Summary:
// Gets or sets the backup policy of the database account.
public PSBackupPolicy BackupPolicy { get; set; }
}
}
47 changes: 39 additions & 8 deletions src/CosmosDB/CosmosDB/help/New-AzCosmosDBAccount.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,15 @@ Create a new CosmosDB Account.

```
New-AzCosmosDBAccount [-EnableAutomaticFailover] [-EnableMultipleWriteLocations] [-EnableVirtualNetwork]
[-ApiKind <String>] [-DisableKeyBasedMetadataWriteAccess] [-EnableFreeTier <Boolean>]
[-ServerVersion <String>] [-Location <String[]>] [-LocationObject <PSLocation[]>] -ResourceGroupName <String>
-Name <String> [-DefaultConsistencyLevel <String>] [-IpRule <String[]>]
[-MaxStalenessIntervalInSeconds <Int32>] [-MaxStalenessPrefix <Int32>] [-Tag <Hashtable>]
[-VirtualNetworkRule <String[]>] [-VirtualNetworkRuleObject <PSVirtualNetworkRule[]>]
[-PublicNetworkAccess <String>] [-KeyVaultKeyUri <String>] [-EnableAnalyticalStorage <Boolean>] [-AsJob]
[-NetworkAclBypass <String>] [-NetworkAclBypassResourceId <String[]>]
[-DefaultProfile <IAzureContextContainer>] [-WhatIf] [-Confirm] [<CommonParameters>]
[-ApiKind <String>] [-DisableKeyBasedMetadataWriteAccess] [-EnableFreeTier <Boolean>] [-Location <String[]>]
[-LocationObject <PSLocation[]>] -ResourceGroupName <String> -Name <String>
[-DefaultConsistencyLevel <String>] [-IpRule <String[]>] [-MaxStalenessIntervalInSeconds <Int32>]
[-MaxStalenessPrefix <Int32>] [-Tag <Hashtable>] [-VirtualNetworkRule <String[]>]
[-VirtualNetworkRuleObject <PSVirtualNetworkRule[]>] [-PublicNetworkAccess <String>]
[-KeyVaultKeyUri <String>] [-EnableAnalyticalStorage <Boolean>] [-AsJob] [-NetworkAclBypass <String>]
[-NetworkAclBypassResourceId <String[]>] [-ServerVersion <String>] [-BackupIntervalInMinutes <Int32>]
[-BackupRetentionIntervalInHours <Int32>] [-DefaultProfile <IAzureContextContainer>] [-WhatIf] [-Confirm]
[<CommonParameters>]
```

## DESCRIPTION
Expand Down Expand Up @@ -92,6 +93,36 @@ Accept pipeline input: False
Accept wildcard characters: False
```
### -BackupIntervalInMinutes
The interval(in minutes) with which backup are taken (only for accounts with periodic mode backups)
```yaml
Type: Int32
Parameter Sets: (All)
Aliases:

Required: False
Position: Named
Default value: None
Accept pipeline input: False
Accept wildcard characters: False
```
### -BackupRetentionIntervalInHours
The time(in hours) for which each backup is retained (only for accounts with periodic mode backups)
```yaml
Type: Int32
Parameter Sets: (All)
Aliases:

Required: False
Position: Named
Default value: None
Accept pipeline input: False
Accept wildcard characters: False
```
### -Confirm
Prompts you for confirmation before running the cmdlet.
Expand Down
Loading

0 comments on commit 8a017cf

Please sign in to comment.