-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathModifyTableDefinitionVariables.ps1
More file actions
238 lines (191 loc) · 7.93 KB
/
ModifyTableDefinitionVariables.ps1
File metadata and controls
238 lines (191 loc) · 7.93 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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
<#
.SYNOPSIS
Updates variable details across all user-defined table definitions based on a
spec file.
.DESCRIPTION
Reads a JSON spec file describing variable changes, fetches all user-defined
table definitions from the Connect API, applies the specified variable changes
to any matching variables, then posts an update for each table definition that
was modified.
The spec file must have the following structure:
{
"variable_mapping": [
{
"name": "<current variable name to match>",
"new_definition": {
"id": "<new id>",
"name": "<new name>",
"type": "<new type>",
"properties": { ... }
}
}
]
}
The properties to be sent depend on the variable type. Use the GetTableDefintion
script to see examples from your system.
Only fields present (non-null) in new_definition are applied; omitted fields
are left unchanged on the variable.
.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 SpecFile
Path to the JSON file containing the variable mapping specification.
.EXAMPLE
.\UpdateTableDefinitionVariables.ps1 `
-BaseUrl "https://example.com/holidays/OrbitConnectAPI" `
-DataViewName "holidays" `
-AccessToken "your_access_token_here" `
-SpecFile ".\table-definition-updates-spec.json"
#>
[CmdletBinding()]
param (
[Parameter(Mandatory = $true)]
[string]$BaseUrl,
[Parameter(Mandatory = $true)]
[string]$DataViewName,
[Parameter(Mandatory = $true)]
[string]$AccessToken,
[Parameter(Mandatory = $true)]
[string]$SpecFile
)
# ---------------------------------------------------------------------------
# Helper: exit with a clear error message
# ---------------------------------------------------------------------------
function Stop-WithError {
param([string]$Message)
Write-Error $Message
exit 1
}
# ---------------------------------------------------------------------------
# Read and parse the spec file
# ---------------------------------------------------------------------------
if (-not (Test-Path $SpecFile)) {
Stop-WithError "Spec file not found: $SpecFile"
}
$spec = Get-Content $SpecFile -Raw | ConvertFrom-Json
$variableMapping = $spec.variable_mapping
if (-not $variableMapping -or @($variableMapping).Count -eq 0) {
Stop-WithError "No variable_mapping entries found in spec file."
}
$mappingByName = @{}
foreach ($entry in $variableMapping) {
$mappingByName[$entry.name] = $entry.new_definition
}
Write-Host "Loaded $(@($variableMapping).Count) variable mapping(s) from spec file." -ForegroundColor Cyan
$headers = @{
Authorization = "Bearer $AccessToken"
}
# ---------------------------------------------------------------------------
# Fetch all table definition summaries
# ---------------------------------------------------------------------------
$tableDefinitionsUrl = "$BaseUrl/$DataViewName/TableDefinitions"
Write-Host "Fetching all table definitions..." -ForegroundColor Cyan
$pageSize = 100
$offset = 0
$allSummaries = @()
do {
try {
$listResponse = Invoke-RestMethod `
-Uri "${tableDefinitionsUrl}?offset=$offset&count=$pageSize" `
-Method GET `
-ContentType 'application/json' `
-Headers $headers `
-ErrorAction Stop
}
catch {
$statusCode = if ($_.Exception.Response) { $_.Exception.Response.StatusCode.value__ } else { $null }
$errorBody = if ($_.ErrorDetails) { $_.ErrorDetails.Message } else { $null }
Stop-WithError "TableDefinitions GET failed (HTTP $statusCode). Response: $errorBody`nError: $_"
}
$page = @($listResponse.list)
$allSummaries += $page
$offset += $pageSize
}
while ($page.Count -gt 0)
Write-Host "Found $($allSummaries.Count) table definition(s)." -ForegroundColor Cyan
if ($allSummaries.Count -eq 0) {
Write-Host "No table definitions found. Exiting." -ForegroundColor Yellow
exit 0
}
# ---------------------------------------------------------------------------
# Fetch full details for each table definition
# ---------------------------------------------------------------------------
Write-Host "Fetching full details for each table definition..." -ForegroundColor Cyan
$allDetails = @()
foreach ($summary in $allSummaries) {
try {
$detail = Invoke-RestMethod `
-Uri "${tableDefinitionsUrl}/$($summary.id)" `
-Method GET `
-ContentType 'application/json' `
-Headers $headers `
-ErrorAction Stop
}
catch {
$statusCode = if ($_.Exception.Response) { $_.Exception.Response.StatusCode.value__ } else { $null }
$errorBody = if ($_.ErrorDetails) { $_.ErrorDetails.Message } else { $null }
Stop-WithError "TableDefinition GET failed for id $($summary.id) (HTTP $statusCode). Response: $errorBody`nError: $_"
}
$allDetails += $detail
}
# ---------------------------------------------------------------------------
# Apply variable changes and post an update for each modified definition
# ---------------------------------------------------------------------------
$updatedCount = 0
foreach ($detail in $allDetails) {
$variables = if ($detail.schema -and $detail.schema.table) { $detail.schema.table.variables } else { $null }
if (-not $variables -or @($variables).Count -eq 0) {
continue
}
$modified = $false
foreach ($variable in $variables) {
if (-not $mappingByName.ContainsKey($variable.name)) {
continue
}
$newDef = $mappingByName[$variable.name]
Write-Host " Applying mapping to variable '$($variable.name)' in '$($detail.title)' (id: $($detail.id))" -ForegroundColor Gray
if ($null -ne $newDef.id) { $variable.id = $newDef.id }
if ($null -ne $newDef.name) { $variable.name = $newDef.name }
if ($null -ne $newDef.type) { $variable.type = $newDef.type }
if ($null -ne $newDef.properties) { $variable.properties = $newDef.properties }
$modified = $true
}
if (-not $modified) {
continue
}
$updateBody = @{
title = $detail.title
schema = $detail.schema
}
$referenceVariables = @($variables | Where-Object { $_.type -eq "Reference" })
if ($referenceVariables.Count -eq 1) {
$updateBody.hasReferenceVariable = $true
$updateBody.referenceVariableId = $referenceVariables[0].id
} else {
$updateBody.hasReferenceVariable = $false
$updateBody.referenceVariableId = $null
}
$updateJson = ConvertTo-Json -InputObject $updateBody -Depth 100
Write-Host "Posting update for '$($detail.title)' (id: $($detail.id))..." -ForegroundColor Cyan
try {
$updateResponse = Invoke-RestMethod `
-Uri "${tableDefinitionsUrl}/$($detail.id)/Updates" `
-Method POST `
-ContentType 'application/json' `
-Headers $headers `
-Body $updateJson `
-ErrorAction Stop
}
catch {
$statusCode = if ($_.Exception.Response) { $_.Exception.Response.StatusCode.value__ } else { $null }
$errorBody = if ($_.ErrorDetails) { $_.ErrorDetails.Message } else { $null }
Stop-WithError "TableDefinition update POST failed for id $($detail.id) (HTTP $statusCode). Response: $errorBody`nError: $_"
}
Write-Host " Updated successfully (new update id: $($updateResponse.updateId))." -ForegroundColor Green
$updatedCount++
}
Write-Host ""
Write-Host "Done. $updatedCount table definition(s) updated." -ForegroundColor Green