Skip to content

Commit 40a4004

Browse files
authored
Get-SqlDatabase accepts Workspace as a pipeline and handles errors correctly
Get-SqlDatabase accepts Workspace as a pipeline and handles errors correctly
2 parents 2d0bedb + 768bc5f commit 40a4004

File tree

3 files changed

+65
-32
lines changed

3 files changed

+65
-32
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
99
- Added credits for authors to all functions and Unit tests to verify the existence of such tags #89
1010

1111
### Changed
12+
- Get-SqlDatabase accepts Workspace as a pipeline, handles errors correctly and can filter by name (#117).
13+
1214
### Fixed
1315
### Deprecated
1416
### Removed

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

Lines changed: 48 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,12 @@ function Get-FabricSQLDatabase {
1313
Id of the Fabric Workspace for which the SQLDatabases should be retrieved. The value for WorkspaceId is a GUID.
1414
An example of a GUID is '12345678-1234-1234-1234-123456789012'.
1515
16+
.PARAMETER Workspace
17+
The workspace object. This is a mandatory parameter for the 'WorkspaceObject' parameter set and can be pipelined into the function.
18+
The object can be easily retrieved by Get-FabricWorkspace function.
19+
1620
.PARAMETER SQLDatabaseName
17-
The name of the KQLDatabase to retrieve. This parameter cannot be used together with SQLDatabaseID.
21+
The name of the SQLDatabase to retrieve. This parameter cannot be used together with SQLDatabaseID.
1822
1923
.PARAMETER SQLDatabaseID
2024
The Id of the SQLDatabase to retrieve. This parameter cannot be used together with SQLDatabaseName.
@@ -28,7 +32,7 @@ function Get-FabricSQLDatabase {
2832
This example will retrieve the SQLDatabase with the name 'MySQLDatabase'.
2933
3034
.EXAMPLE
31-
Get-FabricSQLDatabase
35+
Get-FabricSQLDatabase -WorkspaceId '12345678-1234-1234-1234-123456789012'
3236
3337
This example will retrieve all SQLDatabases in the workspace that is specified
3438
by the WorkspaceId.
@@ -40,6 +44,11 @@ function Get-FabricSQLDatabase {
4044
4145
This example will retrieve the SQLDatabase with the ID '12345678-1234-1234-1234-123456789012'.
4246
47+
.EXAMPLE
48+
Get-FabricWorkspace -WorkspaceName 'MsLearn-dev' | Get-FabricSQLDatabase
49+
50+
This example will retrieve all SQLDatabases from the workspace with the name 'MsLearn-dev' (using the pipeline).
51+
4352
.NOTES
4453
Revision History:
4554
- 2025-03-06 - KNO: Init version of the function
@@ -51,31 +60,54 @@ function Get-FabricSQLDatabase {
5160

5261
[CmdletBinding()]
5362
param (
54-
[Parameter(Mandatory = $true)]
63+
[Parameter(Mandatory = $true, ParameterSetName = 'WorkspaceId')]
5564
[string]$WorkspaceId,
5665

66+
[Parameter(Mandatory = $true, ParameterSetName = 'WorkspaceObject', ValueFromPipeline = $true )]
67+
$Workspace,
68+
5769
[Alias("Name", "DisplayName")]
5870
[string]$SQLDatabaseName,
5971

6072
[Alias("Id")]
6173
[string]$SQLDatabaseId
6274
)
6375

64-
Confirm-TokenState
76+
begin {
77+
Confirm-TokenState
6578

66-
Write-Verbose "You can either use SQLDatabaseName or SQLDatabaseID not both. If both are used throw error"
67-
if ($PSBoundParameters.ContainsKey("SQLDatabaseName") -and $PSBoundParameters.ContainsKey("SQLDatabaseId")) {
68-
throw "Parameters SQLDatabaseName and SQLDatabaseId cannot be used together"
79+
if ($PSBoundParameters.ContainsKey("SQLDatabaseName") -and $PSBoundParameters.ContainsKey("SQLDatabaseId")) {
80+
throw "Parameters SQLDatabaseName and SQLDatabaseId cannot be used together"
81+
}
6982
}
7083

71-
# Create SQLDatabase API
72-
$uri = "$($FabricSession.BaseApiUrl)/workspaces/$workspaceId/SqlDatabases"
73-
if ($SQLDatabaseId) {
74-
$uri = "$uri/$SQLDatabaseId"
84+
process {
85+
if ($PSCmdlet.ParameterSetName -eq 'WorkspaceObject') {
86+
$WorkspaceId = $Workspace.id
87+
}
88+
89+
# Create SQLDatabase API
90+
$uri = "$($FabricSession.BaseApiUrl)/workspaces/$WorkspaceId/SqlDatabases"
91+
if ($SQLDatabaseId) {
92+
$uri = "$uri/$SQLDatabaseId"
93+
}
94+
$response = Invoke-FabricRestMethod -Uri $uri
95+
##$databases.Where({$_.displayName -eq $body.displayName}).id
96+
97+
# Step: Validate the response code
98+
if ($statusCode -ne 200) {
99+
Write-Message -Message "Unexpected response code: $statusCode from the API." -Level Error
100+
Write-Message -Message "Error: $($response.message)" -Level Error
101+
Write-Message -Message "Error Details: $($response.moreDetails)" -Level Error
102+
Write-Message "Error Code: $($response.errorCode)" -Level Error
103+
return $null
104+
}
105+
106+
$response = $response.value
107+
if ($SQLDatabaseName) {
108+
# Filter the SQLDatabase by name
109+
$response = $response | Where-Object { $_.displayName -eq $SQLDatabaseName }
110+
}
111+
return $response
75112
}
76-
$result = Invoke-RestMethod -Headers $FabricSession.HeaderParams -Uri $uri
77-
##$databases.Where({$_.displayName -eq $body.displayName}).id
78-
79-
return $result.value
80-
81113
}

tests/Unit/Get-FabricSQLDatabase.Tests.ps1

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,21 @@ param(
33
$ModuleName = "FabricTools",
44
$expectedParams = @(
55
"WorkspaceId"
6-
"SQLDatabaseName"
7-
"SQLDatabaseId"
8-
"Verbose"
9-
"Debug"
10-
"ErrorAction"
11-
"WarningAction"
12-
"InformationAction"
13-
"ProgressAction"
14-
"ErrorVariable"
15-
"WarningVariable"
16-
"InformationVariable"
17-
"OutVariable"
18-
"OutBuffer"
19-
"PipelineVariable"
20-
6+
"Workspace"
7+
"SQLDatabaseName"
8+
"SQLDatabaseId"
9+
"Verbose"
10+
"Debug"
11+
"ErrorAction"
12+
"WarningAction"
13+
"InformationAction"
14+
"ProgressAction"
15+
"ErrorVariable"
16+
"WarningVariable"
17+
"InformationVariable"
18+
"OutVariable"
19+
"OutBuffer"
20+
"PipelineVariable"
2121
)
2222
)
2323

@@ -45,4 +45,3 @@ Describe "Get-FabricSQLDatabase" -Tag "UnitTests" {
4545
}
4646
}
4747
}
48-

0 commit comments

Comments
 (0)