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