-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathcreateEventhouse.ps1
202 lines (179 loc) · 7.68 KB
/
createEventhouse.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
<#
.Description
Deploy Eventhouse, KQL Database, Tables, Functions, and Materialized View in Microsoft Fabric
.LINK
https://blog.fabric.microsoft.com/en-us/blog/using-apis-with-fabric-real-time-analytics/
.EXAMPLE
.\createEventhouse.ps1 -workspaceId 'xxxxx-yyyy-yyyy-aaaa-aaaaaaaaaaaa' -eventhouseName 'Eventhouse_001' -kqlDBName 'KQLDB_001' -dbScriptName 'CreateDB_script.csl' -tenantId 'xxxxxx-yyyy-aaaa-bbbb-aaaaaaaaa'
#>
######################################################################################################################################
## Eventhouse Accelerator
## Author : Surya Teja Josyula
## Created: 2024-06-26
## Modified: , Modified By:
## Make sure Az Modules are installed on your system by running 'Install-Module Az'
## The execution will ask for selectin subscription even though we are running for Fabric which only required tenantId.
## You can set default subscription by running 'Update-AzConfig -DefaultSubscriptionForLogin "Kusto_PM_Experiments"'
######################################################################################################################################
## User Parameters
param (
#Tenant Id
[Parameter(Mandatory=$true)]
$tenantId = "",
#Workspace Id
[Parameter(Mandatory=$true)]
$workspaceId = "",
#Eventhouse Name
[Parameter(Mandatory=$true)]
$eventhouseName = "",
#KQL DB Name
[Parameter(Mandatory=$false)]
$kqlDBName = "",
#File name containing entities creation script
[Parameter(Mandatory=$false)]
$dbScriptName = ""
)
## STEP 1 Connect to Fabric
# Setup parameters
$baseFabricUrl = "https://api.fabric.microsoft.com"
# Login into Fabric
Connect-AzAccount -TenantId $tenantId | Out-Null
# Get authentication
$fabricToken = (Get-AzAccessToken -ResourceUrl $baseFabricUrl).Token
## STEP 2 Create headers for API calls
# Setup headers for API call
$headerParams = @{'Authorization'="Bearer {0}" -f $fabricToken}
$contentType = @{'Content-Type' = "application/json"}
## STEP 3 Get Workspace Details
$workspaceUri = "{0}/v1/workspaces/{1}" -f $baseFabricUrl, $workspaceId
$workspaceDetails = Invoke-RestMethod -Headers $headerParams -ContentType $contentType -Method GET -Uri $workspaceUri
$workspaceName = $workspaceDetails.displayName
## STEP 4 Create Eventhouse
$body = @{
'displayName' = $eventhouseName
} | ConvertTo-Json -Depth 1
$eventhouseAPI = "https://api.fabric.microsoft.com/v1/workspaces/$workspaceId/eventhouses"
# Check if Eventhouse with same name already exists
$eventhouseCheck=Invoke-RestMethod -Headers $headerParams -Method GET -Uri $eventhouseAPI -ContentType "application/json"
if(($eventhouseCheck.value.displayName).Contains($eventhouseName)) {Write-Host "Eventhouse with name $eventhouseName already exists. Please use different name" -ForegroundColor Red EXIT}
# Else continue creation
try {
$eventhouseCreate = Invoke-RestMethod -Headers $headerParams -Method POST -Uri $eventhouseAPI -Body ($body) -ContentType "application/json"
}
catch {
$streamReader = [System.IO.StreamReader]::new($_.Exception.Response.GetResponseStream())
$ErrResp = $streamReader.ReadToEnd() | ConvertFrom-Json
$streamReader.Close()
Exit
}
if ($ErrResp.message)
{
$ErrResp.message
EXIT
}
else {Write-Host "Eventhouse '$eventhouseName' is created in workspace '$workspaceName'" -ForegroundColor Green}
$eventhouseId= ($eventhouseCreate.id).ToString()
## STEP 5 Create KQL Database
if ($null -eq $kqlDBName) { $kqlDBName= $eventhouseName }
else {
$body = @{
'displayName' = $kqlDBName;
'creationPayload'= @{
'databaseType' = "ReadWrite";
'parentEventhouseItemId' = $eventhouseId}
} | ConvertTo-Json -Depth 2
$kqlDBAPI = "https://api.fabric.microsoft.com/v1/workspaces/$workspaceId/kqlDatabases"
# Check if KQL Database with same name already exists
# try{
# Invoke-RestMethod -Headers $headerParams -Method GET -Uri $kqlDBAPI -ContentType "application/json" -verbose
# }
# catch {
# $streamReader = [System.IO.StreamReader]::new($_.Exception.Response.GetResponseStream())
# $ErrResp = $streamReader.ReadToEnd() | ConvertFrom-Json
# $streamReader.Close()
# Exit
# }
# $ErrResp.message
# if(($kqlDBCheck.value.displayName).Contains($kqlDBName)) {Write-Host "KQL Database with name $kqlDBName already exists. Please use different name" -ForegroundColor Red EXIT}
#Else continue creation
try {
Invoke-RestMethod -Headers $headerParams -Method POST -Uri $kqlDBAPI -Body ($body) -ContentType "application/json"
}
catch {
$streamReader = [System.IO.StreamReader]::new($_.Exception.Response.GetResponseStream())
$ErrResp = $streamReader.ReadToEnd() | ConvertFrom-Json
$streamReader.Close()
Exit
}
if ($null -eq $ErrResp.message)
{
Write-Host "KQL Database '$kqlDBName' is created in Eventhouse '$eventhouseName' in workspace '$workspaceName'" -ForegroundColor Green
}
else {$ErrResp.message}
}
## STEP 6 Get Query Uri for the above Eventhouse
$eventhouseDetailsAPI = "$eventhouseAPI/$eventhouseId"
$eventhouseDetails = Invoke-RestMethod -Headers $headerParams -ContentType $contentType -Method GET -Uri $eventhouseDetailsAPI
$queryUri = $eventhouseDetails.properties.queryServiceUri
## STEP 7 Read database script from a file
if($dbScriptName)
{
function Get-ScriptDirectory {
if ($psise) {
Split-Path $psise.CurrentFile.FullPath
}
else {
$PSScriptRoot
}
}
$DBScriptPath=Get-ScriptDirectory
$DBScriptloc = (Join-Path $DBScriptPath $dbScriptName)
try {
$DBScript=Get-content -Path $DBScriptloc
}
catch {
$streamReader = [System.IO.StreamReader]::new($_.Exception.Response.GetResponseStream())
$ErrResp = $streamReader.ReadToEnd() | ConvertFrom-Json
$streamReader.Close()
Exit
}
if ($null -ne $ErrResp.message)
{
$ErrResp.message
}
else
{
## STEP 8 Execute database script to create all entities in the Eventhouse
Write-Host "Waiting for 1 min before running database scripts"
Start-Sleep -Seconds 60
$kustoUrl = "https://api.kusto.windows.net"
# Get authentication
$kustoToken = (Get-AzAccessToken -ResourceUrl $kustoUrl).Token
# Setup headers for API call
$headerParams = @{'Authorization'="Bearer {0}" -f $kustoToken}
$queryAPI = "$queryUri/v1/rest/mgmt"
$DBScript=$DBScript | Out-String
#$DBScript.GetType()
$body = @{
'csl' = $DBScript;
'db'= $kqlDBName
} | ConvertTo-Json -Depth 1
#$body
try {
Invoke-RestMethod -Headers $headerParams -Method POST -Uri $queryAPI -Body ($body) -ContentType "application/json; charset=utf-8"
}
catch {
$streamReader = [System.IO.StreamReader]::new($_.Exception.Response.GetResponseStream())
$ErrResp = $streamReader.ReadToEnd() | ConvertFrom-Json
$streamReader.Close()
Exit
}
$ErrResp.message
if ($null -ne $ErrResp.message)
{
$ErrResp.message
}
else { Write-Host "'$dbScriptName' is executed in '$eventhouseName' Eventhouse " -ForegroundColor Green}
}
}
else {Write-Host "Database script was not provided. No entities were created in the Database $kqlDBName" -ForegroundColor Green}