Skip to content

Commit 1fd0662

Browse files
authored
Merge pull request #173 from dataplat/Update-FabricCapacity-161
Added function `Update-FabricCapacity`
2 parents d467af3 + c0854d7 commit 1fd0662

File tree

6 files changed

+316
-5
lines changed

6 files changed

+316
-5
lines changed

CHANGELOG.md

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

88
### Added
99

10+
- Added function `Update-FabricCapacity`
11+
- Added Error Detailed Info in `Test-FabricApiResponse` (Debug mode) when `response.error` exists
12+
1013
### Changed
1114

1215
- Version of `Microsoft.PowerShell.PSResourceGet` and `Microsoft.PowerShell.PlatyPS` updated

source/Private/Test-FabricApiResponse.ps1

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -142,11 +142,19 @@ function Test-FabricApiResponse {
142142
default {
143143
Write-Message -Message "Test-FabricApiResponse::default" -Level Debug
144144
Write-Message -Message "Unexpected response code: $statusCode from the API." -Level Error
145-
Write-Message -Message "Error: $($response.message)" -Level Error
146-
if ($response.moreDetails) {
147-
Write-Message -Message "More Details: $($response.moreDetails)" -Level Error
145+
146+
if ($null -ne $Response.error) {
147+
Write-Message -Message "Error Code: $($Response.error.code)" -Level Error
148+
Write-Message -Message "Error Msg: $($Response.error.message)" -Level Error
149+
$errorJson = $Response.error | ConvertTo-Json -Depth 10
150+
Write-Message -Message "Error Detailed Info:`n$errorJson" -Level Debug
151+
} else {
152+
Write-Message -Message "Error Code: $($Response.errorCode)" -Level Error
153+
Write-Message -Message "Error Msg: $($Response.message)" -Level Error
154+
if ($Response.moreDetails) {
155+
Write-Message -Message "More Details: $($Response.moreDetails)" -Level Error
156+
}
148157
}
149-
Write-Message "Error Code: $($response.errorCode)" -Level Error
150158
throw "API request failed with status code $statusCode."
151159
}
152160
}
Lines changed: 155 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,155 @@
1+
function Update-FabricCapacity
2+
{
3+
<#
4+
.SYNOPSIS
5+
Creates or updates a Microsoft Fabric capacity.
6+
7+
.DESCRIPTION
8+
The `Update-FabricCapacity` function sends a PATCH request to the Microsoft Fabric API to create or update a capacity
9+
in the specified Azure subscription and resource group. It supports parameters for capacity administration,
10+
SKU details, and optional tags.
11+
12+
.PARAMETER SubscriptionId
13+
The ID of the target subscription. The value must be a UUID.
14+
15+
.PARAMETER ResourceGroupName
16+
The name of the resource group. The name is case insensitive.
17+
18+
.PARAMETER CapacityName
19+
The name of the Microsoft Fabric capacity. It must be a minimum of 3 characters, and a maximum of 63.
20+
Must match pattern: ^[a-z][a-z0-9]*$
21+
22+
.PARAMETER SkuName
23+
The name of the SKU level (e.g., "F2").
24+
25+
.PARAMETER Location
26+
The Azure region where the capacity is located (e.g., "uksouth").
27+
28+
.PARAMETER AdministrationMembers
29+
An array of administrator user identities for the capacity administration.
30+
31+
.PARAMETER Tags
32+
Optional resource tags as a hashtable.
33+
34+
.PARAMETER NoWait
35+
If specified, the function will not wait for the operation to complete and will return immediately.
36+
37+
.EXAMPLE
38+
```powershell
39+
$azureResource = @{
40+
subscriptionID = 'GUID-GUID'
41+
ResourceGroup = 'TestRG'
42+
CapacityName = 'fabricblogdemof4'
43+
Location = 'uksouth'
44+
}
45+
Update-FabricCapacity @azureResource -SkuName 'F8' -AdministrationMembers 'azsdktest@microsoft.com' -Debug -Confirm:$false
46+
```
47+
48+
.EXAMPLE
49+
```powershell
50+
$azureResource = @{
51+
subscriptionID = 'GUID-GUID'
52+
ResourceGroup = 'TestRG'
53+
CapacityName = 'fabricblogdemof4'
54+
Location = 'uksouth'
55+
SkuName = 'F8'
56+
AdministrationMembers = 'azsdktest@microsoft.com'
57+
}
58+
Update-FabricCapacity @azureResource -Tags @{Environment="Production"; Owner="IT Team"} -Confirm:$false
59+
```
60+
61+
.NOTES
62+
- Calls `Confirm-TokenState` to ensure token validity before making the API request.
63+
- Uses Azure Resource Manager API endpoint for capacity management.
64+
65+
Author: Kamil Nowinski
66+
67+
#>
68+
[CmdletBinding(SupportsShouldProcess)]
69+
param (
70+
[Parameter(Mandatory = $true)]
71+
[ValidateNotNullOrEmpty()]
72+
[guid]$SubscriptionId,
73+
74+
[Parameter(Mandatory = $true)]
75+
[ValidateNotNullOrEmpty()]
76+
[ValidateLength(1, 90)]
77+
[string]$ResourceGroupName,
78+
79+
[Parameter(Mandatory = $true)]
80+
[ValidateNotNullOrEmpty()]
81+
[ValidateLength(3, 63)]
82+
[ValidatePattern('^[a-z][a-z0-9]*$')]
83+
[string]$CapacityName,
84+
85+
[Parameter(Mandatory = $true)]
86+
[ValidateNotNullOrEmpty()]
87+
[string]$SkuName,
88+
89+
[Parameter(Mandatory = $true)]
90+
[ValidateNotNullOrEmpty()]
91+
[string]$Location,
92+
93+
[Parameter(Mandatory = $true)]
94+
[ValidateNotNullOrEmpty()]
95+
[string[]]$AdministrationMembers,
96+
97+
[Parameter(Mandatory = $false)]
98+
[hashtable]$Tags,
99+
100+
[switch]$NoWait = $false
101+
)
102+
103+
$SkuTier = "Fabric"
104+
105+
try
106+
{
107+
# Step 1: Ensure token validity
108+
Confirm-TokenState
109+
110+
# Step 2: Construct the API URL
111+
$apiEndpointUrl = "$($AzureSession.BaseApiUrl)/subscriptions/{0}/resourceGroups/{1}/providers/Microsoft.Fabric/capacities/{2}?api-version=2023-11-01" -f $SubscriptionId, $ResourceGroupName, $CapacityName
112+
113+
# Step 3: Construct the request body
114+
$body = @{
115+
properties = @{
116+
administration = @{
117+
members = $AdministrationMembers
118+
}
119+
}
120+
sku = @{
121+
name = $SkuName
122+
tier = $SkuTier
123+
}
124+
location = $Location
125+
}
126+
127+
if ($Tags)
128+
{
129+
$body.tags = $Tags
130+
}
131+
132+
# Step 4: Make the API request
133+
if ($PSCmdlet.ShouldProcess($apiEndpointUrl, "Update Fabric Capacity"))
134+
{
135+
$apiParams = @{
136+
Uri = $apiEndpointUrl
137+
Method = 'PUT'
138+
Body = $body
139+
TypeName = 'Fabric Capacity'
140+
NoWait = $NoWait
141+
HandleResponse = $true
142+
ObjectIdOrName = $CapacityName
143+
}
144+
$response = Invoke-FabricRestMethod @apiParams
145+
$response
146+
}
147+
}
148+
catch
149+
{
150+
# Step 5: Handle and log errors
151+
$errorDetails = $_.Exception.Message
152+
Write-Message -Message "Failed to update Fabric Capacity. Error: $errorDetails" -Level Error
153+
throw
154+
}
155+
}

source/Public/Invoke-FabricRestMethod.ps1

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,12 @@ Function Invoke-FabricRestMethod {
133133
Write-Message -Message "PowerBIApi param is ignored when full Uri is provided." -Level Warning
134134
}
135135

136+
$Headers = $FabricSession.HeaderParams
137+
if ($Uri.StartsWith($AzureSession.BaseApiUrl)) {
138+
$Headers = $AzureSession.HeaderParams
139+
Write-Message -Message "Using AzureSession headers for request." -Level Debug
140+
}
141+
136142
if ($Body -is [hashtable]) {
137143
$Body = $Body | ConvertTo-Json -Depth 10
138144
Write-Message -Message "Request Body: $Body" -Level Debug

tests/QA/module.tests.ps1

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,8 @@ Describe 'Quality for module' -Tags 'TestQuality' {
137137
$Result = (Invoke-ScriptAnalyzer -Path $functionFile.FullName)
138138
$report = $Result | Format-Table -AutoSize | Out-String -Width 110
139139
$Errors = if ($Result) { $Result | Where-Object { $_.Severity -ne 'Warning' } } else { @() }
140-
$Errors | Should -BeNullOrEmpty -Because "some rule triggered.`r`n`r`n $report" }
140+
$Errors | Should -BeNullOrEmpty -Because "some rule triggered.`r`n`r`n $report"
141+
}
141142
}
142143

143144
Describe 'Help for module' -Tags 'helpQuality' {
Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
#Requires -Module @{ ModuleName="Pester"; ModuleVersion="5.0"}
2+
param(
3+
$ModuleName = "FabricTools",
4+
$expectedParams = @(
5+
"SubscriptionId"
6+
"ResourceGroupName"
7+
"CapacityName"
8+
"SkuName"
9+
"Location"
10+
"AdministrationMembers"
11+
"Tags"
12+
"NoWait"
13+
"WhatIf"
14+
"Confirm"
15+
"Verbose"
16+
"Debug"
17+
"ErrorAction"
18+
"WarningAction"
19+
"InformationAction"
20+
"ProgressAction"
21+
"ErrorVariable"
22+
"WarningVariable"
23+
"InformationVariable"
24+
"OutVariable"
25+
"OutBuffer"
26+
"PipelineVariable"
27+
)
28+
)
29+
30+
Describe "Update-FabricCapacity" -Tag "UnitTests" {
31+
32+
BeforeDiscovery {
33+
$command = Get-Command -Name Update-FabricCapacity
34+
$expected = $expectedParams
35+
}
36+
37+
Context "Parameter validation" {
38+
BeforeAll {
39+
$command = Get-Command -Name Update-FabricCapacity
40+
$expected = $expectedParams
41+
}
42+
43+
It "Has parameter: <_>" -ForEach $expected {
44+
$command | Should -HaveParameter $PSItem
45+
}
46+
47+
It "Should have exactly the number of expected parameters $($expected.Count)" {
48+
$hasParams = $command.Parameters.Values.Name
49+
Compare-Object -ReferenceObject $expected -DifferenceObject $hasParams | Should -BeNullOrEmpty
50+
}
51+
}
52+
53+
Context "Parameter validation rules" {
54+
BeforeAll {
55+
$command = Get-Command -Name Update-FabricCapacity
56+
}
57+
58+
It "SubscriptionId should be mandatory" {
59+
$command.Parameters['SubscriptionId'].Attributes.Mandatory | Should -Be $true
60+
}
61+
62+
It "ResourceGroupName should be mandatory" {
63+
$command.Parameters['ResourceGroupName'].Attributes.Mandatory | Should -Be $true
64+
}
65+
66+
It "CapacityName should be mandatory" {
67+
$command.Parameters['CapacityName'].Attributes.Mandatory | Should -Be $true
68+
}
69+
70+
It "SkuName should be mandatory" {
71+
$command.Parameters['SkuName'].Attributes.Mandatory | Should -Be $true
72+
}
73+
74+
It "AdministrationMembers should be mandatory" {
75+
$command.Parameters['AdministrationMembers'].Attributes.Mandatory | Should -Be $true
76+
}
77+
78+
It "Tags should not be mandatory" {
79+
$command.Parameters['Tags'].Attributes.Mandatory | Should -Be $false
80+
}
81+
82+
It "NoWait should not be mandatory" {
83+
$command.Parameters['NoWait'].Attributes.Mandatory | Should -Be $false
84+
}
85+
86+
It "SubscriptionId should be of type Guid" {
87+
$command.Parameters['SubscriptionId'].ParameterType.Name | Should -Be "Guid"
88+
}
89+
90+
It "ResourceGroupName should be of type String" {
91+
$command.Parameters['ResourceGroupName'].ParameterType.Name | Should -Be "String"
92+
}
93+
94+
It "CapacityName should be of type String" {
95+
$command.Parameters['CapacityName'].ParameterType.Name | Should -Be "String"
96+
}
97+
98+
It "SkuName should be of type String" {
99+
$command.Parameters['SkuName'].ParameterType.Name | Should -Be "String"
100+
}
101+
102+
It "AdministrationMembers should be of type String array" {
103+
$command.Parameters['AdministrationMembers'].ParameterType.Name | Should -Be "String[]"
104+
}
105+
106+
It "Tags should be of type Hashtable" {
107+
$command.Parameters['Tags'].ParameterType.Name | Should -Be "Hashtable"
108+
}
109+
110+
It "NoWait should be of type SwitchParameter" {
111+
$command.Parameters['NoWait'].ParameterType.Name | Should -Be "SwitchParameter"
112+
}
113+
}
114+
115+
Context "Parameter validation attributes" {
116+
BeforeAll {
117+
$command = Get-Command -Name Update-FabricCapacity
118+
}
119+
120+
It "ResourceGroupName should have ValidateLength attribute with max length 90" {
121+
$validateLengthAttr = $command.Parameters['ResourceGroupName'].Attributes | Where-Object { $_.GetType().Name -eq "ValidateLengthAttribute" }
122+
$validateLengthAttr | Should -Not -BeNullOrEmpty
123+
$validateLengthAttr.MaxLength | Should -Be 90
124+
}
125+
126+
It "CapacityName should have ValidateLength attribute with min length 3 and max length 63" {
127+
$validateLengthAttr = $command.Parameters['CapacityName'].Attributes | Where-Object { $_.GetType().Name -eq "ValidateLengthAttribute" }
128+
$validateLengthAttr | Should -Not -BeNullOrEmpty
129+
$validateLengthAttr.MinLength | Should -Be 3
130+
$validateLengthAttr.MaxLength | Should -Be 63
131+
}
132+
133+
It "CapacityName should have ValidatePattern attribute" {
134+
$validatePatternAttr = $command.Parameters['CapacityName'].Attributes | Where-Object { $_.GetType().Name -eq "ValidatePatternAttribute" }
135+
$validatePatternAttr | Should -Not -BeNullOrEmpty
136+
}
137+
}
138+
}

0 commit comments

Comments
 (0)