Update ZARestRequest and associated tests.

This commit is contained in:
Wes Carroll
2020-03-06 13:42:20 -05:00
parent 526093afc6
commit 40331c26f9
2 changed files with 18 additions and 12 deletions
@@ -12,7 +12,6 @@ Describe $global:function -Tag 'Unit', 'Source', 'Built' {
@{ParameterName = 'uri'; Type = 'String'; Mandatory = $true; TestName = 'URI' } @{ParameterName = 'uri'; Type = 'String'; Mandatory = $true; TestName = 'URI' }
@{ParameterName = 'method'; Type = 'String'; Mandatory = $false; TestName = 'Method' } @{ParameterName = 'method'; Type = 'String'; Mandatory = $false; TestName = 'Method' }
@{ParameterName = 'body'; Type = 'String'; Mandatory = $false; TestName = 'Body' } @{ParameterName = 'body'; Type = 'String'; Mandatory = $false; TestName = 'Body' }
@{ParameterName = 'contentType'; Type = 'String'; Mandatory = $false; TestName = 'contentType' }
) )
It "Parameter present and Type test for: <TestName> " -TestCases $testCases { It "Parameter present and Type test for: <TestName> " -TestCases $testCases {
@@ -29,14 +28,9 @@ Describe $global:function -Tag 'Unit', 'Source', 'Built' {
Get-Command $global:function | Should -HaveParameter Method -DefaultValue "GET" Get-Command $global:function | Should -HaveParameter Method -DefaultValue "GET"
} }
It "ContentType parameter default is 'application/json'" {
Get-Command $global:function | Should -HaveParameter contentType -DefaultValue "application/json"
}
$NotNullOrEmptyTests = @( $NotNullOrEmptyTests = @(
@{ParameterName = 'uri'; TestName = 'Uri' } @{ParameterName = 'uri'; TestName = 'Uri' }
@{ParameterName = 'body'; TestName = 'Body' } @{ParameterName = 'body'; TestName = 'Body' }
@{ParameterName = 'contentType'; TestName = 'ContentType' }
) )
It "<TestName> parameter does not accecpt a null or empty value" -TestCases $NotNullOrEmptyTests { It "<TestName> parameter does not accecpt a null or empty value" -TestCases $NotNullOrEmptyTests {
@@ -1,26 +1,38 @@
<# .ExternalHelp ./en-us/ZertoApiWrapper-help.xml #>
function Invoke-ZARestRequest { function Invoke-ZARestRequest {
[cmdletbinding()] [cmdletbinding()]
param( param(
# Parameter help description # Parameter help description
[Parameter(Mandatory)] [Parameter(
Mandatory,
Helpmessage = "URI endpoint to be utilized. When submitting the URI, only the endpoint needs to be submitted. Please review the help documentation for examples."
)]
[ValidateNotNullOrEmpty()] [ValidateNotNullOrEmpty()]
[string]$uri, [string]$uri,
[Parameter(
Helpmessage = "API method to be used. GET, PUT, POST, or DELETE. Refer to documentation for the API endpoint to ensure the correct method is being used. If unspecified, defaults to GET"
)]
[ValidateSet("GET", "PUT", "POST", "DELETE")] [ValidateSet("GET", "PUT", "POST", "DELETE")]
[string]$method = "GET", [string]$method = "GET",
[Parameter(
Helpmessage = "Body to be submitted to the REST API endpoint. This needs to be submitted in JSON format"
)]
[ValidateNotNullOrEmpty()] [ValidateNotNullOrEmpty()]
[string]$body, [string]$body
[ValidateNotNullOrEmpty()]
[string]$contentType = "application/json"
) )
# While the API can use XML or JSON, this module is built on JSON functionality. Currently forcing all
# content types and language to JSON.
[string]$contentType = "application/json"
# Check to see if the required variables are present and currently valid # Check to see if the required variables are present and currently valid
if ( -not ((Test-Path variable:script:zaLastActionTime) -and (Test-Path variable:script:zaHeaders)) ) { if ( -not ((Test-Path variable:script:zaLastActionTime) -and (Test-Path variable:script:zaHeaders)) ) {
Throw "Zerto Analytics Connection does not Exist. Please run Connect-ZertoAnalytics first to establish a connection" Throw "Zerto Analytics Connection does not Exist. Please run Connect-ZertoAnalytics first to establish a connection"
} elseif ( (Test-Path variable:script:zaHeaders) -and $([datetime]$script:zaLastActionTime).addMinutes(60) -lt $(get-date) ) { } elseif ( (Test-Path variable:script:zaHeaders) -and $([datetime]$script:zaLastActionTime).addMinutes(60) -lt $(Get-Date) ) {
Throw "Authorization Token has Expired. Please re-authorize to the Zerto Analytics Portal" Throw "Authorization Token has Expired. Please re-authorize to the Zerto Analytics Portal"
} else { } else {
# Update the last action time and submit the request based on PS Version. # Update the last action time and submit the request based on PS Version.
Set-Variable -Name zaLastActionTime -Scope Script -Value $(Get-date).Ticks Set-Variable -Name zaLastActionTime -Scope Script -Value $(Get-Date).Ticks
$submittedUri = "https://analytics.api.zerto.com/v2/{0}" -f $uri $submittedUri = "https://analytics.api.zerto.com/v2/{0}" -f $uri
if ($PSVersionTable.PSVersion.Major -ge 6) { if ($PSVersionTable.PSVersion.Major -ge 6) {
Invoke-RestMethod -Uri $submittedUri -Method $method -Body $body -Headers $Script:zaHeaders -ContentType $contentType -TimeoutSec 100 Invoke-RestMethod -Uri $submittedUri -Method $method -Body $body -Headers $Script:zaHeaders -ContentType $contentType -TimeoutSec 100