-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmigrateDataSources.ps1
233 lines (201 loc) · 6.82 KB
/
migrateDataSources.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
cls
add-type @"
using System.Net;
using System.Security.Cryptography.X509Certificates;
public class TrustAllCertsPolicy : ICertificatePolicy {
public bool CheckValidationResult(
ServicePoint srvPoint, X509Certificate certificate,
WebRequest request, int certificateProblem) {
return true;
}
}
"@
[System.Net.ServicePointManager]::CertificatePolicy = New-Object TrustAllCertsPolicy
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
##############################
# Change variables accordingly
##############################
$global:esmIP = '10.10.10.10'
###########################
# Some Inits
###########################
$global:esmAPI = 'https://' + $global:esmIP + '/rs/esm/v2/'
$global:headers = $null
$global:cookie = $null
###########################
# Main Function
###########################
Function Main
{
# Please enter Source and Target ID of the Event Receiver here
$erc_source = '144112345678912345'
$erc_target = '144112345678912346'
Login;
$datasources = (dsGetDataSourceList $erc_source) | ConvertFrom-Json
$new_datasources = @()
# Iterate through all data sources from old ERC
Foreach ($ds in $datasources)
{
# Get Details per data source
$detail = dsGetDataSourceDetail ($ds.id) | ConvertFrom-Json
# in case this data source holds clients (child type = 2), get them
if ($detail.childType -eq 2)
{
$detail | Add-Member -MemberType NoteProperty -Name 'client_ds' -Value (dsGetDataSourceClients $ds.id | ConvertFrom-Json)
}
$new_datasources += $detail
}
dsAddDataSources $erc_target $new_datasources
Write-Output ('Done.')
}
#################################################################################################
# ESM API IMPLEMENTATION
#################################################################################################
###########################
# Get Data Sources per Device
###########################
Function dsGetDataSourceList ($receiverID)
{
$params = @{
receiverId = $receiverId
}
$response = CallEsmApi 'dsGetDataSourceList' $params
return $response.Content
}
###########################
# Get Data Source Details
###########################
Function dsGetDataSourceDetail ($dsId)
{
$params = @{
datasourceId = $dsId
}
$response = CallEsmApi 'dsGetDataSourceDetail' $params
return $response.Content
}
###########################
# Get Data Source Clients
###########################
Function dsGetDataSourceClients ($dsId)
{
$params = @{
datasourceId = $dsId
}
$response = CallEsmApi 'dsGetDataSourceClients' $params
return $response.Content
}
###########################
# Add Data Sources
###########################
Function dsAddDataSources ($erc, $datasources)
{
$ds_all = @()
$ds_single = @()
foreach ($ds in $new_datasources)
{
if (-not ($ds.client_ds))
{
$ds_all += ($ds | select name, ipAddress, typeId, zoneId, enabled, url, parameters)
} else {
$ds_single += @{
'ds' = $ds | select name, ipAddress, typeId, zoneId, enabled, url, parameters
'client_ds' = $ds.client_ds
}
}
}
# Add all data sources that do not have clients at once
$params = @{
'receiverId' = $erc
'datasources' = $ds_all
}
$jobId = ((CallEsmApi 'dsAddDataSources' $params).content | ConvertFrom-Json).value
Write-Output('Creating parent data sources w/o client; jobID: ' + $jobId)
$response = (jobStatus 'dsAddDataSourcesStatus' $jobId)
Write-Output('Creating parent data sources w/o client; jobID: ' + $jobId + ' ...Done')
# Add data sources with clients one by one
foreach ($datasource in $ds_single)
{
# create parent Data Source
$params = @{
'receiverId' = $erc
'datasources' = @($datasource.ds)
}
$jobId = ((CallEsmApi 'dsAddDataSources' $params).content | ConvertFrom-Json).value
Write-Output('Creating parent data source that has clients; jobID: ' + $jobId)
$parentId = (jobStatus 'dsAddDataSourcesStatus' $jobId).successfulDatasources[0]
Write-Output('Creating parent data source that has clients; jobID: ' + $jobId + ' new parent: ' + $parentId + '...Done')
# create client Data Sources
dsAddDataSourceClients $parentId $datasource.client_ds
}
return 'Done dsAddDataSources.'
}
###########################
# Add Data Source Clients
###########################
Function dsAddDataSourceClients ($parentId, $datasourceclients)
{
Write-Output ('ParentID ' + $parentId + ': Creating clients...')
$new_clients = @()
foreach ($client in $datasourceclients)
{
$new_clients += ($client | select dateOrder, port, useTls, host, timezone, type, name, ipAddress)
}
$params = @{
'parentId' = $parentId
'clients' = $new_clients
}
$jobId = ((CallEsmApi 'dsAddDataSourceClients' $params).content | ConvertFrom-Json).value
Write-Output ('ParentID ' + $parentId + ': Creating clients...Done')
return $jobId
}
#################################################################################################
# HELPER FUNCTIONS
#################################################################################################
###########################
# Job Status
###########################
Function jobStatus([String]$method, $jobId)
{
do
{
$status = (CallEsmApi $method @{'jobId' = $jobId}).content | ConvertFrom-Json
} until ($status.jobStatus -eq 'COMPLETE')
return $status
}
###########################
# Login
###########################
Function Login
{
$esmhost = 'https://' + $global:esmIP + '/rs/esm/'
$cred = Get-Credential
$username = $cred.GetNetworkCredential().Username
$passwd = $cred.GetNetworkCredential().Password
$login_url = $esmhost + "login"
$b64_user = [System.Convert]::ToBase64String([System.Text.Encoding]::UTF8.GetBytes($username))
$b64_passwd = [System.Convert]::ToBase64String([System.Text.Encoding]::UTF8.GetBytes($passwd))
$params = @{
username = $b64_user
password = $b64_passwd
locale = 'en_US'
os = 'Win32'};
$body = $params | ConvertTo-Json
$global:headers = @{
'Content-Type' = 'application/json'
};
$login_headers = $global:headers
$login_headers.Add("Authorization", "Basic "+[System.Convert]::ToBase64String([System.Text.Encoding]::UTF8.GetBytes($username+":"+$passwd )))
$response = Invoke-WebRequest $login_url -Method Post -Headers $login_headers -Body $body -SessionVariable global:cookie
$global:headers.Add('X-Xsrf-Token', $response.headers.Get_Item('Xsrf-Token'))
}
###########################
# Call ESM API
###########################
Function CallEsmApi ([String]$method, $params)
{
$url = -join($global:esmAPI, $method)
$body = $params | ConvertTo-Json -depth 10
$response = Invoke-WebRequest $url -Method Post -Headers $global:headers -WebSession $global:cookie -Body $body
return $response
}
Main;