Skip to content

Commit 4f39acf

Browse files
committed
Fixed bug #131 and few SQLDatabase functions
Added -NoWait for New-FabricSQLDatabase function #123
1 parent c7f0638 commit 4f39acf

9 files changed

+196
-170
lines changed

CHANGELOG.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,22 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
77

88
### Added
99
- Added credits for authors to all functions and Unit tests to verify the existence of such tags #89
10+
- Added `NoWait` switch parameter to `New-FabricSQLDatabase` (#123)
1011

1112
### Changed
1213
- Get-FabricSqlDatabase accepts Workspace as a pipeline, handles errors correctly and can filter by name (#117).
14+
- Applied splatting for several parameters in `Invoke-FabricRestMethod` and output results in debug mode
15+
- `Remove-FabricSQLDatabase` uses unified function to handle API results
1316

1417
### Fixed
18+
- Enhanced logic in unified function `Test-FabricApiResponse` to handle API results and moved it to private functions
19+
- Fixed bug in `Get-FabricLongRunningOperation` - Uri was incorectly created (#131)
20+
- Fixed bug in `Get-FabricLongRunningOperationResult` - uses correct statusCode (#131)
21+
1522
### Deprecated
1623
### Removed
24+
- Removed Revision History from `Get-FabricSQLDatabase`, `Get-FabricSQLDatabase`
25+
1726
### Security
1827

1928
## 0.22.0 - 20250609
Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
function Test-FabricApiResponse {
2+
3+
<#
4+
.SYNOPSIS
5+
Tests the response from a Fabric API call and handles long-running operations.
6+
7+
.DESCRIPTION
8+
Tests the response from a Fabric API call and handles long-running operations. It checks the status code and processes the response accordingly.
9+
10+
.PARAMETER statusCode
11+
The HTTP status code returned from the API call.
12+
13+
.PARAMETER response
14+
The response body from the API call.
15+
16+
.PARAMETER responseHeader
17+
The response headers from the API call.
18+
19+
.PARAMETER Name
20+
The name of the resource being operated.
21+
22+
.PARAMETER typeName
23+
The type of resource being operated (default: 'Fabric Item').
24+
25+
.PARAMETER NoWait
26+
If specified, the function will not wait for the operation to complete and will return immediately.
27+
28+
.EXAMPLE
29+
Test-FabricApiResponse -statusCode 201 -response $response -responseHeader $header -Name "MyResource" -typeName "Fabric Item"
30+
31+
Handles the response from a Fabric API call with a 201 status code, indicating successful creation of a resource.
32+
33+
.EXAMPLE
34+
Test-FabricApiResponse -statusCode 202 -response $response -responseHeader $header -Name "MyResource" -typeName "Fabric Item"
35+
36+
Handles the response from a Fabric API call with a 202 status code, indicating that the request has been accepted for processing.
37+
38+
.NOTES
39+
- This function is designed to be used within the context of a Fabric API client.
40+
- It requires the `Write-Message` function to log messages at different levels (Info, Debug, Error).
41+
- The function handles long-running operations by checking the status of the operation and retrieving the result if it has succeeded.
42+
43+
Author: Kamil Nowinski
44+
45+
#>
46+
47+
[CmdletBinding()]
48+
param (
49+
[Parameter(Mandatory = $false)]
50+
$response,
51+
52+
[Parameter(Mandatory = $false)]
53+
$Name,
54+
55+
[Parameter(Mandatory = $false)]
56+
$typeName = 'Fabric Item',
57+
58+
[Parameter(Mandatory = $false)]
59+
[switch] $NoWait = $false
60+
)
61+
62+
$responseHeader = $script:responseHeader
63+
$statusCode = $script:statusCode
64+
65+
$verb = (Get-PSCallStack)[1].Command.Split('-')[0]
66+
Write-Message -Message "Testing API response for '$verb' operation. StatusCode: $statusCode." -Level Debug
67+
68+
switch ($statusCode) {
69+
200 {
70+
return $null
71+
}
72+
201 {
73+
switch ($verb) {
74+
'New' { $msg = "$typeName '$Name' created successfully!" }
75+
'Update' { $msg = "$typeName '$Name' updated successfully!" }
76+
'Remove' { $msg = "$typeName '$Name' deleted successfully!" }
77+
default { $msg = "Received 201 status code for $verb operation on $typeName '$Name'" }
78+
}
79+
Write-Message -Message $msg -Level Info
80+
return $response
81+
}
82+
202 {
83+
Write-Message -Message "$verb Request for $typeName '$Name' accepted. Provisioning in progress!" -Level Info
84+
[string]$operationId = $responseHeader["x-ms-operation-id"]
85+
86+
if ($NoWait) {
87+
Write-Message -Message "NoWait parameter is set. Operation ID: $operationId" -Level Info
88+
Write-Message -Message "Run to check the progress: Get-FabricLongRunningOperationResult -operationId '$operationId'" -Level Verbose
89+
return $responseHeader
90+
}
91+
92+
Write-Message -Message "[Test-FabricApiResponse] Operation ID: '$operationId'" -Level Debug
93+
Write-Message -Message "[Test-FabricApiResponse] Getting Long Running Operation status" -Level Debug
94+
95+
$operationStatus = Get-FabricLongRunningOperation -operationId $operationId
96+
Write-Message -Message "[Test-FabricApiResponse] Long Running Operation status: $operationStatus" -Level Debug
97+
# Handle operation result
98+
if ($operationStatus.status -eq "Succeeded") {
99+
Write-Message -Message "Operation Succeeded" -Level Debug
100+
Write-Message -Message "Getting Long Running Operation result" -Level Debug
101+
102+
$operationResult = Get-FabricLongRunningOperationResult -operationId $operationId
103+
Write-Message -Message "Long Running Operation status: $operationResult" -Level Debug
104+
105+
return $operationResult
106+
} else {
107+
Write-Message -Message "Operation failed. Status: $($operationStatus)" -Level Debug
108+
Write-Message -Message "Operation failed. Status: $($operationStatus)" -Level Error
109+
return $operationStatus
110+
}
111+
}
112+
default {
113+
Write-Message -Message "Test-FabricApiResponse::default" -Level Debug
114+
Write-Message -Message "Unexpected response code: $statusCode from the API." -Level Error
115+
Write-Message -Message "Error: $($response.message)" -Level Error
116+
if ($response.moreDetails) {
117+
Write-Message -Message "More Details: $($response.moreDetails)" -Level Error
118+
}
119+
Write-Message "Error Code: $($response.errorCode)" -Level Error
120+
throw "API request failed with status code $statusCode."
121+
}
122+
}
123+
124+
}

source/Public/Invoke-FabricRestMethod.ps1

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,8 @@ Function Invoke-FabricRestMethod {
5555
[switch] $PowerBIApi
5656
)
5757

58+
Write-Message -Message "[Invoke-FabricRestMethod]::Begin" -Level Debug
59+
5860
if ($TestTokenExpired) {
5961
Confirm-TokenState
6062
}
@@ -73,26 +75,31 @@ Function Invoke-FabricRestMethod {
7375

7476
if ($Body -is [hashtable]) {
7577
$Body = $Body | ConvertTo-Json -Depth 10
76-
Write-Message -Message "Request Body: $Body" -Level Debug
78+
Write-Message -Message "[Invoke-FabricRestMethod] Request Body: $Body" -Level Debug
7779
} elseif ($Body -is [string]) {
78-
Write-Message -Message "Request Body: $Body" -Level Debug
80+
Write-Message -Message "[Invoke-FabricRestMethod] Request Body: $Body" -Level Debug
7981
} else {
80-
Write-Message -Message "No request body provided." -Level Debug
82+
Write-Message -Message "[Invoke-FabricRestMethod] No request body provided." -Level Debug
8183
}
8284

83-
$response = Invoke-RestMethod `
84-
-Headers $FabricSession.HeaderParams `
85-
-Uri $Uri `
86-
-Method $Method `
87-
-Body $Body `
88-
-ContentType "application/json" `
89-
-ErrorAction 'Stop' `
90-
-SkipHttpErrorCheck `
91-
-ResponseHeadersVariable "responseHeader" `
92-
-StatusCodeVariable "statusCode"
85+
$request = @{
86+
Headers = $FabricSession.HeaderParams
87+
Uri = $Uri
88+
Method = $Method
89+
Body = $Body
90+
ContentType = "application/json"
91+
ErrorAction = 'Stop'
92+
SkipHttpErrorCheck = $true
93+
ResponseHeadersVariable = "responseHeader"
94+
StatusCodeVariable = "statusCode"
95+
}
96+
$response = Invoke-RestMethod @request
9397

98+
Write-Message -Message "[Invoke-FabricRestMethod] Result response code: $statusCode" -Level Debug
99+
Write-Message -Message "[Invoke-FabricRestMethod] Result return: $response" -Level Debug
94100
$script:statusCode = $statusCode
95101
$script:responseHeader = $responseHeader
96102

103+
Write-Message -Message "[Invoke-FabricRestMethod]::End" -Level Debug
97104
return $response
98105
}

source/Public/SQL Database/Get-FabricSQLDatabase.ps1

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -54,10 +54,6 @@ function Get-FabricSQLDatabase
5454
Returns the details of the Fabric SQL Databases in the MsLearn-dev workspace.
5555
5656
.NOTES
57-
Revision History:
58-
- 2025-03-06 - KNO: Init version of the function
59-
- 2025-06-14 - Update the examples to remove backticks
60-
6157
Author: Kamil Nowinski
6258
6359
#>
@@ -89,7 +85,6 @@ function Get-FabricSQLDatabase
8985
return
9086
}
9187

92-
9388
}
9489

9590
process
@@ -101,23 +96,15 @@ function Get-FabricSQLDatabase
10196
}
10297

10398
# Create SQLDatabase API
104-
$uri = "$($FabricSession.BaseApiUrl)/workspaces/$WorkspaceId/sqlDatabases"
99+
$uri = "workspaces/$WorkspaceId/sqlDatabases"
105100
if ($SQLDatabaseId)
106101
{
107102
$uri = "$uri/$SQLDatabaseId"
108103
}
109104
$response = Invoke-FabricRestMethod -Uri $uri
110-
##$databases.Where({$_.displayName -eq $body.displayName}).id
111105

112106
# Step: Validate the response code
113-
if ($statusCode -ne 200)
114-
{
115-
Write-Message -Message "Unexpected response code: $statusCode from the API." -Level Error
116-
Write-Message -Message "Error: $($response.message)" -Level Error
117-
Write-Message -Message "Error Details: $($response.moreDetails)" -Level Error
118-
Write-Message "Error Code: $($response.errorCode)" -Level Error
119-
return $null
120-
}
107+
Test-FabricApiResponse -response $response -Name $SQLDatabaseId -typeName "SQL Database"
121108

122109
$response = $response.value
123110
if ($SQLDatabaseName)

source/Public/SQL Database/New-FabricSQLDatabase.ps1

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,28 @@
11
<#
22
.SYNOPSIS
3-
Creates a new SQL Database in a specified Microsoft Fabric workspace.
3+
Creates a new SQL Database in a specified Microsoft Fabric workspace.
44
55
.DESCRIPTION
6-
This function sends a POST request to the Microsoft Fabric API to create a new SQL Database
7-
in the specified workspace. It supports optional parameters for SQL Database description
8-
and path definitions for the SQL Database content.
6+
This function sends a POST request to the Microsoft Fabric API to create a new SQL Database
7+
in the specified workspace. It supports optional parameters for SQL Database description
8+
and path definitions for the SQL Database content.
99
1010
.PARAMETER WorkspaceId
11-
The unique identifier of the workspace where the SQL Database will be created.
11+
The unique identifier of the workspace where the SQL Database will be created.
1212
1313
.PARAMETER Name
14-
The name of the SQL Database to be created.
14+
The name of the SQL Database to be created.
1515
1616
.PARAMETER Description
17-
An optional description for the SQL Database.
17+
An optional description for the SQL Database.
1818
19+
.PARAMETER NoWait
20+
If specified, the function will not wait for the operation to complete and will return immediately.
1921
2022
.EXAMPLE
21-
New-FabricSQLDatabase -WorkspaceId "workspace-12345" -Name "NewDatabase"
23+
New-FabricSQLDatabase -WorkspaceId "workspace-12345" -Name "NewDatabase"
2224
23-
.NOTES
24-
- Requires `$FabricConfig` global configuration, including `BaseUrl` and `FabricHeaders`.
25+
.NOTES
2526
- Calls `Confirm-TokenState` to ensure token validity before making the API request.
2627
2728
Author: Kamil Nowinski
@@ -42,7 +43,10 @@ function New-FabricSQLDatabase
4243

4344
[Parameter(Mandatory = $false)]
4445
[ValidateNotNullOrEmpty()]
45-
[string]$Description
46+
[string]$Description,
47+
48+
[Parameter(Mandatory = $false)]
49+
[switch]$NoWait = $false
4650
)
4751

4852
try
@@ -51,7 +55,7 @@ function New-FabricSQLDatabase
5155
Confirm-TokenState
5256

5357
# Step 2: Construct the API URL
54-
$apiEndpointUrl = "{0}/workspaces/{1}/sqldatabases" -f $FabricConfig.BaseUrl, $WorkspaceId
58+
$apiEndpointUrl = "workspaces/{0}/sqldatabases" -f $WorkspaceId
5559
Write-Message -Message "API Endpoint: $apiEndpointUrl" -Level Debug
5660

5761
# Step 3: Construct the request body
@@ -67,18 +71,14 @@ function New-FabricSQLDatabase
6771
$bodyJson = $body | ConvertTo-Json -Depth 10
6872
Write-Message -Message "Request Body: $bodyJson" -Level Debug
6973

74+
# Step 4: Make the API request
7075
if ($PSCmdlet.ShouldProcess($apiEndpointUrl, "Create SQL Database"))
7176
{
72-
73-
# Step 4: Make the API request
74-
$response = Invoke-FabricRestMethod `
75-
-Uri $apiEndpointUrl `
76-
-Method Post `
77-
-Body $bodyJson
77+
$response = Invoke-FabricRestMethod -Uri $apiEndpointUrl -Method Post -Body $bodyJson
7878
}
7979
# Step 5: Handle and log the response
80-
Write-Message "RESPONSE: $response" -Level Debug
81-
Test-FabricApiResponse -response $response -responseHeader $responseHeader -statusCode $statusCode -Name $Name -TypeName 'SQL Database'
80+
Test-FabricApiResponse -response $response -Name $Name -TypeName 'SQL Database' -NoWait:$NoWait
81+
Write-Message -Message "SQL Database '$Name' created successfully!" -Level Info
8282
}
8383
catch
8484
{

source/Public/SQL Database/Remove-FabricSQLDatabase.ps1

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ Remove-FabricSQLDatabas -WorkspaceId "12345" -SQLDatabaseId "67890"
1717
Deletes the SQL Database with ID "67890" from workspace "12345".
1818
1919
.NOTES
20-
- Requires `$FabricConfig` global configuration, including `BaseUrl` and `FabricHeaders`.
2120
- Validates token expiration before making the API request.
2221
2322
Author: Kamil Nowinski
@@ -30,11 +29,11 @@ function Remove-FabricSQLDatabase
3029
param (
3130
[Parameter(Mandatory = $true)]
3231
[ValidateNotNullOrEmpty()]
33-
[string]$WorkspaceId,
32+
[Guid]$WorkspaceId,
3433

3534
[Parameter(Mandatory = $true)]
3635
[ValidateNotNullOrEmpty()]
37-
[string]$SQLDatabaseId
36+
[Guid]$SQLDatabaseId
3837
)
3938

4039
try
@@ -43,28 +42,19 @@ function Remove-FabricSQLDatabase
4342
Confirm-TokenState
4443

4544
# Step 2: Construct the API URL
46-
$apiEndpointUrl = "{0}/workspaces/{1}/sqldatabases/{2}" -f $FabricConfig.BaseUrl, $WorkspaceId, $SQLDatabaseId
45+
$apiEndpointUrl = "workspaces/{0}/sqldatabases/{1}" -f $WorkspaceId, $SQLDatabaseId
4746
Write-Message -Message "API Endpoint: $apiEndpointUrl" -Level Debug
4847

4948
if ($PSCmdlet.ShouldProcess($apiEndpointUrl, "Delete SQL Database"))
5049
{
51-
5250
# Step 3: Make the API request
53-
$response = Invoke-FabricRestMethod `
54-
-Uri $apiEndpointUrl `
55-
-Method Delete
51+
$response = Invoke-FabricRestMethod -Uri $apiEndpointUrl -Method Delete
5652
}
5753

5854
# Step 4: Validate the response code
59-
if ($statusCode -ne 200)
60-
{
61-
Write-Message -Message "Unexpected response code: $statusCode from the API." -Level Error
62-
Write-Message -Message "Error: $($response.message)" -Level Error
63-
Write-Message "Error Code: $($response.errorCode)" -Level Error
64-
return $null
65-
}
66-
Write-Message -Message "SQL Database '$SQLDatabaseId' deleted successfully from workspace '$WorkspaceId'." -Level Info
55+
Test-FabricApiResponse -response $response -Name $SQLDatabaseId -typeName "SQL Database"
6756

57+
Write-Message -Message "SQL Database '$SQLDatabaseId' deleted successfully!" -Level Info
6858
}
6959
catch
7060
{

0 commit comments

Comments
 (0)