From 648d2b61dc9b6acfa36dfaad0f595d3517253a78 Mon Sep 17 00:00:00 2001 From: Chris Jantzen Date: Mon, 13 Sep 2021 17:17:02 -0700 Subject: [PATCH 1/4] Added Implemented a Capacity param on New-CosmosDbAccount --- CHANGELOG.md | 1 + docs/New-CosmosDbAccount.md | 31 ++++++++- .../Public/accounts/New-CosmosDbAccount.ps1 | 19 ++++++ tests/Unit/CosmosDB.accounts.Tests.ps1 | 64 ++++++++++++++++++- 4 files changed, 113 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 919a0ffd..a7bbd830 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -13,6 +13,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 to be `ubuntu-latest` - Fixes [Issue #422](https://github.com/PlagueHO/CosmosDB/issues/422). - Updated PSScriptAnalyzer tests to be skipped when PowerShell Core version is less than 7.0.3 - Fixes [Issue #431](https://github.com/PlagueHO/CosmosDB/issues/431). +- Updated the New-CosmosDbAccount command to add a new Capability parameter - Fixes [Issue #439](https://github.com/PlagueHO/CosmosDB/issues/439). ### Added diff --git a/docs/New-CosmosDbAccount.md b/docs/New-CosmosDbAccount.md index ccc77195..2bab3ac2 100644 --- a/docs/New-CosmosDbAccount.md +++ b/docs/New-CosmosDbAccount.md @@ -16,7 +16,7 @@ Create a new Cosmos DB account in Azure. ```powershell New-CosmosDbAccount [-Name] [-ResourceGroupName] [-Location] [[-LocationRead] ] [[-DefaultConsistencyLevel] ] [[-MaxIntervalInSeconds] ] - [[-MaxStalenessPrefix] ] [[-IpRangeFilter] ] [[-AllowedOrgin] ] + [[-MaxStalenessPrefix] ] [[-IpRangeFilter] ] [[-Capability] ] [[-AllowedOrgin] ] [-AsJob] [-WhatIf] [-Confirm] [] ``` @@ -86,6 +86,17 @@ Resource Group caled 'MyData'. The account will be created in the 'WestUS' Azure region. The Cosmos DB will have the CORS allowed origins set to 'https://www.contoso.com' and 'https://www.fabrikam.com'. +### Example 6 + +```powershell +PS C:\> New-CosmosDbAccount -Name 'MyCosmosDB' -ResourceGroup 'MyData' -Location 'WestUS' -Capability 'EnableServerless' +``` + +Create a new Cosmos DB account called 'MyCosmosDB' in an existing +Resource Group caled 'MyData'. The account will be created in the +'WestUS' Azure region. The Cosmos DB will be provisioned in the +Serverless capacity mode. + ## PARAMETERS ### -AllowedOrigin @@ -123,6 +134,24 @@ Accept pipeline input: False Accept wildcard characters: False ``` +### -Capability + +The capability of the database account. +For more information see https://docs.microsoft.com/en-us/azure/templates/microsoft.documentdb/databaseaccounts?tabs=json#capability. + +```yaml +Type: String +Parameter Sets: (All) +Aliases: +Accepted values: EnableCassandra, EnableTable, EnableGremlin, EnableServerless + +Required: False +Position: Named +Default value: None +Accept pipeline input: False +Accept wildcard characters: False +``` + ### -Confirm Prompts you for confirmation before running the cmdlet. diff --git a/source/Public/accounts/New-CosmosDbAccount.ps1 b/source/Public/accounts/New-CosmosDbAccount.ps1 index 311b5cbd..d09844df 100644 --- a/source/Public/accounts/New-CosmosDbAccount.ps1 +++ b/source/Public/accounts/New-CosmosDbAccount.ps1 @@ -47,6 +47,11 @@ function New-CosmosDbAccount [System.String[]] $IpRangeFilter = @(), + [Parameter()] + [ValidateSet('EnableCassandra', 'EnableTable', 'EnableGremlin', 'EnableServerless')] + [System.String] + $Capability, + [Parameter()] [ValidateNotNullOrEmpty()] [System.String[]] @@ -91,6 +96,19 @@ function New-CosmosDbAccount ipRangeFilter = ($IpRangeFilter -join ',') } + if ($PSBoundParameters.ContainsKey('Capability')) + { + $capabilityObject = @( + @{ + name = $Capability + } + ) + + $cosmosDBProperties += @{ + capabilities = $capabilityObject + } + } + if ($PSBoundParameters.ContainsKey('AllowedOrigin')) { $corsObject = @( @@ -109,6 +127,7 @@ function New-CosmosDbAccount $null = $PSBoundParameters.Remove('MaxIntervalInSeconds') $null = $PSBoundParameters.Remove('MaxStalenessPrefix') $null = $PSBoundParameters.Remove('IpRangeFilter') + $null = $PSBoundParameters.Remove('Capability') $null = $PSBoundParameters.Remove('AllowedOrigin') $newAzResource_parameters = $PSBoundParameters + @{ diff --git a/tests/Unit/CosmosDB.accounts.Tests.ps1 b/tests/Unit/CosmosDB.accounts.Tests.ps1 index 696ab395..421c43f9 100644 --- a/tests/Unit/CosmosDB.accounts.Tests.ps1 +++ b/tests/Unit/CosmosDB.accounts.Tests.ps1 @@ -24,6 +24,7 @@ InModuleScope $ProjectName { $script:testConsistencyLevel = 'Strong' $script:testMaxIntervalInSeconds = 60 $script:testMaxStalenessPrefix = 900 + $script:testCapability = 'EnableServerless' $script:testCorsAllowedOrigins = @('https://www.contoso.com', 'https://www.fabrikam.com') $script:mockGetAzResource = @{ ResourceId = 'ignore' @@ -76,7 +77,7 @@ InModuleScope $ProjectName { allowedOrigins = ($script:testCorsAllowedOrigins -join ',') } ) - capabilities = @() + capabilities = @(@{name = $script:testCapability}) ResourceGroupName = $script:testResourceGroupName ResourceType = 'Microsoft.DocumentDB/databaseAccounts' Sku = $null @@ -709,6 +710,67 @@ InModuleScope $ProjectName { -Exactly -Times 1 } } + + Context 'When called with a Location specified and the EnableServerless capability' { + $script:result = $null + $testCosmosDBProperties = @{ + databaseAccountOfferType = 'Standard' + locations = @( + @{ + locationName = $script:testLocation + failoverPriority = 0 + } + ) + consistencyPolicy = @{ + defaultConsistencyLevel = 'Session' + maxIntervalInSeconds = 5 + maxStalenessPrefix = 100 + } + ipRangeFilter = '' + capabilities = @( + @{ + name = 'EnableServerless' + } + ) + } + + $newAzResource_parameterFilter = { + ($ResourceType -eq 'Microsoft.DocumentDb/databaseAccounts') -and ` + ($ApiVersion -eq '2015-04-08') -and ` + ($ResourceName -eq $script:testName) -and ` + ($ResourceGroupName -eq $script:testResourceGroupName) -and ` + ($Location -eq $script:testLocation) -and ` + ($Force -eq $true) -and ` + (ConvertTo-Json -InputObject $Properties) -eq (ConvertTo-Json -InputObject $testCosmosDBProperties) + } + + Mock ` + -CommandName New-AzResource ` + -MockWith { 'Account' } + + It 'Should not throw exception' { + $newCosmosDbAccountParameters = @{ + Name = $script:testName + ResourceGroupName = $script:testResourceGroupName + Location = $script:testLocation + Capability = $script:testCapability + Verbose = $true + } + + { $script:result = New-CosmosDbAccount @newCosmosDbAccountParameters } | Should -Not -Throw + } + + It 'Should return expected result' { + $script:result | Should -Be 'Account' + } + + It 'Should call expected mocks' { + Assert-MockCalled ` + -CommandName New-AzResource ` + -ParameterFilter $newAzResource_parameterFilter ` + -Exactly -Times 1 + } + } } Describe 'Set-CosmosDbAccount' -Tag 'Unit' { From 879174a437e1b7e6a472c0a1fd943154e378c41f Mon Sep 17 00:00:00 2001 From: Chris Jantzen Date: Tue, 14 Sep 2021 15:22:43 -0700 Subject: [PATCH 2/4] Updating new Capability param to allow an array of capabilities. --- docs/New-CosmosDbAccount.md | 12 ++++++++---- source/Public/accounts/New-CosmosDbAccount.ps1 | 11 ++++++----- tests/Unit/CosmosDB.accounts.Tests.ps1 | 7 +++++-- 3 files changed, 19 insertions(+), 11 deletions(-) diff --git a/docs/New-CosmosDbAccount.md b/docs/New-CosmosDbAccount.md index 2bab3ac2..6d271a41 100644 --- a/docs/New-CosmosDbAccount.md +++ b/docs/New-CosmosDbAccount.md @@ -89,13 +89,13 @@ origins set to 'https://www.contoso.com' and 'https://www.fabrikam.com'. ### Example 6 ```powershell -PS C:\> New-CosmosDbAccount -Name 'MyCosmosDB' -ResourceGroup 'MyData' -Location 'WestUS' -Capability 'EnableServerless' +PS C:\> New-CosmosDbAccount -Name 'MyCosmosDB' -ResourceGroup 'MyData' -Location 'WestUS' -Capability @('EnableCassandra', 'EnableServerless') ``` Create a new Cosmos DB account called 'MyCosmosDB' in an existing Resource Group caled 'MyData'. The account will be created in the 'WestUS' Azure region. The Cosmos DB will be provisioned in the -Serverless capacity mode. +Serverless capacity mode with the Cassandra API. ## PARAMETERS @@ -136,11 +136,15 @@ Accept wildcard characters: False ### -Capability -The capability of the database account. +The capabilities of the database account. Only one API type should +be used. For more information see https://docs.microsoft.com/en-us/azure/templates/microsoft.documentdb/databaseaccounts?tabs=json#capability. +If one of the API type's is not specified, the Core (SQL) API type +will be used. + ```yaml -Type: String +Type: String[] Parameter Sets: (All) Aliases: Accepted values: EnableCassandra, EnableTable, EnableGremlin, EnableServerless diff --git a/source/Public/accounts/New-CosmosDbAccount.ps1 b/source/Public/accounts/New-CosmosDbAccount.ps1 index d09844df..b3a4dd08 100644 --- a/source/Public/accounts/New-CosmosDbAccount.ps1 +++ b/source/Public/accounts/New-CosmosDbAccount.ps1 @@ -49,7 +49,7 @@ function New-CosmosDbAccount [Parameter()] [ValidateSet('EnableCassandra', 'EnableTable', 'EnableGremlin', 'EnableServerless')] - [System.String] + [System.String[]] $Capability, [Parameter()] @@ -98,11 +98,12 @@ function New-CosmosDbAccount if ($PSBoundParameters.ContainsKey('Capability')) { - $capabilityObject = @( - @{ - name = $Capability + $capabilityObject = @() + foreach ($param in $Capability) { + $capabilityObject += @{ + name = $param } - ) + } $cosmosDBProperties += @{ capabilities = $capabilityObject diff --git a/tests/Unit/CosmosDB.accounts.Tests.ps1 b/tests/Unit/CosmosDB.accounts.Tests.ps1 index 421c43f9..929cb6ba 100644 --- a/tests/Unit/CosmosDB.accounts.Tests.ps1 +++ b/tests/Unit/CosmosDB.accounts.Tests.ps1 @@ -24,7 +24,7 @@ InModuleScope $ProjectName { $script:testConsistencyLevel = 'Strong' $script:testMaxIntervalInSeconds = 60 $script:testMaxStalenessPrefix = 900 - $script:testCapability = 'EnableServerless' + $script:testCapability = @('EnableServerless', 'EnableCassandra') $script:testCorsAllowedOrigins = @('https://www.contoso.com', 'https://www.fabrikam.com') $script:mockGetAzResource = @{ ResourceId = 'ignore' @@ -711,7 +711,7 @@ InModuleScope $ProjectName { } } - Context 'When called with a Location specified and the EnableServerless capability' { + Context 'When called with a Location specified and the EnableServerless and EnableCassandra capabilities' { $script:result = $null $testCosmosDBProperties = @{ databaseAccountOfferType = 'Standard' @@ -730,6 +730,9 @@ InModuleScope $ProjectName { capabilities = @( @{ name = 'EnableServerless' + }, + @{ + name = 'EnableCassandra' } ) } From e8ad3400742f7be2553a0c88f83bff707d712185 Mon Sep 17 00:00:00 2001 From: Chris Jantzen Date: Thu, 16 Sep 2021 09:10:11 -0700 Subject: [PATCH 3/4] Fixing documentation on the Capability param --- docs/New-CosmosDbAccount.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/New-CosmosDbAccount.md b/docs/New-CosmosDbAccount.md index 6d271a41..de9d9148 100644 --- a/docs/New-CosmosDbAccount.md +++ b/docs/New-CosmosDbAccount.md @@ -16,7 +16,7 @@ Create a new Cosmos DB account in Azure. ```powershell New-CosmosDbAccount [-Name] [-ResourceGroupName] [-Location] [[-LocationRead] ] [[-DefaultConsistencyLevel] ] [[-MaxIntervalInSeconds] ] - [[-MaxStalenessPrefix] ] [[-IpRangeFilter] ] [[-Capability] ] [[-AllowedOrgin] ] + [[-MaxStalenessPrefix] ] [[-IpRangeFilter] ] [[-Capability] ] [[-AllowedOrgin] ] [-AsJob] [-WhatIf] [-Confirm] [] ``` From 2c6bf241ef6d8fbf4064ffb642a04b1996a95741 Mon Sep 17 00:00:00 2001 From: Chris Jantzen Date: Thu, 16 Sep 2021 09:44:41 -0700 Subject: [PATCH 4/4] Fixed Capability param test order issues. --- tests/Unit/CosmosDB.accounts.Tests.ps1 | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/tests/Unit/CosmosDB.accounts.Tests.ps1 b/tests/Unit/CosmosDB.accounts.Tests.ps1 index 929cb6ba..34e6c6db 100644 --- a/tests/Unit/CosmosDB.accounts.Tests.ps1 +++ b/tests/Unit/CosmosDB.accounts.Tests.ps1 @@ -77,7 +77,14 @@ InModuleScope $ProjectName { allowedOrigins = ($script:testCorsAllowedOrigins -join ',') } ) - capabilities = @(@{name = $script:testCapability}) + capabilities = @( + @{ + name = $script:testCapability[0] + }, + @{ + name = $script:testCapability[1] + } + ) ResourceGroupName = $script:testResourceGroupName ResourceType = 'Microsoft.DocumentDB/databaseAccounts' Sku = $null @@ -744,7 +751,15 @@ InModuleScope $ProjectName { ($ResourceGroupName -eq $script:testResourceGroupName) -and ` ($Location -eq $script:testLocation) -and ` ($Force -eq $true) -and ` - (ConvertTo-Json -InputObject $Properties) -eq (ConvertTo-Json -InputObject $testCosmosDBProperties) + ($Properties.databaseAccountOfferType -eq $testCosmosDBProperties.databaseAccountOfferType) -and ` + ($Properties.locations[0].locationName -eq $testCosmosDBProperties.locations[0].locationName) -and ` + ($Properties.locations[0].failoverPriority -eq $testCosmosDBProperties.locations[0].failoverPriority) -and ` + ($Properties.consistencyPolicy.defaultConsistencyLevel -eq $testCosmosDBProperties.consistencyPolicy.defaultConsistencyLevel) -and ` + ($Properties.consistencyPolicy.maxStalenessPrefix -eq $testCosmosDBProperties.consistencyPolicy.maxStalenessPrefix) -and ` + ($Properties.consistencyPolicy.maxIntervalInSeconds -eq $testCosmosDBProperties.consistencyPolicy.maxIntervalInSeconds) -and ` + ($Properties.ipRangeFilter -eq $testCosmosDBProperties.ipRangeFilter) -and ` + ($Properties.capabilities[0].name -eq $testCosmosDBProperties.capabilities[0].name) -and ` + ($Properties.capabilities[1].name -eq $testCosmosDBProperties.capabilities[1].name) } Mock `