Skip to content

Commit ed114fe

Browse files
authored
Merge pull request #132 from dataplat/bug-131-and-nowait
Enhanced unified function to handle errors, added `NoWait` and fix endless loop for LRO
2 parents 7b588c0 + 8d70e40 commit ed114fe

12 files changed

+304
-205
lines changed

CHANGELOG.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,19 +9,28 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
99
- added unit tests for `Get-FabricWorkspaceUser` function to ensure it works correctly with multiple workspaces both in the pipeline and passed to a parameter.
1010
- Added unit tests for Aliases for `Get-FabricWorkspaceUser` function to ensure backward compatibility.
1111
- Added credits for authors to all functions and Unit tests to verify the existence of such tags #89
12+
- Added `NoWait` switch parameter to `New-FabricSQLDatabase` (#123)
1213

1314
### Changed
1415
- Updated `Get-FabricWorkspaceUser` to support pipeline input for `WorkspaceId` and `WorkspaceName` parameters.
1516
- Renamed `Get-FabricWorkspaceUsers` to match the singular form
1617
- Get-FabricSqlDatabase accepts Workspace as a pipeline, handles errors correctly and can filter by name (#117).
18+
- Applied splatting for several parameters in `Invoke-FabricRestMethod` and output results in debug mode
19+
- `Remove-FabricSQLDatabase` uses unified function to handle API results
1720

1821
Updated the `WorkspaceId`, `CapacitiesIds`,`CapacityId`,`CopyJobId`,`datamartId`,`DataPipelineId`,`DataWarehouseGUID`,`DomainId`,`EnvironmentId`,`EventhouseId`,`EventstreamId`,`ExternalDataShareId`,`ItemId`,`KQLDashboardId`,`KQLDatabaseId`,`KQLQuerysetId`,`LakehouseId`,`MirroredDatabaseId`,`MirroredWarehouseId`,`MLExperimentId`,`MLModelId`,`NotebookId`,`operationId`,`PaginatedReportId`,`ParentDomainId`,`parentEventhouseId`,`PrincipalId`,`ReflexId`,`ReportId`,`SemanticModelId`,`SparkCustomPoolId`,`SparkJobDefinitionId`,`SQLDatabaseId`,`SQLEndpointId`,`subscriptionID`,`UserId`,`WarehouseId`,`WorkspaceGUID`,`WorkspaceId`,`WorkspaceIds`,and `WorkspaceRoleAssignmentId` parameters to the datatype GUID [#125](https://github.com/dataplat/FabricTools/issues/125)
1922

2023

2124

2225
### Fixed
26+
- Enhanced logic in unified function `Test-FabricApiResponse` to handle API results and moved it to private functions
27+
- Fixed bug in `Get-FabricLongRunningOperation` - Uri was incorectly created (#131)
28+
- Fixed bug in `Get-FabricLongRunningOperationResult` - uses correct statusCode (#131)
29+
2330
### Deprecated
2431
### Removed
32+
- Removed Revision History from `Get-FabricSQLDatabase`, `Get-FabricSQLDatabase`
33+
2534
### Security
2635

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

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 -ObjectIdOrName $SQLDatabaseId -TypeName "SQL Database"
121108

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

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

Lines changed: 18 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,9 @@ function New-FabricSQLDatabase
4243

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

4851
try
@@ -51,7 +54,7 @@ function New-FabricSQLDatabase
5154
Confirm-TokenState
5255

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

5760
# Step 3: Construct the request body
@@ -67,18 +70,13 @@ function New-FabricSQLDatabase
6770
$bodyJson = $body | ConvertTo-Json -Depth 10
6871
Write-Message -Message "Request Body: $bodyJson" -Level Debug
6972

73+
# Step 4: Make the API request
7074
if ($PSCmdlet.ShouldProcess($apiEndpointUrl, "Create SQL Database"))
7175
{
72-
73-
# Step 4: Make the API request
74-
$response = Invoke-FabricRestMethod `
75-
-Uri $apiEndpointUrl `
76-
-Method Post `
77-
-Body $bodyJson
76+
$response = Invoke-FabricRestMethod -Uri $apiEndpointUrl -Method Post -Body $bodyJson
7877
}
7978
# 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'
79+
Test-FabricApiResponse -Response $response -ObjectIdOrName $Name -TypeName 'SQL Database' -NoWait:$NoWait
8280
}
8381
catch
8482
{

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

Lines changed: 3 additions & 15 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
@@ -43,28 +42,17 @@ 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
67-
55+
Test-FabricApiResponse -Response $response -ObjectIdOrName $SQLDatabaseId -TypeName "SQL Database"
6856
}
6957
catch
7058
{

0 commit comments

Comments
 (0)