Skip to content

Commit

Permalink
Added a Capacity param on New-CosmosDbAccount - Fixes #439 (#440)
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisjantzen authored Sep 17, 2021
1 parent 259d9d1 commit f6d628c
Show file tree
Hide file tree
Showing 4 changed files with 136 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
35 changes: 34 additions & 1 deletion docs/New-CosmosDbAccount.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ Create a new Cosmos DB account in Azure.
```powershell
New-CosmosDbAccount [-Name] <String> [-ResourceGroupName] <String> [-Location] <String>
[[-LocationRead] <String[]>] [[-DefaultConsistencyLevel] <String>] [[-MaxIntervalInSeconds] <Int32>]
[[-MaxStalenessPrefix] <Int32>] [[-IpRangeFilter] <String[]>] [[-AllowedOrgin] <String[]>]
[[-MaxStalenessPrefix] <Int32>] [[-IpRangeFilter] <String[]>] [[-Capability] <String[]>] [[-AllowedOrgin] <String[]>]
[-AsJob] [-WhatIf] [-Confirm]
[<CommonParameters>]
```
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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.
Expand Down
20 changes: 20 additions & 0 deletions source/Public/accounts/New-CosmosDbAccount.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,11 @@ function New-CosmosDbAccount
[System.String[]]
$IpRangeFilter = @(),

[Parameter()]
[ValidateSet('EnableCassandra', 'EnableTable', 'EnableGremlin', 'EnableServerless')]
[System.String[]]
$Capability,

[Parameter()]
[ValidateNotNullOrEmpty()]
[System.String[]]
Expand Down Expand Up @@ -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 = @(
Expand All @@ -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 + @{
Expand Down
82 changes: 81 additions & 1 deletion tests/Unit/CosmosDB.accounts.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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' {
Expand Down

0 comments on commit f6d628c

Please sign in to comment.