#Requires -Version 5.1 <# .SYNOPSIS RasterizedDB Enterprise: add a customer to the per-user-shard cluster. The gateway's configuration API creates the customer's database and login role on the chosen shard and pins the login there. Needs release 1.1.0 or later. .DESCRIPTION The login role's password comes from $env:RDB_CUSTOMER_PASSWORD, never from the command line. .EXAMPLE $env:RDB_CUSTOMER_PASSWORD = 'acme-secret-123' .\add-customer.ps1 -Customer acme -Shard 1 #> [CmdletBinding()] param( # Holds api.token. [string]$Work = $(if ($env:RDB_WORK) { $env:RDB_WORK } else { '.\per-user-cluster' }), [string]$Api = $(if ($env:RDB_API) { $env:RDB_API } else { 'http://127.0.0.1:6480' }), [int]$GatewayPort = $(if ($env:RDB_GATEWAY_PORT) { [int]$env:RDB_GATEWAY_PORT } else { 6432 }), # The customer's database. [Parameter(Mandatory = $true)][string]$Customer, # Its login role; _app by default. [string]$Role = '', # The shard id it lives on. [Parameter(Mandatory = $true)][int]$Shard ) $ErrorActionPreference = 'Stop' $tokenFile = Join-Path $Work 'api.token' if (-not (Test-Path -LiteralPath $tokenFile)) { throw "No $tokenFile; run setup.ps1 first, or pass -Work." } if (-not $env:RDB_CUSTOMER_PASSWORD) { throw 'Set $env:RDB_CUSTOMER_PASSWORD to the password for the login role.' } if (-not $Role) { $Role = "${Customer}_app" } foreach ($v in @($Customer, $Role)) { if ($v -notmatch '^[A-Za-z][A-Za-z0-9_]{0,62}$') { throw "${v}: start with a letter, then letters, digits and _ (63 at most)." } } $headers = @{ Authorization = 'Bearer ' + (Get-Content -LiteralPath $tokenFile -Raw).Trim() } $body = @{ database = $Customer; role = $Role; password = $env:RDB_CUSTOMER_PASSWORD; shard = $Shard } | ConvertTo-Json -Compress $made = Invoke-RestMethod -Method Post -Uri "$Api/v1/customers" -Headers $headers -ContentType 'application/json' -Body $body $made | ConvertTo-Json -Compress | Write-Host (Invoke-RestMethod -Method Get -Uri "$Api/v1/routes" -Headers $headers) | ConvertTo-Json -Depth 4 | Write-Host Write-Host '' Write-Host "The customer connects through the gateway, and lands on shard ${Shard}:" Write-Host " psql `"postgresql://$Role@127.0.0.1:$GatewayPort/${Customer}?sslmode=disable`""