Skip to content

Commit

Permalink
Merge pull request #53 from rubrikinc/protect-tag
Browse files Browse the repository at this point in the history
Update Protect-RubrikTag function :shipit:
  • Loading branch information
chriswahl authored Dec 28, 2016
2 parents 523a7d5 + cd781aa commit 2c89ade
Show file tree
Hide file tree
Showing 3 changed files with 127 additions and 98 deletions.
5 changes: 5 additions & 0 deletions Rubrik/Private/Connect-TovCenter.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@ function ConnectTovCenter($vCenter)

Write-Verbose -Message 'Ignoring self-signed SSL certificates for vCenter Server (optional)'
$null = Set-PowerCLIConfiguration -InvalidCertificateAction Ignore -DisplayDeprecationWarnings:$false -Scope User -Confirm:$false

if ($vCenter -eq $null)
{
$vCenter = $global:DefaultVIServer.Name
}

Write-Verbose -Message 'Connecting to vCenter'
try
Expand Down
6 changes: 3 additions & 3 deletions Rubrik/Private/Get-RubrikAPIData.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -386,12 +386,12 @@ function GetRubrikAPIData($endpoint)
}
SLADomainAssignPost = @{
v1 = @{
URI = '/api/v1/sla_domain/{id}/assign_sync'
URI = '/api/v1/sla_domain/{id}/assign'
Body = @{
managedIds = 'managedIds'
}
Method = 'Post'
SuccessCode = '204'
SuccessCode = '202'
SuccessMock = ''
FailureCode = ''
FailureMock = ''
Expand Down Expand Up @@ -577,7 +577,7 @@ function GetRubrikAPIData($endpoint)
FailureCode = ''
FailureMock = ''
}
}
}
} # End of API

return $api.$endpoint
Expand Down
214 changes: 119 additions & 95 deletions Rubrik/Public/Protect-RubrikTag.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -2,107 +2,131 @@

function Protect-RubrikTag
{
<#
.SYNOPSIS
Connects to Rubrik and assigns an SLA Domain based on a vSphere category and tag value
.DESCRIPTION
The Protect-RubrikTag cmdlet will update a virtual machine's SLA Domain assignment within the Rubrik cluster. The SLA Domain contains all policy-driven values needed to protect workloads.
.NOTES
Written by Jason Burrell for community usage
Twitter: @jasonburrell2
.LINK
https://github.com/rubrikinc/PowerShell-Module
.EXAMPLE
Protect-RubrikTag -Tag 'Gold' -Category 'Rubrik' -vCenter '172.17.48.17'
This will assign the Gold SLA Domain to any VM tagged with Gold in the Rubrik category
.EXAMPLE
Protect-RubrikTag -Tag 'Gold' -Category 'Rubrik' -SLA 'Titanium' -vCenter '172.17.48.17'
This will assign the Titanium SLA Domain to any VM tagged with Gold in the Rubrik category
#>

[CmdletBinding(SupportsShouldProcess = $true,ConfirmImpact = 'High')]
Param(
[Parameter(Mandatory = $true,Position = 0,HelpMessage = 'vSphere Tag value',ValueFromPipeline = $true)]
[ValidateNotNullorEmpty()]
[String]$Tag,
[Parameter(Mandatory = $true,Position = 1,HelpMessage = 'vSphere Tag Category value',ValueFromPipeline = $true)]
[ValidateNotNullorEmpty()]
[String]$Category,
[Parameter(Mandatory = $false,Position = 2,HelpMessage = 'The Rubrik SLA Domain',ValueFromPipeline = $true)]
[ValidateNotNullorEmpty()]
[String]$SLA,
[Parameter(Mandatory = $true,Position = 3,HelpMessage = 'vCenter Server FQDN or IP',ValueFromPipeline = $true)]
[ValidateNotNullorEmpty()]
[String]$vCenter,
[Parameter(Mandatory = $false,Position = 4,HelpMessage = 'Rubrik FQDN or IP address')]
[ValidateNotNullorEmpty()]
[String]$Server = $global:rubrikConnection.server
)

Process {

TestRubrikConnection

ConnectTovCenter -vCenter $vCenter

Write-Verbose -Message 'Gathering the SLA Domain id'
try
{
if ($SLA)
{
Write-Verbose -Message 'Using explicit SLA request from input'
$slaid = Get-RubrikSLA -SLA $SLA
}
else
{
Write-Verbose -Message 'No explicit SLA requested; deferring to Tag name'
$slaid = Get-RubrikSLA -SLA $Tag
}
}
catch
{
throw $_
}
<#
.SYNOPSIS
Connects to Rubrik and assigns an SLA Domain based on a vSphere category and tag value
Write-Verbose -Message 'Gathering the vCenter Server Instance UUID'
$VCuuid = $global:DefaultVIServer.InstanceUuid
.DESCRIPTION
The Protect-RubrikTag cmdlet will update a virtual machine's SLA Domain assignment within the Rubrik cluster.
The SLA Domain contains all policy-driven values needed to protect workloads.
Make sure you have PowerCLI installed and connect to the required vCenter Server.
Write-Verbose -Message "Gathering a list of VMs associated with Category $Category and Tag $Tag"
try
{
$vmlist = Get-VM -Tag (Get-Tag -Name $Tag -Category $Category) | Get-View
}
catch
{
throw $_
}
.NOTES
Written by Jason Burrell for community usage
Twitter: @jasonburrell2
Write-Verbose -Message 'Building an array of Rubrik Managed IDs'
[array]$vmbulk = @()
foreach ($_ in $vmlist)
{
$vmbulk += 'VirtualMachine:::' + $VCuuid + '-' + $($_.moref.value)
Write-Verbose -Message "Found $($vmbulk.count) records" -Verbose
}
.LINK
https://github.com/rubrikinc/PowerShell-Module
Write-Verbose -Message 'Creating the array to mass assign the list of IDs'
$body = @{
managedIds = $vmbulk
}
.EXAMPLE
Protect-RubrikTag -Tag 'Gold' -Category 'Rubrik'
This will assign the Gold SLA Domain to any VM tagged with Gold in the Rubrik category
Write-Verbose -Message 'Submit the request'
try
{
if ($PSCmdlet.ShouldProcess("$Tag tagged objects","Assign $($slaid.name) SLA Domain"))
{
$uri = 'https://'+$Server+'/slaDomainAssign/'+$slaid.id
$r = Invoke-WebRequest -Uri $uri -Headers $header -Method Patch -Body (ConvertTo-Json -InputObject $body)
}
}
catch
.EXAMPLE
Protect-RubrikTag -Tag 'Gold' -Category 'Rubrik' -SLA 'Titanium'
This will assign the Titanium SLA Domain to any VM tagged with Gold in the Rubrik category
#>

[CmdletBinding(SupportsShouldProcess = $true,ConfirmImpact = 'High')]
Param(
# vSphere Tag
[Parameter(Mandatory = $true,Position = 0)]
[ValidateNotNullorEmpty()]
[String]$Tag,
# vSphere Tag Category
[Parameter(Mandatory = $true,Position = 1)]
[ValidateNotNullorEmpty()]
[String]$Category,
# Rubrik SLA Domain
[Parameter(Position = 2)]
[ValidateNotNullorEmpty()]
[String]$SLA,
# Rubrik server IP or FQDN
[Parameter(Position = 3)]
[String]$Server = $global:RubrikConnection.server,
# API version
[Parameter(Position = 4)]
[String]$api = $global:RubrikConnection.api
)

Process {

TestRubrikConnection

ConnectTovCenter -vCenter $vCenter

Write-Verbose -Message 'Determining which version of the API to use'
$resources = GetRubrikAPIData -endpoint ('SLADomainAssignPost')

Write-Verbose -Message 'Gathering the SLA Domain id'
try
{
if ($SLA)
{
Write-Verbose -Message 'Using explicit SLA request from input'
$slaid = (Get-RubrikSLA -SLA $SLA).id
}
else
{
Write-Verbose -Message 'No explicit SLA requested; deferring to Tag name'
$slaid = (Get-RubrikSLA -SLA $Tag).id
}
}
catch
{
throw $_
}

Write-Verbose -Message "Gathering a list of VMs associated with Category $Category and Tag $Tag"
try
{
$vmlist = Get-VM -Tag (Get-Tag -Name $Tag -Category $Category) | Get-View
# This will pull out the vCenter UUID assigned to the parent vCenter Server by Rubrik
$vcuuid = (Get-RubrikVM -VM $vmlist[0].Name).vCenterId
}
catch
{
throw $_
}

Write-Verbose -Message 'Building an array of Rubrik Managed IDs'
[array]$vmbulk = @()
foreach ($_ in $vmlist)
{
$vmbulk += 'VirtualMachine:::' + $vcuuid + '-' + $($_.moref.value)
Write-Verbose -Message "Found $($vmbulk.count) records"
}

Write-Verbose -Message 'Creating the array to mass assign the list of IDs'
$body = @{
managedIds = $vmbulk
}

Write-Verbose -Message 'Building the URI'
$uri = 'https://'+$Server+$resources.$api.URI
# Replace the placeholder of {id} with the actual VM ID
$uri = $uri -replace '{id}', $slaid

# Set the method
$method = $resources.$api.Method

Write-Verbose -Message 'Submit the request'
try
{
if ($PSCmdlet.ShouldProcess("$($vmbulk.count) $Tag tagged object(s)","Assign $($slaid.name) SLA Domain"))
{
$r = Invoke-WebRequest -Uri $uri -Headers $Header -Method $method -Body (ConvertTo-Json -InputObject $body)
if ($r.StatusCode -ne $resources.$api.SuccessCode)
{
throw $_
Write-Warning -Message 'Did not receive successful status code from Rubrik'
throw $_
}
}
}
catch
{
throw $_
}

} # End of process
} # End of process
} # End of function

0 comments on commit 2c89ade

Please sign in to comment.