From 40331c26f93b0cc965279cb0847456dbb73c1984 Mon Sep 17 00:00:00 2001 From: Wes Carroll Date: Fri, 6 Mar 2020 13:42:20 -0500 Subject: [PATCH] Update ZARestRequest and associated tests. --- Tests/Public/Invoke-ZARestRequest.Tests.ps1 | 6 ----- .../Public/Invoke-ZARestRequest.ps1 | 24 ++++++++++++++----- 2 files changed, 18 insertions(+), 12 deletions(-) diff --git a/Tests/Public/Invoke-ZARestRequest.Tests.ps1 b/Tests/Public/Invoke-ZARestRequest.Tests.ps1 index 78bb6f3..c545e56 100644 --- a/Tests/Public/Invoke-ZARestRequest.Tests.ps1 +++ b/Tests/Public/Invoke-ZARestRequest.Tests.ps1 @@ -12,7 +12,6 @@ Describe $global:function -Tag 'Unit', 'Source', 'Built' { @{ParameterName = 'uri'; Type = 'String'; Mandatory = $true; TestName = 'URI' } @{ParameterName = 'method'; Type = 'String'; Mandatory = $false; TestName = 'Method' } @{ParameterName = 'body'; Type = 'String'; Mandatory = $false; TestName = 'Body' } - @{ParameterName = 'contentType'; Type = 'String'; Mandatory = $false; TestName = 'contentType' } ) It "Parameter present and Type test for: " -TestCases $testCases { @@ -29,14 +28,9 @@ Describe $global:function -Tag 'Unit', 'Source', 'Built' { 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 = @( @{ParameterName = 'uri'; TestName = 'Uri' } @{ParameterName = 'body'; TestName = 'Body' } - @{ParameterName = 'contentType'; TestName = 'ContentType' } ) It " parameter does not accecpt a null or empty value" -TestCases $NotNullOrEmptyTests { diff --git a/ZertoApiWrapper/Public/Invoke-ZARestRequest.ps1 b/ZertoApiWrapper/Public/Invoke-ZARestRequest.ps1 index 0304c88..52aab99 100644 --- a/ZertoApiWrapper/Public/Invoke-ZARestRequest.ps1 +++ b/ZertoApiWrapper/Public/Invoke-ZARestRequest.ps1 @@ -1,26 +1,38 @@ +<# .ExternalHelp ./en-us/ZertoApiWrapper-help.xml #> function Invoke-ZARestRequest { [cmdletbinding()] param( # 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()] [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")] [string]$method = "GET", + [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" + [string]$body + ) + # 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 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" - } 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" } else { # 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 if ($PSVersionTable.PSVersion.Major -ge 6) { Invoke-RestMethod -Uri $submittedUri -Method $method -Body $body -Headers $Script:zaHeaders -ContentType $contentType -TimeoutSec 100