Last active
February 4, 2021 01:58
-
-
Save alexdrenea/29db10d2c6d3f2ae71ac6c17d7a3ecb3 to your computer and use it in GitHub Desktop.
Resize script for an Azure CosmosDB account of any type.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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