Skip to content

Instantly share code, notes, and snippets.

@alexdrenea
Last active February 4, 2021 01:58
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alexdrenea/29db10d2c6d3f2ae71ac6c17d7a3ecb3 to your computer and use it in GitHub Desktop.
Save alexdrenea/29db10d2c6d3f2ae71ac6c17d7a3ecb3 to your computer and use it in GitHub Desktop.
Resize script for an Azure CosmosDB account of any type.
param(
[Parameter(Mandatory = $true)]
[string] $ResourceGroupName,
[Parameter(Mandatory = $true)]
[string] $AccountName,
[Parameter(Mandatory = $true)]
[string] $DatabaseName,
[Parameter(Mandatory = $true)]
[string] $ContainerName,
[Parameter(Mandatory = $true, HelpMessage="Values between 400 and 1000000 inclusive in increments of 100")]
[int] $Throughput
)
if ( $Throughput -lt 400 -or $Throughput -gt 1000000 -or ($Throughput % 100) -ne 0 ){
$message = "Invalid Throughput $Throughput. Values between 400 and 1000000 inclusive in increments of 100"
Write-Error -Message $message
return
}
$ErrorActionPreference = "Continue"
$p = Get-AzResource `
-ResourceType "Microsoft.DocumentDb/databaseAccounts" -ApiVersion "2016-03-31" `
-ResourceGroupName $ResourceGroupName -Name $accountName `
| Select-Object -expand Properties
$accountType = $p.EnabledApiTypes
$resourceType = ''
$resourceName = ''
if ($accountType -match 'Table'){
$resourceType = 'Microsoft.DocumentDb/databaseAccounts/apis/tables/settings'
$resourceName = $accountName + "/table/" + $ContainerName + "/throughput"
}
elseif ($accountType -match 'MongoDB'){
$resourceType = 'Microsoft.DocumentDb/databaseAccounts/apis/databases/collections/settings';
$resourceName = $accountName + "/mongodb/" + $DatabaseName + "/" + $ContainerName + "/throughput"
}
elseif ($accountType -match 'Gremlin'){
$resourceType = 'Microsoft.DocumentDb/databaseAccounts/apis/databases/graphs/settings';
$resourceName = $accountName + "/gremlin/" + $DatabaseName + "/" + $ContainerName + "/throughput"
}
elseif ($accountType -match 'Cassandra'){
$resourceType = 'Microsoft.DocumentDB/databaseAccounts/apis/keyspaces/tables/settings';
$resourceName = $accountName + "/cassandra/" + $DatabaseName + "/" + $ContainerName + "/throughput"
}
elseif ($accountType -match 'Sql'){
$resourceType = 'Microsoft.DocumentDb/databaseAccounts/apis/databases/containers/settings';
$resourceName = $accountName + "/sql/" + $DatabaseName + "/" + $ContainerName + "/throughput"
}
else{
$message = "Unsupported CosmosDB account type '$accountType'. Supported APIs are: SQL, Gremlin, MongoDB, Table and Cassandra."
Write-Error -Message $message
return
}
$properties = @{
"resource"=@{"throughput"=$throughput}
}
try {
Set-AzResource `
-ResourceType $resourceType -ApiVersion "2016-03-31" `
-ResourceGroupName $ResourceGroupName -Name $resourceName `
-PropertyObject $properties `
-Force
}
catch {
Write-Error -Message $_.Exception
throw $_.Exception
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment