Skip to content

Commit 1e3e51d

Browse files
Enable Get-FabricWorkspaceUser to accept pipeline input (#128)
* Rename function, improve examples and add alias * Refactor Get-FabricWorkspaceUserto enable processing via the pipeline #75 Updated the Get-FabricWorkspaceUser function to enhance readability and maintainability. Added additional examples to the documentation for better user guidance. Improved the process block to handle multiple workspaces and return user details more effectively. Thank you! * Add unit tests for Get-FabricWorkspaceUser function This commit introduces a comprehensive set of unit tests for the Get-FabricWorkspaceUser function. The tests validate parameter presence, alias functionality, and ensure correct behavior when handling multiple workspaces. This enhancement improves code reliability and maintainability. Thank you! * Add unit tests and update Get-FabricWorkspaceUser Added unit tests for the `Get-FabricWorkspaceUser` function to ensure it operates correctly with multiple workspaces, both in the pipeline and as parameters. Updated the function to support pipeline input for `WorkspaceId` and `WorkspaceName`, and renamed `Get-FabricWorkspaceUsers` to its singular form for consistency. Thank you! * Refactor test for Get-FabricWorkspaceUser to use script block Updated the test case to wrap the command in a script block for better readability and consistency. This change enhances the clarity of the test and aligns with best practices for PowerShell testing. Thank you! * Update Get-FabricWorkspaceUser to handle null responses should never be required but use lessons learned from the database code Enhance the Get-FabricWorkspaceUser function to ensure that it properly checks for null responses from the PowerBI API before adding users to the return value. This change improves the robustness of the function and prevents potential errors when no users are returned. Thank you! * Remove comments
1 parent c7f0638 commit 1e3e51d

File tree

5 files changed

+213
-107
lines changed

5 files changed

+213
-107
lines changed

CHANGELOG.md

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

88
### Added
9+
- added unit tests for `Get-FabricWorkspaceUser` function to ensure it works correctly with multiple workspaces both in the pipeline and passed to a parameter.
10+
- Added unit tests for Aliases for `Get-FabricWorkspaceUser` function to ensure backward compatibility.
911
- Added credits for authors to all functions and Unit tests to verify the existence of such tags #89
1012

1113
### Changed
14+
- Updated `Get-FabricWorkspaceUser` to support pipeline input for `WorkspaceId` and `WorkspaceName` parameters.
15+
- Renamed `Get-FabricWorkspaceUsers` to match the singular form
1216
- Get-FabricSqlDatabase accepts Workspace as a pipeline, handles errors correctly and can filter by name (#117).
1317

1418
### Fixed
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
function Get-FabricWorkspaceUser
2+
{
3+
<#
4+
.SYNOPSIS
5+
Retrieves the user(s) of a workspace.
6+
7+
.DESCRIPTION
8+
The Get-FabricWorkspaceUser function retrieves the details of the users of a workspace.
9+
10+
.PARAMETER WorkspaceId
11+
The ID of the workspace. This is a mandatory parameter if Workspace is not provided.
12+
13+
.PARAMETER Workspace
14+
The workspace object. This normally comes from the Get-FabricWorkspace cmdlet. This is a mandatory parameter if WorkspaceId is not provided.
15+
16+
.EXAMPLE
17+
Get-FabricWorkspaceUser -WorkspaceId '12345678-1234-1234-1234-123456789012
18+
19+
This example retrieves the users of a workspace of the workspace with the ID '12345678-1234-1234-1234-123456789012'.
20+
21+
.EXAMPLE
22+
$workspace = Get-FabricWorkspace -WorkspaceName 'prod-workspace'
23+
$workspace | Get-FabricWorkspaceUser
24+
25+
This example retrieves the users of the prod-workspace workspace by piping the object.
26+
27+
.EXAMPLE
28+
Get-FabricWorkspaceUser -Workspace (Get-FabricWorkspace -WorkspaceName 'prod-workspace')
29+
30+
This example retrieves the users of the prod-workspace workspace by piping the object.
31+
32+
.EXAMPLE
33+
Get-FabricWorkspace| Get-FabricWorkspaceUser
34+
35+
This example retrieves the users of all of the workspaces. IMPORTANT: This will not return the workspace name or ID at present.
36+
37+
.EXAMPLE
38+
Get-FabricWorkspace| Foreach-Object {
39+
Get-FabricWorkspaceUser -WorkspaceId $_.Id | Select-Object @{Name='WorkspaceName';Expression={$_.displayName;}}, *
40+
}
41+
42+
This example retrieves the users of all of the workspaces. It will also add the workspace name to the output.
43+
44+
.NOTES
45+
It supports multiple aliases for backward compatibility.
46+
The function defines parameters for the workspace ID and workspace object. If the parameter set name is 'WorkspaceId', it retrieves the workspace object. It then makes a GET request to the PowerBI API to retrieve the users of the workspace and returns the 'value' property of the response, which contains the users.
47+
48+
Author: Ioana Bouariu
49+
50+
#>
51+
52+
# This function retrieves the users of a workspace.
53+
# Define aliases for the function for flexibility.
54+
[Alias("Get-FabWorkspaceUsers")]
55+
[Alias("Get-FabricWorkspaceUsers")]
56+
57+
# Define parameters for the workspace ID and workspace object.
58+
param(
59+
[Parameter(Mandatory = $true, ParameterSetName = 'WorkspaceId')]
60+
[string]$WorkspaceId,
61+
62+
[Parameter(Mandatory = $true, ParameterSetName = 'WorkspaceObject', ValueFromPipeline = $true )]
63+
[object[]]
64+
$Workspace
65+
)
66+
67+
begin
68+
{
69+
Confirm-TokenState
70+
# If the parameter set name is 'WorkspaceId', return the workspace object so that it can be used in the process block.
71+
if ($PSCmdlet.ParameterSetName -eq 'WorkspaceId')
72+
{
73+
$Workspace = Get-FabricWorkspace -WorkspaceId $WorkspaceId
74+
}
75+
$returnValue = @()
76+
}
77+
78+
process
79+
{
80+
81+
foreach ($space in $Workspace)
82+
{
83+
84+
$Uri = "groups/{0}/users" -f $space.id
85+
86+
# Make a GET request to the PowerBI API to retrieve the users of the workspace.
87+
# The function returns the 'value' property of the response, which contains the users.
88+
89+
$response = (Invoke-FabricRestMethod -Method get -PowerBIApi -Uri $Uri).value
90+
if ($response)
91+
{
92+
93+
# Add the users to the return value.
94+
$returnValue += $response
95+
}
96+
}
97+
}
98+
end
99+
{
100+
$returnValue
101+
}
102+
}

source/Public/Workspace/Get-FabricWorkspaceUsers.ps1

Lines changed: 0 additions & 60 deletions
This file was deleted.
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
#Requires -Module @{ ModuleName="Pester"; ModuleVersion="5.0"}
2+
param(
3+
$ModuleName = "FabricTools",
4+
$expectedParams = @(
5+
"WorkspaceId"
6+
"Workspace"
7+
"Verbose"
8+
"Debug"
9+
"ErrorAction"
10+
"WarningAction"
11+
"InformationAction"
12+
"ProgressAction"
13+
"ErrorVariable"
14+
"WarningVariable"
15+
"InformationVariable"
16+
"OutVariable"
17+
"OutBuffer"
18+
"PipelineVariable"
19+
20+
)
21+
)
22+
23+
Describe "Get-FabricWorkspaceUser" -Tag "UnitTests" {
24+
25+
BeforeDiscovery {
26+
$command = Get-Command -Name Get-FabricWorkspaceUser
27+
$expected = $expectedParams
28+
}
29+
30+
Context "Parameter validation" {
31+
BeforeAll {
32+
$command = Get-Command -Name Get-FabricWorkspaceUser
33+
$expected = $expectedParams
34+
}
35+
36+
It "Has parameter: <_>" -ForEach $expected {
37+
$command | Should -HaveParameter $PSItem
38+
}
39+
40+
It "Should have exactly the number of expected parameters $($expected.Count)" {
41+
$hasparms = $command.Parameters.Values.Name
42+
#$hasparms.Count | Should -BeExactly $expected.Count
43+
Compare-Object -ReferenceObject $expected -DifferenceObject $hasparms | Should -BeNullOrEmpty
44+
}
45+
}
46+
47+
Context "Alias validation" {
48+
$testCases = @('Get-FabWorkspaceUsers', 'Get-FabricWorkspaceUsers')
49+
50+
It "Should have the alias <_>" -TestCases $TestCases {
51+
$Alias = Get-Alias -Name $_ -ErrorAction SilentlyContinue
52+
$Alias | Should -Not -BeNullOrEmpty
53+
$Alias.ResolvedCommand.Name | Should -Be 'Get-FabricWorkspaceUser'
54+
}
55+
}
56+
57+
Context "Multiple Workspaces" {
58+
59+
BeforeEach {
60+
61+
function Confirm-TokenState {}
62+
Mock Confirm-TokenState {}
63+
64+
Mock Get-FabricWorkspace {
65+
return @(
66+
@{
67+
displayName = 'prod-workspace'
68+
# until the guid datatype is added
69+
Id = [guid]::NewGuid().Guid.ToString()
70+
}, @{
71+
displayName = "test-workspace"
72+
# until the guid datatype is added
73+
Id = [guid]::NewGuid().Guid.ToString()
74+
}
75+
)
76+
}
77+
Mock Invoke-FabricRestMethod {
78+
return @{
79+
value = @(
80+
@{
81+
emailAddress = '[email protected]'
82+
groupUserAccessRight = 'Admin'
83+
displayName = 'Fabric'
84+
identifier = '[email protected]'
85+
principalType = 'User'
86+
}, @{
87+
emailAddress = '[email protected]'
88+
groupUserAccessRight = 'Viewer'
89+
displayName = 'Fabric viewer'
90+
identifier = '[email protected]'
91+
principalType = 'User'
92+
}
93+
)
94+
}
95+
}
96+
}
97+
98+
It "Should return users for multiple workspaces passed to the Workspace parameter" {
99+
{Get-FabricWorkspaceUser -Workspace (Get-FabricWorkspace) }| Should -Not -BeNullOrEmpty
100+
}
101+
102+
It "Should return users for multiple workspaces passed to the Workspace parameter from the pipeline" {
103+
{ Get-FabricWorkspace | Get-FabricWorkspaceUser
104+
} | Should -Not -BeNullOrEmpty
105+
}
106+
}
107+
}

tests/Unit/Get-FabricWorkspaceUsers.Tests.ps1

Lines changed: 0 additions & 47 deletions
This file was deleted.

0 commit comments

Comments
 (0)