forked from anshulagrawal07/sfsnowsightextensions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Update-SFDocuments.ps1
398 lines (358 loc) · 17.3 KB
/
Update-SFDocuments.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
function Update-SFDocuments ()
{
[CmdletBinding()]
param (
[Parameter(Mandatory)] [String]$SFObjectTypes,
[Parameter(Mandatory)] [String]$SFRole,
[Parameter(Mandatory)] [String]$SFWarehouse,
[Parameter()] [String]$WorksheetsPath,
[Parameter()] [String]$DashboardsPath,
[Parameter()] [String]$FiltersPath,
[Parameter()] [String]$SFDatabase,
[Parameter()] [String]$SFSchema,
[Parameter()] [String]$OutputDirectory
)
if ($SFObjectTypes.Trim().ToLower() -eq "all") {
if (-Not ($WorksheetsPath, $DashboardsPath, $FiltersPath)) {
Write-Host "Please supply a path for each object type (-WorksheetsPath, -DashboardsPath, -FiltersPath)" -ForegroundColor Yellow
break
}
$tmp_filters = Get-ChildItem $FiltersPath
if ($tmp_filters) {
Write-Host "`r`nFound Files at $FiltersPath" -ForegroundColor Cyan
echo "`r"
echo $tmp_filters.NAME
echo "`r`n"
}
else {
Write-Host "`r`nNo files found at $FiltersPath" -ForegroundColor Yellow
}
$tmp_dashboards = Get-ChildItem $DashboardsPath
if ($tmp_dashboards) {
Write-Host "`r`nFound files at $DashboardsPath" -ForegroundColor Cyan
echo "`r"
echo $tmp_dashboards.Name
echo "`r`n"
}
else {
Write-Host "`r`nNo files found at $DashboardsPath" -ForegroundColor Yellow
}
$tmp_worksheets = Get-ChildItem $WorksheetsPath
if ($tmp_worksheets) {
Write-Host "Found files at $WorksheetsPath" -ForegroundColor Cyan
echo "`r"
echo $tmp_worksheets.Name
echo "`r`n"
}
else {
Write-Host "`r`nNo files found at $WorksheetsPath" -ForegroundColor Yellow
}
Invoke-Command -ScriptBlock {
Update-Filters -FiltersPath $FiltersPath -SFRole $SFRole -SFWarehouse $SFWarehouse
echo "`r`n"
Update-Dashboards -DashboardsPath $DashboardsPath -SFRole $SFRole -SFWarehouse $SFWarehouse
echo "`r`n"
Update-Worksheets -WorksheetsPath $WorksheetsPath -SFRole $SFRole -SFWarehouse $SFWarehouse
echo "`r`n"
}
} else {
$objs = $SFObjectTypes -Split ','
# Begin for loop through array
foreach ($_ in $objs) {
$obj = $_.Trim().ToLower()
Write-Host "`r`nSearching for $obj files.`r`n" -ForegroundColor Magenta
if($obj -eq "filters") {
if (-Not ($FiltersPath)) {
echo "Please supply a path for -FiltersPath argument. Skipping."
continue
}
$tmp_filters = Get-ChildItem $FiltersPath
if ($tmp_filters) {
Write-Host "`r`nFound Files at $FiltersPath" -ForegroundColor Cyan
echo "`r"
$tmp_filters.Name
echo "`r`n"
}
else {
Write-Host "`r`nNo files found at $FiltersPath. Skipping." -ForegroundColor Yellow
continue
}
Invoke-Command -ScriptBlock { Update-Filters -FiltersPath $FiltersPath -SFRole $SFRole -SFWarehouse $SFWarehouse }
echo "`r`n"
}
elseif ($obj -eq "dashboards") {
if (-Not ($DashboardsPath)) {
echo "Please supply a path for -DashboardsPath argument. Skipping."
continue
}
$tmp_dashboards = Get-ChildItem $DashboardsPath
if ($tmp_dashboards) {
Write-Host "`r`nFound files at $DashboardsPath" -ForegroundColor Cyan
echo "`r"
echo $tmp_dashboards.Name
echo "`r`n"
}
else {
Write-Host "`r`nNo files Found at $DashboardsPath. Skipping." -ForegroundColor Yellow
continue
}
Invoke-Command -ScriptBlock { Update-Dashboards -DashboardsPath $DashboardsPath -SFRole $SFRole -SFWarehouse $SFWarehouse }
}
elseif ($obj -eq "worksheets") {
if (-Not ($WorksheetsPath)) {
echo "Please supply a path for -WorksheetsPath argument. Skipping."
continue
}
$tmp_worksheets = Get-ChildItem $WorksheetsPath
if ($tmp_worksheets) {
Write-Host "`r`nFound files at $WorksheetsPath" -ForegroundColor Cyan
echo "`r"
echo $tmp_worksheets.Name
echo "`r`n"
}
else {
Write-Host "`r`nNo files found at $WorksheetsPath" -ForegroundColor Yellow
continue
}
Invoke-Command -ScriptBlock { Update-Worksheets -WorksheetsPath $WorksheetsPath -SFRole $SFRole -SFWarehouse $SFWarehouse }
}
else {
Write-Host "$obj not a valid object. Use 'Filters, Dashboards, Worksheets, a combination of the three in a comma seperated list, or All.'`r`n" -ForegroundColor Magenta
continue
}
}
}
<#
.SYNOPSIS
Update Snowflake context for Filters, Dashboards, or Worksheets files together or separately.
.DESCRIPTION
This function allows the user to update the Snowflake context, such as Role, Virtual Warehouse, Database, or Schema
in the exported Filter, Dashboard, or Worksheet files together or separately.
Please refer to the examples for syntax. If a Role is not provided, ACCOUNTADMIN will be the designated Role.
.PARAMETER SFObjectTypes
Specifies the object type being updated.
Filter, Dashboard, Worksheet, or or any combination of the three can be entered.
Choose All to update Filters, Dashboards, and Worksheets with one command.
Casing, ordering, and spaces are not important for this argument. 'filter,dashboard,worksheet' = 'Worksheet, Dashboard, Filter'
Do not enter All in combination with any of the other selections or duplicate objects/files will be created.
.PARAMETER FiltersPath
Specifies the path to the directory where the filters are located. Case sensitive.
.PARAMETER DashboardsPath
Specifies the path to the directory where the dashboards are located. Case sensitive.
.PARAMETER WorksheetsPath
Specifies the path to the directory where the worksheets are located. Case sensitive.
.PARAMETER SFRole
Specifies the Snowflake Role required to run the specific object. Default is ACCOUNTADMIN.
.PARAMETER SFWarehouse
Specifies the Snowflake Warehouse required to run the specific object. An error will display if no warehouse is designated.
.PARAMETER OutputDirectory
A relative or fully qualified path where files will be placed.
.INPUTS
None. You cannot pipe objects to Update-Documents.
.OUTPUTS
set-content. Update-Documents updates the JSON values of the Snowflake context in order to ensure the respective object works appropriately.
.EXAMPLE
---------------- All ----------------
Update-Documents -SFObjectTypes 'All' -FiltersPath <path-to-filters-dir> -DashboardsPath <path-to-dashboards-dir> -WorksheetsPath <path-to-worksheets-dir> -SFRole THISMEANSEVERYTHINGWORKS -SFWarehouse SUPERNEWWAREHOUSE
.EXAMPLE
---------------- Filters ----------------
Update-Documents -SFObjectTypes 'Filters' -FiltersPath <path-to-filters-dir> -SFRole THISMEANSEVERYTHINGWORKS -SFWarehouse SUPERNEWWAREHOUSE
.EXAMPLE
---------------- Dashboard ----------------
Update-Documents -SFObjectTypes 'Dashboards' -DashboardsPath <path-to-dashboards-dir> -SFRole THISMEANSEVERYTHINGWORKS -SFWarehouse SUPERNEWWAREHOUSE
.EXAMPLE
---------------- Worksheet ----------------
Update-Documents -SFObjectTypes 'Worksheets' -WorksheetsPath <path-to-worksheets-dir> -SFRole THISMEANSEVERYTHINGWORKS -SFWarehouse SUPERNEWWAREHOUSE
.EXAMPLE
---------------- Mix of assets ----------------
Update-Documents -SFObjectTypes 'dashboards, filters, worksheets' -FiltersPath <path-to-filters-dir> -DashboardsPath <path-to-dashboards-dir> -WorksheetsPath <path-to-worksheets-dir> -SFWarehouse SUPERNEWWAREHOUSE
#>
}
function Update-Filters ()
{
if($FiltersPath) {
# Update Filters files
foreach ($f in $tmp_filters){
$fparam = Get-Content $f.FullName | ConvertFrom-JSON
$fname = $f.Name
$fparam.update | % { #Manual Filter Update
if($fparam.Type -eq 'manual'){
Write-Host 'Updating MANUAL FILTER values for' -ForegroundColor Cyan
Write-Host $f.Name -ForegroundColor Green
$fparam.Role = $SFRole
$fparam.Warehouse = $SFWarehouse
$fparam.Configuration.context.role = $SFRole
$fparam.Configuration.context.warehouse = $SFWarehouse
$fparam.Database = $SFDatabase
$fparam.Schema = $SFSchema
$fparam.Configuration.context.database = $SFSchema
$fparam.Configuration.context.schema = $SFSchema
$fparam.FileSystemSafeName = ""
$fparam.AccountName = ""
$fparam.AccountFullName = ""
$fparam.AccountUrl = ""
$fparam.OrganizationID = ""
$fparam.Region = ""
}
#Query Filter Update
elseif($fparam.Type -eq 'query') {
Write-Host 'Updating QUERY FILTER values for' -ForegroundColor Cyan
Write-Host $f.Name -ForegroundColor Yellow
$fparam.Worksheet.OwnerUserID = ""
$fparam.Worksheet.OwnerUserName = ""
$fparam.Worksheet.Role = $SFRole
$fparam.Worksheet.Warehouse = $SFWarehouse
$fparam.Worksheet.FileSystemSafeName = ""
$fparam.Worksheet.AccountName = ""
$fparam.Worksheet.AccountFullName = ""
$fparam.Worksheet.AccountUrl = ""
$fparam.Worksheet.OrganizationID = ""
$fparam.Worksheet.Region = ""
$fparam.Worksheet.Database = $SFDatabase
$fparam.Worksheet.Schema = $SFSchema
$fparam.Role = $SFRole
$fparam.Warehouse = $SFWarehouse
$fparam.Configuration.context.role = $SFRole
$fparam.Configuration.context.warehouse = $SFWarehouse
$fparam.Database = $SFDatabase
$fparam.Schema = $SFSchema
$fparam.Configuration.context.database = $SFDatabase
$fparam.Configuration.context.schema = $SFSchema
$fparam.FileSystemSafeName = ""
$fparam.AccountName = ""
$fparam.AccountFullName = ""
$fparam.AccountUrl = ""
$fparam.OrganizationID = ""
$fparam.Region = ""
}
}
if ($OutputDirectory) {
if (-Not (Test-Path $OutputDirectory/filters)) {
# Create the directory if not exists
New-Item $OutputDirectory/filters -ItemType Directory
Write-Host "Created path $OutputDirectory/filters"
$fparam | ConvertTo-JSON -depth 10| Out-File -FilePath $OutputDirectory/filters/$fname
} else {
$fparam | ConvertTo-JSON -depth 10| Out-File -FilePath $OutputDirectory/filters/$fname
}
}
else {
$fparam | ConvertTo-JSON -depth 10| set-content $f.FullName
}
}
}
else { Write-Host "-FiltersPath was not entered"}
}
function Update-Dashboards ()
{
if($DashboardsPath) {
# Update DASHBOARD files
foreach ($f in $tmp_dashboards){
$fparam = Get-Content $f.FullName | ConvertFrom-JSON
$fname = $f.Name
$fparam.update | % {
Write-Host 'Updating DASHBOARD values for' -ForegroundColor Cyan
Write-Host $f.Name -ForegroundColor Yellow
$fparam.OwnerUserID = ""
$fparam.OwnerUserName = ""
$fparam.URL = ""
$fparam.Role = $SFRole
$fparam.Warehouse = $SFWarehouse
$fparam.Database = $SFDatabase
$fparam.Schema = $SFSchema
$fparam.Database = $SFDatabase
$fparam.Schema = $SFSchema
$fparam.FileSystemSafeName = ""
$fparam.AccountName = ""
$fparam.AccountFullName = ""
$fparam.AccountUrl = ""
$fparam.OrganizationID = ""
$fparam.Region = ""
$fparam.Contents.context.role = $SFRole
$fparam.Contents.context.warehouse = $SFWarehouse
$fparam.Contents.context.database = $SFDatabase
$fparam.Contents.context.schema = $SFSchema
$fparam.URL = ""
foreach ($worksheet in $fparam.Worksheets) {
$worksheet.OwnerUserID = ""
$worksheet.OwnerUserName = ""
$worksheet.URL = ""
$worksheet.Role = $SFRole
$worksheet.Warehouse = $SFWarehouse
$worksheet.Database = $SFDatabase
$worksheet.Schema = $SFSchema
$worksheet.FileSystemSafeName = ""
$worksheet.AccountName = ""
$worksheet.AccountFullName = ""
$worksheet.AccountUrl = ""
$worksheet.OrganizationID = ""
$worksheet.Region = ""
$worksheet.URL = ""
#CHARTS
foreach ($chart in $fparam.Worksheets.Charts) {
$chart.AccountName = ""
$chart.AccountFullName = ""
$chart.AccountUrl = ""
$chart.OrganizationID = ""
$chart.Region = ""
}
}
}
if ($OutputDirectory) {
if (-Not (Test-Path $OutputDirectory/dashboards)) {
# Create the directory if not exists
New-Item $OutputDirectory/dashboards -ItemType Directory
Write-Host "Created path $OutputDirectory/dashboards"
$fparam | ConvertTo-JSON -depth 10| Out-File -FilePath $OutputDirectory/dashboards/$fname
}
else {
$fparam | ConvertTo-JSON -depth 10| Out-File -FilePath $OutputDirectory/dashboards/$fname
}
} else {
$fparam | ConvertTo-JSON -depth 10| Set-Content $f.FullName
}
}
}
else { Write-Host "-DashboardsPath was not entered"}
}
function Update-Worksheets ()
{
if($WorksheetsPath) {
# Update WORKSHEET files
foreach ($f in $tmp_worksheets){
$fparam = Get-Content $f.FullName | ConvertFrom-JSON
$fname = $f.Name
$fparam.update | % {
Write-Host 'Updating WORKSHEET values for' -ForegroundColor Cyan
Write-Host $f.Name -ForegroundColor Yellow
$fparam.OwnerUserID = ""
$fparam.OwnerUserName = ""
$fparam.Role = $SFRole
$fparam.Warehouse = $SFWarehouse
$fparam.Database = $SFDatabase
$fparam.Schema = $SFSchema
$fparam.FileSystemSafeName = ""
$fparam.AccountName = ""
$fparam.AccountFullName = ""
$fparam.AccountUrl = ""
$fparam.OrganizationID = ""
$fparam.Region = ""
$fparam.URL = ""
}
if ($OutputDirectory) {
if (-Not (Test-Path $OutputDirectory/worksheets)) {
# Create the directory if not exists
New-Item $OutputDirectory/worksheets -ItemType Directory
Write-Host "Created path $OutputDirectory/worksheets"
$fparam | ConvertTo-JSON -depth 10| Out-File -FilePath $OutputDirectory/worksheets/$fname
} else {
$fparam | ConvertTo-JSON -depth 10| Out-File -FilePath $OutputDirectory/worksheets/$fname
}
}
else {
$fparam | ConvertTo-JSON -depth 10| set-content $f.FullName
}
}
}
else { Write-Host "-WorksheetsPath was not entered"}
}