Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added a Capacity param on New-CosmosDbAccount - Fixes #439 #440

Merged
merged 4 commits into from
Sep 17, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
31 changes: 30 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 '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
Expand Down Expand Up @@ -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.
Expand Down
19 changes: 19 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,19 @@ function New-CosmosDbAccount
ipRangeFilter = ($IpRangeFilter -join ',')
}

if ($PSBoundParameters.ContainsKey('Capability'))
{
$capabilityObject = @(
@{
name = $Capability
}
)

$cosmosDBProperties += @{
capabilities = $capabilityObject
}
}

if ($PSBoundParameters.ContainsKey('AllowedOrigin'))
{
$corsObject = @(
Expand All @@ -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 + @{
Expand Down
64 changes: 63 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'
$script:testCorsAllowedOrigins = @('https://www.contoso.com', 'https://www.fabrikam.com')
$script:mockGetAzResource = @{
ResourceId = 'ignore'
Expand Down Expand Up @@ -76,7 +77,7 @@ InModuleScope $ProjectName {
allowedOrigins = ($script:testCorsAllowedOrigins -join ',')
}
)
capabilities = @()
capabilities = @(@{name = $script:testCapability})
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this line might need to be:

capabilities = @(
  @{
    name = $script:testCapability[0]
  },
  @{
    name = $script:testCapability[1]
  }
)

ResourceGroupName = $script:testResourceGroupName
ResourceType = 'Microsoft.DocumentDB/databaseAccounts'
Sku = $null
Expand Down Expand Up @@ -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)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the tests are failing here on PS 6/7 because the output of ConvertTo-Json is non-determinate. I've run into this before - essentially the properties will not be output in the same order. So you'd need to actually compare object properties (which is a pain).

}

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