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..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] ] [[-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 @('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 with the Cassandra API. + ## PARAMETERS ### -AllowedOrigin @@ -123,6 +134,28 @@ Accept pipeline input: False Accept wildcard characters: False ``` +### -Capability + +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[] +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..b3a4dd08 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,20 @@ function New-CosmosDbAccount ipRangeFilter = ($IpRangeFilter -join ',') } + if ($PSBoundParameters.ContainsKey('Capability')) + { + $capabilityObject = @() + foreach ($param in $Capability) { + $capabilityObject += @{ + name = $param + } + } + + $cosmosDBProperties += @{ + capabilities = $capabilityObject + } + } + if ($PSBoundParameters.ContainsKey('AllowedOrigin')) { $corsObject = @( @@ -109,6 +128,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..34e6c6db 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', 'EnableCassandra') $script:testCorsAllowedOrigins = @('https://www.contoso.com', 'https://www.fabrikam.com') $script:mockGetAzResource = @{ ResourceId = 'ignore' @@ -76,7 +77,14 @@ InModuleScope $ProjectName { allowedOrigins = ($script:testCorsAllowedOrigins -join ',') } ) - capabilities = @() + capabilities = @( + @{ + name = $script:testCapability[0] + }, + @{ + name = $script:testCapability[1] + } + ) ResourceGroupName = $script:testResourceGroupName ResourceType = 'Microsoft.DocumentDB/databaseAccounts' Sku = $null @@ -709,6 +717,78 @@ InModuleScope $ProjectName { -Exactly -Times 1 } } + + Context 'When called with a Location specified and the EnableServerless and EnableCassandra capabilities' { + $script:result = $null + $testCosmosDBProperties = @{ + databaseAccountOfferType = 'Standard' + locations = @( + @{ + locationName = $script:testLocation + failoverPriority = 0 + } + ) + consistencyPolicy = @{ + defaultConsistencyLevel = 'Session' + maxIntervalInSeconds = 5 + maxStalenessPrefix = 100 + } + ipRangeFilter = '' + capabilities = @( + @{ + name = 'EnableServerless' + }, + @{ + name = 'EnableCassandra' + } + ) + } + + $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 ` + ($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 ` + -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' {