-
Notifications
You must be signed in to change notification settings - Fork 2
/
BusinessObjectsUpload.ps1
1106 lines (938 loc) · 41.5 KB
/
BusinessObjectsUpload.ps1
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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# Script start parameter
param (
[Parameter(Mandatory = $False)] $ConfigPath
)
function ExitHelper {
if ($Configuration.execByScheduler) {
# Exit without prompt to prevent scheduler from hanging
exit -1
}
# Started via explorer right click
Read-Host -Prompt "Press Enter to exit"
exit -1
}
#-------------------------------------------
#----------- Global Variables --------------
#-------------------------------------------
#
# Version of this template.
# Version is part of the HTTP Header-Attributes "User-Agent" and is sent with every request.
[String] $version = "1.2.1"
# Script Config
$Configuration = New-Object -TypeName ScriptConfiguration
[STRING] $scriptConfigFilePath = "$PSScriptRoot\ScriptConfig.json" # Default value
[STRING] $configBasePath = $PSScriptRoot # Default value
if ($ConfigPath) {
$scriptConfigFilePath = $ConfigPath
$configBasePath = Split-Path -Path $scriptConfigFilePath
}
if (!(Test-Path $scriptConfigFilePath)) {
Write-Host "Error while trying to initialize Configuration from ScriptConfig.json. Path $scriptConfigFilePath not found!"
ExitHelper
}
$scriptConfigJson = Get-Content -Raw -Path $scriptConfigFilePath
try {
# The implementation for parsing the config json is different in PS Version 5 and lower compared to that in PS version 5 and higher,
# because the used library types are not present in all PS versions.
if ($PSVersionTable.PSVersion.Major -gt 5) {
# Using the .net json serializer and not 'ConvertFrom-Json' to enforce typing.
# 'ConvertFrom-Json' does not enforce typing, but:
# - parses boolean type based on truthy and falsy
# - numbers, booleans are parsed as a string without an error
# - ...
$Configuration = [System.Text.Json.JsonSerializer]::Deserialize([STRING] $scriptConfigJson, [ScriptConfiguration])
}
else {
[System.Reflection.Assembly]::LoadWithPartialName("System.Web.Extensions")
$serializer = [System.Web.Script.Serialization.JavaScriptSerializer]::new()
$Configuration = $serializer.Deserialize($scriptConfigJson, [ScriptConfiguration])
}
}
catch {
Write-Host "Could not parse json config file. Additional information: '$_'."
ExitHelper
}
# Constructed User-agent value
[String] $userAgent = If (-not $Configuration.scriptName) { "dvelop-bo-toolkit_script/$version ($($Configuration.dbType))" } Else { "dvelop-bo-toolkit_script_$($Configuration.scriptName)/$version ($($Configuration.dbType))" }
[System.Uri] $global:parsedBaseUri = ""
# Request-Rate-Limits related variables
$global:ReadLimits = New-Object -TypeName Limits
$global:ReadLimits.type = "read"
$global:WriteLimits = New-Object -TypeName Limits
$global:WriteLimits.type = "write"
[scriptblock] $global:MappingCode = $null;
# Batching related variables
$BatchingListUpsert = New-Object "System.Collections.ArrayList"
$BatchingListDelete = New-Object "System.Collections.ArrayList"
# Metrics
$global:Metrics = [PSCustomObject]@{
NoOfRequestsToBO = 0
}
#
#--------------------------------------------
#----------- Global Variables ---------------
#--------------------------------------------
#--------------------------------------------
#----------- Script Functions ---------------
#--------------------------------------------
function Main {
CheckConfig($Configuration)
$modelName = $Configuration.modelName + $(if ($Configuration.modelIsStaged) { "-staged" } else { "" })
$businessobjectsUri = $global:parsedBaseUri.ToString() + "businessobjects/custom/" + $modelName + "/" + $Configuration.entityPluralName
$authSessionID = IdpAuth
if ($authSessionID -ne "") {
if ($Configuration.dbType -eq "Oracle") {
# Connection to Oracle DB
# Library is deprecated https://docs.microsoft.com/en-us/dotnet/api/system.data.oracleclient?view=dotnet-plat-ext-6.0
# Replace with the ODP.NET library in the future
add-type -AssemblyName System.Data.OracleClient
$connection = ConnectToOracleDB
}
elseif ($Configuration.dbType -eq "MSSQL") {
# Connection to MSSQL DB
$connection = ConnectToMSSQLDB
}
if (($connection.State -eq "Open") -or ($Configuration.DBType -eq "CSV")) {
# Execute DB query
if ($connection.State -eq "Open") {
$dbEntityArray = @(ExecuteQuery -connection $connection -query $Configuration.query)
}
elseif ($Configuration.DBType -eq "CSV") {
$dbEntityArray = @(ReadCSVFile)
}
if ($dbEntityArray.count -gt 0) {
# For every row of the data source
foreach ($row in $dbEntityArray) {
if (!$Configuration.noBatching) {
# Use batching
Write-Log -level 3 -logtext ("Upserting business objects entity: " + $row.($Configuration.entityKey))
UpdateEntity -authSessionID $authSessionID -body $row -key $row.($Configuration.entityKey)
}
else {
# Do not use batching
# Check if entity exists in business objects
$response = CheckIfEntityExists -authSessionID $authSessionID -key $row.($Configuration.entityKey)
if ($response) {
# If so -> update entity
Write-Log -level 3 -logtext ("Updating business objects entity with key: " + $row.($Configuration.entityKey))
$response = UpdateEntity -authSessionID $authSessionID -body $row -key $row.($Configuration.entityKey)
}
elseif (!$response) {
# If not -> create entity
Write-Log -level 3 -logtext ("Creating new business objects entity: " + $row)
$response = CreateEntity -authSessionID $authSessionID -body $row
}
}
}
# Cleanup batch lists
if ((-not $Configuration.noBatching) -and $BatchingListUpsert.Count -gt 0) {
ExecuteBatchRequest -Requests $BatchingListUpsert
}
# Delete entities that do not exist in the data source but in business objects
if ($Configuration.deleteNonExistingEntities) {
Write-Log -level 0 -logtext ("Deleting non existing entities in business objects ...")
DeleteNonExistingEntities(GetEntities($authSessionID, $null))
# Cleanup batch list
if ((-not $Configuration.noBatching) -and $BatchingListDelete.Count -gt 0) {
ExecuteBatchRequest -Requests $BatchingListDelete
}
}
}
else {
Write-Log -level 2 -logtext ("DBQuery returned no entities. Please check the DBQuery.")
}
}
if ($connection.State -eq "Open") {
Write-Log -level 3 -logtext ("Closing database connection.")
$connection.close()
Write-Log -level 3 -logtext ("Database connection was closed successfully.")
}
}
}
#--------------
#IDP Authentication
function IdpAuth {
Write-Log -level 3 -logtext ("Starting IdpAuth")
try {
$headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
$headers.Add("Authorization", "Bearer " + $Configuration.apiKey)
$headers.Add("Origin", $global:parsedBaseUri.Scheme + "://" + $global:parsedBaseUri.Host)
$headers.Add("Accept", "application/hal+json")
$headers.Add("Content-Type", "application/hal+json")
$url = $global:parsedBaseUri.ToString() + "identityprovider/login"
$url = [uri]::EscapeUriString($url)
Write-Log -level 3 -logtext ("URL: $url")
$response = Invoke-RestMethod -UseBasicParsing $url -Method Get -Headers $headers -UserAgent $userAgent
$token = $response.AuthSessionId
return $token
}
catch {
Write-Log -level 2 -logtext ("IDP Login failed: " + $_ + " Please check credentials.")
ExitHelper
}
}
#--------------
function DeleteNonExistingEntities($entities) {
# If "$entities" is "null": condition evaluates to false (no runtime error)
if ($entities.value.count -gt 0) {
foreach ($businessobjectsEntity in $entities.value) {
#Resetting $existsInDB for each business objects entity
$existsInDB = $false
$dbEntityResult = $dbEntityArray | Where-Object $Configuration.entityKey -eq $businessobjectsEntity.($Configuration.entityKey) | Select-Object { $_.($Configuration.entityKey) }
if ($null -ne $dbEntityResult) {
Write-Log -level 3 -logtext ("Business Objects entity with key: " + $businessobjectsEntity.($Configuration.entityKey) + " exists in Database")
$existsInDB = $true
}
if (!$existsInDB) {
Write-Log -level 3 -logtext ("Entity should be deleted. Key: " + $businessobjectsEntity.($Configuration.entityKey))
DeleteEntity -authSessionID $authSessionID -entityKeyType $Configuration.entityKeyType -key $businessobjectsEntity.($Configuration.entityKey)
}
}
# All entities in current page processed
if ($null -ne $entities."@odata.nextLink") {
Write-Log -level 3 -logtext ("Page processed. 'nextLink' attribute present, start retrieving next page: " + $entities."@odata.nextLink")
DeleteNonExistingEntities(GetEntities -authSessionID $authSessionID -nextLink $entities."@odata.nextLink")
}
else {
# No more entities, processing finished
Write-Log -level 3 -logtext ("Page processed. No 'nextLink' attribute present, all entities processed")
}
}
else {
Write-Log -level 1 -logtext ("GetEntities returned no business objects entities.")
return
}
}
#--------------
function CheckIfEntityExists([STRING] $authSessionID, $key) {
Write-Log -level 3 -logtext ("Starting CheckIfEntityExists with key: $key")
if ($Configuration.entityKeyType -eq "String") {
$url = "$businessobjectsUri('$key')"
}
elseif ($Configuration.entityKeyType -in ("Guid", "Int32", "Int64")) {
$url = "$businessobjectsUri($key)"
}
else {
Write-Log -level 2 -logtext ("The Entitykeytype is not configured correctly!")
}
$url = [uri]::EscapeUriString($Url)
Write-Log -level 3 -logtext ("Escaped URL: $url")
$response = BusinessObjectsRequestHandler $url -Method "GET" -Headers $headers -Limits $global:ReadLimits -OverrideLimitsValue $Configuration.readRequestsLimit
if ($response.StatusCode -eq 200) {
#Entity exists
Write-Log -level 3 -logtext ("Entity exists. key: $key")
return $True
}
elseif ($response.StatusCode -eq 404) {
Write-Log -level 3 -logtext ("Entity does not exist. key: $key")
return $False
}
else {
Write-Log -level 2 -logtext ("Checking if entity exists failed with statuscode: $($response.StatusCode). Further information: " + $($response.Body))
if ($Configuration.failOnError) {
ExitHelper
}
}
}
#---------------------
function CreateEntity([STRING] $authSessionID, $body) {
Write-Log -level 3 -logtext ("Starting CreateEntity")
$url = [uri]::EscapeUriString("$businessobjectsUri")
Write-Log -level 3 -logtext ("Escaped URL: $url")
$body = $body | ConvertTo-Json -Depth 1
Write-Log -level 3 -logtext ("Body: $body")
$bytesBody = [System.Text.Encoding]::UTF8.GetBytes($body)
$response = BusinessObjectsRequestHandler -Url $url -Method "POST" -Headers $headers -Body $bytesBody -Limits $global:WriteLimits -OverrideLimitsValue $Configuration.writeRequestsLimit
if ($response.StatusCode -ne 201) {
Write-Log -level 2 -logtext ("Creating entity failed with statuscode: $($response.StatusCode). Further information: " + $($response.Body))
if ($Configuration.failOnError) {
ExitHelper
}
}
else {
Write-Log -level 0 -logtext ("Created new entity")
}
}
#---------------------
function UpdateEntity([STRING] $authSessionID, $body, $key) {
Write-Log -level 3 -logtext ("Starting UpdateEntity with key: $key")
$keyFormatting = ""
if ($Configuration.entityKeyType -eq "String") {
$keyFormatting = "('$key')"
}
elseif ($Configuration.entityKeyType -in ("Guid", "Int32", "Int64")) {
$keyFormatting = "($key)"
}
else {
Write-Log -level 2 -logtext ("The Entitykeytype is not configured correctly!")
}
if (!$Configuration.noBatching) {
$headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
$headers.Add("content-type", "application/json")
$request = [PSCustomObject]@{
id = "$($BatchingListUpsert.Count + 1)";
method = "PUT"
url = "$($Configuration.entityPluralName)$keyFormatting"
body = $body
headers = $headers
}
AddElementToBatchingList -BatchingList $BatchingListUpsert -Request $request
}
else {
$body = $body | ConvertTo-Json -Depth 2
Write-Log -level 3 -logtext ("Body: $body")
$bytesBody = [System.Text.Encoding]::UTF8.GetBytes($body)
$url = [uri]::EscapeUriString("$businessobjectsUri$keyFormatting")
Write-Log -level 3 -logtext ("Escaped URL: $url")
$response = BusinessObjectsRequestHandler -Url $url -Method "PUT" -Headers $headers -Body $bytesBody -Limits $global:WriteLimits -OverrideLimitsValue $Configuration.writeRequestsLimit
if ($response.StatusCode -ne 204) {
Write-Log -level 2 -logtext ("Updating entity failed with statuscode: $($response.StatusCode). Further information: " + $($response.Body))
if ($Configuration.failOnError) {
ExitHelper
}
}
else {
Write-Log -level 0 -logtext ("Entity was updated. key: $key")
}
}
}
#---------------------
function DeleteEntity([STRING] $authSessionID, $entityKeyType, $key) {
Write-Log -level 3 -logtext ("Starting DeleteEntity with key: $key")
$keyFormatting = ""
if ($Configuration.entityKeyType -eq "String") {
$keyFormatting = "('$key')"
}
elseif ($Configuration.entityKeyType -in ("Guid", "Int32", "Int64")) {
$keyFormatting = "($key)"
}
else {
Write-Log -level 2 -logtext ("The Entitykeytype is not configured correctly!")
}
if (!$Configuration.noBatching) {
$request = [PSCustomObject]@{
id = "$($BatchingListDelete.Count + 1)";
method = "DELETE"
url = "$($Configuration.entityPluralName)$keyFormatting"
}
AddElementToBatchingList -BatchingList $BatchingListDelete -Request $request
}
else {
# Attempt to delete an entity that does not exist returns the response code 204 and does not cause the WebRequest to throw an exception.
$url = [uri]::EscapeUriString("$businessobjectsUri$keyFormatting")
Write-Log -level 3 -logtext ("Escaped URL: $url")
$response = BusinessObjectsRequestHandler -Url $url -Method "DELETE" -Headers $headers -Limits $global:WriteLimits -OverrideLimitsValue $Configuration.writeRequestsLimit
if ($response.StatusCode -eq 200) {
Write-Log -level 0 -logtext ("Entity was deleted successfully. key: $key")
}
else {
Write-Log -level 2 -logtext ("Deleting entity failed with statuscode: $($response.StatusCode). Further information: " + $($response.Body))
if ($Configuration.failOnError) {
ExitHelper
}
}
}
}
#---------------------
function GetEntities([STRING] $authSessionID, $nextLink) {
Write-Log -level 3 -logtext ("Starting GetEntities")
# No nextLink present
if ($null -eq $nextLink) {
Write-Log -level 3 -logtext ("No NextLink attribute provided.")
$url = [uri]::EscapeUriString($businessobjectsUri)
}
else {
Write-Log -level 3 -logtext ("NextLink attribute provided: $nextLink")
$url = [uri]::EscapeUriString($nextLink)
}
Write-Log -level 3 -logtext ("Escaped URL: $url")
$response = BusinessObjectsRequestHandler $url -Method "GET" -Headers $headers -Limits $global:ReadLimits -OverrideLimitsValue $Configuration.readRequestsLimit
if ($response.StatusCode -eq 200) {
try {
$resultJson = ConvertFrom-Json $([String]::new($response.Body))
Write-Log -level 0 -logtext ("Business objects entities returned: " + $resultJson.value.count )
return $resultJson
}
catch {
Write-Log -level 2 -logtext ("Getting all entities failed: converting business objects response to json failed.")
if ($Configuration.failOnError) {
ExitHelper
}
}
}
else {
Write-Log -level 2 -logtext ("Getting all entities failed with statuscode: $($response.StatusCode). Further information: " + $($response.Body))
if ($Configuration.failOnError) {
ExitHelper
}
}
}
function BusinessObjectsRequestHandler {
param( [Parameter()] $Url, [Parameter()] $Method, [Parameter()] $Headers, [Parameter()] $Body, [Parameter()] $Limits, [Parameter()] $OverrideLimitsValue)
$headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
$headers.Add("Authorization", "Bearer $authSessionID")
$headers.Add("Origin", $global:parsedBaseUri.Scheme + "://" + $global:parsedBaseUri.Host)
$headers.Add("Content-Type", "application/json; charset=utf-8")
$headers.Add("Accept", "application/json")
# If server does not support limits
if ($Limits) {
return BusinessObjectsRequestHandlerLimits $url -Method $Method -Headers $Headers -Body $Body -Limits $Limits -OverrideLimitsValue $OverrideLimitsValue
}
else {
return BusinessObjectsRequestHandlerNoLimits $url -Method $Method -Headers $Headers -Body $Body
}
}
function BusinessObjectsRequestHandlerLimits {
param( [Parameter()] $Url, [Parameter()] $Method, [Parameter()] $Headers, [Parameter()] $Body, [Parameter()] $Limits, [Parameter()] $OverrideLimitsValue)
$milliSecondsBetweenRequests = $null
if ($OverrideLimitsValue) {
$milliSecondsBetweenRequests = [int](60000 / $(If ($OverrideLimitsValue) { $OverrideLimitsValue } Else { 1 } ))
}
else {
$milliSecondsBetweenRequests = [int]($Limits.timeUnitAsMilliseconds / $(If ($Limits.remainingNumberOfRequests) { $Limits.remainingNumberOfRequests } Else { 1 } ))
}
Write-Log -level 3 -logtext ("MilliSecondsBetweenRequests: $milliSecondsBetweenRequests")
$elapsedTime = ([DateTimeOffset]::UtcNow.ToUnixTimeMilliseconds()) - ($Limits.timeStampLastRequest)
$waitTime = $([math]::max(0l, ($milliSecondsBetweenRequests - $elapsedTime)))
Write-Log -level 3 -logtext ("Waiting: $waitTime milliseconds")
Start-Sleep -Milliseconds $waitTime
$responseHeader = $null
$statusCode = $null
$responseBody = $null
try {
$Limits.timeStampLastRequest = [DateTimeOffset]::UtcNow.ToUnixTimeMilliseconds()
$global:Metrics.NoOfRequestsToBO++;
$response = Invoke-WebRequest -UseBasicParsing $Url -Method $Method -body $Body -Headers $Headers -UserAgent $UserAgent
$statusCode = $response.StatusCode
$responseBody = $response.Content
$responseHeader = $response.Headers
}
catch {
$statusCode = $_.Exception.Response.StatusCode.value__
$responseHeader = $_.Exception.Response.Headers
if ($statusCode -eq 429) {
$retryAfterInSeconds = [int](HeaderValueByNameOrNull -Header $responseHeader -Name "Retry-After")
Write-Log -level 1 -logtext ("Reached $($Limits.type) request rate limits. Retrying in $retryAfterInSeconds seconds")
Start-Sleep -Seconds $retryAfterInSeconds
# Recursive call
return BusinessObjectsRequestHandler $Url -Method $Method -Headers $Headers -Body $Body -Limits $Limits
}
$responseBody = RetrieveResponseBodyFromFailedWebRequest($_)
}
$remaingRequests = HeaderValueByNameOrNull -Header $responseHeader -Name "X-RateLimit-Remaining"
if (-not ($null -eq $remaingRequests)) {
$Limits.remainingNumberOfRequests = $remaingRequests
}
# Check if limits are not yet set but the corresponding header field is present -> If so, this request if the first one for that limit type (read or write).
# If the limits are not yet set and the header field is not present disable the limits.
if (-not $Limits.totalNumberOfRequests -and $(-not $null -eq $(HeaderValueByNameOrNull -Header $responseHeader -Name "X-RateLimit-Limit"))) {
ParseLimitHeader -Limits $Limits -LimitsHeader $(HeaderValueByNameOrNull -Header $responseHeader -Name "X-RateLimit-Limit")
}
elseif (-not $Limits.totalNumberOfRequests) {
# Set limits to null so signalise that the business objects instance has limits disabled and the 'BusinessObjectsRequestHandlerNoLimits'
# function will be used for future requests for this limit type.
if ($Limits.Type -eq "write") {
$global:WriteLimits = $null;
}
elseif ($Limits.Type -eq "read") {
$global:ReadLimits = $null
}
}
Write-Log -level 3 -logtext ("Updated Limits: remaining $($Limits.type) requests: $($Limits.remainingNumberOfRequests)")
return [PSCustomObject]@{
StatusCode = $statusCode
Body = $responseBody
}
}
function BusinessObjectsRequestHandlerNoLimits {
param( [Parameter()] $Url, [Parameter()] $Method, [Parameter()] $Headers, [Parameter()] $Body )
$statusCode = $null
$responseBody = $null
try {
$global:Metrics.NoOfRequestsToBO++;
$response = Invoke-WebRequest -UseBasicParsing $Url -Method $Method -body $Body -Headers $Headers -UserAgent $UserAgent
$statusCode = $response.StatusCode
$responseBody = $response.Content
}
catch {
$statusCode = $_.Exception.Response.StatusCode.value__
$responseBody = RetrieveResponseBodyFromFailedWebRequest($_)
}
return [PSCustomObject]@{
StatusCode = $statusCode
Body = $responseBody
}
}
#---------------------
function ParseLimitHeader {
param([Parameter()] $Limits, [Parameter()] $LimitsHeader)
# ----- NOTE Format can change! -----
$headerComponents = $Limitsheader.Split(" ")
# Read the maximum number od requests per time unit
$Limits.totalNumberOfRequests = $headerComponents[0] -as [int]
# Read the time unit
# Replace new line needed because for some reason powershell inserts a newline after the last splitted element
$timeUnit = $null
# If is needed since PS5 does not support '?' for null checks
if (-not $null -eq $headerComponents[3]) {
$timeUnit = $headerComponents[3].replace("`n", "").replace("`r", "")
}
if ($timeUnit -eq "minute") {
$Limits.timeUnitAsMilliseconds = 60 * 1000
}
elseif ($timeUnit -eq "hour") {
$Limits.timeUnitAsMilliseconds = 60 * 60 * 1000
}
# If header can not be parsed log and exit script
if (-not $Limits.totalNumberOfRequests -or (-not $Limits.timeUnitAsMilliseconds)) {
Write-Log -level 2 -logtext ("Limits header 'X-RateLimit-Limit' is set '$LimitsHeader' but its value cloud not be parsed. You are probably using an old version of this script. Exiting ...")
ExitHelper
}
}
#---------------------
function ConnectToMSSQLDB {
Write-Log -level 3 -logtext ("Starting ConnectToMSSQLDB")
try {
$connectionString = $("Server=$($Configuration.dbServer);Database=$($Configuration.databaseName);User Id=$($Configuration.user);Password=$($Configuration.password)");
$connection = New-Object System.Data.SqlClient.SqlConnection
$connection.ConnectionString = $connectionString
$connection.Open()
Write-Log -level 3 -logtext ("Connection to MSSQL DB was successfull.")
return $connection
}
catch {
Write-Log -level 2 -logtext ("Connection to MSSQL DB failed. " + $_)
ExitHelper
}
}
#---------------------
function ConnectToOracleDB {
Write-Log -level 3 -logtext ("Starting ConnectToOracleDB")
try {
$connectionString = "User Id=$($Configuration.user);Password=$($Configuration.password);Data Source=$($Configuration.dataSource)"
$connection = New-Object System.Data.OracleClient.OracleConnection($connectionString)
$connection.Open()
Write-Log -level 3 -logtext ("Connection to Oracle DB was successfull.")
return $connection
}
catch {
Write-Log -level 2 -logtext ("Connection to Oracle DB failed. " + $_)
ExitHelper
}
}
#--------------------
function ReadCSVFile {
if ($Configuration.csvHeader) {
$csv = Import-Csv -Path $(PathHelper($Configuration.csvPath)) -Header $Configuration.csvHeader -Delimiter $Configuration.csvFileDelimiter
$result = $csv | Select-Object -Skip 1 | ForEach-Object {
$row = $_;
& $MappingCode
}
return $result;
}
else {
$csv = Import-Csv -Path $(PathHelper($Configuration.csvPath)) -Delimiter $Configuration.csvFileDelimiter
$result = $csv | Select-Object | ForEach-Object {
$row = $_;
& $MappingCode
}
return $result;
}
}
#--------------------
function ExecuteQuery($connection, $query) {
Write-Log -level 3 -logtext ("Starting ExecuteQuery")
Write-Log -level 3 -logtext ("Query: " + $query)
try {
$command = $connection.CreateCommand()
$command.CommandText = $query
$reader = $command.ExecuteReader()
$result = @()
$row = $reader;
while ($row.read()) {
$temp = & $MappingCode
$result = $result + $temp;
}
Write-Log -level 3 -logtext ("Closing database connection")
$connection.Close()
Write-Log -level 3 -logtext ("Database connection closed")
return $result
}
catch {
Write-Log -level 2 -logtext ("Executing Database query failed " + $_.Exception.ToString())
ExitHelper
}
finally {
if ($connection.State -eq "Open") {
$connection.close()
}
}
}
function AddElementToBatchingList {
param ([Parameter()] $BatchingList, [Parameter()] $Request)
$BatchingList.Add($Request) | Out-Null
if ($BatchingList.Count -eq 100) {
Write-Log -level 3 -logtext ("Batching list reached maximum number of entries. Batch request will be sent to business objects.")
ExecuteBatchRequest -Requests $BatchingList
}
}
function ExecuteBatchRequest {
param ([Parameter()] $Requests)
$body = [PSCustomObject]@{
requests = $Requests
} | ConvertTo-Json -Depth 100
Write-Log -level 3 -logtext ("Body: $body")
$bytesBody = [System.Text.Encoding]::UTF8.GetBytes($body)
Write-Log -level 0 -logtext ("Executing batch request with $($Requests.Count) entries")
$url = [uri]::EscapeUriString($($global:parsedBaseUri.ToString() + "businessobjects/custom/" + $modelName + '/$batch'))
Write-Log -level 3 -logtext ("Escaped URL: $url")
$response = BusinessObjectsRequestHandler -Url $url -Method "POST" -Body $bytesBody -Headers $headers -Limits $global:WriteLimits -OverrideLimitsValue $Configuration.writeRequestsLimit
if ($response.StatusCode -ne 200) {
Write-Log -level 2 -logtext ("Batch request failed with statuscode: $($response.StatusCode). Further information: $($response.Body)")
if ($Configuration.failOnError) {
ExitHelper
}
}
else {
Write-Log -level 0 -logtext ("Batch request was successfully executed.")
Write-Log -level 3 -logtext ("Batch response: $response")
$resultJson = ConvertFrom-Json $([String]::new($response.Body))
$resultJson.responses | ForEach-Object {
if (!($_.status -in (200, 201, 204))) {
Write-Log -level 1 -logtext ("Batch request entry failed: $($_ | ConvertTo-Json -Depth 5)")
}
}
}
$Requests.Clear();
}
#--------------------
# Helper method to determine
function HeaderValueByNameOrNull {
param ( [Parameter()] $Header, [Parameter()] $Name )
if (!$Header) {
return $null;
}
if ($Header.GetType().ToString() -eq "System.Net.Http.Headers.HttpResponseHeaders" -or $Header.GetType().ToString() -eq "System.Net.WebHeaderCollection") {
if ($Header.Contains($Name)) {
return ($Header.GetValues($Name) | Out-String)
}
}
else {
return $Header[$Name]
}
}
#--------------------
function RetrieveResponseBodyFromFailedWebRequest($exception) {
# The following operations are not needed with PowerShell version greater than 5.x.x
if (($PSVersionTable.PSVersion.Major -lt 6) -and ($null -ne $exception.Exception.Response)) {
$reader = New-Object System.IO.StreamReader($exception.Exception.Response.GetResponseStream())
$reader.BaseStream.Position = 0
$reader.DiscardBufferedData()
$responseBody = $reader.ReadToEnd()
return $responseBody
}
return $exception
}
#--------------------
function PathHelper($path) {
if ([System.IO.Path]::IsPathRooted($path)) {
return $path
}
return $(Join-Path -Path $configBasePath -ChildPath $path)
}
#--------------------
function CheckConfig($Configuration) {
Write-Host "Checking Config.."
# Logging Config check
# If 'logDir' is set it must be a valid path
if ($Configuration.logDir) {
if (!(Test-Path $(PathHelper($Configuration.logDir)))) {
Write-Host "Log Directory does not exist! Exiting..." -ForegroundColor Red
ExitHelper
}
Write-Log -level 0 -logtext ("Logging to log file")
}
else {
Write-Log -level 0 -logtext ("Logging to console")
}
# If 'deleteOldLogs' is set it must be a positive integer value
if ($Configuration.deleteOldLogs -And $Configuration.deleteOldLogs -lt 0) {
Write-Log -level 2 -logtext ("deleteOldLogs must be a positive value! Exiting...")
ExitHelper
}
if ($Configuration.modelIsStaged -And !$Configuration.noBatching) {
Write-Log -level 2 -logtext ("You are trying to perform batching in a staged model. Batching is only provided for published models. Please set either noBatching to true or modelIsStaged to false.")
ExitHelper
}
# Config check
if (![System.Uri]::TryCreate($Configuration.baseUri, 'Absolute', [ref]$global:parsedBaseUri)) {
Write-Log -level 2 -logtext ("The BaseUri '$($Configuration.baseUri)' is not valid! Exiting...")
ExitHelper
}
if (-Not $Configuration.apiKey) {
Write-Log -level 2 -logtext ("The API Key is not configured! Exiting...")
Write-Log -level 0 -logtext ("Did you rename the API key property name? It has been renamed: 'apikey' -> 'apiKey'.")
ExitHelper
}
if (-Not $Configuration.modelName) {
Write-Log -level 2 -logtext ("The Modelname is not configured! Exiting...")
ExitHelper
}
elseif ($Configuration.modelName -like "*-staged") {
Write-Log -level 2 -logtext (
"The Modelname contains a '-staged'. Please configure staged models by setting the 'modelIsStaged' parameter to 'true' and not by appending '-staged' to the model name. Exiting..."
)
ExitHelper
}
if (-Not $Configuration.entityPluralName) {
Write-Log -level 2 -logtext ("The entityPluralName is not configured! Exiting...")
ExitHelper
}
if (-Not $Configuration.entityKey) {
Write-Log -level 2 -logtext ("The Entitykey is not configured! Exiting...")
ExitHelper
}
if (!($Configuration.entityKeyType -in ("Guid", "Int32", "Int64", "String"))) {
Write-Log -level 2 -logtext ("The Entitykeytype is not configured correctly! Exiting...")
ExitHelper
}
if ($Configuration.writeRequestsLimit -lt 0) {
Write-Log -level 2 -logtext ("The WriteRequestLimit is negative! Exiting...")
ExitHelper
}
if ($Configuration.readRequestsLimit -lt 0) {
Write-Log -level 2 -logtext ("The ReadRequestLimit is negative! Exiting...")
ExitHelper
}
# Database Config check
if ($Configuration.dbType -eq "Oracle") {
if ($Configuration.dataSource -eq "") {
Write-Log -level 2 -logtext ("The Datasource is not configured! Exiting...")
ExitHelper
}
if ($Configuration.user -eq "") {
Write-Log -level 2 -logtext ("The Databaseuser is not configured! Exiting...")
ExitHelper
}
if ($Configuration.password -eq "") {
Write-Log -level 2 -logtext ("The Databasepassword is not configured! Exiting...")
ExitHelper
}
}
elseif ($Configuration.dbType -eq "MSSQL") {
if ($Configuration.dbServer -eq "") {
Write-Log -level 2 -logtext ("The Databaseserver is not configured! Exiting...")
ExitHelper
}
if ($Configuration.databaseName -eq "") {
Write-Log -level 2 -logtext ("The Databasename is not configured! Exiting...")
ExitHelper
}
if ($Configuration.user -eq "") {
Write-Log -level 2 -logtext ("The Databaseuser is not configured! Exiting...")
ExitHelper
}
if ($Configuration.password -eq "") {
Write-Log -level 2 -logtext ("The Databasepassword is not configured! Exiting...")
ExitHelper
}
}
elseif ($Configuration.dbType -eq "CSV") {
if ($Configuration.csvPath -eq "") {
Write-Log -level 2 -logtext ("The CSV-Path is not configured! Exiting...")
ExitHelper
}
if (!(Test-Path $(PathHelper($Configuration.csvPath)) -PathType Leaf)) {
Write-Log -level 2 "CSV-Path '$(PathHelper($Configuration.csvPath))' does not exist! Exiting..." -ForegroundColor Red
ExitHelper
}
elseif ([System.IO.Path]::GetExtension($(PathHelper($Configuration.csvPath))).ToLower() -ne ".csv") {
Write-Log -level 2 "Configured CSV file at '$($Configuration.csvPath)' is not a valid CSV file! Exiting..." -ForegroundColor Red
ExitHelper
}
if (-Not $Configuration.csvFileDelimiter) {
Write-Log -level 2 -logtext ("The csvFileDelimiter is not configured! Exiting...")
ExitHelper
}
}
else {
Write-Log -level 2 -logtext ("The Database Type is not configured correctly! Exiting..." )
ExitHelper
}
if ($Configuration.query -eq "") {
Write-Log -level 2 -logtext ("The Databasequery is not configured! Exiting...")
ExitHelper
}
# If 'importIntervalTime' is set it must be a valid integer
if ($Configuration.importIntervalTime) {
if (-Not ($Configuration.importIntervalTime -is [int])) {
Write-Log -level 2 -logtext ("The value of importIntervalTime is not numeric! Exiting...")
ExitHelper
}
# If the value of 'importIntervalTime' is below "60" and grater "0", throw an error
elseif ($Configuration.importIntervalTime -lt 60 -And $Configuration.importIntervalTime -ne 0) {
Write-Log -level 2 -logtext ("The value of importIntervalTime is below 60, which is the minimum value (exception: the value 0 to disable regular imports) Exiting...")
ExitHelper
}
}
# Check if configs use the old property 'loopExecution'. If that is the case print a hint and exit.
if ($Configuration.loopExecution) {
Write-Log -level 2 -logtext ("Old property 'loopExecution' detected. It has been renamed: 'loopExecution' -> 'importIntervalTime'. Exiting...")
ExitHelper
}
if ($Configuration.importIntervalTime -and $Configuration.execByScheduler) {
Write-Log -level 2 -logtext ("Both 'importIntervalTime' and 'execByScheduler' are configured. Please use only one of them. Exiting...")
ExitHelper
}
# Version Variable Config check
if ($version -eq "") {
Write-Log -level 2 -logtext ("Version is not configured! Exiting...")
ExitHelper
}
# Check if the provided value of the 'mapping' value is valid powershell code
if (-Not $Configuration.mapping) {
Write-Log -level 2 -logtext ("Mapping is not defined! Exiting...")
ExitHelper
}
else {
# Convert the powershell code defined as string in the mapping attribute
# into executable powershell code
try {
$codeAsSingleString = "";
$Configuration.mapping | ForEach-Object {
$codeAsSingleString += "$_;";
}
$rawCode = 'new-object psObject -Property @{' + $codeAsSingleString + '}'
$global:MappingCode = [scriptblock]::Create($rawCode)
}
catch {
Write-Log -level 2 -logtext ("Mapping is defined, but contains no valid PowerShell code. Additional information: $_")
ExitHelper
}
}
}
#-------------------------------------------
#----------- Script Functions --------------
#-------------------------------------------
#-------------------------------------------
#---------------- Logging ------------------
#-------------------------------------------
function Write-Log([string]$logtext, [int]$level = 0) {
$date = get-date -format "yyyy-MM-dd"
$file = ("Log_" + $date + ".log")
$logfile = $(PathHelper($Configuration.logDir)) + "\" + $file
$logdate = get-date -format "yyyy-MM-dd HH:mm:ss"
if ($level -eq 0) {
$logtext = "[INFO] " + $logtext
$text = "[" + $logdate + "] - " + $logtext
Write-Host $text
if ($Configuration.logDir) {
$text | Out-File $logfile -Append
}
}
if ($level -eq 1) {
$logtext = "[WARNING] " + $logtext
$text = "[" + $logdate + "] - " + $logtext
Write-Host $text -ForegroundColor Yellow
if ($Configuration.logDir) {
$text | Out-File $logfile -Append
}
}
if ($level -eq 2) {
$logtext = "[ERROR] " + $logtext
$text = "[" + $logdate + "] - " + $logtext
Write-Host $text -ForegroundColor Red
if ($Configuration.logDir) {
$text | Out-File $logfile -Append
}
}
# DEBUG Log
if (($level -eq 3) -and ($Configuration.debugLog)) {
$logtext = "[DEBUG] " + $logtext
$text = "[" + $logdate + "] - " + $logtext
Write-Host $text -ForegroundColor Green