forked from kunal333/E2ESynapseDemo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsetup.ps1
505 lines (429 loc) · 18.5 KB
/
setup.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
$currentTime = Get-Date
Write-Host "Script started time:" + $currentTime
#Connect-AzAccount
$path = Get-Location
$getDate = Get-Date -Format "MMddyyyy"
$location = "westus3"
$UPN = (Get-AzContext).Account.Id
$principalId = (Get-AzAdUser -UserPrincipalName $UPN).Id
# Get User Input
$resourceCheck = Read-Host "Do you want to use any existing resource(s) or create all resources from scratch? Enter 1 for NEW or 2 for EXISTING"
if ($resourceCheck -eq 1) {
# Set Variables
$resourceGroupName = Read-Host "Enter New Resource Group Name"
#Set default variables
$workspaceName = $resourceGroupName.ToLower()+ $getDate+"ws"
$defaultDataLakeStorageAccountName = $resourceGroupName.ToLower() + ((97..122) | Get-Random -Count 1 | % {[char]$_})
$DataFactoryName = $resourceGroupName.ToLower()+ $getDate+"adf"
$sqlpoolName = "sqlpool1"
}
elseif ($resourceCheck -eq 2) {
$resourceGroupName = Read-Host "Enter Resource Group Name"
$defaultDataLakeStorageAccountName = Read-Host "Enter Azure Blob Storage Name"
$DataFactoryName = Read-Host "Enter Data Factory Name"
$workspaceName = Read-Host "Enter Synapse Workspace Name"
$sqlpoolName = Read-Host "Enter Synapse Dedicated Pool Name"
while ($true) {
$rgInstance = Get-AzResourceGroup -Name $resourceGroupName -ErrorAction SilentlyContinue
if ($rgInstance){
break
}
else {
Write-Host $resourceGroupName "Resource Group Name does't exists."
$check = Read-Host "Do you want to create a new one with this name? Enter 1 for YES 2 for NO"
if ($check -eq 1){
break
}
$resourceGroupName = Read-Host "Enter Existing Resource Group Name"
}
}
while ($true) {
$defaultDataLakeStorageAccountName = $defaultDataLakeStorageAccountName.ToLower()
$storageInstance = Get-AzStorageAccount -ResourceGroupName $resourceGroupName -Name $defaultDataLakeStorageAccountName -ErrorAction SilentlyContinue
if ($storageInstance)
{
break
}
else {
Write-Host $defaultDataLakeStorageAccountName "Storage Name does't exists."
$check = Read-Host "Do you want to create a new one with this name? Enter 1 for YES 2 for NO"
if ($check -eq 1){
break
}
$defaultDataLakeStorageAccountName = Read-Host "Enter Existing Storage Name"
}
}
while ($true) {
$DataFactoryName = $DataFactoryName.ToLower()
$adfInstance = Get-AzDataFactoryV2 -ResourceGroupName $resourceGroupName -Name $DataFactoryName -ErrorAction SilentlyContinue
if ($adfInstance){
break
}
else {
Write-Host $DataFactoryName "Data Factory Name does't exists."
$check = Read-Host "Do you want to create a new one with this name? Enter 1 for YES 2 for NO"
if ($check -eq 1){
break
}
$DataFactoryName = Read-Host "Enter Existing Data Factory Name"
}
}
while ($true) {
$workspaceName = $workspaceName.ToLower()
$wsInstance = Get-AzSynapseWorkspace -ResourceGroupName $resourceGroupName -Name $workspaceName -ErrorAction SilentlyContinue
if ($wsInstance){
break
}
else {
Write-Host $workspaceName "Synapse Analytics Workspace Name does't exists."
$check = Read-Host "Do you want to create a new one with this $workspaceName name? Enter 1 for YES 2 for NO"
if ($check -eq 1){
break
}
$workspaceName = Read-Host "Enter Existing Synapse Analytics Workspace Name"
}
}
while ($true) {
$sqlpoolName = $sqlpoolName.ToLower()
$wspoolInstance = Get-AzSynapseSqlPool -ResourceGroupName $resourceGroupName -WorkspaceName $workspaceName -Name $sqlpoolName -ErrorAction SilentlyContinue
if ($wspoolInstance){
break
}
else {
Write-Host $sqlpoolName "Synapse Dedicated SQL Pool Name does't exists."
$check = Read-Host "Do you want to create a new one with this $sqlpoolName name? Enter 1 for YES 2 for NO"
if ($check -eq 1){
break
}
$sqlpoolName = Read-Host "Enter Existing Synapse Dedicated SQL Pool Name"
}
}
}
else {
Write-Host "Enter either 1 or 2"
}
#Set default variables
$defaultDataLakeStorageFilesystemName = $resourceGroupName.ToLower()+$getDate+"ws"
Write-Host "Default Synapse Admin Username is: sqladminuser"
$sqlAdministratorLoginPassword = Read-Host "Enter Synapse Admin Password" #-assecurestring
# CREATE Resource Group
New-AzResourceGroup -Name $resourceGroupName -Location $location -Force
# CREATE Storage
$StorageParametersVariable = @"
{
"$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentParameters.json#",
"contentVersion": "1.0.0.0",
"parameters": {
"defaultDataLakeStorageAccountName": {
"value": "$defaultDataLakeStorageAccountName"
},
"storageSKU": {
"value": "Standard_RAGRS"
},
"workspaceName": {
"value": "$workspaceName"
}
}
}
"@
Set-Content ParametersStorage.json $StorageParametersVariable
$StorageTemplateFilePath = "./deployStorage.json"
$StorageTemplateParameterFilePath = "./ParametersStorage.json"
New-AzResourceGroupDeployment -ResourceGroupName $resourceGroupName -TemplateFile $StorageTemplateFilePath -TemplateParameterFile $StorageTemplateParameterFilePath
Start-Sleep -Seconds 10
# CREATE Synapse Workspace and SQL Pool
$SynapseWorkspaceParametersVariable = @"
{
"$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentParameters.json#",
"contentVersion": "1.0.0.0",
"parameters": {
"name": {
"value": "$workspaceName"
},
"location": {
"value": "$location"
},
"defaultDataLakeStorageAccountName": {
"value": "$defaultDataLakeStorageAccountName"
},
"defaultDataLakeStorageFilesystemName": {
"value": "$defaultDataLakeStorageFilesystemName"
},
"sqlAdministratorLogin": {
"value": "sqladminuser"
},
"sqlAdministratorLoginPassword": {
"value": "$sqlAdministratorLoginPassword"
},
"setWorkspaceIdentityRbacOnStorageAccount": {
"value": true
},
"allowAllConnections": {
"value": true
},
"grantWorkspaceIdentityControlForSql": {
"value": "Enabled"
},
"managedVirtualNetwork": {
"value": ""
},
"tagValues": {
"value": {}
},
"storageSubscriptionID": {
"value": ""
},
"storageResourceGroupName": {
"value": ""
},
"storageLocation": {
"value": ""
},
"storageRoleUniqueId": {
"value": ""
},
"adlaResourceId": {
"value": ""
},
"storageAccessTier": {
"value": "Hot"
},
"storageKind": {
"value": "StorageV2"
},
"storageAccountType": {
"value": "Standard_RAGRS"
},
"storageSupportsHttpsTrafficOnly": {
"value": true
},
"storageKind": {
"value": "StorageV2"
},
"storageIsHnsEnabled": {
"value": true
},
"userObjectId": {
"value": "$principalId"
},
"setSbdcRbacOnStorageAccount": {
"value": true
}
}
}
"@
Set-Content ParametersSynapseWorkspace.json $SynapseWorkspaceParametersVariable
$SynapseTemplateFilePath = "./deploySynapseWorkspace.json"
$SynapseTemplateParameterFilePath = "./ParametersSynapseWorkspace.json"
New-AzResourceGroupDeployment -ResourceGroupName $resourceGroupName -TemplateFile $SynapseTemplateFilePath -TemplateParameterFile $SynapseTemplateParameterFilePath
# CREATE ADF
Set-AzDataFactoryV2 -ResourceGroupName $resourcegroupname -Name $DataFactoryName -Location $location -Force
Start-Sleep -Seconds 20
# CREATE SQL Pool
$SQLPoolParametersVariable = @"
{
"$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentParameters.json#",
"contentVersion": "1.0.0.0",
"parameters": {
"name": {
"value": "$workspaceName"
},
"location": {
"value": "$location"
},
"sqlpoolName": {
"value": "$sqlpoolName"
},
"sku": {
"Value":"DW100c"
}
}
}
"@
Set-Content ParametersSQLPool.json $SQLPoolParametersVariable
$SQLPoolTemplateFilePath = "./deploySqlPool.json"
$SQLPoolTemplateParameterFilePath = "./ParametersSQLPool.json"
New-AzResourceGroupDeployment -ResourceGroupName $resourceGroupName -TemplateFile $SQLPoolTemplateFilePath -TemplateParameterFile $SQLPoolTemplateParameterFilePath
# CREATE FUNCTIONS
Function Get-StorageKey {
Write-Host "Getting Storage Access Key"
$script:storageKey1 = (Get-AzStorageAccountKey -ResourceGroupName $resourceGroupName -AccountName $defaultDataLakeStorageAccountName -ListKerbKey)[0].Value
Write-Host $script:storageKey1
}
Function Get-ConnectionString {
Write-Host "Getting Connection String for the Synapse Pool"
# $script:SQLPoolconnectionString = "data source="+$servername+".database.windows.net;Initial Catalog="+$database+";Encrypt=True;Connection Timeout=30;"
$script:SQLPoolconnectionString = "data source="+$workspaceName+".sql.azuresynapse.net;Initial Catalog=sqlpool1;Persist Security Info=False;User ID=sqladminuser;Password="+$sqlAdministratorLoginPassword+";MultipleActiveResultSets=False;Encrypt=True;TrustServerCertificate=False;Connection Timeout=30;"
Write-Host $script:SQLPoolconnectionString
}
Function Set-ParametersFile {
Write-Host "Creating Parameters File for the Synapse Integrate ARM Template"
$MyJsonVariable = @"
{
"`$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentParameters.json#",
"contentVersion": "1.0.0.0",
"parameters": {
"factoryName": {
"value": "$DataFactoryName"
},
"AzureDataLakeStorage1_accountKey": {
"value": "$storageKey1"
},
"cmsdemopool_connectionString": {
"value": "$SQLPoolconnectionString"
},
"AzureDataLakeStorage1_properties_typeProperties_url": {
"value": "https://$defaultDataLakeStorageAccountName.dfs.core.windows.net/"
}
}
}
"@
Set-Content ParametersARMTemplate.json $MyJsonVariable
}
Function Set-DeployADFARMTemplate {
Write-Host "Deploying ADF ARM Template with the new Parameters File"
$templateFile = "$path/DeployADFARMTemplate.json"
$parameterFile="$path/ParametersARMTemplate.json"
New-AzResourceGroupDeployment `
-Name $DataFactoryName `
-ResourceGroupName $resourceGroupName `
-TemplateFile $templateFile `
-TemplateParameterFile $parameterFile
}
Function Get-CMSData {
Write-Host "Downloading CMS data from website and saving into ADLS"
Write-Host "Updating IntegrationRuntime TTL to 10 minutes and CoreCount to 16"
Set-AzDataFactoryV2IntegrationRuntime -ResourceGroupName $resourceGroupName -DataFactoryName $DataFactoryName -Name 'AutoResolveIntegrationRuntime' -DataFlowTimeToLive 10 -Type 'Managed' -Location 'AutoResolve' -DataFlowCoreCount 16 -DataFlowComputeType 'General' -ErrorAction SilentlyContinue -Force
$TablesList2 = @("Download_CMSPart13","Download_CMSPart14","Download_CMSPart15","Download_CMSPart16","Download_CMSPart17","Download_CMSPart18")
$myarray2 = [System.Collections.ArrayList]::new()
Foreach ($i in $TablesList2)
{
Write-Host "Loading Table: $i"
$runId = Invoke-AzDataFactoryV2Pipeline -ResourceGroupName $resourceGroupName -DataFactoryName $DataFactoryName -PipelineName $i
$myArray2.Add($runId)
}
# foreach ($element in $myArray1) {$element}
foreach ($element in $myArray2) {
while ($True) {
$run = Get-AzDataFactoryV2PipelineRun -ResourceGroupName $resourceGroupName -DataFactoryName $DataFactoryName -PipelineRunId $element
if ($run) {
if ($run.Status -ne 'InProgress') {
Write-Host "Pipeline run finished. The status is: " $run.Status -foregroundcolor "Green"
$run
break
}
Write-Host "ADF Pipeline is still running... Will check progress in 15 seconds" -foregroundcolor "Yellow"
}
Start-Sleep -Seconds 15
}
}
$newRunId = Invoke-AzDataFactoryV2Pipeline -ResourceGroupName $resourceGroupName -DataFactoryName $DataFactoryName -PipelineName "uncompress_CMS_Files"
while ($True) {
$run = Get-AzDataFactoryV2PipelineRun -ResourceGroupName $resourceGroupName -DataFactoryName $DataFactoryName -PipelineRunId $newRunId
if ($run) {
if ($run.Status -ne 'InProgress') {
Write-Host "Pipeline run finished. The status is: " $run.Status -foregroundcolor "Yellow"
$run
break
}
Write-Host "ADF Pipeline is still running... Will check progress in 15 seconds" -foregroundcolor "Yellow"
}
Start-Sleep -Seconds 15
}
Write-Host "All CMS files needed for the demo saved in the Storage Account: $storageName"
}
Function Set-SynapseDDLs {
Write-Host "Creating Tables and Views in Synapse"
$synapseSqlName = $workspaceName+".sql.azuresynapse.net"
Invoke-Sqlcmd -InputFile "$path/synapseCMSddls.sql" -ServerInstance $synapseSqlName -Database "sqlpool1" -Username "sqladminuser" -Password $sqlAdministratorLoginPassword
}
Function Set-LoadSynapseTables {
Write-Host "Executing ADF Pipelines that loades into Synapse the CMS Data from Storage "
Write-Host "Updating IntegrationRuntime TTL to 10 minutes and CoreCount to 16"
Set-AzDataFactoryV2IntegrationRuntime -ResourceGroupName $resourceGroupName -DataFactoryName $DataFactoryName -Name 'AutoResolveIntegrationRuntime' -DataFlowTimeToLive 10 -Type 'Managed' -Location 'AutoResolve' -DataFlowCoreCount 16 -DataFlowComputeType 'General' -ErrorAction SilentlyContinue -Force
$TablesList1 = @("Drug","Providers","Specialty","States")
$myarray1 = [System.Collections.ArrayList]::new()
Foreach ($i in $TablesList1)
{
Write-Host "Loading Table: $i"
$runId = Invoke-AzDataFactoryV2Pipeline -ResourceGroupName $resourceGroupName -DataFactoryName $DataFactoryName -PipelineName $i
$myArray1.Add($runId)
}
# foreach ($element in $myArray1) {$element}
foreach ($element in $myArray1) {
while ($True) {
$run = Get-AzDataFactoryV2PipelineRun -ResourceGroupName $resourceGroupName -DataFactoryName $DataFactoryName -PipelineRunId $element
if ($run) {
if ($run.Status -ne 'InProgress') {
Write-Host "Pipeline run finished. The status is: " $run.Status -foregroundcolor "Green"
$run
break
}
Write-Host "ADF Pipeline is still running... Will check progress in 60 seconds" -foregroundcolor "Yellow"
}
Start-Sleep -Seconds 60
}
}
$TablesList2 = @("Geography")
$myarray2 = [System.Collections.ArrayList]::new()
Foreach ($i in $TablesList2)
{
Write-Host "Loading Table: $i"
$runId = Invoke-AzDataFactoryV2Pipeline -ResourceGroupName $resourceGroupName -DataFactoryName $DataFactoryName -PipelineName $i
$myArray2.Add($runId)
}
foreach ($element in $myArray2) {$element}
foreach ($element in $myArray2) {
while ($True) {
$run = Get-AzDataFactoryV2PipelineRun -ResourceGroupName $resourceGroupName -DataFactoryName $DataFactoryName -PipelineRunId $element
if ($run) {
if ($run.Status -ne 'InProgress') {
Write-Host "Pipeline run finished. The status is: " $run.Status -foregroundcolor "Green"
$run
break
}
Write-Host "ADF Pipeline is still running... Will check progress in 60 seconds" -foregroundcolor "Yellow"
}
Start-Sleep -Seconds 60
}
}
$TablesList3 = @("Details")
$myarray3 = [System.Collections.ArrayList]::new()
Foreach ($i in $TablesList3)
{
Write-Host "Loading Table: $i"
$runId = Invoke-AzDataFactoryV2Pipeline -ResourceGroupName $resourceGroupName -DataFactoryName $DataFactoryName -PipelineName $i
$myArray3.Add($runId)
}
foreach ($element in $myArray3) {Write-Host "Pipeline IDs as: $element"}
foreach ($element in $myArray3) {
while ($True) {
$run = Get-AzDataFactoryV2PipelineRun -ResourceGroupName $resourceGroupName -DataFactoryName $DataFactoryName -PipelineRunId $element
if ($run) {
if ($run.Status -ne 'InProgress') {
Write-Host "Pipeline run finished. The status is: " $run.Status -foregroundcolor "Green"
$run
break
}
Write-Host "ADF Pipeline is still running... Will check progress in 5 minutes" -foregroundcolor "Yellow"
}
Start-Sleep -Seconds 300
}
}
Write-Host "All Tables loaded to Synapse Instance: $database"
}
Function Cleanup
{
Remove-Item Parameters*.json
}
# Call Functions
Get-StorageKey
Get-ConnectionString
Set-ParametersFile
Set-DeployADFARMTemplate
Get-CMSData
Set-SynapseDDLs
Set-LoadSynapseTables
Cleanup
$currentTime = Get-Date
Write-Host "Script End time:" + $currentTime