Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Invoke RubrikRESTCall #118

Merged
merged 2 commits into from
Oct 18, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Rubrik/Private/Submit-Request.ps1
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
function Submit-Request($uri,$header,$method,$body)
function Submit-Request($uri,$header,$method = $($resources.Method) ,$body)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Curious why this change was required?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Due to the change made to line 15 for the Invoke-WebRequest. The original version was locked into whatever the $resources item was. I needed Submit-Request to not be dependent on the $resources collection (be more generic). However, to maintain the functionality I changed the method to default to $resources.Method .

{
# The Submit-Request function is used to send data to an endpoint and then format the response for further use
# $uri = The endpoint's URI
Expand All @@ -12,7 +12,7 @@
{
Write-Verbose -Message 'Submitting the request'
# Because some calls require more than the default payload limit of 2MB, ExpandPayload dynamically adjusts the payload limit
$result = ExpandPayload -response (Invoke-WebRequest -Uri $uri -Headers $header -Method $($resources.Method) -Body $body)
$result = ExpandPayload -response (Invoke-WebRequest -Uri $uri -Headers $header -Method $method -Body $body)
}
catch
{
Expand Down
101 changes: 101 additions & 0 deletions Rubrik/Public/Invoke-RubrikRESTCall.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
function Invoke-RubrikRESTCall {
<#
.SYNOPSIS
Provides generic interface to make Rubrik REST API calls

.DESCRIPTION
The Invoke-RubrikRESTCall allows users to make raw API endpoint calls to the Rubrik REST interface. The user
will need to manage the format of both the endpoint call(including resource ids) and body, but provides the
option to make cmdlet independent API calls for automating Rubrik actions through PowerShell. The Rubrik API
reference is found on the Rubrik device at:
<Rubrik IP>/docs/v1
<Rubrik IP>/docs/v1/playground

.NOTES
Written by Matt Altimar & Mike Fal for community usage
Twitter: @Mike_Fal
GitHub: mikefal

.LINK
https://github.com/rubrikinc/PowerShell-Module

.EXAMPLE
Invoke-RubrikRESTCall -Endpoint 'vmware/vm' -Method GET

Retrieve the raw output for all VMWare VMs being managed by the Rubrik device.

.EXAMPLE
Invoke-RubrikRESTCall -Endpoint 'vmware/vm' -Method GET -Query (New-Object -TypeName PSObject -Property @{'name'='msf-sql2016'})

Retrieve the raw output for the VMWare VM msf-sql2016 using a query parameter.

.EXAMPLE
$body = New-Object -TypeName PSObject -Property @{'slaID'='INHERIT';'ForceFullSnapshot'='FALSE'}
Invoke-RubrikRESTCall -Endpoint 'vmware/vm/VirtualMachine:::fbcb1f51-9520-4227-a68c-6fe145982f48-vm-649/snapshot' -Method POST -Body $body

Execute an on-demand snapshot for the VMWare VM where the id is part of the endpoint.
#>

[cmdletbinding()]
Param (
#Rubrik API endpoint, DO NOT USE LEADING '/'
[Parameter(Mandatory = $true,HelpMessage = 'REST Endpoint')]
[ValidateNotNullorEmpty()]
[System.String]$Endpoint,
#REST API method
[Parameter(Mandatory = $true,HelpMessage = 'REST Method')]
[ValidateSET('GET','PUT','PATCH','DELETE','POST','HEAD','OPTIONS')]
[System.String]$Method,
#Hash table body to pass to API call
[Parameter(Mandatory = $false,HelpMessage = 'REST Content')]
[ValidateNotNullorEmpty()]
[psobject]$Query,
#Hash table body to pass to API call
[Parameter(Mandatory = $false,HelpMessage = 'REST Content')]
[ValidateNotNullorEmpty()]
[psobject]$Body,
# Rubrik server IP or FQDN
[String]$Server = $global:RubrikConnection.server,
# API version
[ValidateNotNullorEmpty()]
[String]$api = $global:RubrikConnection.api
)
BEGIN
{
#connect to Rubrik if not already connected
Test-RubrikConnection
}

PROCESS
{
#execute REST operation
try {
#construct uri
[string]$uri = New-URIString -server $Server -endpoint "/api/$api/$Endpoint"

#If query object, add query parameters to URI
if($query)
{
$querystring = @()
foreach($q in $query.PSObject.Properties)
{
$querystring += "$($q.name)=$($q.Value)"
}
$uri = New-QueryString -query $querystring -uri $uri
}

#If Method is not a GET call and a REST Body is passed, build the JSON body
if($Method -ne 'GET' -and $body){
[string]$JsonBody = $Body | ConvertTo-Json
}
Write-Verbose "URI string: $uri"

$result = Submit-Request -uri $uri -header $Header -method $Method -body $JsonBody
}
catch {
throw $_
}

return $result
}
}
2 changes: 1 addition & 1 deletion Rubrik/Rubrik.psd1
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ FunctionsToExport = @('Connect-Rubrik', 'Disconnect-Rubrik', 'Export-RubrikDatab
'Get-RubrikMount', 'Get-RubrikReport', 'Get-RubrikReportData',
'Get-RubrikRequest', 'Get-RubrikSLA', 'Get-RubrikSnapshot',
'Get-RubrikSoftwareVersion', 'Get-RubrikUnmanagedObject',
'Get-RubrikVersion', 'Get-RubrikVM', 'Move-RubrikMountVMDK',
'Get-RubrikVersion', 'Get-RubrikVM', 'Invoke-RubrikRESTCall', 'Move-RubrikMountVMDK',
'New-RubrikDatabaseMount', 'New-RubrikHost', 'New-RubrikMount',
'New-RubrikReport', 'New-RubrikSLA', 'New-RubrikSnapshot',
'Protect-RubrikDatabase', 'Protect-RubrikFileset',
Expand Down