-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUpdateTableDefinition.ps1
More file actions
93 lines (74 loc) · 3.24 KB
/
UpdateTableDefinition.ps1
File metadata and controls
93 lines (74 loc) · 3.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
<#
.SYNOPSIS
Posts a JSON file to update an existing user-defined table definition in the Apteco Connect API.
.DESCRIPTION
Uses a valid access token to POST a JSON file to /{dataViewName}/TableDefinitions/{tableDefinitionId}/Updates.
.PARAMETER BaseUrl
The base URL of the Connect API, e.g. https://example.com/holidays/OrbitConnectAPI
.PARAMETER DataViewName
The name of the DataView to act on, e.g. "holidays"
.PARAMETER AccessToken
A valid access token (gained from the Login.ps1 script) to authenticate with.
.PARAMETER TableDefinitionId
The ID of the user-defined table definition to update.
.PARAMETER TableDefinitionJsonPath
The path to the JSON file to post to the TableDefinitions endpoint.
.EXAMPLE
.\UpdateTableDefinition.ps1 `
-BaseUrl "https://example.com/holidays/OrbitConnectAPI" `
-DataViewName "holidays" `
-AccessToken "your_access_token_here" `
-TableDefinitionId 12345 `
-TableDefinitionJsonPath ".\tableDefinition.json"
#>
[CmdletBinding()]
param (
[Parameter(Mandatory = $true)]
[string]$BaseUrl,
[Parameter(Mandatory = $true)]
[string]$DataViewName,
[Parameter(Mandatory = $true)]
[string]$AccessToken,
[Parameter(Mandatory = $true)]
[int]$TableDefinitionId,
[Parameter(Mandatory = $true)]
[string]$TableDefinitionJsonPath
)
# ---------------------------------------------------------------------------
# Helper: exit with a clear error message
# ---------------------------------------------------------------------------
function Stop-WithError {
param([string]$Message)
Write-Error $Message
exit 1
}
# ---------------------------------------------------------------------------
# Validate the table definition JSON file exists
# ---------------------------------------------------------------------------
if (-not (Test-Path -Path $TableDefinitionJsonPath -PathType Leaf)) {
Stop-WithError "Table definition JSON file not found: $TableDefinitionJsonPath"
}
# ---------------------------------------------------------------------------
# Post the table definition JSON
# ---------------------------------------------------------------------------
$tableDefinitionUrl = "$BaseUrl/$DataViewName/TableDefinitions/$TableDefinitionId/Updates"
$tableDefinitionJson = Get-Content -Path $TableDefinitionJsonPath -Raw
Write-Host "Posting table definition JSON from '$TableDefinitionJsonPath' to '$tableDefinitionUrl'..." -ForegroundColor Cyan
$headers = @{
Authorization = "Bearer $AccessToken"
}
try {
$tableDefinitionResponse = Invoke-RestMethod `
-Uri $tableDefinitionUrl `
-Method POST `
-ContentType 'application/json' `
-Headers $headers `
-Body $tableDefinitionJson `
-ErrorAction Stop
}
catch {
$statusCode = if ($_.Exception.Response) { $_.Exception.Response.StatusCode.value__ } else { $null }
$errorBody = if ($_.ErrorDetails) { $_.ErrorDetails.Message } else { $null }
Stop-WithError "TableDefinitions POST failed (HTTP $statusCode). Response: $errorBody`nError: $_"
}
Write-Host "Table definition $($tableDefinitionResponse.title) (Update ID: $($tableDefinitionResponse.updateId)) updated successfully!" -ForegroundColor Green