|
| 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 | +} |
0 commit comments