Skip to content

Commit

Permalink
Merge pull request #636 from rubrikinc/jaap-216
Browse files Browse the repository at this point in the history
Resolves longer running operations causing timeouts - #216
  • Loading branch information
mwpreston authored Jun 18, 2020
2 parents 3cfb6fe + 1e23d73 commit fc17804
Show file tree
Hide file tree
Showing 7 changed files with 107 additions and 74 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ and this project adheres to [Semantic Versioning](http://semver.org/).

### Changed

* Private function `Invoke-RubrikWebRequest` now correctly uses the `DefaultWebRequestTimeOut` default module option. Default is set to 15 seconds, but can be changed [Issue 216](https://github.com/rubrikinc/rubrik-sdk-for-powershell/issues/216)
* `Update-RubrikModuleOption` now also uses `Get-HomePath` private function, its unit tests have been updated as well
* Added new property to `Get-RubrikReportData` : `DataGridObject` which returns the datagrid results as PowerShell custom objects [Issue 549](https://github.com/rubrikinc/rubrik-sdk-for-powershell/issues/549)
* `New-URIString` private function now validates `$id` input [Issue 627](https://github.com/rubrikinc/rubrik-sdk-for-powershell/issues/627)
* Changed how `Set-RubrikNASShare` creates the body object [Issue 614](https://github.com/rubrikinc/rubrik-sdk-for-powershell/issues/614) and added new unit tests for this function
Expand Down Expand Up @@ -72,6 +74,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).

### Fixed

* Fixed `Set-RubrikModuleOption`, it could not set module defaults, now it can
* Azure DevOps issues with Pester v5
* Added error handling for cases where Add-Type in `Unblock-SelfSignedCert` fails. `-Debug` switch can be used to determine root cause of failure. [Issue 613](https://github.com/rubrikinc/rubrik-sdk-for-powershell/issues/613)
* Fixed incorrect array in body of `Restore-RubrikDatabase` and added tests to validate new behavior in `Restore-RubrikDatabase.Tests`
Expand Down
3 changes: 2 additions & 1 deletion Rubrik/OptionsDefault/rubrik_sdk_for_powershell_options.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"DefaultParameterValue": {},
"ModuleOption": {
"ApplyCustomViewDefinitions": "True",
"CredentialPath": ""
"CredentialPath": "",
"DefaultWebRequestTimeOut": 15
}
}
12 changes: 10 additions & 2 deletions Rubrik/Private/Invoke-RubrikWebRequest.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,17 @@ function Invoke-RubrikWebRequest {
)

if (Test-PowerShellSix) {
$result = Invoke-WebRequest -UseBasicParsing -SkipCertificateCheck @PSBoundParameters
if ($rubrikOptions.ModuleOption.DefaultWebRequestTimeOut) {
$result = Invoke-WebRequest -UseBasicParsing -SkipCertificateCheck -TimeoutSec $rubrikOptions.ModuleOption.DefaultWebRequestTimeOut @PSBoundParameters
} else {
$result = Invoke-WebRequest -UseBasicParsing -SkipCertificateCheck @PSBoundParameters
}
} else {
$result = Invoke-WebRequest -UseBasicParsing @PSBoundParameters
if ($rubrikOptions.ModuleOption.DefaultWebRequestTimeOut) {
$result = Invoke-WebRequest -UseBasicParsing -TimeoutSec $rubrikOptions.ModuleOption.DefaultWebRequestTimeOut @PSBoundParameters
} else {
$result = Invoke-WebRequest -UseBasicParsing @PSBoundParameters
}
}

Write-Verbose -Message "Received HTTP Status $($result.StatusCode)"
Expand Down
78 changes: 39 additions & 39 deletions Rubrik/Private/Update-RubrikModuleOption.ps1
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
function Update-RubrikModuleOption([string]$OptionType,[string]$OptionName,[string]$OptionValue,[string]$Action) {
<#
.SYNOPSIS
Retrieves the default parameter values and applies them globally
<#
.SYNOPSIS
Retrieves the default parameter values and applies them globally
.DESCRIPTION
This function will retrieve the default parameter values from the users options file and apply them globally.
.DESCRIPTION
This function will retrieve the default parameter values from the users options file and apply them globally.
.PARAMETER OptionType
Type of module option to set (ModuleOption or DefaultParameterValue)
.PARAMETER OptionType
Type of module option to set (ModuleOption or DefaultParameterValue)
.PARAMETER OptionName
Option name to modify
.PARAMETER OptionName
Option name to modify
.PARAMETER OptionValue
Option Value to modify
.PARAMETER OptionValue
Option Value to modify
.PARAMETER Action
Action to take (AddUpdate, Remove, Defaults, Sync)
#>
.PARAMETER Action
Action to take (AddUpdate, Remove, Defaults, Sync)
#>

# Action of Sync is applies to both ModuleOption and DefaultParameterValue.
# If sync is called, execute and return
Expand All @@ -34,8 +34,8 @@
"Default" {
Write-Verbose -Message "Reseting all Module Options to default values"
# remove all ModuleOptions from global variable and userfile
$global:rubrikoptions.ModuleOption.psobject.properties | ForEach {$global:rubrikOptions.ModuleOption.psobject.properties.remove($_.Name)}
$global:rubrikOptions | ConvertTo-Json | Out-File $Home\rubrik_sdk_for_powershell_options.json
$global:rubrikoptions.ModuleOption.psobject.properties | ForEach-Object {$global:rubrikOptions.ModuleOption.psobject.properties.remove($_.Name)}
$global:rubrikOptions | ConvertTo-Json | Out-File -FilePath "$(Get-HomePath)\rubrik_sdk_for_powershell_options.json"
# run sync to recreate them from template
$global:rubrikOptions = Sync-RubrikOptionsFile
# Remove Credential from PSDefaultParameterValues if it exists as CredentialFiles default is null
Expand All @@ -55,30 +55,28 @@
# If value is already set, reconfigure it
Write-Verbose -Message "Updating PSDefaultParameterValues for Connect-Rubrik:Credential"
$Global:PSDefaultParameterValues."Connect-Rubrik:Credential" = (Import-CliXml -Path $OptionValue)
}
else {
} else {
# Otherwise add it
Write-Verbose -Message "Adding PSDefaultParameterValue for Connect-Rubrik:Credential"
$Global:PSDefaultParameterValues.Add("Connect-Rubrik:Credential",(Import-CliXml -Path $OptionValue))
}
}
elseif ("" -eq $OptionValue) {
} elseif ("" -eq $OptionValue) {
# Remove from DefaultParameterValues
Write-Verbose -Message "Removing PSDefaultParameterValue for Connect-Rubrik:Credential"
if ($Global:PSDefaultParameterValues.Contains("Connect-Rubrik:Credential") ) {
$Global:PSDefaultParameterValues.Remove("Connect-Rubrik:Credential")
}
}
else {
} else {
Throw "$OptionValue does not appear to be a valid credential path"
}
}

# Set value in global variable
Write-Verbose -Message "Setting $OptionName to $OptionValue in global options"
$global:rubrikOptions.ModuleOption.$OptionName = $OptionValue
# overwrite options file with global information
Write-Verbose -Message "Exporting global options to $Home\rubrik_sdk_for_powershell_options.json"
$global:rubrikOptions | ConvertTo-Json | Out-File $Home\rubrik_sdk_for_powershell_options.json
Write-Verbose -Message "Exporting global options to $(Get-HomePath)\rubrik_sdk_for_powershell_options.json"
$global:rubrikOptions | ConvertTo-Json | Out-File -FilePath "$(Get-HomePath)\rubrik_sdk_for_powershell_options.json"
}
Default {
Throw "Invalid Action specified"
Expand All @@ -91,17 +89,17 @@
if ($Global:rubrikOptions.DefaultParameterValue.PSObject.Properties[$OptionName]) {
Write-Verbose -Message "Default Parmater $OptionName exists, updating value to $OptionValue"
$global:rubrikOptions.DefaultParameterValue.$OptionName = $OptionValue
}
else {
Write-Verbose -Message "Default Parameter $OptionName not found, creating and setting value to $OptionValue"
$global:rubrikOptions.DefaultParameterValue | Add-Member -NotePropertyName $OptionName -NotePropertyValue $OptionValue
}
# Write options back to file.
Write-Verbose -Message "Syncing global options back to $Home\rubrik_sdk_for_powershell_options.json"
$global:rubrikOptions | ConvertTo-Json | Out-File $Home\rubrik_sdk_for_powershell_options.json
# Set newly defined values globally.
Write-Verbose -Message "Syncing desired Default Parameters to global PSDefaultParameters"
Set-RubrikDefaultParameterValue
} else {
Write-Verbose -Message "Default Parameter $OptionName not found, creating and setting value to $OptionValue"
$global:rubrikOptions.DefaultParameterValue | Add-Member -NotePropertyName $OptionName -NotePropertyValue $OptionValue
}

# Write options back to file.
Write-Verbose -Message "Syncing global options back to $(Get-HomePath)\rubrik_sdk_for_powershell_options.json"
$global:rubrikOptions | ConvertTo-Json | Out-File -FilePath "$(Get-HomePath)\rubrik_sdk_for_powershell_options.json"
# Set newly defined values globally.
Write-Verbose -Message "Syncing desired Default Parameters to global PSDefaultParameters"
Set-RubrikDefaultParameterValue
}
"RemoveSingle" {
if ($global:rubrikOptions.DefaultParameterValue.PSObject.Properties[$OptionName]) {
Expand All @@ -110,9 +108,10 @@
Write-Verbose -Message "Removing *Rubrik*:$($OptionName) from PSDefaultParameters"
$global:PSDefaultParameterValues.Remove("*Rubrik*:$OptionName")
}

# Write options back to file.
Write-Verbose -Message "Syncing global options back to $Home\rubrik_sdk_for_powershell_options.json"
$global:rubrikOptions | ConvertTo-Json | Out-File $Home\rubrik_sdk_for_powershell_options.json
Write-Verbose -Message "Syncing global options back to $(Get-HomePath)\rubrik_sdk_for_powershell_options.json"
$global:rubrikOptions | ConvertTo-Json | Out-File -FilePath "$(Get-HomePath)\rubrik_sdk_for_powershell_options.json"
# Set newly defined values globally.
Write-Verbose -Message "Syncing desired Default Parameters to global PSDefaultParameters"
Set-RubrikDefaultParameterValue
Expand All @@ -124,9 +123,10 @@
Write-Verbose -Message "Removing *Rubrik*:$($_.Name) from PSDefaultParameters"
$global:PSDefaultParameterValues.Remove("*Rubrik*:$($_.Name)")
}

# Write options back to file.
Write-Verbose -Message "Syncing global options back to $Home\rubrik_sdk_for_powershell_options.json"
$global:rubrikOptions | ConvertTo-Json | Out-File $Home\rubrik_sdk_for_powershell_options.json
Write-Verbose -Message "Syncing global options back to $(Get-HomePath)\rubrik_sdk_for_powershell_options.json"
$global:rubrikOptions | ConvertTo-Json | Out-File -FilePath "$(Get-HomePath)\rubrik_sdk_for_powershell_options.json"
# Set newly defined values globally.
Write-Verbose -Message "Syncing desired Default Parameters to global PSDefaultParameters"
Set-RubrikDefaultParameterValue
Expand Down
72 changes: 42 additions & 30 deletions Rubrik/Public/Set-RubrikModuleOption.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -2,40 +2,50 @@
function Set-RubrikModuleOption
{
<#
.SYNOPSIS
Sets an option value within the users option file
.SYNOPSIS
Sets an option value within the users option file
.DESCRIPTION
The Set-RubrikModuleOption will set an option value within the users option value and immediately apply the change.
.DESCRIPTION
The Set-RubrikModuleOption will set an option value within the users option value and immediately apply the change.
.NOTES
Written by Mike Preston for community usage
Twitter: @mwpreston
GitHub: mwpreston
.NOTES
Written by Mike Preston for community usage
Twitter: @mwpreston
GitHub: mwpreston
.LINK
https://rubrik.gitbook.io/rubrik-sdk-for-powershell/command-documentation/reference/set-rubrikmoduleoption
.LINK
https://rubrik.gitbook.io/rubrik-sdk-for-powershell/command-documentation/reference/set-rubrikmoduleoption
.EXAMPLE
Set-RubrikModuleOption -OptionName ApplyCustomViewDefinitions -OptionValue "False"
This will disable the application of custom view definitions to returned objects, instead displaying a complete list of properties.
.EXAMPLE
Set-RubrikModuleOption -OptionName ApplyCustomViewDefinitions -OptionValue $false
This will disable the application of custom view definitions to returned objects, instead displaying a complete list of properties.
.EXAMPLE
Get-Credential | Export-CliXml -Path c:\creds\creds.xml
Set-RubrikModuleOption -OptionName CredentialPath -OptionValue "c:\creds\creds.xml"
This will create a default credential file to be used with Connect-Rubrik only. Encrypted credentials will be stored on the local filesystem and automatically sent to Connect-Rubrik by applying them to the global $PSDefaultParameterValues variable
.EXAMPLE
Get-Credential | Export-CliXml -Path c:\creds\creds.xml
Set-RubrikModuleOption -OptionName CredentialPath -OptionValue "c:\creds\creds.xml"
.EXAMPLE
Set-RubrikModuleOption -OptionName CredentialPath -OptionValue ""
This will remove the application of sending default credentials to Connect-Rubrik. Note: It will not remove any generated credential files.
This will create a default credential file to be used with Connect-Rubrik only. Encrypted credentials will be stored on the local filesystem and automatically sent to Connect-Rubrik by applying them to the global $PSDefaultParameterValues variable
.EXAMPLE
Set-RubrikModuleOption -Defaults
This will reset all Rubrik module options to their default values
.EXAMPLE
Set-RubrikModuleOption -OptionName CredentialPath -OptionValue $null
.EXAMPLE
Set-RubrikModuleOption -Sync
This will sync any changes made to the user option file manually to the current PowerShell session.
This will remove the application of sending default credentials to Connect-Rubrik. Note: It will not remove any generated credential files.
.EXAMPLE
Set-RubrikModuleOption -Default
This will reset all Rubrik module options to their default values
.EXAMPLE
Set-RubrikModuleOption -Sync
This will sync any changes made to the user option file manually to the current PowerShell session.
.EXAMPLE
Set-RubrikModuleOption -OptionName DefaultWebRequestTimeOut -OptionValue 30
Changes the default timeout for request to the Rubrik cluster to 30 seconds
#>

[CmdletBinding(DefaultParameterSetName = 'NameValue')]
Expand All @@ -46,6 +56,7 @@ function Set-RubrikModuleOption
ParameterSetName='NameValue',
Position=0,
Mandatory=$true)]
[ValidateSet('ApplyCustomViewDefinitions', 'CredentialPath', 'DefaultWebRequestTimeOut')]
[string]$OptionName,
# Desired value for option
[Parameter(
Expand All @@ -61,7 +72,7 @@ function Set-RubrikModuleOption
[switch]$Default,
# Apply manual changes from JSON file to session
[Parameter(
ParameterSetName="Sync",
ParameterSetName='Sync',
Mandatory=$true
)]
[Switch]$Sync
Expand All @@ -78,13 +89,14 @@ function Set-RubrikModuleOption
else {
# This means we are adding or updating (no remove on ModuleOptions)
# First, make sure the option exists
if ($Global:rubrikOptions.ModuleOption.PSObject.Properties[$OptionName]) {
if ($Global:rubrikOptions.ModuleOption.PSObject.Properties.Name -contains $OptionName) {
$ModuleOptionSplat = @{
Action = "AddUpdate"
OptionType = "ModuleOption"
OptionName = $ParameterName
OptionValue = $ParameterValue
OptionName = $OptionName
OptionValue = $OptionValue
}
Write-Verbose ($ModuleOptionSplat|out-string)
Update-RubrikModuleOption @ModuleOptionSplat
}
else {
Expand Down
4 changes: 2 additions & 2 deletions Tests/Set-RubrikModuleOption.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,13 @@ Describe -Name 'Public/Set-RubrikModuleOption' -Tag 'Public', 'Set-RubrikModuleO

It -Name "Should throw not found error" -Test {
{Set-RubrikModuleOption -OptionName 'Test' -OptionValue 'Value' } |
Should -Throw "Test doesn't exist in options file"
Should -Throw '''OptionName''. The argument "Test" does not belong to the set "ApplyCustomViewDefinitions,CredentialPath,DefaultWebRequestTimeOut" specified by the ValidateSet attribute. Supply an argument that is in the set and then try the command again.'
}

}
Context -Name 'Parameter Validation' {
It -Name "Should throw ParameterSet Error" -Test {
{ Set-RubrikModuleOption -OptionName 'Test' -Sync } |
{ Set-RubrikModuleOption -OptionName 'ApplyCustomViewDefinitions' -Sync } |
Should -Throw "Parameter set cannot be resolved using the specified named parameters."
}

Expand Down
9 changes: 9 additions & 0 deletions Tests/Update-RubrikModuleOption.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,11 @@ Describe -Name 'Public/Update-RubrikModuleOption' -Tag 'Public', 'Update-RubrikM
Mock -CommandName Sync-RubrikOptionsFile -ModuleName 'Rubrik' -MockWith {
return $global:rubrikOptions
}

Mock -CommandName Get-HomePath -Verifiable -MockWith {
'TestDrive:\'
}

It -Name "ApplyCustomViewDefinitions should remain true" -Test {
Update-RubrikModuleOption -Sync
$global:rubrikOptions.ModuleOption.ApplyCustomViewDefinitions |
Expand All @@ -51,6 +56,10 @@ Describe -Name 'Public/Update-RubrikModuleOption' -Tag 'Public', 'Update-RubrikM
Mock -CommandName Sync-RubrikOptionsFile -ModuleName 'Rubrik' -MockWith {
return $global:rubrikOptions
}
Mock -CommandName Get-HomePath -Verifiable -MockWith {
'TestDrive:\'
}

It -Name "PrimaryClusterId should remain 11111" -Test {
Update-RubrikModuleOption -Sync
$global:rubrikOptions.DefaultParameterValue.PrimaryClusterId |
Expand Down

0 comments on commit fc17804

Please sign in to comment.