Update ZertoRestRequest and associated Tests

This commit is contained in:
Wes Carroll
2020-03-06 13:42:39 -05:00
parent 40331c26f9
commit 569d9e264c
2 changed files with 26 additions and 11 deletions
@@ -14,9 +14,7 @@ Describe $global:function -Tag 'Unit', 'Source', 'Built' {
$ParameterNameTestCases = @(
@{ParameterName = "uri"; Type = "String"; Mandatory = $true; DefaultValue = $null; TestName = "URI" }
@{ParameterName = "method"; Type = "String"; Mandatory = $false; DefaultValue = 'GET'; TestName = "Method" }
@{ParameterName = "apiVersion"; Type = "String"; Mandatory = $false; DefaultValue = 'v1'; TestName = "API Version" }
@{ParameterName = "body"; Type = "String"; Mandatory = $false; DefaultValue = $null; TestName = "Body" }
@{ParameterName = "contentType"; Type = "String"; Mandatory = $false; DefaultValue = 'application/json'; TestName = "Content Type" }
@{ParameterName = "credential"; Type = "PSCredential"; Mandatory = $false; DefaultValue = $null; TestName = "Credential" }
@{ParameterName = "returnHeaders"; Type = "Switch"; Mandatory = $false; DefaultValue = $null; TestName = "Return Headers" }
)
@@ -36,9 +34,7 @@ Describe $global:function -Tag 'Unit', 'Source', 'Built' {
$ParameterValidationTestCases = @(
@{ParameterName = "URI"; TestName = "URI" }
@{ParameterName = "apiVersion"; TestName = "Api Version" }
@{ParameterName = "body"; TestName = "Body" }
@{ParameterName = "contentType"; TestName = "Content Type" }
)
It "<TestName> parameter cannot be null or empty" -TestCases $ParameterValidationTestCases {
@@ -1,20 +1,39 @@
<# .ExternalHelp ./en-us/ZertoApiWrapper-help.xml #>
function Invoke-ZertoRestRequest {
[cmdletbinding()]
param(
# Parameter help description
[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")]
[string]$method = "GET",
[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()]
[string]$uri,
[ValidateNotNullOrEmpty()]
[string]$apiVersion = "v1",
[Parameter(
Helpmessage = "Body to be submitted to the REST API endpoint. This needs to be submitted in JSON format"
)]
[ValidateNotNullOrEmpty()]
[string]$body,
[ValidateNotNullOrEmpty()]
[string]$contentType = "application/json",
[Parameter(
Helpmessage = "PSCredential object. This is ONLY used when authenticating with the ZVM. No other endpoints require this and generally is not used."
)]
[PSCredential]$credential,
[Parameter(
Helpmessage = "Use this switch if you would like the request headers returned along with the body. Useful for troubleshooting to get HTTP error codes."
)]
[switch]$returnHeaders
)
# API version. Currently this is locked at v1 in all versions of Zerto. Should this change, will look
# at making this as parameter to be selected during function call.
$apiVersion = "v1"
# While the API can use XML or JSON, this module is built on JSON functionality. Currently forcing all
# content types and language to JSON.
$contentType = "application/json"
$callerErrorActionPreference = $ErrorActionPreference
# If the ZVM server and Port not defined, Stop Call
if ( -not ((Test-Path variable:script:zvmServer) -and (Test-Path variable:script:zvmPort)) ) {
@@ -22,7 +41,7 @@ function Invoke-ZertoRestRequest {
}
# If the Headers exist and the Last action was more than 30 minutes ago, Session is Expired
if ( (Test-Path variable:script:zvmHeaders) -and $([datetime]$script:zvmLastAction).addMinutes(30) -lt $(get-date) ) {
if ( (Test-Path variable:script:zvmHeaders) -and $([datetime]$script:zvmLastAction).addMinutes(30) -lt $(Get-Date) ) {
Throw "Authorization Token has Expired. Please re-authorize to the Zerto Virtual Manager"
} else {
@@ -30,7 +49,7 @@ function Invoke-ZertoRestRequest {
$submittedURI = "https://{0}:{1}/{2}/{3}" -f $script:zvmServer, $script:zvmPort, $apiVersion, $uri
try {
# Set the zvmLastAction time and try to submit the REST Request
$script:zvmLastAction = (get-date).Ticks
$script:zvmLastAction = (Get-Date).Ticks
# If running PwSh - Use this Invoke-RestMethod with passed Variables
if ($PSVersionTable.PSVersion.Major -ge 6) {
$apiRequestResults = Invoke-RestMethod -Uri $submittedURI -Headers $script:zvmHeaders -Method $method -Body $body -ContentType $contentType -Credential $credential -SkipCertificateCheck -ResponseHeadersVariable responseHeaders -TimeoutSec 100