PowerShell and Pure Storage REST API Scripting

Previously I blogged about using PowerShell with the Pure Storage FlashArray to enable scripting of common tasks like provisioning or snapshotting. In that post I showed how to use SSH to run Purity operations, but with the introduction of the REST APIs (fully available in 3.4+) there is now a much better and cleaner way to script this. You no longer need to install extra SSH modules and the like, all you need is the Invoke-RestMethod in PowerShell.

flasharray

The FlashArray controllers run the REST API service, so you do not need any extra management application to provide the REST API services. If you are at the proper Purity level you are essentially all set. With few exceptions, everything that you can do in the CLI you can achieve via REST calls. For a full description check out the document on the Pure Storage support site:

http://support.purestorage.com/entries/38947150-Pure-Storage-REST-API-1-1-Reference-Guide

My coworker (@themsftdude) posted about using REST earlier as well and you can check out that post here:

http://www.themicrosoftdude.com/?p=1651

Of course you can use any method you want that provides for REST calls, I am going to use PowerShell.

Since @themsftdude goes into the details of the how/why of creating a connection/token for making REST calls I won’t dig deep into that but to at least show the process I will put what I did below using PowerShell. $purevip is my virtual IP of the FlashArray.

$AuthAction = @{
    password = ${pureuser}
    username = ${purepass}
}
$ApiToken = Invoke-RestMethod -Method Post -Uri "https://${purevip}/api/1.1/auth/apitoken" -Body $AuthAction
$SessionAction = @{
    api_token = $ApiToken.api_token
}
Invoke-RestMethod -Method Post -Uri "https://${purevip}/api/1.1/auth/session" -Body $SessionAction -SessionVariable Session

I created an authorization using my credentials then created a new token and stored it and then created a new session which I can then use in the rest of my script to make my subsequent REST calls. Pretty straight forward.

So if I want to get a list of all of the volumes on that array I would run:

Invoke-RestMethod -Method Get -Uri "https://${purevip}/api/1.1/volume" -WebSession $Session

If I want to get information on specific volume I can append the name, let’s say the name is cody-volume.

Invoke-RestMethod -Method Get -Uri "https://${purevip}/api/1.1/volume/cody-volume" -WebSession $Session

Or I want to snapshot a volume and name it VMFSSnap from the volume purevol:

$Snapshot = [ordered]@{
 source = "purevol"
 } | ConvertTo-Json
Invoke-RestMethod -Method Post -Uri "https://${purevip}/api/1.1/volume/VMFSsnap" -Body $Snapshot -WebSession $Session -ContentType "application/json"

So what I want to do now is merge this with PowerCLI so I can do some end-to-end operations with VMware and the FlashArray. I will do exactly what I did in a previous post, but instead of using SSH, I am using REST calls from PowerShell. The script attached below does the following:

  1. The user enters in FlashArray and VMware information
  2. The script takes the datastore name and figures out the underlying FlashArray volume information
  3. It then snaps that volume and adds it to the same host group that the source volume uses
  4. Rescans the cluster
  5. Resignatures and mounts the VMFS
  6. Renames it to the name specified in the start

The script is attached below.

PowerShell_REST_snap_script

See a video of the script in action here:

 

 

2 Replies to “PowerShell and Pure Storage REST API Scripting”

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.