Trackyon.Ber.functions.ps1
# Makes sure we don't have to many / in the paths. function _joinParts { param ( [string[]] $parts = $null, [string] $separator = '/' ) ($parts | Where-Object { $_ } | ForEach-Object { ([string]$_).trim($separator) } | Where-Object { $_ } ) -join $separator } # Get the JWT token when hosting in a web site. This is not used when calling an Azure Function. function _auth { [CmdletBinding()] param ( [Parameter(Mandatory = $True)] [string] $berUrl, [Parameter(Mandatory = $True)] [string] $clientId ) # Get the JWT token $authUrl = _joinParts $berUrl, "/api/auth/token/" $body = @{ UserAgent = $clientId } $json = ConvertTo-Json $body -Compress try { $response = Invoke-RestMethod -Uri $authUrl -Method Post -Body $json -ContentType 'application/json' } catch [Exception] { throw "Error attempting to authenticate $($_.Exception.Message)" } Write-Output $response.token } Set-StrictMode -Version Latest function Add-BerRecord { [CmdletBinding(DefaultParameterSetName = 'ByFunctionKey')] param( [Parameter(Mandatory = $True)] [string] $berUrl, [Parameter(ParameterSetName = 'ByJWT', Mandatory = $True)] [string] $clientId, [Parameter(ParameterSetName = 'ByFunctionKey', Mandatory = $True)] [string] $functionKey, [Parameter(Mandatory = $True)] [string] $appName, [Parameter(Mandatory = $True)] [string] $dataType, [Parameter(Mandatory = $True)] [string] $version, [Parameter(Mandatory = $True)] [string] $value ) Process { # Update the BER server $recordsUrl = _joinParts $berUrl, "/api/records/" $body = @{ ApplicationName = $appName DataType = $dataType Version = $version Value = $value } $json = ConvertTo-Json $body -Compress if ($clientId) { $token = _auth -berUrl $berUrl -clientId $clientId -ErrorAction Stop $response = Invoke-RestMethod -Uri $recordsUrl -Method Post -Body $json -ContentType 'application/json' -Headers @{Authorization = "Bearer $token"} } else { $recordsUrl = _joinParts $recordsUrl, "?code=$functionKey" $response = Invoke-RestMethod -Uri $recordsUrl -Method Post -Body $json -ContentType 'application/json' } Write-Output $response } } Set-StrictMode -Version Latest function Get-BerRecord { [CmdletBinding(DefaultParameterSetName = 'ListFunction')] param( [Parameter(Mandatory = $True)] [string] $berUrl, [Parameter(ParameterSetName = 'List', Mandatory = $True)] [Parameter(ParameterSetName = 'ByRecord', Mandatory = $True)] [string] $clientId, [Parameter(ParameterSetName = 'ListFunction', Mandatory = $True)] [Parameter(ParameterSetName = 'ByRecordFunction', Mandatory = $True)] [string] $functionKey, [Parameter(ParameterSetName = 'ByRecord', Mandatory = $True)] [Parameter(ParameterSetName = 'ByRecordFunction', Mandatory = $True)] [string] $appName, [Parameter(ParameterSetName = 'ByRecord', Mandatory = $True)] [Parameter(ParameterSetName = 'ByRecordFunction', Mandatory = $True)] [string] $dataType, [Parameter(ParameterSetName = 'ByRecord', Mandatory = $True)] [Parameter(ParameterSetName = 'ByRecordFunction', Mandatory = $True)] [string] $version ) Process { if ($PSCmdlet.ParameterSetName -eq 'ByRecord' -or $PSCmdlet.ParameterSetName -eq 'ByRecordFunction') { $recordsUrl = _joinParts $berUrl, "api/records/$appName/$dataType/$version" } else { $recordsUrl = _joinParts $berUrl, "api/records" } if ($clientId) { $token = _auth -berUrl $berUrl -clientId $clientId -ErrorAction Stop $response = Invoke-RestMethod -Uri $recordsUrl -Method Get -Headers @{Authorization = "Bearer $token"} } else { $recordsUrl = _joinParts $recordsUrl, "?code=$functionKey" $response = Invoke-RestMethod -Uri $recordsUrl -Method Get } Write-Output $response } } Set-StrictMode -Version Latest function Remove-BerRecord { [CmdletBinding(DefaultParameterSetName = 'ByFunctionKey', SupportsShouldProcess = $true, ConfirmImpact = "Medium")] param( [Parameter(Mandatory = $True)] [string] $berUrl, [Parameter(ParameterSetName = 'ByJWT', Mandatory = $True)] [string] $clientId, [Parameter(ParameterSetName = 'ByFunctionKey', Mandatory = $True)] [string] $functionKey, [Parameter(Mandatory = $True)] [string] $appName, [Parameter(Mandatory = $True)] [string] $dataType, [Parameter(Mandatory = $True)] [string] $version, [switch] $force ) Process { if ($Force -or $pscmdlet.ShouldProcess("$appName/$dataType/$version", "Remove Record")) { $recordsUrl = _joinParts $berUrl, "/api/records/$appName/$dataType/$version" if ($clientId) { $token = _auth -berUrl $berUrl -clientId $clientId -ErrorAction Stop Invoke-RestMethod -Uri $recordsUrl -Method Delete -Headers @{Authorization = "Bearer $token"} } else { $recordsUrl = _joinParts $recordsUrl, "?code=$functionKey" Invoke-RestMethod -Uri $recordsUrl -Method Delete } } } } |